blob: 16eaf982fefd5c67b6e47ed94734dde4cc4e51cd [file] [log] [blame]
Douglas Gregor2cf26342009-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 Gregore7785042009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregor3251ceb2009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000036#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000037using namespace clang;
38
39//===----------------------------------------------------------------------===//
40// Type serialization
41//===----------------------------------------------------------------------===//
42namespace {
43 class VISIBILITY_HIDDEN PCHTypeWriter {
44 PCHWriter &Writer;
45 PCHWriter::RecordData &Record;
46
47 public:
48 /// \brief Type code that corresponds to the record generated.
49 pch::TypeCode Code;
50
51 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
52 : Writer(Writer), Record(Record) { }
53
54 void VisitArrayType(const ArrayType *T);
55 void VisitFunctionType(const FunctionType *T);
56 void VisitTagType(const TagType *T);
57
58#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
59#define ABSTRACT_TYPE(Class, Base)
60#define DEPENDENT_TYPE(Class, Base)
61#include "clang/AST/TypeNodes.def"
62 };
63}
64
65void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
66 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
67 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
68 Record.push_back(T->getAddressSpace());
69 Code = pch::TYPE_EXT_QUAL;
70}
71
72void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
73 assert(false && "Built-in types are never serialized");
74}
75
76void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
77 Record.push_back(T->getWidth());
78 Record.push_back(T->isSigned());
79 Code = pch::TYPE_FIXED_WIDTH_INT;
80}
81
82void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
83 Writer.AddTypeRef(T->getElementType(), Record);
84 Code = pch::TYPE_COMPLEX;
85}
86
87void PCHTypeWriter::VisitPointerType(const PointerType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_POINTER;
90}
91
92void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_BLOCK_POINTER;
95}
96
97void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
98 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Code = pch::TYPE_LVALUE_REFERENCE;
100}
101
102void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
103 Writer.AddTypeRef(T->getPointeeType(), Record);
104 Code = pch::TYPE_RVALUE_REFERENCE;
105}
106
107void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
108 Writer.AddTypeRef(T->getPointeeType(), Record);
109 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
110 Code = pch::TYPE_MEMBER_POINTER;
111}
112
113void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
114 Writer.AddTypeRef(T->getElementType(), Record);
115 Record.push_back(T->getSizeModifier()); // FIXME: stable values
116 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
117}
118
119void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
120 VisitArrayType(T);
121 Writer.AddAPInt(T->getSize(), Record);
122 Code = pch::TYPE_CONSTANT_ARRAY;
123}
124
125void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
126 VisitArrayType(T);
127 Code = pch::TYPE_INCOMPLETE_ARRAY;
128}
129
130void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
131 VisitArrayType(T);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000132 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133 Code = pch::TYPE_VARIABLE_ARRAY;
134}
135
136void PCHTypeWriter::VisitVectorType(const VectorType *T) {
137 Writer.AddTypeRef(T->getElementType(), Record);
138 Record.push_back(T->getNumElements());
139 Code = pch::TYPE_VECTOR;
140}
141
142void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
143 VisitVectorType(T);
144 Code = pch::TYPE_EXT_VECTOR;
145}
146
147void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
148 Writer.AddTypeRef(T->getResultType(), Record);
149}
150
151void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
152 VisitFunctionType(T);
153 Code = pch::TYPE_FUNCTION_NO_PROTO;
154}
155
156void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
157 VisitFunctionType(T);
158 Record.push_back(T->getNumArgs());
159 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
160 Writer.AddTypeRef(T->getArgType(I), Record);
161 Record.push_back(T->isVariadic());
162 Record.push_back(T->getTypeQuals());
163 Code = pch::TYPE_FUNCTION_PROTO;
164}
165
166void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
167 Writer.AddDeclRef(T->getDecl(), Record);
168 Code = pch::TYPE_TYPEDEF;
169}
170
171void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000172 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000173 Code = pch::TYPE_TYPEOF_EXPR;
174}
175
176void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
177 Writer.AddTypeRef(T->getUnderlyingType(), Record);
178 Code = pch::TYPE_TYPEOF;
179}
180
181void PCHTypeWriter::VisitTagType(const TagType *T) {
182 Writer.AddDeclRef(T->getDecl(), Record);
183 assert(!T->isBeingDefined() &&
184 "Cannot serialize in the middle of a type definition");
185}
186
187void PCHTypeWriter::VisitRecordType(const RecordType *T) {
188 VisitTagType(T);
189 Code = pch::TYPE_RECORD;
190}
191
192void PCHTypeWriter::VisitEnumType(const EnumType *T) {
193 VisitTagType(T);
194 Code = pch::TYPE_ENUM;
195}
196
197void
198PCHTypeWriter::VisitTemplateSpecializationType(
199 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000200 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201 assert(false && "Cannot serialize template specialization types");
202}
203
204void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000205 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000206 assert(false && "Cannot serialize qualified name types");
207}
208
209void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
210 Writer.AddDeclRef(T->getDecl(), Record);
211 Code = pch::TYPE_OBJC_INTERFACE;
212}
213
214void
215PCHTypeWriter::VisitObjCQualifiedInterfaceType(
216 const ObjCQualifiedInterfaceType *T) {
217 VisitObjCInterfaceType(T);
218 Record.push_back(T->getNumProtocols());
219 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
220 Writer.AddDeclRef(T->getProtocol(I), Record);
221 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
222}
223
224void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
225 Record.push_back(T->getNumProtocols());
226 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
227 Writer.AddDeclRef(T->getProtocols(I), Record);
228 Code = pch::TYPE_OBJC_QUALIFIED_ID;
229}
230
231void
232PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) {
233 Record.push_back(T->getNumProtocols());
234 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
235 Writer.AddDeclRef(T->getProtocols(I), Record);
236 Code = pch::TYPE_OBJC_QUALIFIED_CLASS;
237}
238
239//===----------------------------------------------------------------------===//
240// Declaration serialization
241//===----------------------------------------------------------------------===//
242namespace {
243 class VISIBILITY_HIDDEN PCHDeclWriter
244 : public DeclVisitor<PCHDeclWriter, void> {
245
246 PCHWriter &Writer;
Douglas Gregor72971342009-04-18 00:02:19 +0000247 ASTContext &Context;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 PCHWriter::RecordData &Record;
249
250 public:
251 pch::DeclCode Code;
252
Douglas Gregor72971342009-04-18 00:02:19 +0000253 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
254 PCHWriter::RecordData &Record)
255 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256
257 void VisitDecl(Decl *D);
258 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
259 void VisitNamedDecl(NamedDecl *D);
260 void VisitTypeDecl(TypeDecl *D);
261 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000262 void VisitTagDecl(TagDecl *D);
263 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000264 void VisitRecordDecl(RecordDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000265 void VisitValueDecl(ValueDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000266 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000267 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000268 void VisitFieldDecl(FieldDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000269 void VisitVarDecl(VarDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000270 void VisitParmVarDecl(ParmVarDecl *D);
271 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor1028bc62009-04-13 22:49:25 +0000272 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
273 void VisitBlockDecl(BlockDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000274 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
275 uint64_t VisibleOffset);
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000276 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff33feeb02009-04-20 20:09:33 +0000277 void VisitObjCContainerDecl(ObjCContainerDecl *D);
278 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
279 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff30833f82009-04-21 15:12:33 +0000280 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
281 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
282 void VisitObjCClassDecl(ObjCClassDecl *D);
283 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
284 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
285 void VisitObjCImplDecl(ObjCImplDecl *D);
286 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
287 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
288 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
289 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
290 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000291 };
292}
293
294void PCHDeclWriter::VisitDecl(Decl *D) {
295 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
296 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
297 Writer.AddSourceLocation(D->getLocation(), Record);
298 Record.push_back(D->isInvalidDecl());
Douglas Gregor68a2eb02009-04-15 21:30:51 +0000299 Record.push_back(D->hasAttrs());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000300 Record.push_back(D->isImplicit());
301 Record.push_back(D->getAccess());
302}
303
304void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
305 VisitDecl(D);
306 Code = pch::DECL_TRANSLATION_UNIT;
307}
308
309void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
310 VisitDecl(D);
311 Writer.AddDeclarationName(D->getDeclName(), Record);
312}
313
314void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
315 VisitNamedDecl(D);
316 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
317}
318
319void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
320 VisitTypeDecl(D);
321 Writer.AddTypeRef(D->getUnderlyingType(), Record);
322 Code = pch::DECL_TYPEDEF;
323}
324
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000325void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
326 VisitTypeDecl(D);
327 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
328 Record.push_back(D->isDefinition());
329 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
330}
331
332void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
333 VisitTagDecl(D);
334 Writer.AddTypeRef(D->getIntegerType(), Record);
335 Code = pch::DECL_ENUM;
336}
337
Douglas Gregor8c700062009-04-13 21:20:57 +0000338void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
339 VisitTagDecl(D);
340 Record.push_back(D->hasFlexibleArrayMember());
341 Record.push_back(D->isAnonymousStructOrUnion());
342 Code = pch::DECL_RECORD;
343}
344
Douglas Gregor2cf26342009-04-09 22:27:44 +0000345void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
346 VisitNamedDecl(D);
347 Writer.AddTypeRef(D->getType(), Record);
348}
349
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000350void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
351 VisitValueDecl(D);
Douglas Gregor0b748912009-04-14 21:18:50 +0000352 Record.push_back(D->getInitExpr()? 1 : 0);
353 if (D->getInitExpr())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000354 Writer.AddStmt(D->getInitExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000355 Writer.AddAPSInt(D->getInitVal(), Record);
356 Code = pch::DECL_ENUM_CONSTANT;
357}
358
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000359void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
360 VisitValueDecl(D);
Douglas Gregor025452f2009-04-17 00:04:06 +0000361 Record.push_back(D->isThisDeclarationADefinition());
362 if (D->isThisDeclarationADefinition())
Douglas Gregor72971342009-04-18 00:02:19 +0000363 Writer.AddStmt(D->getBody(Context));
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000364 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
365 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
366 Record.push_back(D->isInline());
367 Record.push_back(D->isVirtual());
368 Record.push_back(D->isPure());
369 Record.push_back(D->inheritedPrototype());
370 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
371 Record.push_back(D->isDeleted());
372 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
373 Record.push_back(D->param_size());
374 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
375 P != PEnd; ++P)
376 Writer.AddDeclRef(*P, Record);
377 Code = pch::DECL_FUNCTION;
378}
379
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000380void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
381 VisitNamedDecl(D);
382 // FIXME: convert to LazyStmtPtr?
383 // Unlike C/C++, method bodies will never be in header files.
384 Record.push_back(D->getBody() != 0);
385 if (D->getBody() != 0) {
386 Writer.AddStmt(D->getBody(Context));
387 Writer.AddDeclRef(D->getSelfDecl(), Record);
388 Writer.AddDeclRef(D->getCmdDecl(), Record);
389 }
390 Record.push_back(D->isInstanceMethod());
391 Record.push_back(D->isVariadic());
392 Record.push_back(D->isSynthesized());
393 // FIXME: stable encoding for @required/@optional
394 Record.push_back(D->getImplementationControl());
395 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
396 Record.push_back(D->getObjCDeclQualifier());
397 Writer.AddTypeRef(D->getResultType(), Record);
398 Writer.AddSourceLocation(D->getLocEnd(), Record);
399 Record.push_back(D->param_size());
400 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
401 PEnd = D->param_end(); P != PEnd; ++P)
402 Writer.AddDeclRef(*P, Record);
403 Code = pch::DECL_OBJC_METHOD;
404}
405
Steve Naroff33feeb02009-04-20 20:09:33 +0000406void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
407 VisitNamedDecl(D);
408 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
409 // Abstract class (no need to define a stable pch::DECL code).
410}
411
412void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
413 VisitObjCContainerDecl(D);
414 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
415 Writer.AddDeclRef(D->getSuperClass(), Record);
416 Record.push_back(D->ivar_size());
417 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
418 IEnd = D->ivar_end(); I != IEnd; ++I)
419 Writer.AddDeclRef(*I, Record);
420 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);
425 // FIXME: add protocols, categories.
Steve Naroff30833f82009-04-21 15:12:33 +0000426 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff33feeb02009-04-20 20:09:33 +0000427}
428
429void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
430 VisitFieldDecl(D);
431 // FIXME: stable encoding for @public/@private/@protected/@package
432 Record.push_back(D->getAccessControl());
Steve Naroff30833f82009-04-21 15:12:33 +0000433 Code = pch::DECL_OBJC_IVAR;
434}
435
436void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
437 VisitObjCContainerDecl(D);
438 Record.push_back(D->isForwardDecl());
439 Writer.AddSourceLocation(D->getLocEnd(), Record);
440 Record.push_back(D->protocol_size());
441 for (ObjCProtocolDecl::protocol_iterator
442 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
443 Writer.AddDeclRef(*I, Record);
444 Code = pch::DECL_OBJC_PROTOCOL;
445}
446
447void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
448 VisitFieldDecl(D);
449 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
450}
451
452void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
453 VisitDecl(D);
454 Record.push_back(D->size());
455 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
456 Writer.AddDeclRef(*I, Record);
457 Code = pch::DECL_OBJC_CLASS;
458}
459
460void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
461 VisitDecl(D);
462 Record.push_back(D->protocol_size());
463 for (ObjCProtocolDecl::protocol_iterator
464 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
465 Writer.AddDeclRef(*I, Record);
466 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
467}
468
469void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
470 VisitObjCContainerDecl(D);
471 Writer.AddDeclRef(D->getClassInterface(), Record);
472 Record.push_back(D->protocol_size());
473 for (ObjCProtocolDecl::protocol_iterator
474 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
475 Writer.AddDeclRef(*I, Record);
476 Writer.AddDeclRef(D->getNextClassCategory(), Record);
477 Writer.AddSourceLocation(D->getLocEnd(), Record);
478 Code = pch::DECL_OBJC_CATEGORY;
479}
480
481void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
482 VisitNamedDecl(D);
483 Writer.AddDeclRef(D->getClassInterface(), Record);
484 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
485}
486
487void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
488 VisitNamedDecl(D);
489 // FIXME: Implement.
490 Code = pch::DECL_OBJC_PROPERTY;
491}
492
493void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
494 VisitDecl(D);
495 // FIXME: Implement.
496 // Abstract class (no need to define a stable pch::DECL code).
497}
498
499void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
500 VisitObjCImplDecl(D);
501 // FIXME: Implement.
502 Code = pch::DECL_OBJC_CATEGORY_IMPL;
503}
504
505void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
506 VisitObjCImplDecl(D);
507 // FIXME: Implement.
508 Code = pch::DECL_OBJC_IMPLEMENTATION;
509}
510
511void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
512 VisitDecl(D);
513 // FIXME: Implement.
514 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff33feeb02009-04-20 20:09:33 +0000515}
516
Douglas Gregor8c700062009-04-13 21:20:57 +0000517void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
518 VisitValueDecl(D);
519 Record.push_back(D->isMutable());
Douglas Gregor0b748912009-04-14 21:18:50 +0000520 Record.push_back(D->getBitWidth()? 1 : 0);
521 if (D->getBitWidth())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000522 Writer.AddStmt(D->getBitWidth());
Douglas Gregor8c700062009-04-13 21:20:57 +0000523 Code = pch::DECL_FIELD;
524}
525
Douglas Gregor2cf26342009-04-09 22:27:44 +0000526void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
527 VisitValueDecl(D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000528 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor2cf26342009-04-09 22:27:44 +0000529 Record.push_back(D->isThreadSpecified());
530 Record.push_back(D->hasCXXDirectInitializer());
531 Record.push_back(D->isDeclaredInCondition());
532 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
533 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregor0b748912009-04-14 21:18:50 +0000534 Record.push_back(D->getInit()? 1 : 0);
535 if (D->getInit())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000536 Writer.AddStmt(D->getInit());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000537 Code = pch::DECL_VAR;
538}
539
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000540void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
541 VisitVarDecl(D);
542 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000543 // FIXME: emit default argument (C++)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000544 // FIXME: why isn't the "default argument" just stored as the initializer
545 // in VarDecl?
546 Code = pch::DECL_PARM_VAR;
547}
548
549void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
550 VisitParmVarDecl(D);
551 Writer.AddTypeRef(D->getOriginalType(), Record);
552 Code = pch::DECL_ORIGINAL_PARM_VAR;
553}
554
Douglas Gregor1028bc62009-04-13 22:49:25 +0000555void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
556 VisitDecl(D);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000557 Writer.AddStmt(D->getAsmString());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000558 Code = pch::DECL_FILE_SCOPE_ASM;
559}
560
561void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
562 VisitDecl(D);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000563 Writer.AddStmt(D->getBody());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000564 Record.push_back(D->param_size());
565 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
566 P != PEnd; ++P)
567 Writer.AddDeclRef(*P, Record);
568 Code = pch::DECL_BLOCK;
569}
570
Douglas Gregor2cf26342009-04-09 22:27:44 +0000571/// \brief Emit the DeclContext part of a declaration context decl.
572///
573/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
574/// block for this declaration context is stored. May be 0 to indicate
575/// that there are no declarations stored within this context.
576///
577/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
578/// block for this declaration context is stored. May be 0 to indicate
579/// that there are no declarations visible from this context. Note
580/// that this value will not be emitted for non-primary declaration
581/// contexts.
582void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
583 uint64_t VisibleOffset) {
584 Record.push_back(LexicalOffset);
585 if (DC->getPrimaryContext() == DC)
586 Record.push_back(VisibleOffset);
587}
588
589//===----------------------------------------------------------------------===//
Douglas Gregor0b748912009-04-14 21:18:50 +0000590// Statement/expression serialization
591//===----------------------------------------------------------------------===//
592namespace {
593 class VISIBILITY_HIDDEN PCHStmtWriter
594 : public StmtVisitor<PCHStmtWriter, void> {
595
596 PCHWriter &Writer;
597 PCHWriter::RecordData &Record;
598
599 public:
600 pch::StmtCode Code;
601
602 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
603 : Writer(Writer), Record(Record) { }
604
Douglas Gregor025452f2009-04-17 00:04:06 +0000605 void VisitStmt(Stmt *S);
606 void VisitNullStmt(NullStmt *S);
607 void VisitCompoundStmt(CompoundStmt *S);
608 void VisitSwitchCase(SwitchCase *S);
609 void VisitCaseStmt(CaseStmt *S);
610 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000611 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000612 void VisitIfStmt(IfStmt *S);
613 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000614 void VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000615 void VisitDoStmt(DoStmt *S);
616 void VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000617 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000618 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000619 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000620 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000621 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000622 void VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000623 void VisitAsmStmt(AsmStmt *S);
Douglas Gregor0b748912009-04-14 21:18:50 +0000624 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000625 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000626 void VisitDeclRefExpr(DeclRefExpr *E);
627 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000628 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000629 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000630 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000631 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000632 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000633 void VisitUnaryOperator(UnaryOperator *E);
634 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000635 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000636 void VisitCallExpr(CallExpr *E);
637 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000638 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000639 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000640 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
641 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000642 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000643 void VisitExplicitCastExpr(ExplicitCastExpr *E);
644 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000645 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000646 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000647 void VisitInitListExpr(InitListExpr *E);
648 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
649 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000650 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000651 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000652 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000653 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
654 void VisitChooseExpr(ChooseExpr *E);
655 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000656 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000657 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000658 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000659 };
660}
661
Douglas Gregor025452f2009-04-17 00:04:06 +0000662void PCHStmtWriter::VisitStmt(Stmt *S) {
663}
664
665void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
666 VisitStmt(S);
667 Writer.AddSourceLocation(S->getSemiLoc(), Record);
668 Code = pch::STMT_NULL;
669}
670
671void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
672 VisitStmt(S);
673 Record.push_back(S->size());
674 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
675 CS != CSEnd; ++CS)
676 Writer.WriteSubStmt(*CS);
677 Writer.AddSourceLocation(S->getLBracLoc(), Record);
678 Writer.AddSourceLocation(S->getRBracLoc(), Record);
679 Code = pch::STMT_COMPOUND;
680}
681
682void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
683 VisitStmt(S);
684 Record.push_back(Writer.RecordSwitchCaseID(S));
685}
686
687void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
688 VisitSwitchCase(S);
689 Writer.WriteSubStmt(S->getLHS());
690 Writer.WriteSubStmt(S->getRHS());
691 Writer.WriteSubStmt(S->getSubStmt());
692 Writer.AddSourceLocation(S->getCaseLoc(), Record);
693 Code = pch::STMT_CASE;
694}
695
696void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
697 VisitSwitchCase(S);
698 Writer.WriteSubStmt(S->getSubStmt());
699 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
700 Code = pch::STMT_DEFAULT;
701}
702
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000703void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
704 VisitStmt(S);
705 Writer.AddIdentifierRef(S->getID(), Record);
706 Writer.WriteSubStmt(S->getSubStmt());
707 Writer.AddSourceLocation(S->getIdentLoc(), Record);
708 Record.push_back(Writer.GetLabelID(S));
709 Code = pch::STMT_LABEL;
710}
711
Douglas Gregor025452f2009-04-17 00:04:06 +0000712void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
713 VisitStmt(S);
714 Writer.WriteSubStmt(S->getCond());
715 Writer.WriteSubStmt(S->getThen());
716 Writer.WriteSubStmt(S->getElse());
717 Writer.AddSourceLocation(S->getIfLoc(), Record);
718 Code = pch::STMT_IF;
719}
720
721void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
722 VisitStmt(S);
723 Writer.WriteSubStmt(S->getCond());
724 Writer.WriteSubStmt(S->getBody());
725 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
726 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
727 SC = SC->getNextSwitchCase())
728 Record.push_back(Writer.getSwitchCaseID(SC));
729 Code = pch::STMT_SWITCH;
730}
731
Douglas Gregord921cf92009-04-17 00:16:09 +0000732void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
733 VisitStmt(S);
734 Writer.WriteSubStmt(S->getCond());
735 Writer.WriteSubStmt(S->getBody());
736 Writer.AddSourceLocation(S->getWhileLoc(), Record);
737 Code = pch::STMT_WHILE;
738}
739
Douglas Gregor67d82492009-04-17 00:29:51 +0000740void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
741 VisitStmt(S);
742 Writer.WriteSubStmt(S->getCond());
743 Writer.WriteSubStmt(S->getBody());
744 Writer.AddSourceLocation(S->getDoLoc(), Record);
745 Code = pch::STMT_DO;
746}
747
748void PCHStmtWriter::VisitForStmt(ForStmt *S) {
749 VisitStmt(S);
750 Writer.WriteSubStmt(S->getInit());
751 Writer.WriteSubStmt(S->getCond());
752 Writer.WriteSubStmt(S->getInc());
753 Writer.WriteSubStmt(S->getBody());
754 Writer.AddSourceLocation(S->getForLoc(), Record);
755 Code = pch::STMT_FOR;
756}
757
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000758void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
759 VisitStmt(S);
760 Record.push_back(Writer.GetLabelID(S->getLabel()));
761 Writer.AddSourceLocation(S->getGotoLoc(), Record);
762 Writer.AddSourceLocation(S->getLabelLoc(), Record);
763 Code = pch::STMT_GOTO;
764}
765
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000766void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
767 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000768 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000769 Writer.WriteSubStmt(S->getTarget());
770 Code = pch::STMT_INDIRECT_GOTO;
771}
772
Douglas Gregord921cf92009-04-17 00:16:09 +0000773void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
774 VisitStmt(S);
775 Writer.AddSourceLocation(S->getContinueLoc(), Record);
776 Code = pch::STMT_CONTINUE;
777}
778
Douglas Gregor025452f2009-04-17 00:04:06 +0000779void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
780 VisitStmt(S);
781 Writer.AddSourceLocation(S->getBreakLoc(), Record);
782 Code = pch::STMT_BREAK;
783}
784
Douglas Gregor0de9d882009-04-17 16:34:57 +0000785void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
786 VisitStmt(S);
787 Writer.WriteSubStmt(S->getRetValue());
788 Writer.AddSourceLocation(S->getReturnLoc(), Record);
789 Code = pch::STMT_RETURN;
790}
791
Douglas Gregor84f21702009-04-17 16:55:36 +0000792void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
793 VisitStmt(S);
794 Writer.AddSourceLocation(S->getStartLoc(), Record);
795 Writer.AddSourceLocation(S->getEndLoc(), Record);
796 DeclGroupRef DG = S->getDeclGroup();
797 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
798 Writer.AddDeclRef(*D, Record);
799 Code = pch::STMT_DECL;
800}
801
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000802void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
803 VisitStmt(S);
804 Record.push_back(S->getNumOutputs());
805 Record.push_back(S->getNumInputs());
806 Record.push_back(S->getNumClobbers());
807 Writer.AddSourceLocation(S->getAsmLoc(), Record);
808 Writer.AddSourceLocation(S->getRParenLoc(), Record);
809 Record.push_back(S->isVolatile());
810 Record.push_back(S->isSimple());
811 Writer.WriteSubStmt(S->getAsmString());
812
813 // Outputs
814 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
815 Writer.AddString(S->getOutputName(I), Record);
816 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
817 Writer.WriteSubStmt(S->getOutputExpr(I));
818 }
819
820 // Inputs
821 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
822 Writer.AddString(S->getInputName(I), Record);
823 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
824 Writer.WriteSubStmt(S->getInputExpr(I));
825 }
826
827 // Clobbers
828 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
829 Writer.WriteSubStmt(S->getClobber(I));
830
831 Code = pch::STMT_ASM;
832}
833
Douglas Gregor0b748912009-04-14 21:18:50 +0000834void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000835 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000836 Writer.AddTypeRef(E->getType(), Record);
837 Record.push_back(E->isTypeDependent());
838 Record.push_back(E->isValueDependent());
839}
840
Douglas Gregor17fc2232009-04-14 21:55:33 +0000841void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
842 VisitExpr(E);
843 Writer.AddSourceLocation(E->getLocation(), Record);
844 Record.push_back(E->getIdentType()); // FIXME: stable encoding
845 Code = pch::EXPR_PREDEFINED;
846}
847
Douglas Gregor0b748912009-04-14 21:18:50 +0000848void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
849 VisitExpr(E);
850 Writer.AddDeclRef(E->getDecl(), Record);
851 Writer.AddSourceLocation(E->getLocation(), Record);
852 Code = pch::EXPR_DECL_REF;
853}
854
855void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
856 VisitExpr(E);
857 Writer.AddSourceLocation(E->getLocation(), Record);
858 Writer.AddAPInt(E->getValue(), Record);
859 Code = pch::EXPR_INTEGER_LITERAL;
860}
861
Douglas Gregor17fc2232009-04-14 21:55:33 +0000862void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
863 VisitExpr(E);
864 Writer.AddAPFloat(E->getValue(), Record);
865 Record.push_back(E->isExact());
866 Writer.AddSourceLocation(E->getLocation(), Record);
867 Code = pch::EXPR_FLOATING_LITERAL;
868}
869
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000870void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
871 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000872 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000873 Code = pch::EXPR_IMAGINARY_LITERAL;
874}
875
Douglas Gregor673ecd62009-04-15 16:35:07 +0000876void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
877 VisitExpr(E);
878 Record.push_back(E->getByteLength());
879 Record.push_back(E->getNumConcatenated());
880 Record.push_back(E->isWide());
881 // FIXME: String data should be stored as a blob at the end of the
882 // StringLiteral. However, we can't do so now because we have no
883 // provision for coping with abbreviations when we're jumping around
884 // the PCH file during deserialization.
885 Record.insert(Record.end(),
886 E->getStrData(), E->getStrData() + E->getByteLength());
887 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
888 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
889 Code = pch::EXPR_STRING_LITERAL;
890}
891
Douglas Gregor0b748912009-04-14 21:18:50 +0000892void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
893 VisitExpr(E);
894 Record.push_back(E->getValue());
895 Writer.AddSourceLocation(E->getLoc(), Record);
896 Record.push_back(E->isWide());
897 Code = pch::EXPR_CHARACTER_LITERAL;
898}
899
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000900void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
901 VisitExpr(E);
902 Writer.AddSourceLocation(E->getLParen(), Record);
903 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000904 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000905 Code = pch::EXPR_PAREN;
906}
907
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000908void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
909 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000910 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000911 Record.push_back(E->getOpcode()); // FIXME: stable encoding
912 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
913 Code = pch::EXPR_UNARY_OPERATOR;
914}
915
916void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
917 VisitExpr(E);
918 Record.push_back(E->isSizeOf());
919 if (E->isArgumentType())
920 Writer.AddTypeRef(E->getArgumentType(), Record);
921 else {
922 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000923 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000924 }
925 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
926 Writer.AddSourceLocation(E->getRParenLoc(), Record);
927 Code = pch::EXPR_SIZEOF_ALIGN_OF;
928}
929
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000930void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
931 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000932 Writer.WriteSubStmt(E->getLHS());
933 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000934 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
935 Code = pch::EXPR_ARRAY_SUBSCRIPT;
936}
937
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000938void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
939 VisitExpr(E);
940 Record.push_back(E->getNumArgs());
941 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000942 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000943 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
944 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000945 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000946 Code = pch::EXPR_CALL;
947}
948
949void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
950 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000951 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000952 Writer.AddDeclRef(E->getMemberDecl(), Record);
953 Writer.AddSourceLocation(E->getMemberLoc(), Record);
954 Record.push_back(E->isArrow());
955 Code = pch::EXPR_MEMBER;
956}
957
Douglas Gregor087fd532009-04-14 23:32:43 +0000958void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
959 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000960 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000961}
962
Douglas Gregordb600c32009-04-15 00:25:59 +0000963void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
964 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000965 Writer.WriteSubStmt(E->getLHS());
966 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000967 Record.push_back(E->getOpcode()); // FIXME: stable encoding
968 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
969 Code = pch::EXPR_BINARY_OPERATOR;
970}
971
Douglas Gregorad90e962009-04-15 22:40:36 +0000972void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
973 VisitBinaryOperator(E);
974 Writer.AddTypeRef(E->getComputationLHSType(), Record);
975 Writer.AddTypeRef(E->getComputationResultType(), Record);
976 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
977}
978
979void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
980 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000981 Writer.WriteSubStmt(E->getCond());
982 Writer.WriteSubStmt(E->getLHS());
983 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000984 Code = pch::EXPR_CONDITIONAL_OPERATOR;
985}
986
Douglas Gregor087fd532009-04-14 23:32:43 +0000987void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
988 VisitCastExpr(E);
989 Record.push_back(E->isLvalueCast());
990 Code = pch::EXPR_IMPLICIT_CAST;
991}
992
Douglas Gregordb600c32009-04-15 00:25:59 +0000993void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
994 VisitCastExpr(E);
995 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
996}
997
998void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
999 VisitExplicitCastExpr(E);
1000 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1001 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1002 Code = pch::EXPR_CSTYLE_CAST;
1003}
1004
Douglas Gregorba6d7e72009-04-16 02:33:48 +00001005void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1006 VisitExpr(E);
1007 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001008 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +00001009 Record.push_back(E->isFileScope());
1010 Code = pch::EXPR_COMPOUND_LITERAL;
1011}
1012
Douglas Gregord3c98a02009-04-15 23:02:49 +00001013void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1014 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001015 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +00001016 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1017 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1018 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1019}
1020
Douglas Gregord077d752009-04-16 00:55:48 +00001021void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1022 VisitExpr(E);
1023 Record.push_back(E->getNumInits());
1024 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001025 Writer.WriteSubStmt(E->getInit(I));
1026 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +00001027 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1028 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1029 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1030 Record.push_back(E->hadArrayRangeDesignator());
1031 Code = pch::EXPR_INIT_LIST;
1032}
1033
1034void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1035 VisitExpr(E);
1036 Record.push_back(E->getNumSubExprs());
1037 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001038 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +00001039 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1040 Record.push_back(E->usesGNUSyntax());
1041 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1042 DEnd = E->designators_end();
1043 D != DEnd; ++D) {
1044 if (D->isFieldDesignator()) {
1045 if (FieldDecl *Field = D->getField()) {
1046 Record.push_back(pch::DESIG_FIELD_DECL);
1047 Writer.AddDeclRef(Field, Record);
1048 } else {
1049 Record.push_back(pch::DESIG_FIELD_NAME);
1050 Writer.AddIdentifierRef(D->getFieldName(), Record);
1051 }
1052 Writer.AddSourceLocation(D->getDotLoc(), Record);
1053 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1054 } else if (D->isArrayDesignator()) {
1055 Record.push_back(pch::DESIG_ARRAY);
1056 Record.push_back(D->getFirstExprIndex());
1057 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1058 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1059 } else {
1060 assert(D->isArrayRangeDesignator() && "Unknown designator");
1061 Record.push_back(pch::DESIG_ARRAY_RANGE);
1062 Record.push_back(D->getFirstExprIndex());
1063 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1064 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1065 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1066 }
1067 }
1068 Code = pch::EXPR_DESIGNATED_INIT;
1069}
1070
1071void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1072 VisitExpr(E);
1073 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1074}
1075
Douglas Gregord3c98a02009-04-15 23:02:49 +00001076void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1077 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001078 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +00001079 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1080 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1081 Code = pch::EXPR_VA_ARG;
1082}
1083
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00001084void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1085 VisitExpr(E);
1086 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1087 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1088 Record.push_back(Writer.GetLabelID(E->getLabel()));
1089 Code = pch::EXPR_ADDR_LABEL;
1090}
1091
Douglas Gregor6a2dd552009-04-17 19:05:30 +00001092void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1093 VisitExpr(E);
1094 Writer.WriteSubStmt(E->getSubStmt());
1095 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1096 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1097 Code = pch::EXPR_STMT;
1098}
1099
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001100void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1101 VisitExpr(E);
1102 Writer.AddTypeRef(E->getArgType1(), Record);
1103 Writer.AddTypeRef(E->getArgType2(), Record);
1104 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1105 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1106 Code = pch::EXPR_TYPES_COMPATIBLE;
1107}
1108
1109void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1110 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001111 Writer.WriteSubStmt(E->getCond());
1112 Writer.WriteSubStmt(E->getLHS());
1113 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001114 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1115 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1116 Code = pch::EXPR_CHOOSE;
1117}
1118
1119void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1120 VisitExpr(E);
1121 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1122 Code = pch::EXPR_GNU_NULL;
1123}
1124
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001125void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1126 VisitExpr(E);
1127 Record.push_back(E->getNumSubExprs());
1128 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001129 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001130 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1131 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1132 Code = pch::EXPR_SHUFFLE_VECTOR;
1133}
1134
Douglas Gregor84af7c22009-04-17 19:21:43 +00001135void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1136 VisitExpr(E);
1137 Writer.AddDeclRef(E->getBlockDecl(), Record);
1138 Record.push_back(E->hasBlockDeclRefExprs());
1139 Code = pch::EXPR_BLOCK;
1140}
1141
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001142void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1143 VisitExpr(E);
1144 Writer.AddDeclRef(E->getDecl(), Record);
1145 Writer.AddSourceLocation(E->getLocation(), Record);
1146 Record.push_back(E->isByRef());
1147 Code = pch::EXPR_BLOCK_DECL_REF;
1148}
1149
Douglas Gregor0b748912009-04-14 21:18:50 +00001150//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00001151// PCHWriter Implementation
1152//===----------------------------------------------------------------------===//
1153
Douglas Gregor2bec0412009-04-10 21:16:55 +00001154/// \brief Write the target triple (e.g., i686-apple-darwin9).
1155void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1156 using namespace llvm;
1157 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1158 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001160 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001161
1162 RecordData Record;
1163 Record.push_back(pch::TARGET_TRIPLE);
1164 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001165 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +00001166}
1167
1168/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001169void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1170 RecordData Record;
1171 Record.push_back(LangOpts.Trigraphs);
1172 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1173 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1174 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1175 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1176 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1177 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1178 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1179 Record.push_back(LangOpts.C99); // C99 Support
1180 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1181 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1182 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1183 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1184 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1185
1186 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1187 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1188 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1189
1190 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1191 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1192 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1193 Record.push_back(LangOpts.LaxVectorConversions);
1194 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1195
1196 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1197 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1198 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1199
1200 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1201 // by locks.
1202 Record.push_back(LangOpts.Blocks); // block extension to C
1203 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1204 // they are unused.
1205 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1206 // (modulo the platform support).
1207
1208 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1209 // signed integer arithmetic overflows.
1210
1211 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1212 // may be ripped out at any time.
1213
1214 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1215 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1216 // defined.
1217 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1218 // opposed to __DYNAMIC__).
1219 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1220
1221 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1222 // used (instead of C99 semantics).
1223 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1224 Record.push_back(LangOpts.getGCMode());
1225 Record.push_back(LangOpts.getVisibilityMode());
1226 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001227 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001228}
1229
Douglas Gregor14f79002009-04-10 03:52:48 +00001230//===----------------------------------------------------------------------===//
1231// Source Manager Serialization
1232//===----------------------------------------------------------------------===//
1233
1234/// \brief Create an abbreviation for the SLocEntry that refers to a
1235/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001236static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001237 using namespace llvm;
1238 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1239 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +00001244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001245 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001246}
1247
1248/// \brief Create an abbreviation for the SLocEntry that refers to a
1249/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001250static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001251 using namespace llvm;
1252 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1253 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001259 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001260}
1261
1262/// \brief Create an abbreviation for the SLocEntry that refers to a
1263/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001264static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001265 using namespace llvm;
1266 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1267 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001269 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001270}
1271
1272/// \brief Create an abbreviation for the SLocEntry that refers to an
1273/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001274static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001275 using namespace llvm;
1276 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1277 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001283 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001284}
1285
1286/// \brief Writes the block containing the serialized form of the
1287/// source manager.
1288///
1289/// TODO: We should probably use an on-disk hash table (stored in a
1290/// blob), indexed based on the file name, so that we only create
1291/// entries for files that we actually need. In the common case (no
1292/// errors), we probably won't have to create file entries for any of
1293/// the files in the AST.
1294void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001295 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001296 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001297
1298 // Abbreviations for the various kinds of source-location entries.
1299 int SLocFileAbbrv = -1;
1300 int SLocBufferAbbrv = -1;
1301 int SLocBufferBlobAbbrv = -1;
1302 int SLocInstantiationAbbrv = -1;
1303
1304 // Write out the source location entry table. We skip the first
1305 // entry, which is always the same dummy entry.
1306 RecordData Record;
1307 for (SourceManager::sloc_entry_iterator
1308 SLoc = SourceMgr.sloc_entry_begin() + 1,
1309 SLocEnd = SourceMgr.sloc_entry_end();
1310 SLoc != SLocEnd; ++SLoc) {
1311 // Figure out which record code to use.
1312 unsigned Code;
1313 if (SLoc->isFile()) {
1314 if (SLoc->getFile().getContentCache()->Entry)
1315 Code = pch::SM_SLOC_FILE_ENTRY;
1316 else
1317 Code = pch::SM_SLOC_BUFFER_ENTRY;
1318 } else
1319 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1320 Record.push_back(Code);
1321
1322 Record.push_back(SLoc->getOffset());
1323 if (SLoc->isFile()) {
1324 const SrcMgr::FileInfo &File = SLoc->getFile();
1325 Record.push_back(File.getIncludeLoc().getRawEncoding());
1326 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001327 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001328
1329 const SrcMgr::ContentCache *Content = File.getContentCache();
1330 if (Content->Entry) {
1331 // The source location entry is a file. The blob associated
1332 // with this entry is the file name.
1333 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001334 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1335 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001336 Content->Entry->getName(),
1337 strlen(Content->Entry->getName()));
1338 } else {
1339 // The source location entry is a buffer. The blob associated
1340 // with this entry contains the contents of the buffer.
1341 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001342 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1343 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001344 }
1345
1346 // We add one to the size so that we capture the trailing NULL
1347 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1348 // the reader side).
1349 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1350 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001351 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001352 Record.clear();
1353 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001354 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001355 Buffer->getBufferStart(),
1356 Buffer->getBufferSize() + 1);
1357 }
1358 } else {
1359 // The source location entry is an instantiation.
1360 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1361 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1362 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1363 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1364
Douglas Gregorf60e9912009-04-15 18:05:10 +00001365 // Compute the token length for this macro expansion.
1366 unsigned NextOffset = SourceMgr.getNextOffset();
1367 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1368 if (++NextSLoc != SLocEnd)
1369 NextOffset = NextSLoc->getOffset();
1370 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1371
Douglas Gregor14f79002009-04-10 03:52:48 +00001372 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001373 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1374 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001375 }
1376
1377 Record.clear();
1378 }
1379
Douglas Gregorbd945002009-04-13 16:31:14 +00001380 // Write the line table.
1381 if (SourceMgr.hasLineTable()) {
1382 LineTableInfo &LineTable = SourceMgr.getLineTable();
1383
1384 // Emit the file names
1385 Record.push_back(LineTable.getNumFilenames());
1386 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1387 // Emit the file name
1388 const char *Filename = LineTable.getFilename(I);
1389 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1390 Record.push_back(FilenameLen);
1391 if (FilenameLen)
1392 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1393 }
1394
1395 // Emit the line entries
1396 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1397 L != LEnd; ++L) {
1398 // Emit the file ID
1399 Record.push_back(L->first);
1400
1401 // Emit the line entries
1402 Record.push_back(L->second.size());
1403 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1404 LEEnd = L->second.end();
1405 LE != LEEnd; ++LE) {
1406 Record.push_back(LE->FileOffset);
1407 Record.push_back(LE->LineNo);
1408 Record.push_back(LE->FilenameID);
1409 Record.push_back((unsigned)LE->FileKind);
1410 Record.push_back(LE->IncludeOffset);
1411 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001412 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001413 }
1414 }
1415
Douglas Gregorc9490c02009-04-16 22:23:12 +00001416 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001417}
1418
Chris Lattner0b1fb982009-04-10 17:15:23 +00001419/// \brief Writes the block containing the serialized form of the
1420/// preprocessor.
1421///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001422void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001423 // Enter the preprocessor block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001424 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001425
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001426 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1427 // FIXME: use diagnostics subsystem for localization etc.
1428 if (PP.SawDateOrTime())
1429 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001430
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001431 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001432
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001433 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1434 if (PP.getCounterValue() != 0) {
1435 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001436 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001437 Record.clear();
1438 }
1439
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001440 // Loop over all the macro definitions that are live at the end of the file,
1441 // emitting each to the PP section.
1442 // FIXME: Eventually we want to emit an index so that we can lazily load
1443 // macros.
1444 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1445 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001446 // FIXME: This emits macros in hash table order, we should do it in a stable
1447 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001448 MacroInfo *MI = I->second;
1449
1450 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1451 // been redefined by the header (in which case they are not isBuiltinMacro).
1452 if (MI->isBuiltinMacro())
1453 continue;
1454
Chris Lattner7356a312009-04-11 21:15:38 +00001455 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001456 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1457 Record.push_back(MI->isUsed());
1458
1459 unsigned Code;
1460 if (MI->isObjectLike()) {
1461 Code = pch::PP_MACRO_OBJECT_LIKE;
1462 } else {
1463 Code = pch::PP_MACRO_FUNCTION_LIKE;
1464
1465 Record.push_back(MI->isC99Varargs());
1466 Record.push_back(MI->isGNUVarargs());
1467 Record.push_back(MI->getNumArgs());
1468 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1469 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001470 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001471 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001472 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001473 Record.clear();
1474
Chris Lattnerdf961c22009-04-10 18:08:30 +00001475 // Emit the tokens array.
1476 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1477 // Note that we know that the preprocessor does not have any annotation
1478 // tokens in it because they are created by the parser, and thus can't be
1479 // in a macro definition.
1480 const Token &Tok = MI->getReplacementToken(TokNo);
1481
1482 Record.push_back(Tok.getLocation().getRawEncoding());
1483 Record.push_back(Tok.getLength());
1484
Chris Lattnerdf961c22009-04-10 18:08:30 +00001485 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1486 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001487 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001488
1489 // FIXME: Should translate token kind to a stable encoding.
1490 Record.push_back(Tok.getKind());
1491 // FIXME: Should translate token flags to a stable encoding.
1492 Record.push_back(Tok.getFlags());
1493
Douglas Gregorc9490c02009-04-16 22:23:12 +00001494 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001495 Record.clear();
1496 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001497
1498 }
1499
Douglas Gregorc9490c02009-04-16 22:23:12 +00001500 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001501}
1502
1503
Douglas Gregor2cf26342009-04-09 22:27:44 +00001504/// \brief Write the representation of a type to the PCH stream.
1505void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001506 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001507 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001508 ID = NextTypeID++;
1509
1510 // Record the offset for this type.
1511 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001512 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001513 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1514 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001515 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001516 }
1517
1518 RecordData Record;
1519
1520 // Emit the type's representation.
1521 PCHTypeWriter W(*this, Record);
1522 switch (T->getTypeClass()) {
1523 // For all of the concrete, non-dependent types, call the
1524 // appropriate visitor function.
1525#define TYPE(Class, Base) \
1526 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1527#define ABSTRACT_TYPE(Class, Base)
1528#define DEPENDENT_TYPE(Class, Base)
1529#include "clang/AST/TypeNodes.def"
1530
1531 // For all of the dependent type nodes (which only occur in C++
1532 // templates), produce an error.
1533#define TYPE(Class, Base)
1534#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1535#include "clang/AST/TypeNodes.def"
1536 assert(false && "Cannot serialize dependent type nodes");
1537 break;
1538 }
1539
1540 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001541 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001542
1543 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001544 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001545}
1546
1547/// \brief Write a block containing all of the types.
1548void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001549 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001550 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001551
1552 // Emit all of the types in the ASTContext
1553 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1554 TEnd = Context.getTypes().end();
1555 T != TEnd; ++T) {
1556 // Builtin types are never serialized.
1557 if (isa<BuiltinType>(*T))
1558 continue;
1559
1560 WriteType(*T);
1561 }
1562
1563 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001564 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001565}
1566
1567/// \brief Write the block containing all of the declaration IDs
1568/// lexically declared within the given DeclContext.
1569///
1570/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1571/// bistream, or 0 if no block was written.
1572uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1573 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001574 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001575 return 0;
1576
Douglas Gregorc9490c02009-04-16 22:23:12 +00001577 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001578 RecordData Record;
1579 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1580 DEnd = DC->decls_end(Context);
1581 D != DEnd; ++D)
1582 AddDeclRef(*D, Record);
1583
Douglas Gregorc9490c02009-04-16 22:23:12 +00001584 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001585 return Offset;
1586}
1587
1588/// \brief Write the block containing all of the declaration IDs
1589/// visible from the given DeclContext.
1590///
1591/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1592/// bistream, or 0 if no block was written.
1593uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1594 DeclContext *DC) {
1595 if (DC->getPrimaryContext() != DC)
1596 return 0;
1597
Douglas Gregor58f06992009-04-18 15:49:20 +00001598 // Since there is no name lookup into functions or methods, don't
1599 // bother to build a visible-declarations table.
1600 if (DC->isFunctionOrMethod())
1601 return 0;
1602
Douglas Gregor2cf26342009-04-09 22:27:44 +00001603 // Force the DeclContext to build a its name-lookup table.
1604 DC->lookup(Context, DeclarationName());
1605
1606 // Serialize the contents of the mapping used for lookup. Note that,
1607 // although we have two very different code paths, the serialized
1608 // representation is the same for both cases: a declaration name,
1609 // followed by a size, followed by references to the visible
1610 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001611 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001612 RecordData Record;
1613 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001614 if (!Map)
1615 return 0;
1616
Douglas Gregor2cf26342009-04-09 22:27:44 +00001617 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1618 D != DEnd; ++D) {
1619 AddDeclarationName(D->first, Record);
1620 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1621 Record.push_back(Result.second - Result.first);
1622 for(; Result.first != Result.second; ++Result.first)
1623 AddDeclRef(*Result.first, Record);
1624 }
1625
1626 if (Record.size() == 0)
1627 return 0;
1628
Douglas Gregorc9490c02009-04-16 22:23:12 +00001629 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001630 return Offset;
1631}
1632
1633/// \brief Write a block containing all of the declarations.
1634void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001635 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001636 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001637
1638 // Emit all of the declarations.
1639 RecordData Record;
Douglas Gregor72971342009-04-18 00:02:19 +00001640 PCHDeclWriter W(*this, Context, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001641 while (!DeclsToEmit.empty()) {
1642 // Pull the next declaration off the queue
1643 Decl *D = DeclsToEmit.front();
1644 DeclsToEmit.pop();
1645
1646 // If this declaration is also a DeclContext, write blocks for the
1647 // declarations that lexically stored inside its context and those
1648 // declarations that are visible from its context. These blocks
1649 // are written before the declaration itself so that we can put
1650 // their offsets into the record for the declaration.
1651 uint64_t LexicalOffset = 0;
1652 uint64_t VisibleOffset = 0;
1653 DeclContext *DC = dyn_cast<DeclContext>(D);
1654 if (DC) {
1655 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1656 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1657 }
1658
1659 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001660 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001661 if (ID == 0)
1662 ID = DeclIDs.size();
1663
1664 unsigned Index = ID - 1;
1665
1666 // Record the offset for this declaration
1667 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001668 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001669 else if (DeclOffsets.size() < Index) {
1670 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001671 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001672 }
1673
1674 // Build and emit a record for this declaration
1675 Record.clear();
1676 W.Code = (pch::DeclCode)0;
1677 W.Visit(D);
1678 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001679 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001680 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001681
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001682 // If the declaration had any attributes, write them now.
1683 if (D->hasAttrs())
1684 WriteAttributeRecord(D->getAttrs());
1685
Douglas Gregor0b748912009-04-14 21:18:50 +00001686 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001687 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001688
Douglas Gregorfdd01722009-04-14 00:24:19 +00001689 // Note external declarations so that we can add them to a record
1690 // in the PCH file later.
1691 if (isa<FileScopeAsmDecl>(D))
1692 ExternalDefinitions.push_back(ID);
1693 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1694 if (// Non-static file-scope variables with initializers or that
1695 // are tentative definitions.
1696 (Var->isFileVarDecl() &&
1697 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1698 // Out-of-line definitions of static data members (C++).
1699 (Var->getDeclContext()->isRecord() &&
1700 !Var->getLexicalDeclContext()->isRecord() &&
1701 Var->getStorageClass() == VarDecl::Static))
1702 ExternalDefinitions.push_back(ID);
1703 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00001704 if (Func->isThisDeclarationADefinition())
Douglas Gregorfdd01722009-04-14 00:24:19 +00001705 ExternalDefinitions.push_back(ID);
1706 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001707 }
1708
1709 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001710 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001711}
1712
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001713namespace {
1714class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1715 PCHWriter &Writer;
1716
1717public:
1718 typedef const IdentifierInfo* key_type;
1719 typedef key_type key_type_ref;
1720
1721 typedef pch::IdentID data_type;
1722 typedef data_type data_type_ref;
1723
1724 PCHIdentifierTableTrait(PCHWriter &Writer) : Writer(Writer) { }
1725
1726 static unsigned ComputeHash(const IdentifierInfo* II) {
1727 return clang::BernsteinHash(II->getName());
1728 }
1729
1730 static std::pair<unsigned,unsigned>
1731 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1732 pch::IdentID ID) {
1733 unsigned KeyLen = strlen(II->getName()) + 1;
1734 clang::io::Emit16(Out, KeyLen);
1735 unsigned DataLen = 4 + 4 + 2; // 4 bytes for token ID, builtin, flags
1736 // 4 bytes for the persistent ID
1737 // 2 bytes for the length of the decl chain
1738 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1739 DEnd = IdentifierResolver::end();
1740 D != DEnd; ++D)
1741 DataLen += sizeof(pch::DeclID);
1742 return std::make_pair(KeyLen, DataLen);
1743 }
1744
1745 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1746 unsigned KeyLen) {
1747 // Record the location of the key data. This is used when generating
1748 // the mapping from persistent IDs to strings.
1749 Writer.SetIdentifierOffset(II, Out.tell());
1750 Out.write(II->getName(), KeyLen);
1751 }
1752
1753 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1754 pch::IdentID ID, unsigned) {
1755 uint32_t Bits = 0;
1756 Bits = Bits | (uint32_t)II->getTokenID();
1757 Bits = (Bits << 8) | (uint32_t)II->getObjCOrBuiltinID();
1758 Bits = (Bits << 10) | II->hasMacroDefinition();
1759 Bits = (Bits << 1) | II->isExtensionToken();
1760 Bits = (Bits << 1) | II->isPoisoned();
1761 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1762 clang::io::Emit32(Out, Bits);
1763 clang::io::Emit32(Out, ID);
1764
1765 llvm::SmallVector<pch::DeclID, 8> Decls;
1766 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1767 DEnd = IdentifierResolver::end();
1768 D != DEnd; ++D)
1769 Decls.push_back(Writer.getDeclID(*D));
1770
1771 clang::io::Emit16(Out, Decls.size());
1772 for (unsigned I = 0; I < Decls.size(); ++I)
1773 clang::io::Emit32(Out, Decls[I]);
1774 }
1775};
1776} // end anonymous namespace
1777
Douglas Gregorafaf3082009-04-11 00:14:32 +00001778/// \brief Write the identifier table into the PCH file.
1779///
1780/// The identifier table consists of a blob containing string data
1781/// (the actual identifiers themselves) and a separate "offsets" index
1782/// that maps identifier IDs to locations within the blob.
1783void PCHWriter::WriteIdentifierTable() {
1784 using namespace llvm;
1785
1786 // Create and write out the blob that contains the identifier
1787 // strings.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001788 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001789 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001790 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1791
1792 // Create the on-disk hash table representation.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001793 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1794 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1795 ID != IDEnd; ++ID) {
1796 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001797 Generator.insert(ID->first, ID->second);
1798 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001799
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001800 // Create the on-disk hash table in a buffer.
1801 llvm::SmallVector<char, 4096> IdentifierTable;
1802 {
1803 PCHIdentifierTableTrait Trait(*this);
1804 llvm::raw_svector_ostream Out(IdentifierTable);
1805 Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001806 }
1807
1808 // Create a blob abbreviation
1809 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1810 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001812 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001813
1814 // Write the identifier table
1815 RecordData Record;
1816 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001817 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1818 &IdentifierTable.front(),
1819 IdentifierTable.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001820 }
1821
1822 // Write the offsets table for identifier IDs.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001823 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001824}
1825
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001826/// \brief Write a record containing the given attributes.
1827void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1828 RecordData Record;
1829 for (; Attr; Attr = Attr->getNext()) {
1830 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1831 Record.push_back(Attr->isInherited());
1832 switch (Attr->getKind()) {
1833 case Attr::Alias:
1834 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1835 break;
1836
1837 case Attr::Aligned:
1838 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1839 break;
1840
1841 case Attr::AlwaysInline:
1842 break;
1843
1844 case Attr::AnalyzerNoReturn:
1845 break;
1846
1847 case Attr::Annotate:
1848 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1849 break;
1850
1851 case Attr::AsmLabel:
1852 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1853 break;
1854
1855 case Attr::Blocks:
1856 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1857 break;
1858
1859 case Attr::Cleanup:
1860 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1861 break;
1862
1863 case Attr::Const:
1864 break;
1865
1866 case Attr::Constructor:
1867 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1868 break;
1869
1870 case Attr::DLLExport:
1871 case Attr::DLLImport:
1872 case Attr::Deprecated:
1873 break;
1874
1875 case Attr::Destructor:
1876 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1877 break;
1878
1879 case Attr::FastCall:
1880 break;
1881
1882 case Attr::Format: {
1883 const FormatAttr *Format = cast<FormatAttr>(Attr);
1884 AddString(Format->getType(), Record);
1885 Record.push_back(Format->getFormatIdx());
1886 Record.push_back(Format->getFirstArg());
1887 break;
1888 }
1889
Chris Lattnercf2a7212009-04-20 19:12:28 +00001890 case Attr::GNUInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001891 case Attr::IBOutletKind:
1892 case Attr::NoReturn:
1893 case Attr::NoThrow:
1894 case Attr::Nodebug:
1895 case Attr::Noinline:
1896 break;
1897
1898 case Attr::NonNull: {
1899 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1900 Record.push_back(NonNull->size());
1901 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1902 break;
1903 }
1904
1905 case Attr::ObjCException:
1906 case Attr::ObjCNSObject:
1907 case Attr::Overloadable:
1908 break;
1909
1910 case Attr::Packed:
1911 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1912 break;
1913
1914 case Attr::Pure:
1915 break;
1916
1917 case Attr::Regparm:
1918 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1919 break;
1920
1921 case Attr::Section:
1922 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1923 break;
1924
1925 case Attr::StdCall:
1926 case Attr::TransparentUnion:
1927 case Attr::Unavailable:
1928 case Attr::Unused:
1929 case Attr::Used:
1930 break;
1931
1932 case Attr::Visibility:
1933 // FIXME: stable encoding
1934 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1935 break;
1936
1937 case Attr::WarnUnusedResult:
1938 case Attr::Weak:
1939 case Attr::WeakImport:
1940 break;
1941 }
1942 }
1943
Douglas Gregorc9490c02009-04-16 22:23:12 +00001944 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001945}
1946
1947void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1948 Record.push_back(Str.size());
1949 Record.insert(Record.end(), Str.begin(), Str.end());
1950}
1951
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001952/// \brief Note that the identifier II occurs at the given offset
1953/// within the identifier table.
1954void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
1955 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
1956}
1957
Douglas Gregorc9490c02009-04-16 22:23:12 +00001958PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor3e1af842009-04-17 22:13:46 +00001959 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), NumStatements(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001960
Douglas Gregore7785042009-04-20 15:53:59 +00001961void PCHWriter::WritePCH(Sema &SemaRef) {
1962 ASTContext &Context = SemaRef.Context;
1963 Preprocessor &PP = SemaRef.PP;
1964
Douglas Gregor2cf26342009-04-09 22:27:44 +00001965 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001966 Stream.Emit((unsigned)'C', 8);
1967 Stream.Emit((unsigned)'P', 8);
1968 Stream.Emit((unsigned)'C', 8);
1969 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001970
1971 // The translation unit is the first declaration we'll emit.
1972 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1973 DeclsToEmit.push(Context.getTranslationUnitDecl());
1974
1975 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001976 RecordData Record;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001977 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001978 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001979 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001980 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001981 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001982 WriteTypesBlock(Context);
1983 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001984 WriteIdentifierTable();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001985 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1986 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorad1de002009-04-18 05:55:16 +00001987
1988 // Write the record of special types.
1989 Record.clear();
1990 AddTypeRef(Context.getBuiltinVaListType(), Record);
1991 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1992
Douglas Gregorfdd01722009-04-14 00:24:19 +00001993 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00001994 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001995
1996 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00001997 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00001998 Record.push_back(NumStatements);
1999 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002000 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002001}
2002
2003void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2004 Record.push_back(Loc.getRawEncoding());
2005}
2006
2007void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2008 Record.push_back(Value.getBitWidth());
2009 unsigned N = Value.getNumWords();
2010 const uint64_t* Words = Value.getRawData();
2011 for (unsigned I = 0; I != N; ++I)
2012 Record.push_back(Words[I]);
2013}
2014
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002015void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2016 Record.push_back(Value.isUnsigned());
2017 AddAPInt(Value, Record);
2018}
2019
Douglas Gregor17fc2232009-04-14 21:55:33 +00002020void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2021 AddAPInt(Value.bitcastToAPInt(), Record);
2022}
2023
Douglas Gregor2cf26342009-04-09 22:27:44 +00002024void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002025 if (II == 0) {
2026 Record.push_back(0);
2027 return;
2028 }
2029
2030 pch::IdentID &ID = IdentifierIDs[II];
2031 if (ID == 0)
2032 ID = IdentifierIDs.size();
2033
2034 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002035}
2036
2037void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2038 if (T.isNull()) {
2039 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2040 return;
2041 }
2042
2043 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002044 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002045 switch (BT->getKind()) {
2046 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2047 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2048 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2049 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2050 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2051 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2052 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2053 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2054 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2055 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2056 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2057 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2058 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2059 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2060 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2061 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2062 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2063 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2064 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2065 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2066 }
2067
2068 Record.push_back((ID << 3) | T.getCVRQualifiers());
2069 return;
2070 }
2071
Douglas Gregor8038d512009-04-10 17:25:41 +00002072 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002073 if (ID == 0) // we haven't seen this type before
2074 ID = NextTypeID++;
2075
2076 // Encode the type qualifiers in the type reference.
2077 Record.push_back((ID << 3) | T.getCVRQualifiers());
2078}
2079
2080void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2081 if (D == 0) {
2082 Record.push_back(0);
2083 return;
2084 }
2085
Douglas Gregor8038d512009-04-10 17:25:41 +00002086 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002087 if (ID == 0) {
2088 // We haven't seen this declaration before. Give it a new ID and
2089 // enqueue it in the list of declarations to emit.
2090 ID = DeclIDs.size();
2091 DeclsToEmit.push(const_cast<Decl *>(D));
2092 }
2093
2094 Record.push_back(ID);
2095}
2096
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002097pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2098 if (D == 0)
2099 return 0;
2100
2101 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2102 return DeclIDs[D];
2103}
2104
Douglas Gregor2cf26342009-04-09 22:27:44 +00002105void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2106 Record.push_back(Name.getNameKind());
2107 switch (Name.getNameKind()) {
2108 case DeclarationName::Identifier:
2109 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2110 break;
2111
2112 case DeclarationName::ObjCZeroArgSelector:
2113 case DeclarationName::ObjCOneArgSelector:
2114 case DeclarationName::ObjCMultiArgSelector:
2115 assert(false && "Serialization of Objective-C selectors unavailable");
2116 break;
2117
2118 case DeclarationName::CXXConstructorName:
2119 case DeclarationName::CXXDestructorName:
2120 case DeclarationName::CXXConversionFunctionName:
2121 AddTypeRef(Name.getCXXNameType(), Record);
2122 break;
2123
2124 case DeclarationName::CXXOperatorName:
2125 Record.push_back(Name.getCXXOverloadedOperator());
2126 break;
2127
2128 case DeclarationName::CXXUsingDirective:
2129 // No extra data to emit
2130 break;
2131 }
2132}
Douglas Gregor0b748912009-04-14 21:18:50 +00002133
Douglas Gregorc9490c02009-04-16 22:23:12 +00002134/// \brief Write the given substatement or subexpression to the
2135/// bitstream.
2136void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00002137 RecordData Record;
2138 PCHStmtWriter Writer(*this, Record);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002139 ++NumStatements;
Douglas Gregor087fd532009-04-14 23:32:43 +00002140
Douglas Gregorc9490c02009-04-16 22:23:12 +00002141 if (!S) {
2142 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002143 return;
2144 }
2145
Douglas Gregorc9490c02009-04-16 22:23:12 +00002146 Writer.Code = pch::STMT_NULL_PTR;
2147 Writer.Visit(S);
2148 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00002149 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002150 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002151}
2152
Douglas Gregorc9490c02009-04-16 22:23:12 +00002153/// \brief Flush all of the statements that have been added to the
2154/// queue via AddStmt().
2155void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00002156 RecordData Record;
2157 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002158
Douglas Gregorc9490c02009-04-16 22:23:12 +00002159 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor3e1af842009-04-17 22:13:46 +00002160 ++NumStatements;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002161 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00002162
Douglas Gregorc9490c02009-04-16 22:23:12 +00002163 if (!S) {
2164 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002165 continue;
2166 }
2167
Douglas Gregorc9490c02009-04-16 22:23:12 +00002168 Writer.Code = pch::STMT_NULL_PTR;
2169 Writer.Visit(S);
2170 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00002171 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002172 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002173
Douglas Gregorc9490c02009-04-16 22:23:12 +00002174 assert(N == StmtsToEmit.size() &&
2175 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00002176
2177 // Note that we are at the end of a full expression. Any
2178 // expression records that follow this one are part of a different
2179 // expression.
2180 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00002181 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002182 }
Douglas Gregor087fd532009-04-14 23:32:43 +00002183
Douglas Gregorc9490c02009-04-16 22:23:12 +00002184 StmtsToEmit.clear();
Douglas Gregor0de9d882009-04-17 16:34:57 +00002185 SwitchCaseIDs.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00002186}
Douglas Gregor025452f2009-04-17 00:04:06 +00002187
2188unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2189 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2190 "SwitchCase recorded twice");
2191 unsigned NextID = SwitchCaseIDs.size();
2192 SwitchCaseIDs[S] = NextID;
2193 return NextID;
2194}
2195
2196unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2197 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2198 "SwitchCase hasn't been seen yet");
2199 return SwitchCaseIDs[S];
2200}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002201
2202/// \brief Retrieve the ID for the given label statement, which may
2203/// or may not have been emitted yet.
2204unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2205 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2206 if (Pos != LabelIDs.end())
2207 return Pos->second;
2208
2209 unsigned NextID = LabelIDs.size();
2210 LabelIDs[S] = NextID;
2211 return NextID;
2212}