blob: 59aabfe0e4f20e04ac32ceb610001c591b3d8dd6 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorff9a6092009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
20#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000034#include "llvm/Bitcode/BitstreamWriter.h"
35#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000037#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000038using namespace clang;
39
40//===----------------------------------------------------------------------===//
41// Type serialization
42//===----------------------------------------------------------------------===//
43namespace {
44 class VISIBILITY_HIDDEN PCHTypeWriter {
45 PCHWriter &Writer;
46 PCHWriter::RecordData &Record;
47
48 public:
49 /// \brief Type code that corresponds to the record generated.
50 pch::TypeCode Code;
51
52 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
53 : Writer(Writer), Record(Record) { }
54
55 void VisitArrayType(const ArrayType *T);
56 void VisitFunctionType(const FunctionType *T);
57 void VisitTagType(const TagType *T);
58
59#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
60#define ABSTRACT_TYPE(Class, Base)
61#define DEPENDENT_TYPE(Class, Base)
62#include "clang/AST/TypeNodes.def"
63 };
64}
65
66void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
67 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
68 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
69 Record.push_back(T->getAddressSpace());
70 Code = pch::TYPE_EXT_QUAL;
71}
72
73void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
74 assert(false && "Built-in types are never serialized");
75}
76
77void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
78 Record.push_back(T->getWidth());
79 Record.push_back(T->isSigned());
80 Code = pch::TYPE_FIXED_WIDTH_INT;
81}
82
83void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
84 Writer.AddTypeRef(T->getElementType(), Record);
85 Code = pch::TYPE_COMPLEX;
86}
87
88void PCHTypeWriter::VisitPointerType(const PointerType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_POINTER;
91}
92
93void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_BLOCK_POINTER;
96}
97
98void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
99 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Code = pch::TYPE_LVALUE_REFERENCE;
101}
102
103void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
104 Writer.AddTypeRef(T->getPointeeType(), Record);
105 Code = pch::TYPE_RVALUE_REFERENCE;
106}
107
108void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
109 Writer.AddTypeRef(T->getPointeeType(), Record);
110 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
111 Code = pch::TYPE_MEMBER_POINTER;
112}
113
114void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
115 Writer.AddTypeRef(T->getElementType(), Record);
116 Record.push_back(T->getSizeModifier()); // FIXME: stable values
117 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
118}
119
120void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
121 VisitArrayType(T);
122 Writer.AddAPInt(T->getSize(), Record);
123 Code = pch::TYPE_CONSTANT_ARRAY;
124}
125
126void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
127 VisitArrayType(T);
128 Code = pch::TYPE_INCOMPLETE_ARRAY;
129}
130
131void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
132 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000133 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000134 Code = pch::TYPE_VARIABLE_ARRAY;
135}
136
137void PCHTypeWriter::VisitVectorType(const VectorType *T) {
138 Writer.AddTypeRef(T->getElementType(), Record);
139 Record.push_back(T->getNumElements());
140 Code = pch::TYPE_VECTOR;
141}
142
143void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
144 VisitVectorType(T);
145 Code = pch::TYPE_EXT_VECTOR;
146}
147
148void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
149 Writer.AddTypeRef(T->getResultType(), Record);
150}
151
152void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
153 VisitFunctionType(T);
154 Code = pch::TYPE_FUNCTION_NO_PROTO;
155}
156
157void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
158 VisitFunctionType(T);
159 Record.push_back(T->getNumArgs());
160 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
161 Writer.AddTypeRef(T->getArgType(I), Record);
162 Record.push_back(T->isVariadic());
163 Record.push_back(T->getTypeQuals());
164 Code = pch::TYPE_FUNCTION_PROTO;
165}
166
167void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
168 Writer.AddDeclRef(T->getDecl(), Record);
169 Code = pch::TYPE_TYPEDEF;
170}
171
172void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000173 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000174 Code = pch::TYPE_TYPEOF_EXPR;
175}
176
177void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
178 Writer.AddTypeRef(T->getUnderlyingType(), Record);
179 Code = pch::TYPE_TYPEOF;
180}
181
182void PCHTypeWriter::VisitTagType(const TagType *T) {
183 Writer.AddDeclRef(T->getDecl(), Record);
184 assert(!T->isBeingDefined() &&
185 "Cannot serialize in the middle of a type definition");
186}
187
188void PCHTypeWriter::VisitRecordType(const RecordType *T) {
189 VisitTagType(T);
190 Code = pch::TYPE_RECORD;
191}
192
193void PCHTypeWriter::VisitEnumType(const EnumType *T) {
194 VisitTagType(T);
195 Code = pch::TYPE_ENUM;
196}
197
198void
199PCHTypeWriter::VisitTemplateSpecializationType(
200 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000201 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000202 assert(false && "Cannot serialize template specialization types");
203}
204
205void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000206 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000207 assert(false && "Cannot serialize qualified name types");
208}
209
210void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
211 Writer.AddDeclRef(T->getDecl(), Record);
212 Code = pch::TYPE_OBJC_INTERFACE;
213}
214
215void
216PCHTypeWriter::VisitObjCQualifiedInterfaceType(
217 const ObjCQualifiedInterfaceType *T) {
218 VisitObjCInterfaceType(T);
219 Record.push_back(T->getNumProtocols());
220 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
221 Writer.AddDeclRef(T->getProtocol(I), Record);
222 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
223}
224
225void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
226 Record.push_back(T->getNumProtocols());
227 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
228 Writer.AddDeclRef(T->getProtocols(I), Record);
229 Code = pch::TYPE_OBJC_QUALIFIED_ID;
230}
231
Douglas Gregorc34897d2009-04-09 22:27:44 +0000232//===----------------------------------------------------------------------===//
233// Declaration serialization
234//===----------------------------------------------------------------------===//
235namespace {
236 class VISIBILITY_HIDDEN PCHDeclWriter
237 : public DeclVisitor<PCHDeclWriter, void> {
238
239 PCHWriter &Writer;
Douglas Gregore3241e92009-04-18 00:02:19 +0000240 ASTContext &Context;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000241 PCHWriter::RecordData &Record;
242
243 public:
244 pch::DeclCode Code;
245
Douglas Gregore3241e92009-04-18 00:02:19 +0000246 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
247 PCHWriter::RecordData &Record)
248 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000249
250 void VisitDecl(Decl *D);
251 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
252 void VisitNamedDecl(NamedDecl *D);
253 void VisitTypeDecl(TypeDecl *D);
254 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000255 void VisitTagDecl(TagDecl *D);
256 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000257 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000258 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000259 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000260 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000261 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000262 void VisitVarDecl(VarDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000263 void VisitParmVarDecl(ParmVarDecl *D);
264 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000265 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
266 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000267 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
268 uint64_t VisibleOffset);
Steve Naroff79ea0e02009-04-20 15:06:07 +0000269 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +0000270 void VisitObjCContainerDecl(ObjCContainerDecl *D);
271 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
272 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000273 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
274 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
275 void VisitObjCClassDecl(ObjCClassDecl *D);
276 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
277 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
278 void VisitObjCImplDecl(ObjCImplDecl *D);
279 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
280 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
281 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
282 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
283 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000284 };
285}
286
287void PCHDeclWriter::VisitDecl(Decl *D) {
288 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
289 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
290 Writer.AddSourceLocation(D->getLocation(), Record);
291 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000292 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000293 Record.push_back(D->isImplicit());
294 Record.push_back(D->getAccess());
295}
296
297void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
298 VisitDecl(D);
299 Code = pch::DECL_TRANSLATION_UNIT;
300}
301
302void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
303 VisitDecl(D);
304 Writer.AddDeclarationName(D->getDeclName(), Record);
305}
306
307void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
308 VisitNamedDecl(D);
309 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
310}
311
312void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
313 VisitTypeDecl(D);
314 Writer.AddTypeRef(D->getUnderlyingType(), Record);
315 Code = pch::DECL_TYPEDEF;
316}
317
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000318void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
319 VisitTypeDecl(D);
320 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
321 Record.push_back(D->isDefinition());
322 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
323}
324
325void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
326 VisitTagDecl(D);
327 Writer.AddTypeRef(D->getIntegerType(), Record);
328 Code = pch::DECL_ENUM;
329}
330
Douglas Gregor982365e2009-04-13 21:20:57 +0000331void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
332 VisitTagDecl(D);
333 Record.push_back(D->hasFlexibleArrayMember());
334 Record.push_back(D->isAnonymousStructOrUnion());
335 Code = pch::DECL_RECORD;
336}
337
Douglas Gregorc34897d2009-04-09 22:27:44 +0000338void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
339 VisitNamedDecl(D);
340 Writer.AddTypeRef(D->getType(), Record);
341}
342
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000343void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
344 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000345 Record.push_back(D->getInitExpr()? 1 : 0);
346 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000347 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000348 Writer.AddAPSInt(D->getInitVal(), Record);
349 Code = pch::DECL_ENUM_CONSTANT;
350}
351
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000352void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
353 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000354 Record.push_back(D->isThisDeclarationADefinition());
355 if (D->isThisDeclarationADefinition())
Douglas Gregore3241e92009-04-18 00:02:19 +0000356 Writer.AddStmt(D->getBody(Context));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000357 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
358 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
359 Record.push_back(D->isInline());
Douglas Gregor9b6348d2009-04-23 18:22:55 +0000360 Record.push_back(D->isC99InlineDefinition());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000361 Record.push_back(D->isVirtual());
362 Record.push_back(D->isPure());
363 Record.push_back(D->inheritedPrototype());
364 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
365 Record.push_back(D->isDeleted());
366 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
367 Record.push_back(D->param_size());
368 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
369 P != PEnd; ++P)
370 Writer.AddDeclRef(*P, Record);
371 Code = pch::DECL_FUNCTION;
372}
373
Steve Naroff79ea0e02009-04-20 15:06:07 +0000374void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
375 VisitNamedDecl(D);
376 // FIXME: convert to LazyStmtPtr?
377 // Unlike C/C++, method bodies will never be in header files.
378 Record.push_back(D->getBody() != 0);
379 if (D->getBody() != 0) {
380 Writer.AddStmt(D->getBody(Context));
381 Writer.AddDeclRef(D->getSelfDecl(), Record);
382 Writer.AddDeclRef(D->getCmdDecl(), Record);
383 }
384 Record.push_back(D->isInstanceMethod());
385 Record.push_back(D->isVariadic());
386 Record.push_back(D->isSynthesized());
387 // FIXME: stable encoding for @required/@optional
388 Record.push_back(D->getImplementationControl());
389 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
390 Record.push_back(D->getObjCDeclQualifier());
391 Writer.AddTypeRef(D->getResultType(), Record);
392 Writer.AddSourceLocation(D->getLocEnd(), Record);
393 Record.push_back(D->param_size());
394 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
395 PEnd = D->param_end(); P != PEnd; ++P)
396 Writer.AddDeclRef(*P, Record);
397 Code = pch::DECL_OBJC_METHOD;
398}
399
Steve Naroff7333b492009-04-20 20:09:33 +0000400void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
401 VisitNamedDecl(D);
402 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
403 // Abstract class (no need to define a stable pch::DECL code).
404}
405
406void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
407 VisitObjCContainerDecl(D);
408 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
409 Writer.AddDeclRef(D->getSuperClass(), Record);
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000410 Record.push_back(D->protocol_size());
411 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
412 PEnd = D->protocol_end();
413 P != PEnd; ++P)
414 Writer.AddDeclRef(*P, Record);
Steve Naroff7333b492009-04-20 20:09:33 +0000415 Record.push_back(D->ivar_size());
416 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
417 IEnd = D->ivar_end(); I != IEnd; ++I)
418 Writer.AddDeclRef(*I, Record);
Douglas Gregorae660c72009-04-23 22:34:55 +0000419 Writer.AddDeclRef(D->getCategoryList(), Record);
Steve Naroff7333b492009-04-20 20:09:33 +0000420 Record.push_back(D->isForwardDecl());
421 Record.push_back(D->isImplicitInterfaceDecl());
422 Writer.AddSourceLocation(D->getClassLoc(), Record);
423 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
424 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000425 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff7333b492009-04-20 20:09:33 +0000426}
427
428void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
429 VisitFieldDecl(D);
430 // FIXME: stable encoding for @public/@private/@protected/@package
431 Record.push_back(D->getAccessControl());
Steve Naroff97b53bd2009-04-21 15:12:33 +0000432 Code = pch::DECL_OBJC_IVAR;
433}
434
435void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
436 VisitObjCContainerDecl(D);
437 Record.push_back(D->isForwardDecl());
438 Writer.AddSourceLocation(D->getLocEnd(), Record);
439 Record.push_back(D->protocol_size());
440 for (ObjCProtocolDecl::protocol_iterator
441 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
442 Writer.AddDeclRef(*I, Record);
443 Code = pch::DECL_OBJC_PROTOCOL;
444}
445
446void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
447 VisitFieldDecl(D);
448 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
449}
450
451void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
452 VisitDecl(D);
453 Record.push_back(D->size());
454 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
455 Writer.AddDeclRef(*I, Record);
456 Code = pch::DECL_OBJC_CLASS;
457}
458
459void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
460 VisitDecl(D);
461 Record.push_back(D->protocol_size());
462 for (ObjCProtocolDecl::protocol_iterator
463 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
464 Writer.AddDeclRef(*I, Record);
465 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
466}
467
468void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
469 VisitObjCContainerDecl(D);
470 Writer.AddDeclRef(D->getClassInterface(), Record);
471 Record.push_back(D->protocol_size());
472 for (ObjCProtocolDecl::protocol_iterator
473 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
474 Writer.AddDeclRef(*I, Record);
475 Writer.AddDeclRef(D->getNextClassCategory(), Record);
476 Writer.AddSourceLocation(D->getLocEnd(), Record);
477 Code = pch::DECL_OBJC_CATEGORY;
478}
479
480void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
481 VisitNamedDecl(D);
482 Writer.AddDeclRef(D->getClassInterface(), Record);
483 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
484}
485
486void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
487 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000488 Writer.AddTypeRef(D->getType(), Record);
489 // FIXME: stable encoding
490 Record.push_back((unsigned)D->getPropertyAttributes());
491 // FIXME: stable encoding
492 Record.push_back((unsigned)D->getPropertyImplementation());
493 Writer.AddDeclarationName(D->getGetterName(), Record);
494 Writer.AddDeclarationName(D->getSetterName(), Record);
495 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
496 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
497 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000498 Code = pch::DECL_OBJC_PROPERTY;
499}
500
501void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000502 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000503 Writer.AddDeclRef(D->getClassInterface(), Record);
504 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000505 // Abstract class (no need to define a stable pch::DECL code).
506}
507
508void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
509 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000510 Writer.AddIdentifierRef(D->getIdentifier(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000511 Code = pch::DECL_OBJC_CATEGORY_IMPL;
512}
513
514void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
515 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000516 Writer.AddDeclRef(D->getSuperClass(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000517 Code = pch::DECL_OBJC_IMPLEMENTATION;
518}
519
520void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
521 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000522 Writer.AddSourceLocation(D->getLocStart(), Record);
523 Writer.AddDeclRef(D->getPropertyDecl(), Record);
524 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000525 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff7333b492009-04-20 20:09:33 +0000526}
527
Douglas Gregor982365e2009-04-13 21:20:57 +0000528void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
529 VisitValueDecl(D);
530 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000531 Record.push_back(D->getBitWidth()? 1 : 0);
532 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000533 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000534 Code = pch::DECL_FIELD;
535}
536
Douglas Gregorc34897d2009-04-09 22:27:44 +0000537void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
538 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000539 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000540 Record.push_back(D->isThreadSpecified());
541 Record.push_back(D->hasCXXDirectInitializer());
542 Record.push_back(D->isDeclaredInCondition());
543 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
544 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000545 Record.push_back(D->getInit()? 1 : 0);
546 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000547 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000548 Code = pch::DECL_VAR;
549}
550
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000551void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
552 VisitVarDecl(D);
553 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000554 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000555 // FIXME: why isn't the "default argument" just stored as the initializer
556 // in VarDecl?
557 Code = pch::DECL_PARM_VAR;
558}
559
560void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
561 VisitParmVarDecl(D);
562 Writer.AddTypeRef(D->getOriginalType(), Record);
563 Code = pch::DECL_ORIGINAL_PARM_VAR;
564}
565
Douglas Gregor2a491792009-04-13 22:49:25 +0000566void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
567 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000568 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000569 Code = pch::DECL_FILE_SCOPE_ASM;
570}
571
572void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
573 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000574 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000575 Record.push_back(D->param_size());
576 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
577 P != PEnd; ++P)
578 Writer.AddDeclRef(*P, Record);
579 Code = pch::DECL_BLOCK;
580}
581
Douglas Gregorc34897d2009-04-09 22:27:44 +0000582/// \brief Emit the DeclContext part of a declaration context decl.
583///
584/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
585/// block for this declaration context is stored. May be 0 to indicate
586/// that there are no declarations stored within this context.
587///
588/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
589/// block for this declaration context is stored. May be 0 to indicate
590/// that there are no declarations visible from this context. Note
591/// that this value will not be emitted for non-primary declaration
592/// contexts.
593void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
594 uint64_t VisibleOffset) {
595 Record.push_back(LexicalOffset);
Douglas Gregor405b6432009-04-22 19:09:20 +0000596 Record.push_back(VisibleOffset);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000597}
598
599//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000600// Statement/expression serialization
601//===----------------------------------------------------------------------===//
602namespace {
603 class VISIBILITY_HIDDEN PCHStmtWriter
604 : public StmtVisitor<PCHStmtWriter, void> {
605
606 PCHWriter &Writer;
607 PCHWriter::RecordData &Record;
608
609 public:
610 pch::StmtCode Code;
611
612 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
613 : Writer(Writer), Record(Record) { }
614
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000615 void VisitStmt(Stmt *S);
616 void VisitNullStmt(NullStmt *S);
617 void VisitCompoundStmt(CompoundStmt *S);
618 void VisitSwitchCase(SwitchCase *S);
619 void VisitCaseStmt(CaseStmt *S);
620 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000621 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000622 void VisitIfStmt(IfStmt *S);
623 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000624 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000625 void VisitDoStmt(DoStmt *S);
626 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000627 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000628 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000629 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000630 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000631 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000632 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000633 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000634 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000635 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000636 void VisitDeclRefExpr(DeclRefExpr *E);
637 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000638 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000639 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000640 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000641 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000642 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000643 void VisitUnaryOperator(UnaryOperator *E);
644 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000645 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000646 void VisitCallExpr(CallExpr *E);
647 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000648 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000649 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000650 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
651 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000652 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000653 void VisitExplicitCastExpr(ExplicitCastExpr *E);
654 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000655 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000656 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000657 void VisitInitListExpr(InitListExpr *E);
658 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
659 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000660 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000661 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000662 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000663 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
664 void VisitChooseExpr(ChooseExpr *E);
665 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000666 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000667 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000668 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000669
670 // Objective-C
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000671 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000672 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000673 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
674 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000675 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000676 };
677}
678
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000679void PCHStmtWriter::VisitStmt(Stmt *S) {
680}
681
682void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
683 VisitStmt(S);
684 Writer.AddSourceLocation(S->getSemiLoc(), Record);
685 Code = pch::STMT_NULL;
686}
687
688void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
689 VisitStmt(S);
690 Record.push_back(S->size());
691 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
692 CS != CSEnd; ++CS)
693 Writer.WriteSubStmt(*CS);
694 Writer.AddSourceLocation(S->getLBracLoc(), Record);
695 Writer.AddSourceLocation(S->getRBracLoc(), Record);
696 Code = pch::STMT_COMPOUND;
697}
698
699void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
700 VisitStmt(S);
701 Record.push_back(Writer.RecordSwitchCaseID(S));
702}
703
704void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
705 VisitSwitchCase(S);
706 Writer.WriteSubStmt(S->getLHS());
707 Writer.WriteSubStmt(S->getRHS());
708 Writer.WriteSubStmt(S->getSubStmt());
709 Writer.AddSourceLocation(S->getCaseLoc(), Record);
710 Code = pch::STMT_CASE;
711}
712
713void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
714 VisitSwitchCase(S);
715 Writer.WriteSubStmt(S->getSubStmt());
716 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
717 Code = pch::STMT_DEFAULT;
718}
719
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000720void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
721 VisitStmt(S);
722 Writer.AddIdentifierRef(S->getID(), Record);
723 Writer.WriteSubStmt(S->getSubStmt());
724 Writer.AddSourceLocation(S->getIdentLoc(), Record);
725 Record.push_back(Writer.GetLabelID(S));
726 Code = pch::STMT_LABEL;
727}
728
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000729void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
730 VisitStmt(S);
731 Writer.WriteSubStmt(S->getCond());
732 Writer.WriteSubStmt(S->getThen());
733 Writer.WriteSubStmt(S->getElse());
734 Writer.AddSourceLocation(S->getIfLoc(), Record);
735 Code = pch::STMT_IF;
736}
737
738void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
739 VisitStmt(S);
740 Writer.WriteSubStmt(S->getCond());
741 Writer.WriteSubStmt(S->getBody());
742 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
743 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
744 SC = SC->getNextSwitchCase())
745 Record.push_back(Writer.getSwitchCaseID(SC));
746 Code = pch::STMT_SWITCH;
747}
748
Douglas Gregora6b503f2009-04-17 00:16:09 +0000749void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
750 VisitStmt(S);
751 Writer.WriteSubStmt(S->getCond());
752 Writer.WriteSubStmt(S->getBody());
753 Writer.AddSourceLocation(S->getWhileLoc(), Record);
754 Code = pch::STMT_WHILE;
755}
756
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000757void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
758 VisitStmt(S);
759 Writer.WriteSubStmt(S->getCond());
760 Writer.WriteSubStmt(S->getBody());
761 Writer.AddSourceLocation(S->getDoLoc(), Record);
762 Code = pch::STMT_DO;
763}
764
765void PCHStmtWriter::VisitForStmt(ForStmt *S) {
766 VisitStmt(S);
767 Writer.WriteSubStmt(S->getInit());
768 Writer.WriteSubStmt(S->getCond());
769 Writer.WriteSubStmt(S->getInc());
770 Writer.WriteSubStmt(S->getBody());
771 Writer.AddSourceLocation(S->getForLoc(), Record);
772 Code = pch::STMT_FOR;
773}
774
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000775void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
776 VisitStmt(S);
777 Record.push_back(Writer.GetLabelID(S->getLabel()));
778 Writer.AddSourceLocation(S->getGotoLoc(), Record);
779 Writer.AddSourceLocation(S->getLabelLoc(), Record);
780 Code = pch::STMT_GOTO;
781}
782
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000783void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
784 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000785 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000786 Writer.WriteSubStmt(S->getTarget());
787 Code = pch::STMT_INDIRECT_GOTO;
788}
789
Douglas Gregora6b503f2009-04-17 00:16:09 +0000790void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
791 VisitStmt(S);
792 Writer.AddSourceLocation(S->getContinueLoc(), Record);
793 Code = pch::STMT_CONTINUE;
794}
795
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000796void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
797 VisitStmt(S);
798 Writer.AddSourceLocation(S->getBreakLoc(), Record);
799 Code = pch::STMT_BREAK;
800}
801
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000802void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
803 VisitStmt(S);
804 Writer.WriteSubStmt(S->getRetValue());
805 Writer.AddSourceLocation(S->getReturnLoc(), Record);
806 Code = pch::STMT_RETURN;
807}
808
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000809void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
810 VisitStmt(S);
811 Writer.AddSourceLocation(S->getStartLoc(), Record);
812 Writer.AddSourceLocation(S->getEndLoc(), Record);
813 DeclGroupRef DG = S->getDeclGroup();
814 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
815 Writer.AddDeclRef(*D, Record);
816 Code = pch::STMT_DECL;
817}
818
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000819void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
820 VisitStmt(S);
821 Record.push_back(S->getNumOutputs());
822 Record.push_back(S->getNumInputs());
823 Record.push_back(S->getNumClobbers());
824 Writer.AddSourceLocation(S->getAsmLoc(), Record);
825 Writer.AddSourceLocation(S->getRParenLoc(), Record);
826 Record.push_back(S->isVolatile());
827 Record.push_back(S->isSimple());
828 Writer.WriteSubStmt(S->getAsmString());
829
830 // Outputs
831 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
832 Writer.AddString(S->getOutputName(I), Record);
833 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
834 Writer.WriteSubStmt(S->getOutputExpr(I));
835 }
836
837 // Inputs
838 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
839 Writer.AddString(S->getInputName(I), Record);
840 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
841 Writer.WriteSubStmt(S->getInputExpr(I));
842 }
843
844 // Clobbers
845 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
846 Writer.WriteSubStmt(S->getClobber(I));
847
848 Code = pch::STMT_ASM;
849}
850
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000851void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000852 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000853 Writer.AddTypeRef(E->getType(), Record);
854 Record.push_back(E->isTypeDependent());
855 Record.push_back(E->isValueDependent());
856}
857
Douglas Gregore2f37202009-04-14 21:55:33 +0000858void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
859 VisitExpr(E);
860 Writer.AddSourceLocation(E->getLocation(), Record);
861 Record.push_back(E->getIdentType()); // FIXME: stable encoding
862 Code = pch::EXPR_PREDEFINED;
863}
864
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000865void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
866 VisitExpr(E);
867 Writer.AddDeclRef(E->getDecl(), Record);
868 Writer.AddSourceLocation(E->getLocation(), Record);
869 Code = pch::EXPR_DECL_REF;
870}
871
872void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
873 VisitExpr(E);
874 Writer.AddSourceLocation(E->getLocation(), Record);
875 Writer.AddAPInt(E->getValue(), Record);
876 Code = pch::EXPR_INTEGER_LITERAL;
877}
878
Douglas Gregore2f37202009-04-14 21:55:33 +0000879void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
880 VisitExpr(E);
881 Writer.AddAPFloat(E->getValue(), Record);
882 Record.push_back(E->isExact());
883 Writer.AddSourceLocation(E->getLocation(), Record);
884 Code = pch::EXPR_FLOATING_LITERAL;
885}
886
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000887void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
888 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000889 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000890 Code = pch::EXPR_IMAGINARY_LITERAL;
891}
892
Douglas Gregor596e0932009-04-15 16:35:07 +0000893void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
894 VisitExpr(E);
895 Record.push_back(E->getByteLength());
896 Record.push_back(E->getNumConcatenated());
897 Record.push_back(E->isWide());
898 // FIXME: String data should be stored as a blob at the end of the
899 // StringLiteral. However, we can't do so now because we have no
900 // provision for coping with abbreviations when we're jumping around
901 // the PCH file during deserialization.
902 Record.insert(Record.end(),
903 E->getStrData(), E->getStrData() + E->getByteLength());
904 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
905 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
906 Code = pch::EXPR_STRING_LITERAL;
907}
908
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000909void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
910 VisitExpr(E);
911 Record.push_back(E->getValue());
912 Writer.AddSourceLocation(E->getLoc(), Record);
913 Record.push_back(E->isWide());
914 Code = pch::EXPR_CHARACTER_LITERAL;
915}
916
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000917void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
918 VisitExpr(E);
919 Writer.AddSourceLocation(E->getLParen(), Record);
920 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000921 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000922 Code = pch::EXPR_PAREN;
923}
924
Douglas Gregor12d74052009-04-15 15:58:59 +0000925void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
926 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000927 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000928 Record.push_back(E->getOpcode()); // FIXME: stable encoding
929 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
930 Code = pch::EXPR_UNARY_OPERATOR;
931}
932
933void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
934 VisitExpr(E);
935 Record.push_back(E->isSizeOf());
936 if (E->isArgumentType())
937 Writer.AddTypeRef(E->getArgumentType(), Record);
938 else {
939 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000940 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000941 }
942 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
943 Writer.AddSourceLocation(E->getRParenLoc(), Record);
944 Code = pch::EXPR_SIZEOF_ALIGN_OF;
945}
946
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000947void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
948 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000949 Writer.WriteSubStmt(E->getLHS());
950 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000951 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
952 Code = pch::EXPR_ARRAY_SUBSCRIPT;
953}
954
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000955void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
956 VisitExpr(E);
957 Record.push_back(E->getNumArgs());
958 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000959 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000960 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
961 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000962 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000963 Code = pch::EXPR_CALL;
964}
965
966void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
967 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000968 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000969 Writer.AddDeclRef(E->getMemberDecl(), Record);
970 Writer.AddSourceLocation(E->getMemberLoc(), Record);
971 Record.push_back(E->isArrow());
972 Code = pch::EXPR_MEMBER;
973}
974
Douglas Gregora151ba42009-04-14 23:32:43 +0000975void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
976 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000977 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000978}
979
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000980void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
981 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000982 Writer.WriteSubStmt(E->getLHS());
983 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000984 Record.push_back(E->getOpcode()); // FIXME: stable encoding
985 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
986 Code = pch::EXPR_BINARY_OPERATOR;
987}
988
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000989void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
990 VisitBinaryOperator(E);
991 Writer.AddTypeRef(E->getComputationLHSType(), Record);
992 Writer.AddTypeRef(E->getComputationResultType(), Record);
993 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
994}
995
996void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
997 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000998 Writer.WriteSubStmt(E->getCond());
999 Writer.WriteSubStmt(E->getLHS());
1000 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001001 Code = pch::EXPR_CONDITIONAL_OPERATOR;
1002}
1003
Douglas Gregora151ba42009-04-14 23:32:43 +00001004void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
1005 VisitCastExpr(E);
1006 Record.push_back(E->isLvalueCast());
1007 Code = pch::EXPR_IMPLICIT_CAST;
1008}
1009
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001010void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1011 VisitCastExpr(E);
1012 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1013}
1014
1015void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1016 VisitExplicitCastExpr(E);
1017 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1018 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1019 Code = pch::EXPR_CSTYLE_CAST;
1020}
1021
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001022void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1023 VisitExpr(E);
1024 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001025 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001026 Record.push_back(E->isFileScope());
1027 Code = pch::EXPR_COMPOUND_LITERAL;
1028}
1029
Douglas Gregorec0b8292009-04-15 23:02:49 +00001030void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1031 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001032 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001033 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1034 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1035 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1036}
1037
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001038void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1039 VisitExpr(E);
1040 Record.push_back(E->getNumInits());
1041 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001042 Writer.WriteSubStmt(E->getInit(I));
1043 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001044 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1045 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1046 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1047 Record.push_back(E->hadArrayRangeDesignator());
1048 Code = pch::EXPR_INIT_LIST;
1049}
1050
1051void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1052 VisitExpr(E);
1053 Record.push_back(E->getNumSubExprs());
1054 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001055 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001056 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1057 Record.push_back(E->usesGNUSyntax());
1058 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1059 DEnd = E->designators_end();
1060 D != DEnd; ++D) {
1061 if (D->isFieldDesignator()) {
1062 if (FieldDecl *Field = D->getField()) {
1063 Record.push_back(pch::DESIG_FIELD_DECL);
1064 Writer.AddDeclRef(Field, Record);
1065 } else {
1066 Record.push_back(pch::DESIG_FIELD_NAME);
1067 Writer.AddIdentifierRef(D->getFieldName(), Record);
1068 }
1069 Writer.AddSourceLocation(D->getDotLoc(), Record);
1070 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1071 } else if (D->isArrayDesignator()) {
1072 Record.push_back(pch::DESIG_ARRAY);
1073 Record.push_back(D->getFirstExprIndex());
1074 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1075 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1076 } else {
1077 assert(D->isArrayRangeDesignator() && "Unknown designator");
1078 Record.push_back(pch::DESIG_ARRAY_RANGE);
1079 Record.push_back(D->getFirstExprIndex());
1080 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1081 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1082 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1083 }
1084 }
1085 Code = pch::EXPR_DESIGNATED_INIT;
1086}
1087
1088void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1089 VisitExpr(E);
1090 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1091}
1092
Douglas Gregorec0b8292009-04-15 23:02:49 +00001093void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1094 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001095 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001096 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1097 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1098 Code = pch::EXPR_VA_ARG;
1099}
1100
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001101void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1102 VisitExpr(E);
1103 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1104 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1105 Record.push_back(Writer.GetLabelID(E->getLabel()));
1106 Code = pch::EXPR_ADDR_LABEL;
1107}
1108
Douglas Gregoreca12f62009-04-17 19:05:30 +00001109void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1110 VisitExpr(E);
1111 Writer.WriteSubStmt(E->getSubStmt());
1112 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1113 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1114 Code = pch::EXPR_STMT;
1115}
1116
Douglas Gregor209d4622009-04-15 23:33:31 +00001117void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1118 VisitExpr(E);
1119 Writer.AddTypeRef(E->getArgType1(), Record);
1120 Writer.AddTypeRef(E->getArgType2(), Record);
1121 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1122 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1123 Code = pch::EXPR_TYPES_COMPATIBLE;
1124}
1125
1126void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1127 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001128 Writer.WriteSubStmt(E->getCond());
1129 Writer.WriteSubStmt(E->getLHS());
1130 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001131 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1132 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1133 Code = pch::EXPR_CHOOSE;
1134}
1135
1136void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1137 VisitExpr(E);
1138 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1139 Code = pch::EXPR_GNU_NULL;
1140}
1141
Douglas Gregor725e94b2009-04-16 00:01:45 +00001142void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1143 VisitExpr(E);
1144 Record.push_back(E->getNumSubExprs());
1145 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001146 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001147 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1148 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1149 Code = pch::EXPR_SHUFFLE_VECTOR;
1150}
1151
Douglas Gregore246b742009-04-17 19:21:43 +00001152void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1153 VisitExpr(E);
1154 Writer.AddDeclRef(E->getBlockDecl(), Record);
1155 Record.push_back(E->hasBlockDeclRefExprs());
1156 Code = pch::EXPR_BLOCK;
1157}
1158
Douglas Gregor725e94b2009-04-16 00:01:45 +00001159void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1160 VisitExpr(E);
1161 Writer.AddDeclRef(E->getDecl(), Record);
1162 Writer.AddSourceLocation(E->getLocation(), Record);
1163 Record.push_back(E->isByRef());
1164 Code = pch::EXPR_BLOCK_DECL_REF;
1165}
1166
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001167//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001168// Objective-C Expressions and Statements.
1169//===----------------------------------------------------------------------===//
1170
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001171void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1172 VisitExpr(E);
1173 Writer.WriteSubStmt(E->getString());
1174 Writer.AddSourceLocation(E->getAtLoc(), Record);
1175 Code = pch::EXPR_OBJC_STRING_LITERAL;
1176}
1177
Chris Lattner80f83c62009-04-22 05:57:30 +00001178void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1179 VisitExpr(E);
1180 Writer.AddTypeRef(E->getEncodedType(), Record);
1181 Writer.AddSourceLocation(E->getAtLoc(), Record);
1182 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1183 Code = pch::EXPR_OBJC_ENCODE;
1184}
1185
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001186void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1187 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001188 Writer.AddSelectorRef(E->getSelector(), Record);
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001189 Writer.AddSourceLocation(E->getAtLoc(), Record);
1190 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1191 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1192}
1193
1194void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1195 VisitExpr(E);
1196 Writer.AddDeclRef(E->getProtocol(), Record);
1197 Writer.AddSourceLocation(E->getAtLoc(), Record);
1198 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1199 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1200}
1201
Steve Narofffb3e4022009-04-25 14:04:28 +00001202void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1203 VisitExpr(E);
1204 Record.push_back(E->getNumArgs());
1205 Writer.AddSourceLocation(E->getSourceRange().getBegin(), Record);
1206 Writer.AddSourceLocation(E->getSourceRange().getEnd(), Record);
1207 Writer.AddSelectorRef(E->getSelector(), Record);
1208 Writer.AddDeclRef(E->getMethodDecl(), Record); // optional
1209 // FIXME: deal with class messages.
1210 Writer.WriteSubStmt(E->getReceiver());
1211 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
1212 Arg != ArgEnd; ++Arg)
1213 Writer.WriteSubStmt(*Arg);
1214 Code = pch::EXPR_OBJC_MESSAGE_EXPR;
1215}
1216
Chris Lattner80f83c62009-04-22 05:57:30 +00001217
1218//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001219// PCHWriter Implementation
1220//===----------------------------------------------------------------------===//
1221
Douglas Gregorb5887f32009-04-10 21:16:55 +00001222/// \brief Write the target triple (e.g., i686-apple-darwin9).
1223void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1224 using namespace llvm;
1225 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1226 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001228 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001229
1230 RecordData Record;
1231 Record.push_back(pch::TARGET_TRIPLE);
1232 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001233 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001234}
1235
1236/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001237void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1238 RecordData Record;
1239 Record.push_back(LangOpts.Trigraphs);
1240 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1241 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1242 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1243 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1244 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1245 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1246 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1247 Record.push_back(LangOpts.C99); // C99 Support
1248 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1249 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1250 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1251 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1252 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1253
1254 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1255 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1256 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1257
1258 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1259 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1260 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1261 Record.push_back(LangOpts.LaxVectorConversions);
1262 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1263
1264 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1265 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1266 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1267
1268 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1269 // by locks.
1270 Record.push_back(LangOpts.Blocks); // block extension to C
1271 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1272 // they are unused.
1273 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1274 // (modulo the platform support).
1275
1276 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1277 // signed integer arithmetic overflows.
1278
1279 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1280 // may be ripped out at any time.
1281
1282 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1283 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1284 // defined.
1285 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1286 // opposed to __DYNAMIC__).
1287 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1288
1289 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1290 // used (instead of C99 semantics).
1291 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1292 Record.push_back(LangOpts.getGCMode());
1293 Record.push_back(LangOpts.getVisibilityMode());
1294 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001295 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001296}
1297
Douglas Gregorab1cef72009-04-10 03:52:48 +00001298//===----------------------------------------------------------------------===//
1299// Source Manager Serialization
1300//===----------------------------------------------------------------------===//
1301
1302/// \brief Create an abbreviation for the SLocEntry that refers to a
1303/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001304static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001305 using namespace llvm;
1306 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1307 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1308 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1309 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001313 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001314}
1315
1316/// \brief Create an abbreviation for the SLocEntry that refers to a
1317/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001318static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001319 using namespace llvm;
1320 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1321 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1324 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001327 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001328}
1329
1330/// \brief Create an abbreviation for the SLocEntry that refers to a
1331/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001332static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001333 using namespace llvm;
1334 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1335 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001337 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001338}
1339
1340/// \brief Create an abbreviation for the SLocEntry that refers to an
1341/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001342static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001343 using namespace llvm;
1344 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1345 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1348 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1349 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001350 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001351 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001352}
1353
1354/// \brief Writes the block containing the serialized form of the
1355/// source manager.
1356///
1357/// TODO: We should probably use an on-disk hash table (stored in a
1358/// blob), indexed based on the file name, so that we only create
1359/// entries for files that we actually need. In the common case (no
1360/// errors), we probably won't have to create file entries for any of
1361/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001362void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1363 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001364 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001365 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001366
1367 // Abbreviations for the various kinds of source-location entries.
1368 int SLocFileAbbrv = -1;
1369 int SLocBufferAbbrv = -1;
1370 int SLocBufferBlobAbbrv = -1;
1371 int SLocInstantiationAbbrv = -1;
1372
1373 // Write out the source location entry table. We skip the first
1374 // entry, which is always the same dummy entry.
1375 RecordData Record;
1376 for (SourceManager::sloc_entry_iterator
1377 SLoc = SourceMgr.sloc_entry_begin() + 1,
1378 SLocEnd = SourceMgr.sloc_entry_end();
1379 SLoc != SLocEnd; ++SLoc) {
1380 // Figure out which record code to use.
1381 unsigned Code;
1382 if (SLoc->isFile()) {
1383 if (SLoc->getFile().getContentCache()->Entry)
1384 Code = pch::SM_SLOC_FILE_ENTRY;
1385 else
1386 Code = pch::SM_SLOC_BUFFER_ENTRY;
1387 } else
1388 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1389 Record.push_back(Code);
1390
1391 Record.push_back(SLoc->getOffset());
1392 if (SLoc->isFile()) {
1393 const SrcMgr::FileInfo &File = SLoc->getFile();
1394 Record.push_back(File.getIncludeLoc().getRawEncoding());
1395 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001396 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001397
1398 const SrcMgr::ContentCache *Content = File.getContentCache();
1399 if (Content->Entry) {
1400 // The source location entry is a file. The blob associated
1401 // with this entry is the file name.
1402 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001403 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1404 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001405 Content->Entry->getName(),
1406 strlen(Content->Entry->getName()));
1407 } else {
1408 // The source location entry is a buffer. The blob associated
1409 // with this entry contains the contents of the buffer.
1410 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001411 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1412 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001413 }
1414
1415 // We add one to the size so that we capture the trailing NULL
1416 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1417 // the reader side).
1418 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1419 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001420 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001421 Record.clear();
1422 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001423 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001424 Buffer->getBufferStart(),
1425 Buffer->getBufferSize() + 1);
1426 }
1427 } else {
1428 // The source location entry is an instantiation.
1429 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1430 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1431 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1432 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1433
Douglas Gregor364e5802009-04-15 18:05:10 +00001434 // Compute the token length for this macro expansion.
1435 unsigned NextOffset = SourceMgr.getNextOffset();
1436 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1437 if (++NextSLoc != SLocEnd)
1438 NextOffset = NextSLoc->getOffset();
1439 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1440
Douglas Gregorab1cef72009-04-10 03:52:48 +00001441 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001442 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1443 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001444 }
1445
1446 Record.clear();
1447 }
1448
Douglas Gregor635f97f2009-04-13 16:31:14 +00001449 // Write the line table.
1450 if (SourceMgr.hasLineTable()) {
1451 LineTableInfo &LineTable = SourceMgr.getLineTable();
1452
1453 // Emit the file names
1454 Record.push_back(LineTable.getNumFilenames());
1455 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1456 // Emit the file name
1457 const char *Filename = LineTable.getFilename(I);
1458 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1459 Record.push_back(FilenameLen);
1460 if (FilenameLen)
1461 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1462 }
1463
1464 // Emit the line entries
1465 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1466 L != LEnd; ++L) {
1467 // Emit the file ID
1468 Record.push_back(L->first);
1469
1470 // Emit the line entries
1471 Record.push_back(L->second.size());
1472 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1473 LEEnd = L->second.end();
1474 LE != LEEnd; ++LE) {
1475 Record.push_back(LE->FileOffset);
1476 Record.push_back(LE->LineNo);
1477 Record.push_back(LE->FilenameID);
1478 Record.push_back((unsigned)LE->FileKind);
1479 Record.push_back(LE->IncludeOffset);
1480 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001481 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001482 }
1483 }
1484
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001485 // Loop over all the header files.
1486 HeaderSearch &HS = PP.getHeaderSearchInfo();
1487 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1488 E = HS.header_file_end();
1489 I != E; ++I) {
1490 Record.push_back(I->isImport);
1491 Record.push_back(I->DirInfo);
1492 Record.push_back(I->NumIncludes);
1493 if (I->ControllingMacro)
1494 AddIdentifierRef(I->ControllingMacro, Record);
1495 else
1496 Record.push_back(0);
1497 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1498 Record.clear();
1499 }
1500
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001501 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001502}
1503
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001504/// \brief Writes the block containing the serialized form of the
1505/// preprocessor.
1506///
Chris Lattner850eabd2009-04-10 18:08:30 +00001507void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +00001508 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001509
Chris Lattner4b21c202009-04-13 01:29:17 +00001510 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1511 if (PP.getCounterValue() != 0) {
1512 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001513 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001514 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001515 }
1516
1517 // Enter the preprocessor block.
1518 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +00001519
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001520 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1521 // FIXME: use diagnostics subsystem for localization etc.
1522 if (PP.SawDateOrTime())
1523 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1524
Chris Lattner1b094952009-04-10 18:00:12 +00001525 // Loop over all the macro definitions that are live at the end of the file,
1526 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001527 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1528 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001529 // FIXME: This emits macros in hash table order, we should do it in a stable
1530 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001531 MacroInfo *MI = I->second;
1532
1533 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1534 // been redefined by the header (in which case they are not isBuiltinMacro).
1535 if (MI->isBuiltinMacro())
1536 continue;
1537
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001538 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001539 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001540 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001541 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1542 Record.push_back(MI->isUsed());
1543
1544 unsigned Code;
1545 if (MI->isObjectLike()) {
1546 Code = pch::PP_MACRO_OBJECT_LIKE;
1547 } else {
1548 Code = pch::PP_MACRO_FUNCTION_LIKE;
1549
1550 Record.push_back(MI->isC99Varargs());
1551 Record.push_back(MI->isGNUVarargs());
1552 Record.push_back(MI->getNumArgs());
1553 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1554 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001555 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001556 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001557 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001558 Record.clear();
1559
Chris Lattner850eabd2009-04-10 18:08:30 +00001560 // Emit the tokens array.
1561 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1562 // Note that we know that the preprocessor does not have any annotation
1563 // tokens in it because they are created by the parser, and thus can't be
1564 // in a macro definition.
1565 const Token &Tok = MI->getReplacementToken(TokNo);
1566
1567 Record.push_back(Tok.getLocation().getRawEncoding());
1568 Record.push_back(Tok.getLength());
1569
Chris Lattner850eabd2009-04-10 18:08:30 +00001570 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1571 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001572 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001573
1574 // FIXME: Should translate token kind to a stable encoding.
1575 Record.push_back(Tok.getKind());
1576 // FIXME: Should translate token flags to a stable encoding.
1577 Record.push_back(Tok.getFlags());
1578
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001579 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001580 Record.clear();
1581 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001582 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001583 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001584 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001585}
1586
1587
Douglas Gregorc34897d2009-04-09 22:27:44 +00001588/// \brief Write the representation of a type to the PCH stream.
1589void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001590 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001591 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001592 ID = NextTypeID++;
1593
1594 // Record the offset for this type.
1595 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001596 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001597 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1598 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001599 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001600 }
1601
1602 RecordData Record;
1603
1604 // Emit the type's representation.
1605 PCHTypeWriter W(*this, Record);
1606 switch (T->getTypeClass()) {
1607 // For all of the concrete, non-dependent types, call the
1608 // appropriate visitor function.
1609#define TYPE(Class, Base) \
1610 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1611#define ABSTRACT_TYPE(Class, Base)
1612#define DEPENDENT_TYPE(Class, Base)
1613#include "clang/AST/TypeNodes.def"
1614
1615 // For all of the dependent type nodes (which only occur in C++
1616 // templates), produce an error.
1617#define TYPE(Class, Base)
1618#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1619#include "clang/AST/TypeNodes.def"
1620 assert(false && "Cannot serialize dependent type nodes");
1621 break;
1622 }
1623
1624 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001625 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001626
1627 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001628 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001629}
1630
1631/// \brief Write a block containing all of the types.
1632void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001633 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001634 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001635
1636 // Emit all of the types in the ASTContext
1637 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1638 TEnd = Context.getTypes().end();
1639 T != TEnd; ++T) {
1640 // Builtin types are never serialized.
1641 if (isa<BuiltinType>(*T))
1642 continue;
1643
1644 WriteType(*T);
1645 }
1646
1647 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001648 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001649}
1650
1651/// \brief Write the block containing all of the declaration IDs
1652/// lexically declared within the given DeclContext.
1653///
1654/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1655/// bistream, or 0 if no block was written.
1656uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1657 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001658 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001659 return 0;
1660
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001661 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001662 RecordData Record;
1663 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1664 DEnd = DC->decls_end(Context);
1665 D != DEnd; ++D)
1666 AddDeclRef(*D, Record);
1667
Douglas Gregoraf136d92009-04-22 22:34:57 +00001668 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001669 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001670 return Offset;
1671}
1672
1673/// \brief Write the block containing all of the declaration IDs
1674/// visible from the given DeclContext.
1675///
1676/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1677/// bistream, or 0 if no block was written.
1678uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1679 DeclContext *DC) {
1680 if (DC->getPrimaryContext() != DC)
1681 return 0;
1682
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001683 // Since there is no name lookup into functions or methods, and we
1684 // perform name lookup for the translation unit via the
1685 // IdentifierInfo chains, don't bother to build a
1686 // visible-declarations table for these entities.
1687 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001688 return 0;
1689
Douglas Gregorc34897d2009-04-09 22:27:44 +00001690 // Force the DeclContext to build a its name-lookup table.
1691 DC->lookup(Context, DeclarationName());
1692
1693 // Serialize the contents of the mapping used for lookup. Note that,
1694 // although we have two very different code paths, the serialized
1695 // representation is the same for both cases: a declaration name,
1696 // followed by a size, followed by references to the visible
1697 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001698 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001699 RecordData Record;
1700 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001701 if (!Map)
1702 return 0;
1703
Douglas Gregorc34897d2009-04-09 22:27:44 +00001704 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1705 D != DEnd; ++D) {
1706 AddDeclarationName(D->first, Record);
1707 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1708 Record.push_back(Result.second - Result.first);
1709 for(; Result.first != Result.second; ++Result.first)
1710 AddDeclRef(*Result.first, Record);
1711 }
1712
1713 if (Record.size() == 0)
1714 return 0;
1715
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001716 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001717 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001718 return Offset;
1719}
1720
1721/// \brief Write a block containing all of the declarations.
1722void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001723 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001724 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001725
1726 // Emit all of the declarations.
1727 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001728 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001729 while (!DeclsToEmit.empty()) {
1730 // Pull the next declaration off the queue
1731 Decl *D = DeclsToEmit.front();
1732 DeclsToEmit.pop();
1733
1734 // If this declaration is also a DeclContext, write blocks for the
1735 // declarations that lexically stored inside its context and those
1736 // declarations that are visible from its context. These blocks
1737 // are written before the declaration itself so that we can put
1738 // their offsets into the record for the declaration.
1739 uint64_t LexicalOffset = 0;
1740 uint64_t VisibleOffset = 0;
1741 DeclContext *DC = dyn_cast<DeclContext>(D);
1742 if (DC) {
1743 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1744 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1745 }
1746
1747 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001748 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001749 if (ID == 0)
1750 ID = DeclIDs.size();
1751
1752 unsigned Index = ID - 1;
1753
1754 // Record the offset for this declaration
1755 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001756 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001757 else if (DeclOffsets.size() < Index) {
1758 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001759 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001760 }
1761
1762 // Build and emit a record for this declaration
1763 Record.clear();
1764 W.Code = (pch::DeclCode)0;
1765 W.Visit(D);
1766 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001767
1768 if (!W.Code) {
1769 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1770 D->getDeclKindName());
1771 assert(false && "Unhandled declaration kind while generating PCH");
1772 exit(-1);
1773 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001774 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001775
Douglas Gregor1c507882009-04-15 21:30:51 +00001776 // If the declaration had any attributes, write them now.
1777 if (D->hasAttrs())
1778 WriteAttributeRecord(D->getAttrs());
1779
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001780 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001781 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001782
Douglas Gregor631f6c62009-04-14 00:24:19 +00001783 // Note external declarations so that we can add them to a record
1784 // in the PCH file later.
1785 if (isa<FileScopeAsmDecl>(D))
1786 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001787 }
1788
1789 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001790 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001791}
1792
Douglas Gregorff9a6092009-04-20 20:36:09 +00001793namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001794// Trait used for the on-disk hash table used in the method pool.
1795class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1796 PCHWriter &Writer;
1797
1798public:
1799 typedef Selector key_type;
1800 typedef key_type key_type_ref;
1801
1802 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1803 typedef const data_type& data_type_ref;
1804
1805 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1806
1807 static unsigned ComputeHash(Selector Sel) {
1808 unsigned N = Sel.getNumArgs();
1809 if (N == 0)
1810 ++N;
1811 unsigned R = 5381;
1812 for (unsigned I = 0; I != N; ++I)
1813 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1814 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1815 return R;
1816 }
1817
1818 std::pair<unsigned,unsigned>
1819 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1820 data_type_ref Methods) {
1821 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1822 clang::io::Emit16(Out, KeyLen);
1823 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1824 for (const ObjCMethodList *Method = &Methods.first; Method;
1825 Method = Method->Next)
1826 if (Method->Method)
1827 DataLen += 4;
1828 for (const ObjCMethodList *Method = &Methods.second; Method;
1829 Method = Method->Next)
1830 if (Method->Method)
1831 DataLen += 4;
1832 clang::io::Emit16(Out, DataLen);
1833 return std::make_pair(KeyLen, DataLen);
1834 }
1835
Douglas Gregor2d711832009-04-25 17:48:32 +00001836 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1837 uint64_t Start = Out.tell();
1838 assert((Start >> 32) == 0 && "Selector key offset too large");
1839 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001840 unsigned N = Sel.getNumArgs();
1841 clang::io::Emit16(Out, N);
1842 if (N == 0)
1843 N = 1;
1844 for (unsigned I = 0; I != N; ++I)
1845 clang::io::Emit32(Out,
1846 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1847 }
1848
1849 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001850 data_type_ref Methods, unsigned DataLen) {
1851 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001852 unsigned NumInstanceMethods = 0;
1853 for (const ObjCMethodList *Method = &Methods.first; Method;
1854 Method = Method->Next)
1855 if (Method->Method)
1856 ++NumInstanceMethods;
1857
1858 unsigned NumFactoryMethods = 0;
1859 for (const ObjCMethodList *Method = &Methods.second; Method;
1860 Method = Method->Next)
1861 if (Method->Method)
1862 ++NumFactoryMethods;
1863
1864 clang::io::Emit16(Out, NumInstanceMethods);
1865 clang::io::Emit16(Out, NumFactoryMethods);
1866 for (const ObjCMethodList *Method = &Methods.first; Method;
1867 Method = Method->Next)
1868 if (Method->Method)
1869 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001870 for (const ObjCMethodList *Method = &Methods.second; Method;
1871 Method = Method->Next)
1872 if (Method->Method)
1873 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001874
1875 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001876 }
1877};
1878} // end anonymous namespace
1879
1880/// \brief Write the method pool into the PCH file.
1881///
1882/// The method pool contains both instance and factory methods, stored
1883/// in an on-disk hash table indexed by the selector.
1884void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1885 using namespace llvm;
1886
1887 // Create and write out the blob that contains the instance and
1888 // factor method pools.
1889 bool Empty = true;
1890 {
1891 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1892
1893 // Create the on-disk hash table representation. Start by
1894 // iterating through the instance method pool.
1895 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001896 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001897 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1898 Instance = SemaRef.InstanceMethodPool.begin(),
1899 InstanceEnd = SemaRef.InstanceMethodPool.end();
1900 Instance != InstanceEnd; ++Instance) {
1901 // Check whether there is a factory method with the same
1902 // selector.
1903 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1904 = SemaRef.FactoryMethodPool.find(Instance->first);
1905
1906 if (Factory == SemaRef.FactoryMethodPool.end())
1907 Generator.insert(Instance->first,
1908 std::make_pair(Instance->second,
1909 ObjCMethodList()));
1910 else
1911 Generator.insert(Instance->first,
1912 std::make_pair(Instance->second, Factory->second));
1913
Douglas Gregor2d711832009-04-25 17:48:32 +00001914 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001915 Empty = false;
1916 }
1917
1918 // Now iterate through the factory method pool, to pick up any
1919 // selectors that weren't already in the instance method pool.
1920 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1921 Factory = SemaRef.FactoryMethodPool.begin(),
1922 FactoryEnd = SemaRef.FactoryMethodPool.end();
1923 Factory != FactoryEnd; ++Factory) {
1924 // Check whether there is an instance method with the same
1925 // selector. If so, there is no work to do here.
1926 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1927 = SemaRef.InstanceMethodPool.find(Factory->first);
1928
Douglas Gregor2d711832009-04-25 17:48:32 +00001929 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001930 Generator.insert(Factory->first,
1931 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001932 ++NumSelectorsInMethodPool;
1933 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001934
1935 Empty = false;
1936 }
1937
Douglas Gregor2d711832009-04-25 17:48:32 +00001938 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001939 return;
1940
1941 // Create the on-disk hash table in a buffer.
1942 llvm::SmallVector<char, 4096> MethodPool;
1943 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001944 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001945 {
1946 PCHMethodPoolTrait Trait(*this);
1947 llvm::raw_svector_ostream Out(MethodPool);
1948 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001949 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001950 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001951
1952 // For every selector that we have seen but which was not
1953 // written into the hash table, write the selector itself and
1954 // record it's offset.
1955 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1956 if (SelectorOffsets[I] == 0)
1957 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001958 }
1959
1960 // Create a blob abbreviation
1961 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1962 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00001964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1966 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1967
Douglas Gregor2d711832009-04-25 17:48:32 +00001968 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001969 RecordData Record;
1970 Record.push_back(pch::METHOD_POOL);
1971 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00001972 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001973 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
1974 &MethodPool.front(),
1975 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00001976
1977 // Create a blob abbreviation for the selector table offsets.
1978 Abbrev = new BitCodeAbbrev();
1979 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1981 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1982 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1983
1984 // Write the selector offsets table.
1985 Record.clear();
1986 Record.push_back(pch::SELECTOR_OFFSETS);
1987 Record.push_back(SelectorOffsets.size());
1988 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1989 (const char *)&SelectorOffsets.front(),
1990 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001991 }
1992}
1993
1994namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001995class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1996 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001997 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001998
1999public:
2000 typedef const IdentifierInfo* key_type;
2001 typedef key_type key_type_ref;
2002
2003 typedef pch::IdentID data_type;
2004 typedef data_type data_type_ref;
2005
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002006 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
2007 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00002008
2009 static unsigned ComputeHash(const IdentifierInfo* II) {
2010 return clang::BernsteinHash(II->getName());
2011 }
2012
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002013 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00002014 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
2015 pch::IdentID ID) {
2016 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00002017 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
2018 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002019 if (II->hasMacroDefinition() &&
2020 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
2021 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002022 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2023 DEnd = IdentifierResolver::end();
2024 D != DEnd; ++D)
2025 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002026 // We emit the key length after the data length so that the
2027 // "uninteresting" identifiers following the identifier hash table
2028 // structure will have the same (key length, key characters)
2029 // layout as the keys in the hash table. This also matches the
2030 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00002031 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002032 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002033 return std::make_pair(KeyLen, DataLen);
2034 }
2035
2036 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
2037 unsigned KeyLen) {
2038 // Record the location of the key data. This is used when generating
2039 // the mapping from persistent IDs to strings.
2040 Writer.SetIdentifierOffset(II, Out.tell());
2041 Out.write(II->getName(), KeyLen);
2042 }
2043
2044 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
2045 pch::IdentID ID, unsigned) {
2046 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002047 bool hasMacroDefinition =
2048 II->hasMacroDefinition() &&
2049 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002050 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002051 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2052 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002053 Bits = (Bits << 1) | II->isExtensionToken();
2054 Bits = (Bits << 1) | II->isPoisoned();
2055 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2056 clang::io::Emit32(Out, Bits);
2057 clang::io::Emit32(Out, ID);
2058
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002059 if (hasMacroDefinition)
2060 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2061
Douglas Gregorc713da92009-04-21 22:25:48 +00002062 // Emit the declaration IDs in reverse order, because the
2063 // IdentifierResolver provides the declarations as they would be
2064 // visible (e.g., the function "stat" would come before the struct
2065 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2066 // adds declarations to the end of the list (so we need to see the
2067 // struct "status" before the function "status").
2068 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2069 IdentifierResolver::end());
2070 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2071 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002072 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00002073 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002074 }
2075};
2076} // end anonymous namespace
2077
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002078/// \brief Write the identifier table into the PCH file.
2079///
2080/// The identifier table consists of a blob containing string data
2081/// (the actual identifiers themselves) and a separate "offsets" index
2082/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002083void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002084 using namespace llvm;
2085
2086 // Create and write out the blob that contains the identifier
2087 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00002088 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002089 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002090 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2091
Douglas Gregor85c4a872009-04-25 21:04:17 +00002092 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
2093
Douglas Gregorff9a6092009-04-20 20:36:09 +00002094 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002095 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2096 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2097 ID != IDEnd; ++ID) {
2098 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00002099
2100 // Classify each identifier as either "interesting" or "not
2101 // interesting". Interesting identifiers are those that have
2102 // additional information that needs to be read from the PCH
2103 // file, e.g., a built-in ID, declaration chain, or macro
2104 // definition. These identifiers are placed into the hash table
2105 // so that they can be found when looked up in the user program.
2106 // All other identifiers are "uninteresting", which means that
2107 // the IdentifierInfo built by default has all of the
2108 // information we care about. Such identifiers are placed after
2109 // the hash table.
2110 const IdentifierInfo *II = ID->first;
2111 if (II->isPoisoned() ||
2112 II->isExtensionToken() ||
2113 II->hasMacroDefinition() ||
2114 II->getObjCOrBuiltinID() ||
2115 II->getFETokenInfo<void>())
2116 Generator.insert(ID->first, ID->second);
2117 else
2118 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002119 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002120
Douglas Gregorff9a6092009-04-20 20:36:09 +00002121 // Create the on-disk hash table in a buffer.
2122 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00002123 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002124 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002125 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002126 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002127 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002128 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00002129 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002130
2131 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
2132 const IdentifierInfo *II = UninterestingIdentifiers[I];
2133 unsigned N = II->getLength() + 1;
2134 clang::io::Emit16(Out, N);
2135 SetIdentifierOffset(II, Out.tell());
2136 Out.write(II->getName(), N);
2137 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002138 }
2139
2140 // Create a blob abbreviation
2141 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2142 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002145 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002146
2147 // Write the identifier table
2148 RecordData Record;
2149 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002150 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002151 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2152 &IdentifierTable.front(),
2153 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002154 }
2155
2156 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002157 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2158 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2161 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2162
2163 RecordData Record;
2164 Record.push_back(pch::IDENTIFIER_OFFSET);
2165 Record.push_back(IdentifierOffsets.size());
2166 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2167 (const char *)&IdentifierOffsets.front(),
2168 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002169}
2170
Douglas Gregor1c507882009-04-15 21:30:51 +00002171/// \brief Write a record containing the given attributes.
2172void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2173 RecordData Record;
2174 for (; Attr; Attr = Attr->getNext()) {
2175 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2176 Record.push_back(Attr->isInherited());
2177 switch (Attr->getKind()) {
2178 case Attr::Alias:
2179 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2180 break;
2181
2182 case Attr::Aligned:
2183 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2184 break;
2185
2186 case Attr::AlwaysInline:
2187 break;
2188
2189 case Attr::AnalyzerNoReturn:
2190 break;
2191
2192 case Attr::Annotate:
2193 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2194 break;
2195
2196 case Attr::AsmLabel:
2197 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2198 break;
2199
2200 case Attr::Blocks:
2201 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2202 break;
2203
2204 case Attr::Cleanup:
2205 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2206 break;
2207
2208 case Attr::Const:
2209 break;
2210
2211 case Attr::Constructor:
2212 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2213 break;
2214
2215 case Attr::DLLExport:
2216 case Attr::DLLImport:
2217 case Attr::Deprecated:
2218 break;
2219
2220 case Attr::Destructor:
2221 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2222 break;
2223
2224 case Attr::FastCall:
2225 break;
2226
2227 case Attr::Format: {
2228 const FormatAttr *Format = cast<FormatAttr>(Attr);
2229 AddString(Format->getType(), Record);
2230 Record.push_back(Format->getFormatIdx());
2231 Record.push_back(Format->getFirstArg());
2232 break;
2233 }
2234
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002235 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002236 case Attr::IBOutletKind:
2237 case Attr::NoReturn:
2238 case Attr::NoThrow:
2239 case Attr::Nodebug:
2240 case Attr::Noinline:
2241 break;
2242
2243 case Attr::NonNull: {
2244 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2245 Record.push_back(NonNull->size());
2246 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2247 break;
2248 }
2249
2250 case Attr::ObjCException:
2251 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002252 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002253 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002254 case Attr::Overloadable:
2255 break;
2256
2257 case Attr::Packed:
2258 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2259 break;
2260
2261 case Attr::Pure:
2262 break;
2263
2264 case Attr::Regparm:
2265 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2266 break;
2267
2268 case Attr::Section:
2269 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2270 break;
2271
2272 case Attr::StdCall:
2273 case Attr::TransparentUnion:
2274 case Attr::Unavailable:
2275 case Attr::Unused:
2276 case Attr::Used:
2277 break;
2278
2279 case Attr::Visibility:
2280 // FIXME: stable encoding
2281 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2282 break;
2283
2284 case Attr::WarnUnusedResult:
2285 case Attr::Weak:
2286 case Attr::WeakImport:
2287 break;
2288 }
2289 }
2290
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002291 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002292}
2293
2294void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2295 Record.push_back(Str.size());
2296 Record.insert(Record.end(), Str.begin(), Str.end());
2297}
2298
Douglas Gregorff9a6092009-04-20 20:36:09 +00002299/// \brief Note that the identifier II occurs at the given offset
2300/// within the identifier table.
2301void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002302 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002303}
2304
Douglas Gregor2d711832009-04-25 17:48:32 +00002305/// \brief Note that the selector Sel occurs at the given offset
2306/// within the method pool/selector table.
2307void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2308 unsigned ID = SelectorIDs[Sel];
2309 assert(ID && "Unknown selector");
2310 SelectorOffsets[ID - 1] = Offset;
2311}
2312
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002313PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002314 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002315 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2316 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002317
Douglas Gregor87887da2009-04-20 15:53:59 +00002318void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002319 using namespace llvm;
2320
Douglas Gregor87887da2009-04-20 15:53:59 +00002321 ASTContext &Context = SemaRef.Context;
2322 Preprocessor &PP = SemaRef.PP;
2323
Douglas Gregorc34897d2009-04-09 22:27:44 +00002324 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002325 Stream.Emit((unsigned)'C', 8);
2326 Stream.Emit((unsigned)'P', 8);
2327 Stream.Emit((unsigned)'C', 8);
2328 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002329
2330 // The translation unit is the first declaration we'll emit.
2331 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2332 DeclsToEmit.push(Context.getTranslationUnitDecl());
2333
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002334 // Make sure that we emit IdentifierInfos (and any attached
2335 // declarations) for builtins.
2336 {
2337 IdentifierTable &Table = PP.getIdentifierTable();
2338 llvm::SmallVector<const char *, 32> BuiltinNames;
2339 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2340 Context.getLangOptions().NoBuiltin);
2341 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2342 getIdentifierRef(&Table.get(BuiltinNames[I]));
2343 }
2344
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002345 // Build a record containing all of the tentative definitions in
2346 // this header file. Generally, this record will be empty.
2347 RecordData TentativeDefinitions;
2348 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2349 TD = SemaRef.TentativeDefinitions.begin(),
2350 TDEnd = SemaRef.TentativeDefinitions.end();
2351 TD != TDEnd; ++TD)
2352 AddDeclRef(TD->second, TentativeDefinitions);
2353
Douglas Gregor062d9482009-04-22 22:18:58 +00002354 // Build a record containing all of the locally-scoped external
2355 // declarations in this header file. Generally, this record will be
2356 // empty.
2357 RecordData LocallyScopedExternalDecls;
2358 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2359 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2360 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2361 TD != TDEnd; ++TD)
2362 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2363
Douglas Gregorc34897d2009-04-09 22:27:44 +00002364 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002365 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002366 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002367 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002368 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002369 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002370 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002371 WriteTypesBlock(Context);
2372 WriteDeclsBlock(Context);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002373 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002374 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002375
2376 // Write the type offsets array
2377 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2378 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2381 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2382 Record.clear();
2383 Record.push_back(pch::TYPE_OFFSET);
2384 Record.push_back(TypeOffsets.size());
2385 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2386 (const char *)&TypeOffsets.front(),
2387 TypeOffsets.size() * sizeof(uint64_t));
2388
2389 // Write the declaration offsets array
2390 Abbrev = new BitCodeAbbrev();
2391 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2394 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2395 Record.clear();
2396 Record.push_back(pch::DECL_OFFSET);
2397 Record.push_back(DeclOffsets.size());
2398 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2399 (const char *)&DeclOffsets.front(),
2400 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002401
2402 // Write the record of special types.
2403 Record.clear();
2404 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002405 AddTypeRef(Context.getObjCIdType(), Record);
2406 AddTypeRef(Context.getObjCSelType(), Record);
2407 AddTypeRef(Context.getObjCProtoType(), Record);
2408 AddTypeRef(Context.getObjCClassType(), Record);
2409 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2410 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002411 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2412
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002413 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002414 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002415 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002416
2417 // Write the record containing tentative definitions.
2418 if (!TentativeDefinitions.empty())
2419 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002420
2421 // Write the record containing locally-scoped external definitions.
2422 if (!LocallyScopedExternalDecls.empty())
2423 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2424 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002425
2426 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002427 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002428 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002429 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002430 Record.push_back(NumLexicalDeclContexts);
2431 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002432 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002433 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002434}
2435
2436void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2437 Record.push_back(Loc.getRawEncoding());
2438}
2439
2440void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2441 Record.push_back(Value.getBitWidth());
2442 unsigned N = Value.getNumWords();
2443 const uint64_t* Words = Value.getRawData();
2444 for (unsigned I = 0; I != N; ++I)
2445 Record.push_back(Words[I]);
2446}
2447
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002448void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2449 Record.push_back(Value.isUnsigned());
2450 AddAPInt(Value, Record);
2451}
2452
Douglas Gregore2f37202009-04-14 21:55:33 +00002453void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2454 AddAPInt(Value.bitcastToAPInt(), Record);
2455}
2456
Douglas Gregorc34897d2009-04-09 22:27:44 +00002457void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002458 Record.push_back(getIdentifierRef(II));
2459}
2460
2461pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2462 if (II == 0)
2463 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002464
2465 pch::IdentID &ID = IdentifierIDs[II];
2466 if (ID == 0)
2467 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002468 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002469}
2470
Steve Naroff9e84d782009-04-23 10:39:46 +00002471void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2472 if (SelRef.getAsOpaquePtr() == 0) {
2473 Record.push_back(0);
2474 return;
2475 }
2476
2477 pch::SelectorID &SID = SelectorIDs[SelRef];
2478 if (SID == 0) {
2479 SID = SelectorIDs.size();
2480 SelVector.push_back(SelRef);
2481 }
2482 Record.push_back(SID);
2483}
2484
Douglas Gregorc34897d2009-04-09 22:27:44 +00002485void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2486 if (T.isNull()) {
2487 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2488 return;
2489 }
2490
2491 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002492 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002493 switch (BT->getKind()) {
2494 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2495 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2496 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2497 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2498 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2499 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2500 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2501 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2502 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2503 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2504 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2505 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2506 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2507 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2508 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2509 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2510 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2511 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2512 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2513 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2514 }
2515
2516 Record.push_back((ID << 3) | T.getCVRQualifiers());
2517 return;
2518 }
2519
Douglas Gregorac8f2802009-04-10 17:25:41 +00002520 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002521 if (ID == 0) // we haven't seen this type before
2522 ID = NextTypeID++;
2523
2524 // Encode the type qualifiers in the type reference.
2525 Record.push_back((ID << 3) | T.getCVRQualifiers());
2526}
2527
2528void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2529 if (D == 0) {
2530 Record.push_back(0);
2531 return;
2532 }
2533
Douglas Gregorac8f2802009-04-10 17:25:41 +00002534 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002535 if (ID == 0) {
2536 // We haven't seen this declaration before. Give it a new ID and
2537 // enqueue it in the list of declarations to emit.
2538 ID = DeclIDs.size();
2539 DeclsToEmit.push(const_cast<Decl *>(D));
2540 }
2541
2542 Record.push_back(ID);
2543}
2544
Douglas Gregorff9a6092009-04-20 20:36:09 +00002545pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2546 if (D == 0)
2547 return 0;
2548
2549 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2550 return DeclIDs[D];
2551}
2552
Douglas Gregorc34897d2009-04-09 22:27:44 +00002553void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2554 Record.push_back(Name.getNameKind());
2555 switch (Name.getNameKind()) {
2556 case DeclarationName::Identifier:
2557 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2558 break;
2559
2560 case DeclarationName::ObjCZeroArgSelector:
2561 case DeclarationName::ObjCOneArgSelector:
2562 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002563 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002564 break;
2565
2566 case DeclarationName::CXXConstructorName:
2567 case DeclarationName::CXXDestructorName:
2568 case DeclarationName::CXXConversionFunctionName:
2569 AddTypeRef(Name.getCXXNameType(), Record);
2570 break;
2571
2572 case DeclarationName::CXXOperatorName:
2573 Record.push_back(Name.getCXXOverloadedOperator());
2574 break;
2575
2576 case DeclarationName::CXXUsingDirective:
2577 // No extra data to emit
2578 break;
2579 }
2580}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002581
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002582/// \brief Write the given substatement or subexpression to the
2583/// bitstream.
2584void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002585 RecordData Record;
2586 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002587 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002588
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002589 if (!S) {
2590 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002591 return;
2592 }
2593
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002594 Writer.Code = pch::STMT_NULL_PTR;
2595 Writer.Visit(S);
2596 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002597 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002598 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002599}
2600
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002601/// \brief Flush all of the statements that have been added to the
2602/// queue via AddStmt().
2603void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002604 RecordData Record;
2605 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002606
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002607 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002608 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002609 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002610
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002611 if (!S) {
2612 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002613 continue;
2614 }
2615
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002616 Writer.Code = pch::STMT_NULL_PTR;
2617 Writer.Visit(S);
2618 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002619 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002620 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002621
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002622 assert(N == StmtsToEmit.size() &&
2623 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002624
2625 // Note that we are at the end of a full expression. Any
2626 // expression records that follow this one are part of a different
2627 // expression.
2628 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002629 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002630 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002631
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002632 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002633 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002634}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002635
2636unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2637 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2638 "SwitchCase recorded twice");
2639 unsigned NextID = SwitchCaseIDs.size();
2640 SwitchCaseIDs[S] = NextID;
2641 return NextID;
2642}
2643
2644unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2645 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2646 "SwitchCase hasn't been seen yet");
2647 return SwitchCaseIDs[S];
2648}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002649
2650/// \brief Retrieve the ID for the given label statement, which may
2651/// or may not have been emitted yet.
2652unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2653 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2654 if (Pos != LabelIDs.end())
2655 return Pos->second;
2656
2657 unsigned NextID = LabelIDs.size();
2658 LabelIDs[S] = NextID;
2659 return NextID;
2660}