blob: 0d7f58808a210bf860eb1809eb433a42387fedde [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);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000659
660 // Objective-C
Chris Lattner3a57a372009-04-22 06:29:42 +0000661 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000662 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000663 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
664 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000665 };
666}
667
Douglas Gregor025452f2009-04-17 00:04:06 +0000668void PCHStmtWriter::VisitStmt(Stmt *S) {
669}
670
671void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
672 VisitStmt(S);
673 Writer.AddSourceLocation(S->getSemiLoc(), Record);
674 Code = pch::STMT_NULL;
675}
676
677void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
678 VisitStmt(S);
679 Record.push_back(S->size());
680 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
681 CS != CSEnd; ++CS)
682 Writer.WriteSubStmt(*CS);
683 Writer.AddSourceLocation(S->getLBracLoc(), Record);
684 Writer.AddSourceLocation(S->getRBracLoc(), Record);
685 Code = pch::STMT_COMPOUND;
686}
687
688void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
689 VisitStmt(S);
690 Record.push_back(Writer.RecordSwitchCaseID(S));
691}
692
693void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
694 VisitSwitchCase(S);
695 Writer.WriteSubStmt(S->getLHS());
696 Writer.WriteSubStmt(S->getRHS());
697 Writer.WriteSubStmt(S->getSubStmt());
698 Writer.AddSourceLocation(S->getCaseLoc(), Record);
699 Code = pch::STMT_CASE;
700}
701
702void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
703 VisitSwitchCase(S);
704 Writer.WriteSubStmt(S->getSubStmt());
705 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
706 Code = pch::STMT_DEFAULT;
707}
708
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000709void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
710 VisitStmt(S);
711 Writer.AddIdentifierRef(S->getID(), Record);
712 Writer.WriteSubStmt(S->getSubStmt());
713 Writer.AddSourceLocation(S->getIdentLoc(), Record);
714 Record.push_back(Writer.GetLabelID(S));
715 Code = pch::STMT_LABEL;
716}
717
Douglas Gregor025452f2009-04-17 00:04:06 +0000718void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
719 VisitStmt(S);
720 Writer.WriteSubStmt(S->getCond());
721 Writer.WriteSubStmt(S->getThen());
722 Writer.WriteSubStmt(S->getElse());
723 Writer.AddSourceLocation(S->getIfLoc(), Record);
724 Code = pch::STMT_IF;
725}
726
727void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
728 VisitStmt(S);
729 Writer.WriteSubStmt(S->getCond());
730 Writer.WriteSubStmt(S->getBody());
731 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
732 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
733 SC = SC->getNextSwitchCase())
734 Record.push_back(Writer.getSwitchCaseID(SC));
735 Code = pch::STMT_SWITCH;
736}
737
Douglas Gregord921cf92009-04-17 00:16:09 +0000738void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
739 VisitStmt(S);
740 Writer.WriteSubStmt(S->getCond());
741 Writer.WriteSubStmt(S->getBody());
742 Writer.AddSourceLocation(S->getWhileLoc(), Record);
743 Code = pch::STMT_WHILE;
744}
745
Douglas Gregor67d82492009-04-17 00:29:51 +0000746void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
747 VisitStmt(S);
748 Writer.WriteSubStmt(S->getCond());
749 Writer.WriteSubStmt(S->getBody());
750 Writer.AddSourceLocation(S->getDoLoc(), Record);
751 Code = pch::STMT_DO;
752}
753
754void PCHStmtWriter::VisitForStmt(ForStmt *S) {
755 VisitStmt(S);
756 Writer.WriteSubStmt(S->getInit());
757 Writer.WriteSubStmt(S->getCond());
758 Writer.WriteSubStmt(S->getInc());
759 Writer.WriteSubStmt(S->getBody());
760 Writer.AddSourceLocation(S->getForLoc(), Record);
761 Code = pch::STMT_FOR;
762}
763
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000764void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
765 VisitStmt(S);
766 Record.push_back(Writer.GetLabelID(S->getLabel()));
767 Writer.AddSourceLocation(S->getGotoLoc(), Record);
768 Writer.AddSourceLocation(S->getLabelLoc(), Record);
769 Code = pch::STMT_GOTO;
770}
771
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000772void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
773 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000774 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000775 Writer.WriteSubStmt(S->getTarget());
776 Code = pch::STMT_INDIRECT_GOTO;
777}
778
Douglas Gregord921cf92009-04-17 00:16:09 +0000779void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
780 VisitStmt(S);
781 Writer.AddSourceLocation(S->getContinueLoc(), Record);
782 Code = pch::STMT_CONTINUE;
783}
784
Douglas Gregor025452f2009-04-17 00:04:06 +0000785void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
786 VisitStmt(S);
787 Writer.AddSourceLocation(S->getBreakLoc(), Record);
788 Code = pch::STMT_BREAK;
789}
790
Douglas Gregor0de9d882009-04-17 16:34:57 +0000791void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
792 VisitStmt(S);
793 Writer.WriteSubStmt(S->getRetValue());
794 Writer.AddSourceLocation(S->getReturnLoc(), Record);
795 Code = pch::STMT_RETURN;
796}
797
Douglas Gregor84f21702009-04-17 16:55:36 +0000798void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
799 VisitStmt(S);
800 Writer.AddSourceLocation(S->getStartLoc(), Record);
801 Writer.AddSourceLocation(S->getEndLoc(), Record);
802 DeclGroupRef DG = S->getDeclGroup();
803 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
804 Writer.AddDeclRef(*D, Record);
805 Code = pch::STMT_DECL;
806}
807
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000808void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
809 VisitStmt(S);
810 Record.push_back(S->getNumOutputs());
811 Record.push_back(S->getNumInputs());
812 Record.push_back(S->getNumClobbers());
813 Writer.AddSourceLocation(S->getAsmLoc(), Record);
814 Writer.AddSourceLocation(S->getRParenLoc(), Record);
815 Record.push_back(S->isVolatile());
816 Record.push_back(S->isSimple());
817 Writer.WriteSubStmt(S->getAsmString());
818
819 // Outputs
820 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
821 Writer.AddString(S->getOutputName(I), Record);
822 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
823 Writer.WriteSubStmt(S->getOutputExpr(I));
824 }
825
826 // Inputs
827 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
828 Writer.AddString(S->getInputName(I), Record);
829 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
830 Writer.WriteSubStmt(S->getInputExpr(I));
831 }
832
833 // Clobbers
834 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
835 Writer.WriteSubStmt(S->getClobber(I));
836
837 Code = pch::STMT_ASM;
838}
839
Douglas Gregor0b748912009-04-14 21:18:50 +0000840void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000841 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000842 Writer.AddTypeRef(E->getType(), Record);
843 Record.push_back(E->isTypeDependent());
844 Record.push_back(E->isValueDependent());
845}
846
Douglas Gregor17fc2232009-04-14 21:55:33 +0000847void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
848 VisitExpr(E);
849 Writer.AddSourceLocation(E->getLocation(), Record);
850 Record.push_back(E->getIdentType()); // FIXME: stable encoding
851 Code = pch::EXPR_PREDEFINED;
852}
853
Douglas Gregor0b748912009-04-14 21:18:50 +0000854void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
855 VisitExpr(E);
856 Writer.AddDeclRef(E->getDecl(), Record);
857 Writer.AddSourceLocation(E->getLocation(), Record);
858 Code = pch::EXPR_DECL_REF;
859}
860
861void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
862 VisitExpr(E);
863 Writer.AddSourceLocation(E->getLocation(), Record);
864 Writer.AddAPInt(E->getValue(), Record);
865 Code = pch::EXPR_INTEGER_LITERAL;
866}
867
Douglas Gregor17fc2232009-04-14 21:55:33 +0000868void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
869 VisitExpr(E);
870 Writer.AddAPFloat(E->getValue(), Record);
871 Record.push_back(E->isExact());
872 Writer.AddSourceLocation(E->getLocation(), Record);
873 Code = pch::EXPR_FLOATING_LITERAL;
874}
875
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000876void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
877 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000878 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000879 Code = pch::EXPR_IMAGINARY_LITERAL;
880}
881
Douglas Gregor673ecd62009-04-15 16:35:07 +0000882void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
883 VisitExpr(E);
884 Record.push_back(E->getByteLength());
885 Record.push_back(E->getNumConcatenated());
886 Record.push_back(E->isWide());
887 // FIXME: String data should be stored as a blob at the end of the
888 // StringLiteral. However, we can't do so now because we have no
889 // provision for coping with abbreviations when we're jumping around
890 // the PCH file during deserialization.
891 Record.insert(Record.end(),
892 E->getStrData(), E->getStrData() + E->getByteLength());
893 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
894 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
895 Code = pch::EXPR_STRING_LITERAL;
896}
897
Douglas Gregor0b748912009-04-14 21:18:50 +0000898void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
899 VisitExpr(E);
900 Record.push_back(E->getValue());
901 Writer.AddSourceLocation(E->getLoc(), Record);
902 Record.push_back(E->isWide());
903 Code = pch::EXPR_CHARACTER_LITERAL;
904}
905
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000906void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
907 VisitExpr(E);
908 Writer.AddSourceLocation(E->getLParen(), Record);
909 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000910 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000911 Code = pch::EXPR_PAREN;
912}
913
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000914void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
915 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000916 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000917 Record.push_back(E->getOpcode()); // FIXME: stable encoding
918 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
919 Code = pch::EXPR_UNARY_OPERATOR;
920}
921
922void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
923 VisitExpr(E);
924 Record.push_back(E->isSizeOf());
925 if (E->isArgumentType())
926 Writer.AddTypeRef(E->getArgumentType(), Record);
927 else {
928 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000929 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000930 }
931 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
932 Writer.AddSourceLocation(E->getRParenLoc(), Record);
933 Code = pch::EXPR_SIZEOF_ALIGN_OF;
934}
935
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000936void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
937 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000938 Writer.WriteSubStmt(E->getLHS());
939 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000940 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
941 Code = pch::EXPR_ARRAY_SUBSCRIPT;
942}
943
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000944void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
945 VisitExpr(E);
946 Record.push_back(E->getNumArgs());
947 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000948 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000949 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
950 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000951 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000952 Code = pch::EXPR_CALL;
953}
954
955void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
956 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000957 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000958 Writer.AddDeclRef(E->getMemberDecl(), Record);
959 Writer.AddSourceLocation(E->getMemberLoc(), Record);
960 Record.push_back(E->isArrow());
961 Code = pch::EXPR_MEMBER;
962}
963
Douglas Gregor087fd532009-04-14 23:32:43 +0000964void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
965 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000966 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000967}
968
Douglas Gregordb600c32009-04-15 00:25:59 +0000969void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
970 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000971 Writer.WriteSubStmt(E->getLHS());
972 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000973 Record.push_back(E->getOpcode()); // FIXME: stable encoding
974 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
975 Code = pch::EXPR_BINARY_OPERATOR;
976}
977
Douglas Gregorad90e962009-04-15 22:40:36 +0000978void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
979 VisitBinaryOperator(E);
980 Writer.AddTypeRef(E->getComputationLHSType(), Record);
981 Writer.AddTypeRef(E->getComputationResultType(), Record);
982 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
983}
984
985void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
986 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000987 Writer.WriteSubStmt(E->getCond());
988 Writer.WriteSubStmt(E->getLHS());
989 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000990 Code = pch::EXPR_CONDITIONAL_OPERATOR;
991}
992
Douglas Gregor087fd532009-04-14 23:32:43 +0000993void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
994 VisitCastExpr(E);
995 Record.push_back(E->isLvalueCast());
996 Code = pch::EXPR_IMPLICIT_CAST;
997}
998
Douglas Gregordb600c32009-04-15 00:25:59 +0000999void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1000 VisitCastExpr(E);
1001 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1002}
1003
1004void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1005 VisitExplicitCastExpr(E);
1006 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1007 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1008 Code = pch::EXPR_CSTYLE_CAST;
1009}
1010
Douglas Gregorba6d7e72009-04-16 02:33:48 +00001011void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1012 VisitExpr(E);
1013 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001014 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +00001015 Record.push_back(E->isFileScope());
1016 Code = pch::EXPR_COMPOUND_LITERAL;
1017}
1018
Douglas Gregord3c98a02009-04-15 23:02:49 +00001019void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1020 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001021 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +00001022 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1023 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1024 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1025}
1026
Douglas Gregord077d752009-04-16 00:55:48 +00001027void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1028 VisitExpr(E);
1029 Record.push_back(E->getNumInits());
1030 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001031 Writer.WriteSubStmt(E->getInit(I));
1032 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +00001033 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1034 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1035 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1036 Record.push_back(E->hadArrayRangeDesignator());
1037 Code = pch::EXPR_INIT_LIST;
1038}
1039
1040void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1041 VisitExpr(E);
1042 Record.push_back(E->getNumSubExprs());
1043 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001044 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +00001045 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1046 Record.push_back(E->usesGNUSyntax());
1047 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1048 DEnd = E->designators_end();
1049 D != DEnd; ++D) {
1050 if (D->isFieldDesignator()) {
1051 if (FieldDecl *Field = D->getField()) {
1052 Record.push_back(pch::DESIG_FIELD_DECL);
1053 Writer.AddDeclRef(Field, Record);
1054 } else {
1055 Record.push_back(pch::DESIG_FIELD_NAME);
1056 Writer.AddIdentifierRef(D->getFieldName(), Record);
1057 }
1058 Writer.AddSourceLocation(D->getDotLoc(), Record);
1059 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1060 } else if (D->isArrayDesignator()) {
1061 Record.push_back(pch::DESIG_ARRAY);
1062 Record.push_back(D->getFirstExprIndex());
1063 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1064 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1065 } else {
1066 assert(D->isArrayRangeDesignator() && "Unknown designator");
1067 Record.push_back(pch::DESIG_ARRAY_RANGE);
1068 Record.push_back(D->getFirstExprIndex());
1069 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1070 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1071 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1072 }
1073 }
1074 Code = pch::EXPR_DESIGNATED_INIT;
1075}
1076
1077void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1078 VisitExpr(E);
1079 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1080}
1081
Douglas Gregord3c98a02009-04-15 23:02:49 +00001082void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1083 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001084 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +00001085 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1086 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1087 Code = pch::EXPR_VA_ARG;
1088}
1089
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00001090void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1091 VisitExpr(E);
1092 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1093 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1094 Record.push_back(Writer.GetLabelID(E->getLabel()));
1095 Code = pch::EXPR_ADDR_LABEL;
1096}
1097
Douglas Gregor6a2dd552009-04-17 19:05:30 +00001098void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1099 VisitExpr(E);
1100 Writer.WriteSubStmt(E->getSubStmt());
1101 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1102 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1103 Code = pch::EXPR_STMT;
1104}
1105
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001106void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1107 VisitExpr(E);
1108 Writer.AddTypeRef(E->getArgType1(), Record);
1109 Writer.AddTypeRef(E->getArgType2(), Record);
1110 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1111 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1112 Code = pch::EXPR_TYPES_COMPATIBLE;
1113}
1114
1115void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1116 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001117 Writer.WriteSubStmt(E->getCond());
1118 Writer.WriteSubStmt(E->getLHS());
1119 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001120 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1121 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1122 Code = pch::EXPR_CHOOSE;
1123}
1124
1125void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1126 VisitExpr(E);
1127 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1128 Code = pch::EXPR_GNU_NULL;
1129}
1130
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001131void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1132 VisitExpr(E);
1133 Record.push_back(E->getNumSubExprs());
1134 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001135 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001136 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1137 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1138 Code = pch::EXPR_SHUFFLE_VECTOR;
1139}
1140
Douglas Gregor84af7c22009-04-17 19:21:43 +00001141void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1142 VisitExpr(E);
1143 Writer.AddDeclRef(E->getBlockDecl(), Record);
1144 Record.push_back(E->hasBlockDeclRefExprs());
1145 Code = pch::EXPR_BLOCK;
1146}
1147
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001148void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1149 VisitExpr(E);
1150 Writer.AddDeclRef(E->getDecl(), Record);
1151 Writer.AddSourceLocation(E->getLocation(), Record);
1152 Record.push_back(E->isByRef());
1153 Code = pch::EXPR_BLOCK_DECL_REF;
1154}
1155
Douglas Gregor0b748912009-04-14 21:18:50 +00001156//===----------------------------------------------------------------------===//
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001157// Objective-C Expressions and Statements.
1158//===----------------------------------------------------------------------===//
1159
Chris Lattner3a57a372009-04-22 06:29:42 +00001160void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1161 VisitExpr(E);
1162 Writer.WriteSubStmt(E->getString());
1163 Writer.AddSourceLocation(E->getAtLoc(), Record);
1164 Code = pch::EXPR_OBJC_STRING_LITERAL;
1165}
1166
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001167void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1168 VisitExpr(E);
1169 Writer.AddTypeRef(E->getEncodedType(), Record);
1170 Writer.AddSourceLocation(E->getAtLoc(), Record);
1171 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1172 Code = pch::EXPR_OBJC_ENCODE;
1173}
1174
Chris Lattner3a57a372009-04-22 06:29:42 +00001175void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1176 VisitExpr(E);
1177 // FIXME! Write selectors.
1178 //Writer.WriteSubStmt(E->getSelector());
1179 Writer.AddSourceLocation(E->getAtLoc(), Record);
1180 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1181 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1182}
1183
1184void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1185 VisitExpr(E);
1186 Writer.AddDeclRef(E->getProtocol(), Record);
1187 Writer.AddSourceLocation(E->getAtLoc(), Record);
1188 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1189 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1190}
1191
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001192
1193//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00001194// PCHWriter Implementation
1195//===----------------------------------------------------------------------===//
1196
Douglas Gregor2bec0412009-04-10 21:16:55 +00001197/// \brief Write the target triple (e.g., i686-apple-darwin9).
1198void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1199 using namespace llvm;
1200 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1201 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001203 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001204
1205 RecordData Record;
1206 Record.push_back(pch::TARGET_TRIPLE);
1207 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001208 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +00001209}
1210
1211/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001212void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1213 RecordData Record;
1214 Record.push_back(LangOpts.Trigraphs);
1215 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1216 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1217 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1218 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1219 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1220 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1221 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1222 Record.push_back(LangOpts.C99); // C99 Support
1223 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1224 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1225 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1226 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1227 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1228
1229 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1230 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1231 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1232
1233 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1234 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1235 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1236 Record.push_back(LangOpts.LaxVectorConversions);
1237 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1238
1239 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1240 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1241 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1242
1243 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1244 // by locks.
1245 Record.push_back(LangOpts.Blocks); // block extension to C
1246 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1247 // they are unused.
1248 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1249 // (modulo the platform support).
1250
1251 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1252 // signed integer arithmetic overflows.
1253
1254 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1255 // may be ripped out at any time.
1256
1257 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1258 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1259 // defined.
1260 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1261 // opposed to __DYNAMIC__).
1262 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1263
1264 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1265 // used (instead of C99 semantics).
1266 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1267 Record.push_back(LangOpts.getGCMode());
1268 Record.push_back(LangOpts.getVisibilityMode());
1269 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001270 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001271}
1272
Douglas Gregor14f79002009-04-10 03:52:48 +00001273//===----------------------------------------------------------------------===//
1274// Source Manager Serialization
1275//===----------------------------------------------------------------------===//
1276
1277/// \brief Create an abbreviation for the SLocEntry that refers to a
1278/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001279static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001280 using namespace llvm;
1281 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1282 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +00001287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001288 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001289}
1290
1291/// \brief Create an abbreviation for the SLocEntry that refers to a
1292/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001293static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001294 using namespace llvm;
1295 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1296 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001302 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001303}
1304
1305/// \brief Create an abbreviation for the SLocEntry that refers to a
1306/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001307static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001308 using namespace llvm;
1309 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1310 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001312 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001313}
1314
1315/// \brief Create an abbreviation for the SLocEntry that refers to an
1316/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001317static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001318 using namespace llvm;
1319 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1320 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1321 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1324 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001326 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001327}
1328
1329/// \brief Writes the block containing the serialized form of the
1330/// source manager.
1331///
1332/// TODO: We should probably use an on-disk hash table (stored in a
1333/// blob), indexed based on the file name, so that we only create
1334/// entries for files that we actually need. In the common case (no
1335/// errors), we probably won't have to create file entries for any of
1336/// the files in the AST.
1337void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001338 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001339 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001340
1341 // Abbreviations for the various kinds of source-location entries.
1342 int SLocFileAbbrv = -1;
1343 int SLocBufferAbbrv = -1;
1344 int SLocBufferBlobAbbrv = -1;
1345 int SLocInstantiationAbbrv = -1;
1346
1347 // Write out the source location entry table. We skip the first
1348 // entry, which is always the same dummy entry.
1349 RecordData Record;
1350 for (SourceManager::sloc_entry_iterator
1351 SLoc = SourceMgr.sloc_entry_begin() + 1,
1352 SLocEnd = SourceMgr.sloc_entry_end();
1353 SLoc != SLocEnd; ++SLoc) {
1354 // Figure out which record code to use.
1355 unsigned Code;
1356 if (SLoc->isFile()) {
1357 if (SLoc->getFile().getContentCache()->Entry)
1358 Code = pch::SM_SLOC_FILE_ENTRY;
1359 else
1360 Code = pch::SM_SLOC_BUFFER_ENTRY;
1361 } else
1362 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1363 Record.push_back(Code);
1364
1365 Record.push_back(SLoc->getOffset());
1366 if (SLoc->isFile()) {
1367 const SrcMgr::FileInfo &File = SLoc->getFile();
1368 Record.push_back(File.getIncludeLoc().getRawEncoding());
1369 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001370 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001371
1372 const SrcMgr::ContentCache *Content = File.getContentCache();
1373 if (Content->Entry) {
1374 // The source location entry is a file. The blob associated
1375 // with this entry is the file name.
1376 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001377 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1378 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001379 Content->Entry->getName(),
1380 strlen(Content->Entry->getName()));
1381 } else {
1382 // The source location entry is a buffer. The blob associated
1383 // with this entry contains the contents of the buffer.
1384 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001385 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1386 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001387 }
1388
1389 // We add one to the size so that we capture the trailing NULL
1390 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1391 // the reader side).
1392 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1393 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001394 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001395 Record.clear();
1396 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001397 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001398 Buffer->getBufferStart(),
1399 Buffer->getBufferSize() + 1);
1400 }
1401 } else {
1402 // The source location entry is an instantiation.
1403 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1404 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1405 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1406 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1407
Douglas Gregorf60e9912009-04-15 18:05:10 +00001408 // Compute the token length for this macro expansion.
1409 unsigned NextOffset = SourceMgr.getNextOffset();
1410 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1411 if (++NextSLoc != SLocEnd)
1412 NextOffset = NextSLoc->getOffset();
1413 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1414
Douglas Gregor14f79002009-04-10 03:52:48 +00001415 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001416 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1417 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001418 }
1419
1420 Record.clear();
1421 }
1422
Douglas Gregorbd945002009-04-13 16:31:14 +00001423 // Write the line table.
1424 if (SourceMgr.hasLineTable()) {
1425 LineTableInfo &LineTable = SourceMgr.getLineTable();
1426
1427 // Emit the file names
1428 Record.push_back(LineTable.getNumFilenames());
1429 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1430 // Emit the file name
1431 const char *Filename = LineTable.getFilename(I);
1432 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1433 Record.push_back(FilenameLen);
1434 if (FilenameLen)
1435 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1436 }
1437
1438 // Emit the line entries
1439 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1440 L != LEnd; ++L) {
1441 // Emit the file ID
1442 Record.push_back(L->first);
1443
1444 // Emit the line entries
1445 Record.push_back(L->second.size());
1446 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1447 LEEnd = L->second.end();
1448 LE != LEEnd; ++LE) {
1449 Record.push_back(LE->FileOffset);
1450 Record.push_back(LE->LineNo);
1451 Record.push_back(LE->FilenameID);
1452 Record.push_back((unsigned)LE->FileKind);
1453 Record.push_back(LE->IncludeOffset);
1454 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001455 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001456 }
1457 }
1458
Douglas Gregorc9490c02009-04-16 22:23:12 +00001459 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001460}
1461
Chris Lattner0b1fb982009-04-10 17:15:23 +00001462/// \brief Writes the block containing the serialized form of the
1463/// preprocessor.
1464///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001465void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001466 // Enter the preprocessor block.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001467 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001468
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001469 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1470 // FIXME: use diagnostics subsystem for localization etc.
1471 if (PP.SawDateOrTime())
1472 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001473
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001474 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001475
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001476 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1477 if (PP.getCounterValue() != 0) {
1478 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001479 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001480 Record.clear();
1481 }
1482
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001483 // Loop over all the macro definitions that are live at the end of the file,
1484 // emitting each to the PP section.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001485 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1486 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001487 // FIXME: This emits macros in hash table order, we should do it in a stable
1488 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001489 MacroInfo *MI = I->second;
1490
1491 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1492 // been redefined by the header (in which case they are not isBuiltinMacro).
1493 if (MI->isBuiltinMacro())
1494 continue;
1495
Douglas Gregor37e26842009-04-21 23:56:24 +00001496 // FIXME: Remove this identifier reference?
Chris Lattner7356a312009-04-11 21:15:38 +00001497 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001498 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001499 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1500 Record.push_back(MI->isUsed());
1501
1502 unsigned Code;
1503 if (MI->isObjectLike()) {
1504 Code = pch::PP_MACRO_OBJECT_LIKE;
1505 } else {
1506 Code = pch::PP_MACRO_FUNCTION_LIKE;
1507
1508 Record.push_back(MI->isC99Varargs());
1509 Record.push_back(MI->isGNUVarargs());
1510 Record.push_back(MI->getNumArgs());
1511 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1512 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001513 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001514 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001515 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001516 Record.clear();
1517
Chris Lattnerdf961c22009-04-10 18:08:30 +00001518 // Emit the tokens array.
1519 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1520 // Note that we know that the preprocessor does not have any annotation
1521 // tokens in it because they are created by the parser, and thus can't be
1522 // in a macro definition.
1523 const Token &Tok = MI->getReplacementToken(TokNo);
1524
1525 Record.push_back(Tok.getLocation().getRawEncoding());
1526 Record.push_back(Tok.getLength());
1527
Chris Lattnerdf961c22009-04-10 18:08:30 +00001528 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1529 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001530 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001531
1532 // FIXME: Should translate token kind to a stable encoding.
1533 Record.push_back(Tok.getKind());
1534 // FIXME: Should translate token flags to a stable encoding.
1535 Record.push_back(Tok.getFlags());
1536
Douglas Gregorc9490c02009-04-16 22:23:12 +00001537 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001538 Record.clear();
1539 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001540 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001541 }
1542
Douglas Gregorc9490c02009-04-16 22:23:12 +00001543 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001544}
1545
1546
Douglas Gregor2cf26342009-04-09 22:27:44 +00001547/// \brief Write the representation of a type to the PCH stream.
1548void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001549 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001550 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001551 ID = NextTypeID++;
1552
1553 // Record the offset for this type.
1554 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001555 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001556 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1557 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001558 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001559 }
1560
1561 RecordData Record;
1562
1563 // Emit the type's representation.
1564 PCHTypeWriter W(*this, Record);
1565 switch (T->getTypeClass()) {
1566 // For all of the concrete, non-dependent types, call the
1567 // appropriate visitor function.
1568#define TYPE(Class, Base) \
1569 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1570#define ABSTRACT_TYPE(Class, Base)
1571#define DEPENDENT_TYPE(Class, Base)
1572#include "clang/AST/TypeNodes.def"
1573
1574 // For all of the dependent type nodes (which only occur in C++
1575 // templates), produce an error.
1576#define TYPE(Class, Base)
1577#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1578#include "clang/AST/TypeNodes.def"
1579 assert(false && "Cannot serialize dependent type nodes");
1580 break;
1581 }
1582
1583 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001584 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001585
1586 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001587 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001588}
1589
1590/// \brief Write a block containing all of the types.
1591void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001592 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001593 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001594
1595 // Emit all of the types in the ASTContext
1596 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1597 TEnd = Context.getTypes().end();
1598 T != TEnd; ++T) {
1599 // Builtin types are never serialized.
1600 if (isa<BuiltinType>(*T))
1601 continue;
1602
1603 WriteType(*T);
1604 }
1605
1606 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001607 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001608}
1609
1610/// \brief Write the block containing all of the declaration IDs
1611/// lexically declared within the given DeclContext.
1612///
1613/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1614/// bistream, or 0 if no block was written.
1615uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1616 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001617 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001618 return 0;
1619
Douglas Gregorc9490c02009-04-16 22:23:12 +00001620 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001621 RecordData Record;
1622 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1623 DEnd = DC->decls_end(Context);
1624 D != DEnd; ++D)
1625 AddDeclRef(*D, Record);
1626
Douglas Gregorc9490c02009-04-16 22:23:12 +00001627 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001628 return Offset;
1629}
1630
1631/// \brief Write the block containing all of the declaration IDs
1632/// visible from the given DeclContext.
1633///
1634/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1635/// bistream, or 0 if no block was written.
1636uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1637 DeclContext *DC) {
1638 if (DC->getPrimaryContext() != DC)
1639 return 0;
1640
Douglas Gregoraff22df2009-04-21 22:32:33 +00001641 // Since there is no name lookup into functions or methods, and we
1642 // perform name lookup for the translation unit via the
1643 // IdentifierInfo chains, don't bother to build a
1644 // visible-declarations table for these entities.
1645 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001646 return 0;
1647
Douglas Gregor2cf26342009-04-09 22:27:44 +00001648 // Force the DeclContext to build a its name-lookup table.
1649 DC->lookup(Context, DeclarationName());
1650
1651 // Serialize the contents of the mapping used for lookup. Note that,
1652 // although we have two very different code paths, the serialized
1653 // representation is the same for both cases: a declaration name,
1654 // followed by a size, followed by references to the visible
1655 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001656 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001657 RecordData Record;
1658 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001659 if (!Map)
1660 return 0;
1661
Douglas Gregor2cf26342009-04-09 22:27:44 +00001662 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1663 D != DEnd; ++D) {
1664 AddDeclarationName(D->first, Record);
1665 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1666 Record.push_back(Result.second - Result.first);
1667 for(; Result.first != Result.second; ++Result.first)
1668 AddDeclRef(*Result.first, Record);
1669 }
1670
1671 if (Record.size() == 0)
1672 return 0;
1673
Douglas Gregorc9490c02009-04-16 22:23:12 +00001674 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001675 return Offset;
1676}
1677
1678/// \brief Write a block containing all of the declarations.
1679void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001680 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001681 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001682
1683 // Emit all of the declarations.
1684 RecordData Record;
Douglas Gregor72971342009-04-18 00:02:19 +00001685 PCHDeclWriter W(*this, Context, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001686 while (!DeclsToEmit.empty()) {
1687 // Pull the next declaration off the queue
1688 Decl *D = DeclsToEmit.front();
1689 DeclsToEmit.pop();
1690
1691 // If this declaration is also a DeclContext, write blocks for the
1692 // declarations that lexically stored inside its context and those
1693 // declarations that are visible from its context. These blocks
1694 // are written before the declaration itself so that we can put
1695 // their offsets into the record for the declaration.
1696 uint64_t LexicalOffset = 0;
1697 uint64_t VisibleOffset = 0;
1698 DeclContext *DC = dyn_cast<DeclContext>(D);
1699 if (DC) {
1700 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1701 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1702 }
1703
1704 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001705 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001706 if (ID == 0)
1707 ID = DeclIDs.size();
1708
1709 unsigned Index = ID - 1;
1710
1711 // Record the offset for this declaration
1712 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001713 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001714 else if (DeclOffsets.size() < Index) {
1715 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001716 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001717 }
1718
1719 // Build and emit a record for this declaration
1720 Record.clear();
1721 W.Code = (pch::DeclCode)0;
1722 W.Visit(D);
1723 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001724 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001725 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001726
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001727 // If the declaration had any attributes, write them now.
1728 if (D->hasAttrs())
1729 WriteAttributeRecord(D->getAttrs());
1730
Douglas Gregor0b748912009-04-14 21:18:50 +00001731 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001732 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001733
Douglas Gregorfdd01722009-04-14 00:24:19 +00001734 // Note external declarations so that we can add them to a record
1735 // in the PCH file later.
1736 if (isa<FileScopeAsmDecl>(D))
1737 ExternalDefinitions.push_back(ID);
1738 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1739 if (// Non-static file-scope variables with initializers or that
1740 // are tentative definitions.
1741 (Var->isFileVarDecl() &&
1742 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1743 // Out-of-line definitions of static data members (C++).
1744 (Var->getDeclContext()->isRecord() &&
1745 !Var->getLexicalDeclContext()->isRecord() &&
1746 Var->getStorageClass() == VarDecl::Static))
1747 ExternalDefinitions.push_back(ID);
1748 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00001749 if (Func->isThisDeclarationADefinition())
Douglas Gregorfdd01722009-04-14 00:24:19 +00001750 ExternalDefinitions.push_back(ID);
1751 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001752 }
1753
1754 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001755 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001756}
1757
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001758namespace {
1759class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1760 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001761 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001762
1763public:
1764 typedef const IdentifierInfo* key_type;
1765 typedef key_type key_type_ref;
1766
1767 typedef pch::IdentID data_type;
1768 typedef data_type data_type_ref;
1769
Douglas Gregor37e26842009-04-21 23:56:24 +00001770 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1771 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001772
1773 static unsigned ComputeHash(const IdentifierInfo* II) {
1774 return clang::BernsteinHash(II->getName());
1775 }
1776
Douglas Gregor37e26842009-04-21 23:56:24 +00001777 std::pair<unsigned,unsigned>
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001778 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1779 pch::IdentID ID) {
1780 unsigned KeyLen = strlen(II->getName()) + 1;
1781 clang::io::Emit16(Out, KeyLen);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001782 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1783 // 4 bytes for the persistent ID
Douglas Gregor37e26842009-04-21 23:56:24 +00001784 if (II->hasMacroDefinition() &&
1785 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1786 DataLen += 8;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001787 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1788 DEnd = IdentifierResolver::end();
1789 D != DEnd; ++D)
1790 DataLen += sizeof(pch::DeclID);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001791 clang::io::Emit16(Out, DataLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001792 return std::make_pair(KeyLen, DataLen);
1793 }
1794
1795 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1796 unsigned KeyLen) {
1797 // Record the location of the key data. This is used when generating
1798 // the mapping from persistent IDs to strings.
1799 Writer.SetIdentifierOffset(II, Out.tell());
1800 Out.write(II->getName(), KeyLen);
1801 }
1802
1803 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1804 pch::IdentID ID, unsigned) {
1805 uint32_t Bits = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001806 bool hasMacroDefinition =
1807 II->hasMacroDefinition() &&
1808 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001809 Bits = Bits | (uint32_t)II->getTokenID();
1810 Bits = (Bits << 8) | (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor37e26842009-04-21 23:56:24 +00001811 Bits = (Bits << 10) | hasMacroDefinition;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001812 Bits = (Bits << 1) | II->isExtensionToken();
1813 Bits = (Bits << 1) | II->isPoisoned();
1814 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1815 clang::io::Emit32(Out, Bits);
1816 clang::io::Emit32(Out, ID);
1817
Douglas Gregor37e26842009-04-21 23:56:24 +00001818 if (hasMacroDefinition)
1819 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1820
Douglas Gregor668c1a42009-04-21 22:25:48 +00001821 // Emit the declaration IDs in reverse order, because the
1822 // IdentifierResolver provides the declarations as they would be
1823 // visible (e.g., the function "stat" would come before the struct
1824 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1825 // adds declarations to the end of the list (so we need to see the
1826 // struct "status" before the function "status").
1827 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1828 IdentifierResolver::end());
1829 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1830 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001831 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001832 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001833 }
1834};
1835} // end anonymous namespace
1836
Douglas Gregorafaf3082009-04-11 00:14:32 +00001837/// \brief Write the identifier table into the PCH file.
1838///
1839/// The identifier table consists of a blob containing string data
1840/// (the actual identifiers themselves) and a separate "offsets" index
1841/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001842void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001843 using namespace llvm;
1844
1845 // Create and write out the blob that contains the identifier
1846 // strings.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001847 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001848 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001849 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1850
1851 // Create the on-disk hash table representation.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001852 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1853 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1854 ID != IDEnd; ++ID) {
1855 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001856 Generator.insert(ID->first, ID->second);
1857 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001858
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001859 // Create the on-disk hash table in a buffer.
1860 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001861 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001862 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001863 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001864 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001865 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001866 }
1867
1868 // Create a blob abbreviation
1869 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1870 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001873 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001874
1875 // Write the identifier table
1876 RecordData Record;
1877 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001878 Record.push_back(BucketOffset);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001879 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1880 &IdentifierTable.front(),
1881 IdentifierTable.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001882 }
1883
1884 // Write the offsets table for identifier IDs.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001885 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001886}
1887
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001888/// \brief Write a record containing the given attributes.
1889void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1890 RecordData Record;
1891 for (; Attr; Attr = Attr->getNext()) {
1892 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1893 Record.push_back(Attr->isInherited());
1894 switch (Attr->getKind()) {
1895 case Attr::Alias:
1896 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1897 break;
1898
1899 case Attr::Aligned:
1900 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1901 break;
1902
1903 case Attr::AlwaysInline:
1904 break;
1905
1906 case Attr::AnalyzerNoReturn:
1907 break;
1908
1909 case Attr::Annotate:
1910 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1911 break;
1912
1913 case Attr::AsmLabel:
1914 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1915 break;
1916
1917 case Attr::Blocks:
1918 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1919 break;
1920
1921 case Attr::Cleanup:
1922 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1923 break;
1924
1925 case Attr::Const:
1926 break;
1927
1928 case Attr::Constructor:
1929 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1930 break;
1931
1932 case Attr::DLLExport:
1933 case Attr::DLLImport:
1934 case Attr::Deprecated:
1935 break;
1936
1937 case Attr::Destructor:
1938 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1939 break;
1940
1941 case Attr::FastCall:
1942 break;
1943
1944 case Attr::Format: {
1945 const FormatAttr *Format = cast<FormatAttr>(Attr);
1946 AddString(Format->getType(), Record);
1947 Record.push_back(Format->getFormatIdx());
1948 Record.push_back(Format->getFirstArg());
1949 break;
1950 }
1951
Chris Lattnercf2a7212009-04-20 19:12:28 +00001952 case Attr::GNUInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001953 case Attr::IBOutletKind:
1954 case Attr::NoReturn:
1955 case Attr::NoThrow:
1956 case Attr::Nodebug:
1957 case Attr::Noinline:
1958 break;
1959
1960 case Attr::NonNull: {
1961 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1962 Record.push_back(NonNull->size());
1963 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1964 break;
1965 }
1966
1967 case Attr::ObjCException:
1968 case Attr::ObjCNSObject:
1969 case Attr::Overloadable:
1970 break;
1971
1972 case Attr::Packed:
1973 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1974 break;
1975
1976 case Attr::Pure:
1977 break;
1978
1979 case Attr::Regparm:
1980 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1981 break;
1982
1983 case Attr::Section:
1984 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1985 break;
1986
1987 case Attr::StdCall:
1988 case Attr::TransparentUnion:
1989 case Attr::Unavailable:
1990 case Attr::Unused:
1991 case Attr::Used:
1992 break;
1993
1994 case Attr::Visibility:
1995 // FIXME: stable encoding
1996 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1997 break;
1998
1999 case Attr::WarnUnusedResult:
2000 case Attr::Weak:
2001 case Attr::WeakImport:
2002 break;
2003 }
2004 }
2005
Douglas Gregorc9490c02009-04-16 22:23:12 +00002006 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002007}
2008
2009void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2010 Record.push_back(Str.size());
2011 Record.insert(Record.end(), Str.begin(), Str.end());
2012}
2013
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002014/// \brief Note that the identifier II occurs at the given offset
2015/// within the identifier table.
2016void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
2017 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
2018}
2019
Douglas Gregorc9490c02009-04-16 22:23:12 +00002020PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor37e26842009-04-21 23:56:24 +00002021 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
2022 NumStatements(0), NumMacros(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002023
Douglas Gregore7785042009-04-20 15:53:59 +00002024void PCHWriter::WritePCH(Sema &SemaRef) {
2025 ASTContext &Context = SemaRef.Context;
2026 Preprocessor &PP = SemaRef.PP;
2027
Douglas Gregor2cf26342009-04-09 22:27:44 +00002028 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002029 Stream.Emit((unsigned)'C', 8);
2030 Stream.Emit((unsigned)'P', 8);
2031 Stream.Emit((unsigned)'C', 8);
2032 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002033
2034 // The translation unit is the first declaration we'll emit.
2035 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2036 DeclsToEmit.push(Context.getTranslationUnitDecl());
2037
2038 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002039 RecordData Record;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002040 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00002041 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002042 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00002043 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00002044 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002045 WriteTypesBlock(Context);
2046 WriteDeclsBlock(Context);
Douglas Gregor37e26842009-04-21 23:56:24 +00002047 WriteIdentifierTable(PP);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002048 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
2049 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorad1de002009-04-18 05:55:16 +00002050
2051 // Write the record of special types.
2052 Record.clear();
2053 AddTypeRef(Context.getBuiltinVaListType(), Record);
2054 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2055
Douglas Gregorfdd01722009-04-14 00:24:19 +00002056 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002057 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002058
2059 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002060 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002061 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002062 Record.push_back(NumMacros);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002063 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002064 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002065}
2066
2067void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2068 Record.push_back(Loc.getRawEncoding());
2069}
2070
2071void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2072 Record.push_back(Value.getBitWidth());
2073 unsigned N = Value.getNumWords();
2074 const uint64_t* Words = Value.getRawData();
2075 for (unsigned I = 0; I != N; ++I)
2076 Record.push_back(Words[I]);
2077}
2078
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002079void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2080 Record.push_back(Value.isUnsigned());
2081 AddAPInt(Value, Record);
2082}
2083
Douglas Gregor17fc2232009-04-14 21:55:33 +00002084void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2085 AddAPInt(Value.bitcastToAPInt(), Record);
2086}
2087
Douglas Gregor2cf26342009-04-09 22:27:44 +00002088void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002089 if (II == 0) {
2090 Record.push_back(0);
2091 return;
2092 }
2093
2094 pch::IdentID &ID = IdentifierIDs[II];
2095 if (ID == 0)
2096 ID = IdentifierIDs.size();
2097
2098 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002099}
2100
2101void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2102 if (T.isNull()) {
2103 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2104 return;
2105 }
2106
2107 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002108 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002109 switch (BT->getKind()) {
2110 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2111 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2112 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2113 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2114 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2115 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2116 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2117 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2118 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2119 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2120 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2121 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2122 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2123 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2124 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2125 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2126 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2127 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2128 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2129 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2130 }
2131
2132 Record.push_back((ID << 3) | T.getCVRQualifiers());
2133 return;
2134 }
2135
Douglas Gregor8038d512009-04-10 17:25:41 +00002136 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002137 if (ID == 0) // we haven't seen this type before
2138 ID = NextTypeID++;
2139
2140 // Encode the type qualifiers in the type reference.
2141 Record.push_back((ID << 3) | T.getCVRQualifiers());
2142}
2143
2144void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2145 if (D == 0) {
2146 Record.push_back(0);
2147 return;
2148 }
2149
Douglas Gregor8038d512009-04-10 17:25:41 +00002150 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002151 if (ID == 0) {
2152 // We haven't seen this declaration before. Give it a new ID and
2153 // enqueue it in the list of declarations to emit.
2154 ID = DeclIDs.size();
2155 DeclsToEmit.push(const_cast<Decl *>(D));
2156 }
2157
2158 Record.push_back(ID);
2159}
2160
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002161pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2162 if (D == 0)
2163 return 0;
2164
2165 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2166 return DeclIDs[D];
2167}
2168
Douglas Gregor2cf26342009-04-09 22:27:44 +00002169void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2170 Record.push_back(Name.getNameKind());
2171 switch (Name.getNameKind()) {
2172 case DeclarationName::Identifier:
2173 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2174 break;
2175
2176 case DeclarationName::ObjCZeroArgSelector:
2177 case DeclarationName::ObjCOneArgSelector:
2178 case DeclarationName::ObjCMultiArgSelector:
2179 assert(false && "Serialization of Objective-C selectors unavailable");
2180 break;
2181
2182 case DeclarationName::CXXConstructorName:
2183 case DeclarationName::CXXDestructorName:
2184 case DeclarationName::CXXConversionFunctionName:
2185 AddTypeRef(Name.getCXXNameType(), Record);
2186 break;
2187
2188 case DeclarationName::CXXOperatorName:
2189 Record.push_back(Name.getCXXOverloadedOperator());
2190 break;
2191
2192 case DeclarationName::CXXUsingDirective:
2193 // No extra data to emit
2194 break;
2195 }
2196}
Douglas Gregor0b748912009-04-14 21:18:50 +00002197
Douglas Gregorc9490c02009-04-16 22:23:12 +00002198/// \brief Write the given substatement or subexpression to the
2199/// bitstream.
2200void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00002201 RecordData Record;
2202 PCHStmtWriter Writer(*this, Record);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002203 ++NumStatements;
Douglas Gregor087fd532009-04-14 23:32:43 +00002204
Douglas Gregorc9490c02009-04-16 22:23:12 +00002205 if (!S) {
2206 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002207 return;
2208 }
2209
Douglas Gregorc9490c02009-04-16 22:23:12 +00002210 Writer.Code = pch::STMT_NULL_PTR;
2211 Writer.Visit(S);
2212 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00002213 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002214 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002215}
2216
Douglas Gregorc9490c02009-04-16 22:23:12 +00002217/// \brief Flush all of the statements that have been added to the
2218/// queue via AddStmt().
2219void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00002220 RecordData Record;
2221 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002222
Douglas Gregorc9490c02009-04-16 22:23:12 +00002223 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor3e1af842009-04-17 22:13:46 +00002224 ++NumStatements;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002225 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00002226
Douglas Gregorc9490c02009-04-16 22:23:12 +00002227 if (!S) {
2228 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002229 continue;
2230 }
2231
Douglas Gregorc9490c02009-04-16 22:23:12 +00002232 Writer.Code = pch::STMT_NULL_PTR;
2233 Writer.Visit(S);
2234 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00002235 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002236 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002237
Douglas Gregorc9490c02009-04-16 22:23:12 +00002238 assert(N == StmtsToEmit.size() &&
2239 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00002240
2241 // Note that we are at the end of a full expression. Any
2242 // expression records that follow this one are part of a different
2243 // expression.
2244 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00002245 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002246 }
Douglas Gregor087fd532009-04-14 23:32:43 +00002247
Douglas Gregorc9490c02009-04-16 22:23:12 +00002248 StmtsToEmit.clear();
Douglas Gregor0de9d882009-04-17 16:34:57 +00002249 SwitchCaseIDs.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00002250}
Douglas Gregor025452f2009-04-17 00:04:06 +00002251
2252unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2253 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2254 "SwitchCase recorded twice");
2255 unsigned NextID = SwitchCaseIDs.size();
2256 SwitchCaseIDs[S] = NextID;
2257 return NextID;
2258}
2259
2260unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2261 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2262 "SwitchCase hasn't been seen yet");
2263 return SwitchCaseIDs[S];
2264}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002265
2266/// \brief Retrieve the ID for the given label statement, which may
2267/// or may not have been emitted yet.
2268unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2269 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2270 if (Pos != LabelIDs.end())
2271 return Pos->second;
2272
2273 unsigned NextID = LabelIDs.size();
2274 LabelIDs[S] = NextID;
2275 return NextID;
2276}