blob: dd8d95a56dfe1b961be5a4c4d3743d96c4ddfc9a [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
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231//===----------------------------------------------------------------------===//
232// Declaration serialization
233//===----------------------------------------------------------------------===//
234namespace {
235 class VISIBILITY_HIDDEN PCHDeclWriter
236 : public DeclVisitor<PCHDeclWriter, void> {
237
238 PCHWriter &Writer;
Douglas Gregor72971342009-04-18 00:02:19 +0000239 ASTContext &Context;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000240 PCHWriter::RecordData &Record;
241
242 public:
243 pch::DeclCode Code;
244
Douglas Gregor72971342009-04-18 00:02:19 +0000245 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
246 PCHWriter::RecordData &Record)
247 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248
249 void VisitDecl(Decl *D);
250 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
251 void VisitNamedDecl(NamedDecl *D);
252 void VisitTypeDecl(TypeDecl *D);
253 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000254 void VisitTagDecl(TagDecl *D);
255 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000256 void VisitRecordDecl(RecordDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257 void VisitValueDecl(ValueDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000258 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000259 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000260 void VisitFieldDecl(FieldDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000261 void VisitVarDecl(VarDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000262 void VisitParmVarDecl(ParmVarDecl *D);
263 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor1028bc62009-04-13 22:49:25 +0000264 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
265 void VisitBlockDecl(BlockDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000266 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
267 uint64_t VisibleOffset);
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000268 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff33feeb02009-04-20 20:09:33 +0000269 void VisitObjCContainerDecl(ObjCContainerDecl *D);
270 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
271 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff30833f82009-04-21 15:12:33 +0000272 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
273 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
274 void VisitObjCClassDecl(ObjCClassDecl *D);
275 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
276 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
277 void VisitObjCImplDecl(ObjCImplDecl *D);
278 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
279 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
280 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
281 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
282 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000283 };
284}
285
286void PCHDeclWriter::VisitDecl(Decl *D) {
287 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
288 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
289 Writer.AddSourceLocation(D->getLocation(), Record);
290 Record.push_back(D->isInvalidDecl());
Douglas Gregor68a2eb02009-04-15 21:30:51 +0000291 Record.push_back(D->hasAttrs());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000292 Record.push_back(D->isImplicit());
293 Record.push_back(D->getAccess());
294}
295
296void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
297 VisitDecl(D);
298 Code = pch::DECL_TRANSLATION_UNIT;
299}
300
301void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
302 VisitDecl(D);
303 Writer.AddDeclarationName(D->getDeclName(), Record);
304}
305
306void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
307 VisitNamedDecl(D);
308 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
309}
310
311void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
312 VisitTypeDecl(D);
313 Writer.AddTypeRef(D->getUnderlyingType(), Record);
314 Code = pch::DECL_TYPEDEF;
315}
316
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000317void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
318 VisitTypeDecl(D);
319 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
320 Record.push_back(D->isDefinition());
321 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
322}
323
324void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
325 VisitTagDecl(D);
326 Writer.AddTypeRef(D->getIntegerType(), Record);
327 Code = pch::DECL_ENUM;
328}
329
Douglas Gregor8c700062009-04-13 21:20:57 +0000330void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
331 VisitTagDecl(D);
332 Record.push_back(D->hasFlexibleArrayMember());
333 Record.push_back(D->isAnonymousStructOrUnion());
334 Code = pch::DECL_RECORD;
335}
336
Douglas Gregor2cf26342009-04-09 22:27:44 +0000337void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
338 VisitNamedDecl(D);
339 Writer.AddTypeRef(D->getType(), Record);
340}
341
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000342void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
343 VisitValueDecl(D);
Douglas Gregor0b748912009-04-14 21:18:50 +0000344 Record.push_back(D->getInitExpr()? 1 : 0);
345 if (D->getInitExpr())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000346 Writer.AddStmt(D->getInitExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000347 Writer.AddAPSInt(D->getInitVal(), Record);
348 Code = pch::DECL_ENUM_CONSTANT;
349}
350
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000351void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
352 VisitValueDecl(D);
Douglas Gregor025452f2009-04-17 00:04:06 +0000353 Record.push_back(D->isThisDeclarationADefinition());
354 if (D->isThisDeclarationADefinition())
Douglas Gregor72971342009-04-18 00:02:19 +0000355 Writer.AddStmt(D->getBody(Context));
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000356 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
357 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
358 Record.push_back(D->isInline());
359 Record.push_back(D->isVirtual());
360 Record.push_back(D->isPure());
361 Record.push_back(D->inheritedPrototype());
362 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
363 Record.push_back(D->isDeleted());
364 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
365 Record.push_back(D->param_size());
366 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
367 P != PEnd; ++P)
368 Writer.AddDeclRef(*P, Record);
369 Code = pch::DECL_FUNCTION;
370}
371
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000372void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
373 VisitNamedDecl(D);
374 // FIXME: convert to LazyStmtPtr?
375 // Unlike C/C++, method bodies will never be in header files.
376 Record.push_back(D->getBody() != 0);
377 if (D->getBody() != 0) {
378 Writer.AddStmt(D->getBody(Context));
379 Writer.AddDeclRef(D->getSelfDecl(), Record);
380 Writer.AddDeclRef(D->getCmdDecl(), Record);
381 }
382 Record.push_back(D->isInstanceMethod());
383 Record.push_back(D->isVariadic());
384 Record.push_back(D->isSynthesized());
385 // FIXME: stable encoding for @required/@optional
386 Record.push_back(D->getImplementationControl());
387 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
388 Record.push_back(D->getObjCDeclQualifier());
389 Writer.AddTypeRef(D->getResultType(), Record);
390 Writer.AddSourceLocation(D->getLocEnd(), Record);
391 Record.push_back(D->param_size());
392 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
393 PEnd = D->param_end(); P != PEnd; ++P)
394 Writer.AddDeclRef(*P, Record);
395 Code = pch::DECL_OBJC_METHOD;
396}
397
Steve Naroff33feeb02009-04-20 20:09:33 +0000398void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
399 VisitNamedDecl(D);
400 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
401 // Abstract class (no need to define a stable pch::DECL code).
402}
403
404void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
405 VisitObjCContainerDecl(D);
406 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
407 Writer.AddDeclRef(D->getSuperClass(), Record);
Douglas Gregor291be392009-04-23 03:59:07 +0000408 Record.push_back(D->protocol_size());
409 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
410 PEnd = D->protocol_end();
411 P != PEnd; ++P)
412 Writer.AddDeclRef(*P, Record);
Steve Naroff33feeb02009-04-20 20:09:33 +0000413 Record.push_back(D->ivar_size());
414 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
415 IEnd = D->ivar_end(); I != IEnd; ++I)
416 Writer.AddDeclRef(*I, Record);
417 Record.push_back(D->isForwardDecl());
418 Record.push_back(D->isImplicitInterfaceDecl());
419 Writer.AddSourceLocation(D->getClassLoc(), Record);
420 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
421 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor291be392009-04-23 03:59:07 +0000422 // FIXME: add categories.
Steve Naroff30833f82009-04-21 15:12:33 +0000423 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff33feeb02009-04-20 20:09:33 +0000424}
425
426void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
427 VisitFieldDecl(D);
428 // FIXME: stable encoding for @public/@private/@protected/@package
429 Record.push_back(D->getAccessControl());
Steve Naroff30833f82009-04-21 15:12:33 +0000430 Code = pch::DECL_OBJC_IVAR;
431}
432
433void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
434 VisitObjCContainerDecl(D);
435 Record.push_back(D->isForwardDecl());
436 Writer.AddSourceLocation(D->getLocEnd(), Record);
437 Record.push_back(D->protocol_size());
438 for (ObjCProtocolDecl::protocol_iterator
439 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
440 Writer.AddDeclRef(*I, Record);
441 Code = pch::DECL_OBJC_PROTOCOL;
442}
443
444void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
445 VisitFieldDecl(D);
446 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
447}
448
449void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
450 VisitDecl(D);
451 Record.push_back(D->size());
452 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
453 Writer.AddDeclRef(*I, Record);
454 Code = pch::DECL_OBJC_CLASS;
455}
456
457void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
458 VisitDecl(D);
459 Record.push_back(D->protocol_size());
460 for (ObjCProtocolDecl::protocol_iterator
461 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
462 Writer.AddDeclRef(*I, Record);
463 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
464}
465
466void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
467 VisitObjCContainerDecl(D);
468 Writer.AddDeclRef(D->getClassInterface(), Record);
469 Record.push_back(D->protocol_size());
470 for (ObjCProtocolDecl::protocol_iterator
471 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
472 Writer.AddDeclRef(*I, Record);
473 Writer.AddDeclRef(D->getNextClassCategory(), Record);
474 Writer.AddSourceLocation(D->getLocEnd(), Record);
475 Code = pch::DECL_OBJC_CATEGORY;
476}
477
478void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
479 VisitNamedDecl(D);
480 Writer.AddDeclRef(D->getClassInterface(), Record);
481 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
482}
483
484void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
485 VisitNamedDecl(D);
Douglas Gregor70e5a142009-04-22 23:20:34 +0000486 Writer.AddTypeRef(D->getType(), Record);
487 // FIXME: stable encoding
488 Record.push_back((unsigned)D->getPropertyAttributes());
489 // FIXME: stable encoding
490 Record.push_back((unsigned)D->getPropertyImplementation());
491 Writer.AddDeclarationName(D->getGetterName(), Record);
492 Writer.AddDeclarationName(D->getSetterName(), Record);
493 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
494 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
495 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000496 Code = pch::DECL_OBJC_PROPERTY;
497}
498
499void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
500 VisitDecl(D);
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000501 Writer.AddDeclRef(D->getClassInterface(), Record);
502 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000503 // Abstract class (no need to define a stable pch::DECL code).
504}
505
506void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
507 VisitObjCImplDecl(D);
Douglas Gregor10b0e1f2009-04-23 02:53:57 +0000508 Writer.AddIdentifierRef(D->getIdentifier(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000509 Code = pch::DECL_OBJC_CATEGORY_IMPL;
510}
511
512void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
513 VisitObjCImplDecl(D);
Douglas Gregor8f36aba2009-04-23 03:23:08 +0000514 Writer.AddDeclRef(D->getSuperClass(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000515 Code = pch::DECL_OBJC_IMPLEMENTATION;
516}
517
518void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
519 VisitDecl(D);
Douglas Gregor8818c4f2009-04-23 03:43:53 +0000520 Writer.AddSourceLocation(D->getLocStart(), Record);
521 Writer.AddDeclRef(D->getPropertyDecl(), Record);
522 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000523 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff33feeb02009-04-20 20:09:33 +0000524}
525
Douglas Gregor8c700062009-04-13 21:20:57 +0000526void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
527 VisitValueDecl(D);
528 Record.push_back(D->isMutable());
Douglas Gregor0b748912009-04-14 21:18:50 +0000529 Record.push_back(D->getBitWidth()? 1 : 0);
530 if (D->getBitWidth())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000531 Writer.AddStmt(D->getBitWidth());
Douglas Gregor8c700062009-04-13 21:20:57 +0000532 Code = pch::DECL_FIELD;
533}
534
Douglas Gregor2cf26342009-04-09 22:27:44 +0000535void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
536 VisitValueDecl(D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000537 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor2cf26342009-04-09 22:27:44 +0000538 Record.push_back(D->isThreadSpecified());
539 Record.push_back(D->hasCXXDirectInitializer());
540 Record.push_back(D->isDeclaredInCondition());
541 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
542 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregor0b748912009-04-14 21:18:50 +0000543 Record.push_back(D->getInit()? 1 : 0);
544 if (D->getInit())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000545 Writer.AddStmt(D->getInit());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000546 Code = pch::DECL_VAR;
547}
548
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000549void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
550 VisitVarDecl(D);
551 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000552 // FIXME: emit default argument (C++)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000553 // FIXME: why isn't the "default argument" just stored as the initializer
554 // in VarDecl?
555 Code = pch::DECL_PARM_VAR;
556}
557
558void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
559 VisitParmVarDecl(D);
560 Writer.AddTypeRef(D->getOriginalType(), Record);
561 Code = pch::DECL_ORIGINAL_PARM_VAR;
562}
563
Douglas Gregor1028bc62009-04-13 22:49:25 +0000564void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
565 VisitDecl(D);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000566 Writer.AddStmt(D->getAsmString());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000567 Code = pch::DECL_FILE_SCOPE_ASM;
568}
569
570void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
571 VisitDecl(D);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000572 Writer.AddStmt(D->getBody());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000573 Record.push_back(D->param_size());
574 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
575 P != PEnd; ++P)
576 Writer.AddDeclRef(*P, Record);
577 Code = pch::DECL_BLOCK;
578}
579
Douglas Gregor2cf26342009-04-09 22:27:44 +0000580/// \brief Emit the DeclContext part of a declaration context decl.
581///
582/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
583/// block for this declaration context is stored. May be 0 to indicate
584/// that there are no declarations stored within this context.
585///
586/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
587/// block for this declaration context is stored. May be 0 to indicate
588/// that there are no declarations visible from this context. Note
589/// that this value will not be emitted for non-primary declaration
590/// contexts.
591void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
592 uint64_t VisibleOffset) {
593 Record.push_back(LexicalOffset);
Douglas Gregor0af2ca42009-04-22 19:09:20 +0000594 Record.push_back(VisibleOffset);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000595}
596
597//===----------------------------------------------------------------------===//
Douglas Gregor0b748912009-04-14 21:18:50 +0000598// Statement/expression serialization
599//===----------------------------------------------------------------------===//
600namespace {
601 class VISIBILITY_HIDDEN PCHStmtWriter
602 : public StmtVisitor<PCHStmtWriter, void> {
603
604 PCHWriter &Writer;
605 PCHWriter::RecordData &Record;
606
607 public:
608 pch::StmtCode Code;
609
610 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
611 : Writer(Writer), Record(Record) { }
612
Douglas Gregor025452f2009-04-17 00:04:06 +0000613 void VisitStmt(Stmt *S);
614 void VisitNullStmt(NullStmt *S);
615 void VisitCompoundStmt(CompoundStmt *S);
616 void VisitSwitchCase(SwitchCase *S);
617 void VisitCaseStmt(CaseStmt *S);
618 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000619 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000620 void VisitIfStmt(IfStmt *S);
621 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000622 void VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000623 void VisitDoStmt(DoStmt *S);
624 void VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000625 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000626 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000627 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000628 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000629 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000630 void VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000631 void VisitAsmStmt(AsmStmt *S);
Douglas Gregor0b748912009-04-14 21:18:50 +0000632 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000633 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000634 void VisitDeclRefExpr(DeclRefExpr *E);
635 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000636 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000637 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000638 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000639 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000640 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000641 void VisitUnaryOperator(UnaryOperator *E);
642 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000643 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000644 void VisitCallExpr(CallExpr *E);
645 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000646 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000647 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000648 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
649 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000650 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000651 void VisitExplicitCastExpr(ExplicitCastExpr *E);
652 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000653 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000654 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000655 void VisitInitListExpr(InitListExpr *E);
656 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
657 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000658 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000659 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000660 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000661 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
662 void VisitChooseExpr(ChooseExpr *E);
663 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000664 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000665 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000666 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000667
668 // Objective-C
Chris Lattner3a57a372009-04-22 06:29:42 +0000669 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000670 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000671 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
672 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000673 };
674}
675
Douglas Gregor025452f2009-04-17 00:04:06 +0000676void PCHStmtWriter::VisitStmt(Stmt *S) {
677}
678
679void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
680 VisitStmt(S);
681 Writer.AddSourceLocation(S->getSemiLoc(), Record);
682 Code = pch::STMT_NULL;
683}
684
685void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
686 VisitStmt(S);
687 Record.push_back(S->size());
688 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
689 CS != CSEnd; ++CS)
690 Writer.WriteSubStmt(*CS);
691 Writer.AddSourceLocation(S->getLBracLoc(), Record);
692 Writer.AddSourceLocation(S->getRBracLoc(), Record);
693 Code = pch::STMT_COMPOUND;
694}
695
696void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
697 VisitStmt(S);
698 Record.push_back(Writer.RecordSwitchCaseID(S));
699}
700
701void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
702 VisitSwitchCase(S);
703 Writer.WriteSubStmt(S->getLHS());
704 Writer.WriteSubStmt(S->getRHS());
705 Writer.WriteSubStmt(S->getSubStmt());
706 Writer.AddSourceLocation(S->getCaseLoc(), Record);
707 Code = pch::STMT_CASE;
708}
709
710void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
711 VisitSwitchCase(S);
712 Writer.WriteSubStmt(S->getSubStmt());
713 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
714 Code = pch::STMT_DEFAULT;
715}
716
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000717void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
718 VisitStmt(S);
719 Writer.AddIdentifierRef(S->getID(), Record);
720 Writer.WriteSubStmt(S->getSubStmt());
721 Writer.AddSourceLocation(S->getIdentLoc(), Record);
722 Record.push_back(Writer.GetLabelID(S));
723 Code = pch::STMT_LABEL;
724}
725
Douglas Gregor025452f2009-04-17 00:04:06 +0000726void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
727 VisitStmt(S);
728 Writer.WriteSubStmt(S->getCond());
729 Writer.WriteSubStmt(S->getThen());
730 Writer.WriteSubStmt(S->getElse());
731 Writer.AddSourceLocation(S->getIfLoc(), Record);
732 Code = pch::STMT_IF;
733}
734
735void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
736 VisitStmt(S);
737 Writer.WriteSubStmt(S->getCond());
738 Writer.WriteSubStmt(S->getBody());
739 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
740 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
741 SC = SC->getNextSwitchCase())
742 Record.push_back(Writer.getSwitchCaseID(SC));
743 Code = pch::STMT_SWITCH;
744}
745
Douglas Gregord921cf92009-04-17 00:16:09 +0000746void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
747 VisitStmt(S);
748 Writer.WriteSubStmt(S->getCond());
749 Writer.WriteSubStmt(S->getBody());
750 Writer.AddSourceLocation(S->getWhileLoc(), Record);
751 Code = pch::STMT_WHILE;
752}
753
Douglas Gregor67d82492009-04-17 00:29:51 +0000754void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
755 VisitStmt(S);
756 Writer.WriteSubStmt(S->getCond());
757 Writer.WriteSubStmt(S->getBody());
758 Writer.AddSourceLocation(S->getDoLoc(), Record);
759 Code = pch::STMT_DO;
760}
761
762void PCHStmtWriter::VisitForStmt(ForStmt *S) {
763 VisitStmt(S);
764 Writer.WriteSubStmt(S->getInit());
765 Writer.WriteSubStmt(S->getCond());
766 Writer.WriteSubStmt(S->getInc());
767 Writer.WriteSubStmt(S->getBody());
768 Writer.AddSourceLocation(S->getForLoc(), Record);
769 Code = pch::STMT_FOR;
770}
771
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000772void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
773 VisitStmt(S);
774 Record.push_back(Writer.GetLabelID(S->getLabel()));
775 Writer.AddSourceLocation(S->getGotoLoc(), Record);
776 Writer.AddSourceLocation(S->getLabelLoc(), Record);
777 Code = pch::STMT_GOTO;
778}
779
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000780void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
781 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000782 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000783 Writer.WriteSubStmt(S->getTarget());
784 Code = pch::STMT_INDIRECT_GOTO;
785}
786
Douglas Gregord921cf92009-04-17 00:16:09 +0000787void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
788 VisitStmt(S);
789 Writer.AddSourceLocation(S->getContinueLoc(), Record);
790 Code = pch::STMT_CONTINUE;
791}
792
Douglas Gregor025452f2009-04-17 00:04:06 +0000793void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
794 VisitStmt(S);
795 Writer.AddSourceLocation(S->getBreakLoc(), Record);
796 Code = pch::STMT_BREAK;
797}
798
Douglas Gregor0de9d882009-04-17 16:34:57 +0000799void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
800 VisitStmt(S);
801 Writer.WriteSubStmt(S->getRetValue());
802 Writer.AddSourceLocation(S->getReturnLoc(), Record);
803 Code = pch::STMT_RETURN;
804}
805
Douglas Gregor84f21702009-04-17 16:55:36 +0000806void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
807 VisitStmt(S);
808 Writer.AddSourceLocation(S->getStartLoc(), Record);
809 Writer.AddSourceLocation(S->getEndLoc(), Record);
810 DeclGroupRef DG = S->getDeclGroup();
811 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
812 Writer.AddDeclRef(*D, Record);
813 Code = pch::STMT_DECL;
814}
815
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000816void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
817 VisitStmt(S);
818 Record.push_back(S->getNumOutputs());
819 Record.push_back(S->getNumInputs());
820 Record.push_back(S->getNumClobbers());
821 Writer.AddSourceLocation(S->getAsmLoc(), Record);
822 Writer.AddSourceLocation(S->getRParenLoc(), Record);
823 Record.push_back(S->isVolatile());
824 Record.push_back(S->isSimple());
825 Writer.WriteSubStmt(S->getAsmString());
826
827 // Outputs
828 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
829 Writer.AddString(S->getOutputName(I), Record);
830 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
831 Writer.WriteSubStmt(S->getOutputExpr(I));
832 }
833
834 // Inputs
835 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
836 Writer.AddString(S->getInputName(I), Record);
837 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
838 Writer.WriteSubStmt(S->getInputExpr(I));
839 }
840
841 // Clobbers
842 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
843 Writer.WriteSubStmt(S->getClobber(I));
844
845 Code = pch::STMT_ASM;
846}
847
Douglas Gregor0b748912009-04-14 21:18:50 +0000848void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000849 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000850 Writer.AddTypeRef(E->getType(), Record);
851 Record.push_back(E->isTypeDependent());
852 Record.push_back(E->isValueDependent());
853}
854
Douglas Gregor17fc2232009-04-14 21:55:33 +0000855void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
856 VisitExpr(E);
857 Writer.AddSourceLocation(E->getLocation(), Record);
858 Record.push_back(E->getIdentType()); // FIXME: stable encoding
859 Code = pch::EXPR_PREDEFINED;
860}
861
Douglas Gregor0b748912009-04-14 21:18:50 +0000862void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
863 VisitExpr(E);
864 Writer.AddDeclRef(E->getDecl(), Record);
865 Writer.AddSourceLocation(E->getLocation(), Record);
866 Code = pch::EXPR_DECL_REF;
867}
868
869void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
870 VisitExpr(E);
871 Writer.AddSourceLocation(E->getLocation(), Record);
872 Writer.AddAPInt(E->getValue(), Record);
873 Code = pch::EXPR_INTEGER_LITERAL;
874}
875
Douglas Gregor17fc2232009-04-14 21:55:33 +0000876void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
877 VisitExpr(E);
878 Writer.AddAPFloat(E->getValue(), Record);
879 Record.push_back(E->isExact());
880 Writer.AddSourceLocation(E->getLocation(), Record);
881 Code = pch::EXPR_FLOATING_LITERAL;
882}
883
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000884void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
885 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000886 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000887 Code = pch::EXPR_IMAGINARY_LITERAL;
888}
889
Douglas Gregor673ecd62009-04-15 16:35:07 +0000890void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
891 VisitExpr(E);
892 Record.push_back(E->getByteLength());
893 Record.push_back(E->getNumConcatenated());
894 Record.push_back(E->isWide());
895 // FIXME: String data should be stored as a blob at the end of the
896 // StringLiteral. However, we can't do so now because we have no
897 // provision for coping with abbreviations when we're jumping around
898 // the PCH file during deserialization.
899 Record.insert(Record.end(),
900 E->getStrData(), E->getStrData() + E->getByteLength());
901 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
902 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
903 Code = pch::EXPR_STRING_LITERAL;
904}
905
Douglas Gregor0b748912009-04-14 21:18:50 +0000906void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
907 VisitExpr(E);
908 Record.push_back(E->getValue());
909 Writer.AddSourceLocation(E->getLoc(), Record);
910 Record.push_back(E->isWide());
911 Code = pch::EXPR_CHARACTER_LITERAL;
912}
913
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000914void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
915 VisitExpr(E);
916 Writer.AddSourceLocation(E->getLParen(), Record);
917 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000918 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000919 Code = pch::EXPR_PAREN;
920}
921
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000922void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
923 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000924 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000925 Record.push_back(E->getOpcode()); // FIXME: stable encoding
926 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
927 Code = pch::EXPR_UNARY_OPERATOR;
928}
929
930void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
931 VisitExpr(E);
932 Record.push_back(E->isSizeOf());
933 if (E->isArgumentType())
934 Writer.AddTypeRef(E->getArgumentType(), Record);
935 else {
936 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000937 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000938 }
939 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
940 Writer.AddSourceLocation(E->getRParenLoc(), Record);
941 Code = pch::EXPR_SIZEOF_ALIGN_OF;
942}
943
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000944void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
945 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000946 Writer.WriteSubStmt(E->getLHS());
947 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000948 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
949 Code = pch::EXPR_ARRAY_SUBSCRIPT;
950}
951
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000952void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
953 VisitExpr(E);
954 Record.push_back(E->getNumArgs());
955 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000956 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000957 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
958 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000959 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000960 Code = pch::EXPR_CALL;
961}
962
963void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
964 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000965 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000966 Writer.AddDeclRef(E->getMemberDecl(), Record);
967 Writer.AddSourceLocation(E->getMemberLoc(), Record);
968 Record.push_back(E->isArrow());
969 Code = pch::EXPR_MEMBER;
970}
971
Douglas Gregor087fd532009-04-14 23:32:43 +0000972void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
973 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000974 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000975}
976
Douglas Gregordb600c32009-04-15 00:25:59 +0000977void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
978 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000979 Writer.WriteSubStmt(E->getLHS());
980 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000981 Record.push_back(E->getOpcode()); // FIXME: stable encoding
982 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
983 Code = pch::EXPR_BINARY_OPERATOR;
984}
985
Douglas Gregorad90e962009-04-15 22:40:36 +0000986void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
987 VisitBinaryOperator(E);
988 Writer.AddTypeRef(E->getComputationLHSType(), Record);
989 Writer.AddTypeRef(E->getComputationResultType(), Record);
990 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
991}
992
993void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
994 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000995 Writer.WriteSubStmt(E->getCond());
996 Writer.WriteSubStmt(E->getLHS());
997 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000998 Code = pch::EXPR_CONDITIONAL_OPERATOR;
999}
1000
Douglas Gregor087fd532009-04-14 23:32:43 +00001001void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
1002 VisitCastExpr(E);
1003 Record.push_back(E->isLvalueCast());
1004 Code = pch::EXPR_IMPLICIT_CAST;
1005}
1006
Douglas Gregordb600c32009-04-15 00:25:59 +00001007void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1008 VisitCastExpr(E);
1009 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1010}
1011
1012void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1013 VisitExplicitCastExpr(E);
1014 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1015 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1016 Code = pch::EXPR_CSTYLE_CAST;
1017}
1018
Douglas Gregorba6d7e72009-04-16 02:33:48 +00001019void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1020 VisitExpr(E);
1021 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001022 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +00001023 Record.push_back(E->isFileScope());
1024 Code = pch::EXPR_COMPOUND_LITERAL;
1025}
1026
Douglas Gregord3c98a02009-04-15 23:02:49 +00001027void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1028 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001029 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +00001030 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1031 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1032 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1033}
1034
Douglas Gregord077d752009-04-16 00:55:48 +00001035void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1036 VisitExpr(E);
1037 Record.push_back(E->getNumInits());
1038 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001039 Writer.WriteSubStmt(E->getInit(I));
1040 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +00001041 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1042 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1043 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1044 Record.push_back(E->hadArrayRangeDesignator());
1045 Code = pch::EXPR_INIT_LIST;
1046}
1047
1048void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1049 VisitExpr(E);
1050 Record.push_back(E->getNumSubExprs());
1051 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001052 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +00001053 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1054 Record.push_back(E->usesGNUSyntax());
1055 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1056 DEnd = E->designators_end();
1057 D != DEnd; ++D) {
1058 if (D->isFieldDesignator()) {
1059 if (FieldDecl *Field = D->getField()) {
1060 Record.push_back(pch::DESIG_FIELD_DECL);
1061 Writer.AddDeclRef(Field, Record);
1062 } else {
1063 Record.push_back(pch::DESIG_FIELD_NAME);
1064 Writer.AddIdentifierRef(D->getFieldName(), Record);
1065 }
1066 Writer.AddSourceLocation(D->getDotLoc(), Record);
1067 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1068 } else if (D->isArrayDesignator()) {
1069 Record.push_back(pch::DESIG_ARRAY);
1070 Record.push_back(D->getFirstExprIndex());
1071 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1072 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1073 } else {
1074 assert(D->isArrayRangeDesignator() && "Unknown designator");
1075 Record.push_back(pch::DESIG_ARRAY_RANGE);
1076 Record.push_back(D->getFirstExprIndex());
1077 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1078 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1079 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1080 }
1081 }
1082 Code = pch::EXPR_DESIGNATED_INIT;
1083}
1084
1085void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1086 VisitExpr(E);
1087 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1088}
1089
Douglas Gregord3c98a02009-04-15 23:02:49 +00001090void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1091 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001092 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +00001093 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1094 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1095 Code = pch::EXPR_VA_ARG;
1096}
1097
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00001098void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1099 VisitExpr(E);
1100 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1101 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1102 Record.push_back(Writer.GetLabelID(E->getLabel()));
1103 Code = pch::EXPR_ADDR_LABEL;
1104}
1105
Douglas Gregor6a2dd552009-04-17 19:05:30 +00001106void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1107 VisitExpr(E);
1108 Writer.WriteSubStmt(E->getSubStmt());
1109 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1110 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1111 Code = pch::EXPR_STMT;
1112}
1113
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001114void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1115 VisitExpr(E);
1116 Writer.AddTypeRef(E->getArgType1(), Record);
1117 Writer.AddTypeRef(E->getArgType2(), Record);
1118 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1119 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1120 Code = pch::EXPR_TYPES_COMPATIBLE;
1121}
1122
1123void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1124 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001125 Writer.WriteSubStmt(E->getCond());
1126 Writer.WriteSubStmt(E->getLHS());
1127 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001128 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1129 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1130 Code = pch::EXPR_CHOOSE;
1131}
1132
1133void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1134 VisitExpr(E);
1135 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1136 Code = pch::EXPR_GNU_NULL;
1137}
1138
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001139void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1140 VisitExpr(E);
1141 Record.push_back(E->getNumSubExprs());
1142 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001143 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001144 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1145 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1146 Code = pch::EXPR_SHUFFLE_VECTOR;
1147}
1148
Douglas Gregor84af7c22009-04-17 19:21:43 +00001149void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1150 VisitExpr(E);
1151 Writer.AddDeclRef(E->getBlockDecl(), Record);
1152 Record.push_back(E->hasBlockDeclRefExprs());
1153 Code = pch::EXPR_BLOCK;
1154}
1155
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001156void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1157 VisitExpr(E);
1158 Writer.AddDeclRef(E->getDecl(), Record);
1159 Writer.AddSourceLocation(E->getLocation(), Record);
1160 Record.push_back(E->isByRef());
1161 Code = pch::EXPR_BLOCK_DECL_REF;
1162}
1163
Douglas Gregor0b748912009-04-14 21:18:50 +00001164//===----------------------------------------------------------------------===//
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001165// Objective-C Expressions and Statements.
1166//===----------------------------------------------------------------------===//
1167
Chris Lattner3a57a372009-04-22 06:29:42 +00001168void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1169 VisitExpr(E);
1170 Writer.WriteSubStmt(E->getString());
1171 Writer.AddSourceLocation(E->getAtLoc(), Record);
1172 Code = pch::EXPR_OBJC_STRING_LITERAL;
1173}
1174
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001175void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1176 VisitExpr(E);
1177 Writer.AddTypeRef(E->getEncodedType(), Record);
1178 Writer.AddSourceLocation(E->getAtLoc(), Record);
1179 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1180 Code = pch::EXPR_OBJC_ENCODE;
1181}
1182
Chris Lattner3a57a372009-04-22 06:29:42 +00001183void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1184 VisitExpr(E);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00001185 assert(0 && "Can't write a selector yet!");
Chris Lattner3a57a372009-04-22 06:29:42 +00001186 // FIXME! Write selectors.
1187 //Writer.WriteSubStmt(E->getSelector());
1188 Writer.AddSourceLocation(E->getAtLoc(), Record);
1189 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1190 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1191}
1192
1193void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1194 VisitExpr(E);
1195 Writer.AddDeclRef(E->getProtocol(), Record);
1196 Writer.AddSourceLocation(E->getAtLoc(), Record);
1197 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1198 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1199}
1200
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001201
1202//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00001203// PCHWriter Implementation
1204//===----------------------------------------------------------------------===//
1205
Douglas Gregor2bec0412009-04-10 21:16:55 +00001206/// \brief Write the target triple (e.g., i686-apple-darwin9).
1207void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1208 using namespace llvm;
1209 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1210 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001212 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001213
1214 RecordData Record;
1215 Record.push_back(pch::TARGET_TRIPLE);
1216 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001217 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +00001218}
1219
1220/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001221void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1222 RecordData Record;
1223 Record.push_back(LangOpts.Trigraphs);
1224 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1225 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1226 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1227 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1228 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1229 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1230 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1231 Record.push_back(LangOpts.C99); // C99 Support
1232 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1233 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1234 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1235 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1236 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1237
1238 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1239 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1240 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1241
1242 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1243 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1244 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1245 Record.push_back(LangOpts.LaxVectorConversions);
1246 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1247
1248 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1249 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1250 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1251
1252 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1253 // by locks.
1254 Record.push_back(LangOpts.Blocks); // block extension to C
1255 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1256 // they are unused.
1257 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1258 // (modulo the platform support).
1259
1260 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1261 // signed integer arithmetic overflows.
1262
1263 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1264 // may be ripped out at any time.
1265
1266 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1267 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1268 // defined.
1269 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1270 // opposed to __DYNAMIC__).
1271 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1272
1273 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1274 // used (instead of C99 semantics).
1275 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1276 Record.push_back(LangOpts.getGCMode());
1277 Record.push_back(LangOpts.getVisibilityMode());
1278 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001279 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001280}
1281
Douglas Gregor14f79002009-04-10 03:52:48 +00001282//===----------------------------------------------------------------------===//
1283// Source Manager Serialization
1284//===----------------------------------------------------------------------===//
1285
1286/// \brief Create an abbreviation for the SLocEntry that refers to a
1287/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001288static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001289 using namespace llvm;
1290 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1291 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1295 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +00001296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001297 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001298}
1299
1300/// \brief Create an abbreviation for the SLocEntry that refers to a
1301/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001302static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001303 using namespace llvm;
1304 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1305 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1308 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1309 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001311 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001312}
1313
1314/// \brief Create an abbreviation for the SLocEntry that refers to a
1315/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001316static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001317 using namespace llvm;
1318 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1319 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001321 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001322}
1323
1324/// \brief Create an abbreviation for the SLocEntry that refers to an
1325/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001326static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001327 using namespace llvm;
1328 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1329 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001335 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001336}
1337
1338/// \brief Writes the block containing the serialized form of the
1339/// source manager.
1340///
1341/// TODO: We should probably use an on-disk hash table (stored in a
1342/// blob), indexed based on the file name, so that we only create
1343/// entries for files that we actually need. In the common case (no
1344/// errors), we probably won't have to create file entries for any of
1345/// the files in the AST.
1346void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001347 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001348 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001349
1350 // Abbreviations for the various kinds of source-location entries.
1351 int SLocFileAbbrv = -1;
1352 int SLocBufferAbbrv = -1;
1353 int SLocBufferBlobAbbrv = -1;
1354 int SLocInstantiationAbbrv = -1;
1355
1356 // Write out the source location entry table. We skip the first
1357 // entry, which is always the same dummy entry.
1358 RecordData Record;
1359 for (SourceManager::sloc_entry_iterator
1360 SLoc = SourceMgr.sloc_entry_begin() + 1,
1361 SLocEnd = SourceMgr.sloc_entry_end();
1362 SLoc != SLocEnd; ++SLoc) {
1363 // Figure out which record code to use.
1364 unsigned Code;
1365 if (SLoc->isFile()) {
1366 if (SLoc->getFile().getContentCache()->Entry)
1367 Code = pch::SM_SLOC_FILE_ENTRY;
1368 else
1369 Code = pch::SM_SLOC_BUFFER_ENTRY;
1370 } else
1371 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1372 Record.push_back(Code);
1373
1374 Record.push_back(SLoc->getOffset());
1375 if (SLoc->isFile()) {
1376 const SrcMgr::FileInfo &File = SLoc->getFile();
1377 Record.push_back(File.getIncludeLoc().getRawEncoding());
1378 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001379 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001380
1381 const SrcMgr::ContentCache *Content = File.getContentCache();
1382 if (Content->Entry) {
1383 // The source location entry is a file. The blob associated
1384 // with this entry is the file name.
1385 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001386 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1387 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001388 Content->Entry->getName(),
1389 strlen(Content->Entry->getName()));
1390 } else {
1391 // The source location entry is a buffer. The blob associated
1392 // with this entry contains the contents of the buffer.
1393 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001394 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1395 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001396 }
1397
1398 // We add one to the size so that we capture the trailing NULL
1399 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1400 // the reader side).
1401 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1402 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001403 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001404 Record.clear();
1405 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001406 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001407 Buffer->getBufferStart(),
1408 Buffer->getBufferSize() + 1);
1409 }
1410 } else {
1411 // The source location entry is an instantiation.
1412 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1413 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1414 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1415 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1416
Douglas Gregorf60e9912009-04-15 18:05:10 +00001417 // Compute the token length for this macro expansion.
1418 unsigned NextOffset = SourceMgr.getNextOffset();
1419 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1420 if (++NextSLoc != SLocEnd)
1421 NextOffset = NextSLoc->getOffset();
1422 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1423
Douglas Gregor14f79002009-04-10 03:52:48 +00001424 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001425 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1426 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001427 }
1428
1429 Record.clear();
1430 }
1431
Douglas Gregorbd945002009-04-13 16:31:14 +00001432 // Write the line table.
1433 if (SourceMgr.hasLineTable()) {
1434 LineTableInfo &LineTable = SourceMgr.getLineTable();
1435
1436 // Emit the file names
1437 Record.push_back(LineTable.getNumFilenames());
1438 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1439 // Emit the file name
1440 const char *Filename = LineTable.getFilename(I);
1441 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1442 Record.push_back(FilenameLen);
1443 if (FilenameLen)
1444 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1445 }
1446
1447 // Emit the line entries
1448 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1449 L != LEnd; ++L) {
1450 // Emit the file ID
1451 Record.push_back(L->first);
1452
1453 // Emit the line entries
1454 Record.push_back(L->second.size());
1455 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1456 LEEnd = L->second.end();
1457 LE != LEEnd; ++LE) {
1458 Record.push_back(LE->FileOffset);
1459 Record.push_back(LE->LineNo);
1460 Record.push_back(LE->FilenameID);
1461 Record.push_back((unsigned)LE->FileKind);
1462 Record.push_back(LE->IncludeOffset);
1463 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001464 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001465 }
1466 }
1467
Douglas Gregorc9490c02009-04-16 22:23:12 +00001468 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001469}
1470
Chris Lattner0b1fb982009-04-10 17:15:23 +00001471/// \brief Writes the block containing the serialized form of the
1472/// preprocessor.
1473///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001474void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001475 // Enter the preprocessor block.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001476 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001477
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001478 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1479 // FIXME: use diagnostics subsystem for localization etc.
1480 if (PP.SawDateOrTime())
1481 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001482
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001483 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001484
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001485 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1486 if (PP.getCounterValue() != 0) {
1487 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001488 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001489 Record.clear();
1490 }
1491
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001492 // Loop over all the macro definitions that are live at the end of the file,
1493 // emitting each to the PP section.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001494 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1495 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001496 // FIXME: This emits macros in hash table order, we should do it in a stable
1497 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001498 MacroInfo *MI = I->second;
1499
1500 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1501 // been redefined by the header (in which case they are not isBuiltinMacro).
1502 if (MI->isBuiltinMacro())
1503 continue;
1504
Douglas Gregor37e26842009-04-21 23:56:24 +00001505 // FIXME: Remove this identifier reference?
Chris Lattner7356a312009-04-11 21:15:38 +00001506 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001507 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001508 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1509 Record.push_back(MI->isUsed());
1510
1511 unsigned Code;
1512 if (MI->isObjectLike()) {
1513 Code = pch::PP_MACRO_OBJECT_LIKE;
1514 } else {
1515 Code = pch::PP_MACRO_FUNCTION_LIKE;
1516
1517 Record.push_back(MI->isC99Varargs());
1518 Record.push_back(MI->isGNUVarargs());
1519 Record.push_back(MI->getNumArgs());
1520 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1521 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001522 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001523 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001524 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001525 Record.clear();
1526
Chris Lattnerdf961c22009-04-10 18:08:30 +00001527 // Emit the tokens array.
1528 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1529 // Note that we know that the preprocessor does not have any annotation
1530 // tokens in it because they are created by the parser, and thus can't be
1531 // in a macro definition.
1532 const Token &Tok = MI->getReplacementToken(TokNo);
1533
1534 Record.push_back(Tok.getLocation().getRawEncoding());
1535 Record.push_back(Tok.getLength());
1536
Chris Lattnerdf961c22009-04-10 18:08:30 +00001537 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1538 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001539 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001540
1541 // FIXME: Should translate token kind to a stable encoding.
1542 Record.push_back(Tok.getKind());
1543 // FIXME: Should translate token flags to a stable encoding.
1544 Record.push_back(Tok.getFlags());
1545
Douglas Gregorc9490c02009-04-16 22:23:12 +00001546 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001547 Record.clear();
1548 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001549 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001550 }
1551
Douglas Gregorc9490c02009-04-16 22:23:12 +00001552 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001553}
1554
1555
Douglas Gregor2cf26342009-04-09 22:27:44 +00001556/// \brief Write the representation of a type to the PCH stream.
1557void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001558 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001559 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001560 ID = NextTypeID++;
1561
1562 // Record the offset for this type.
1563 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001564 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001565 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1566 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001567 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001568 }
1569
1570 RecordData Record;
1571
1572 // Emit the type's representation.
1573 PCHTypeWriter W(*this, Record);
1574 switch (T->getTypeClass()) {
1575 // For all of the concrete, non-dependent types, call the
1576 // appropriate visitor function.
1577#define TYPE(Class, Base) \
1578 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1579#define ABSTRACT_TYPE(Class, Base)
1580#define DEPENDENT_TYPE(Class, Base)
1581#include "clang/AST/TypeNodes.def"
1582
1583 // For all of the dependent type nodes (which only occur in C++
1584 // templates), produce an error.
1585#define TYPE(Class, Base)
1586#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1587#include "clang/AST/TypeNodes.def"
1588 assert(false && "Cannot serialize dependent type nodes");
1589 break;
1590 }
1591
1592 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001593 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001594
1595 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001596 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001597}
1598
1599/// \brief Write a block containing all of the types.
1600void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001601 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001602 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001603
1604 // Emit all of the types in the ASTContext
1605 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1606 TEnd = Context.getTypes().end();
1607 T != TEnd; ++T) {
1608 // Builtin types are never serialized.
1609 if (isa<BuiltinType>(*T))
1610 continue;
1611
1612 WriteType(*T);
1613 }
1614
1615 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001616 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001617}
1618
1619/// \brief Write the block containing all of the declaration IDs
1620/// lexically declared within the given DeclContext.
1621///
1622/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1623/// bistream, or 0 if no block was written.
1624uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1625 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001626 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001627 return 0;
1628
Douglas Gregorc9490c02009-04-16 22:23:12 +00001629 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001630 RecordData Record;
1631 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1632 DEnd = DC->decls_end(Context);
1633 D != DEnd; ++D)
1634 AddDeclRef(*D, Record);
1635
Douglas Gregor25123082009-04-22 22:34:57 +00001636 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001637 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001638 return Offset;
1639}
1640
1641/// \brief Write the block containing all of the declaration IDs
1642/// visible from the given DeclContext.
1643///
1644/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1645/// bistream, or 0 if no block was written.
1646uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1647 DeclContext *DC) {
1648 if (DC->getPrimaryContext() != DC)
1649 return 0;
1650
Douglas Gregoraff22df2009-04-21 22:32:33 +00001651 // Since there is no name lookup into functions or methods, and we
1652 // perform name lookup for the translation unit via the
1653 // IdentifierInfo chains, don't bother to build a
1654 // visible-declarations table for these entities.
1655 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001656 return 0;
1657
Douglas Gregor2cf26342009-04-09 22:27:44 +00001658 // Force the DeclContext to build a its name-lookup table.
1659 DC->lookup(Context, DeclarationName());
1660
1661 // Serialize the contents of the mapping used for lookup. Note that,
1662 // although we have two very different code paths, the serialized
1663 // representation is the same for both cases: a declaration name,
1664 // followed by a size, followed by references to the visible
1665 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001666 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001667 RecordData Record;
1668 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001669 if (!Map)
1670 return 0;
1671
Douglas Gregor2cf26342009-04-09 22:27:44 +00001672 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1673 D != DEnd; ++D) {
1674 AddDeclarationName(D->first, Record);
1675 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1676 Record.push_back(Result.second - Result.first);
1677 for(; Result.first != Result.second; ++Result.first)
1678 AddDeclRef(*Result.first, Record);
1679 }
1680
1681 if (Record.size() == 0)
1682 return 0;
1683
Douglas Gregorc9490c02009-04-16 22:23:12 +00001684 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001685 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001686 return Offset;
1687}
1688
1689/// \brief Write a block containing all of the declarations.
1690void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001691 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001692 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001693
1694 // Emit all of the declarations.
1695 RecordData Record;
Douglas Gregor72971342009-04-18 00:02:19 +00001696 PCHDeclWriter W(*this, Context, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001697 while (!DeclsToEmit.empty()) {
1698 // Pull the next declaration off the queue
1699 Decl *D = DeclsToEmit.front();
1700 DeclsToEmit.pop();
1701
1702 // If this declaration is also a DeclContext, write blocks for the
1703 // declarations that lexically stored inside its context and those
1704 // declarations that are visible from its context. These blocks
1705 // are written before the declaration itself so that we can put
1706 // their offsets into the record for the declaration.
1707 uint64_t LexicalOffset = 0;
1708 uint64_t VisibleOffset = 0;
1709 DeclContext *DC = dyn_cast<DeclContext>(D);
1710 if (DC) {
1711 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1712 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1713 }
1714
1715 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001716 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001717 if (ID == 0)
1718 ID = DeclIDs.size();
1719
1720 unsigned Index = ID - 1;
1721
1722 // Record the offset for this declaration
1723 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001724 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001725 else if (DeclOffsets.size() < Index) {
1726 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001727 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001728 }
1729
1730 // Build and emit a record for this declaration
1731 Record.clear();
1732 W.Code = (pch::DeclCode)0;
1733 W.Visit(D);
1734 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor70e5a142009-04-22 23:20:34 +00001735
1736 if (!W.Code) {
1737 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1738 D->getDeclKindName());
1739 assert(false && "Unhandled declaration kind while generating PCH");
1740 exit(-1);
1741 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001742 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001743
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001744 // If the declaration had any attributes, write them now.
1745 if (D->hasAttrs())
1746 WriteAttributeRecord(D->getAttrs());
1747
Douglas Gregor0b748912009-04-14 21:18:50 +00001748 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001749 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001750
Douglas Gregorfdd01722009-04-14 00:24:19 +00001751 // Note external declarations so that we can add them to a record
1752 // in the PCH file later.
1753 if (isa<FileScopeAsmDecl>(D))
1754 ExternalDefinitions.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001755 }
1756
1757 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001758 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001759}
1760
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001761namespace {
1762class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1763 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001764 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001765
1766public:
1767 typedef const IdentifierInfo* key_type;
1768 typedef key_type key_type_ref;
1769
1770 typedef pch::IdentID data_type;
1771 typedef data_type data_type_ref;
1772
Douglas Gregor37e26842009-04-21 23:56:24 +00001773 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1774 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001775
1776 static unsigned ComputeHash(const IdentifierInfo* II) {
1777 return clang::BernsteinHash(II->getName());
1778 }
1779
Douglas Gregor37e26842009-04-21 23:56:24 +00001780 std::pair<unsigned,unsigned>
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001781 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1782 pch::IdentID ID) {
1783 unsigned KeyLen = strlen(II->getName()) + 1;
1784 clang::io::Emit16(Out, KeyLen);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001785 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1786 // 4 bytes for the persistent ID
Douglas Gregor37e26842009-04-21 23:56:24 +00001787 if (II->hasMacroDefinition() &&
1788 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1789 DataLen += 8;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001790 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1791 DEnd = IdentifierResolver::end();
1792 D != DEnd; ++D)
1793 DataLen += sizeof(pch::DeclID);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001794 clang::io::Emit16(Out, DataLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001795 return std::make_pair(KeyLen, DataLen);
1796 }
1797
1798 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1799 unsigned KeyLen) {
1800 // Record the location of the key data. This is used when generating
1801 // the mapping from persistent IDs to strings.
1802 Writer.SetIdentifierOffset(II, Out.tell());
1803 Out.write(II->getName(), KeyLen);
1804 }
1805
1806 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1807 pch::IdentID ID, unsigned) {
1808 uint32_t Bits = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001809 bool hasMacroDefinition =
1810 II->hasMacroDefinition() &&
1811 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001812 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregor2deaea32009-04-22 18:49:13 +00001813 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
1814 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001815 Bits = (Bits << 1) | II->isExtensionToken();
1816 Bits = (Bits << 1) | II->isPoisoned();
1817 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1818 clang::io::Emit32(Out, Bits);
1819 clang::io::Emit32(Out, ID);
1820
Douglas Gregor37e26842009-04-21 23:56:24 +00001821 if (hasMacroDefinition)
1822 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1823
Douglas Gregor668c1a42009-04-21 22:25:48 +00001824 // Emit the declaration IDs in reverse order, because the
1825 // IdentifierResolver provides the declarations as they would be
1826 // visible (e.g., the function "stat" would come before the struct
1827 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1828 // adds declarations to the end of the list (so we need to see the
1829 // struct "status" before the function "status").
1830 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1831 IdentifierResolver::end());
1832 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1833 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001834 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001835 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001836 }
1837};
1838} // end anonymous namespace
1839
Douglas Gregorafaf3082009-04-11 00:14:32 +00001840/// \brief Write the identifier table into the PCH file.
1841///
1842/// The identifier table consists of a blob containing string data
1843/// (the actual identifiers themselves) and a separate "offsets" index
1844/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001845void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001846 using namespace llvm;
1847
1848 // Create and write out the blob that contains the identifier
1849 // strings.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001850 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001851 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001852 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1853
1854 // Create the on-disk hash table representation.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001855 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1856 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1857 ID != IDEnd; ++ID) {
1858 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001859 Generator.insert(ID->first, ID->second);
1860 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001861
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001862 // Create the on-disk hash table in a buffer.
1863 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001864 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001865 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001866 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001867 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001868 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001869 }
1870
1871 // Create a blob abbreviation
1872 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1873 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001876 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001877
1878 // Write the identifier table
1879 RecordData Record;
1880 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001881 Record.push_back(BucketOffset);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001882 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1883 &IdentifierTable.front(),
1884 IdentifierTable.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001885 }
1886
1887 // Write the offsets table for identifier IDs.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001888 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001889}
1890
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001891/// \brief Write a record containing the given attributes.
1892void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1893 RecordData Record;
1894 for (; Attr; Attr = Attr->getNext()) {
1895 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1896 Record.push_back(Attr->isInherited());
1897 switch (Attr->getKind()) {
1898 case Attr::Alias:
1899 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1900 break;
1901
1902 case Attr::Aligned:
1903 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1904 break;
1905
1906 case Attr::AlwaysInline:
1907 break;
1908
1909 case Attr::AnalyzerNoReturn:
1910 break;
1911
1912 case Attr::Annotate:
1913 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1914 break;
1915
1916 case Attr::AsmLabel:
1917 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1918 break;
1919
1920 case Attr::Blocks:
1921 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1922 break;
1923
1924 case Attr::Cleanup:
1925 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1926 break;
1927
1928 case Attr::Const:
1929 break;
1930
1931 case Attr::Constructor:
1932 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1933 break;
1934
1935 case Attr::DLLExport:
1936 case Attr::DLLImport:
1937 case Attr::Deprecated:
1938 break;
1939
1940 case Attr::Destructor:
1941 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1942 break;
1943
1944 case Attr::FastCall:
1945 break;
1946
1947 case Attr::Format: {
1948 const FormatAttr *Format = cast<FormatAttr>(Attr);
1949 AddString(Format->getType(), Record);
1950 Record.push_back(Format->getFormatIdx());
1951 Record.push_back(Format->getFirstArg());
1952 break;
1953 }
1954
Chris Lattnercf2a7212009-04-20 19:12:28 +00001955 case Attr::GNUInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001956 case Attr::IBOutletKind:
1957 case Attr::NoReturn:
1958 case Attr::NoThrow:
1959 case Attr::Nodebug:
1960 case Attr::Noinline:
1961 break;
1962
1963 case Attr::NonNull: {
1964 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1965 Record.push_back(NonNull->size());
1966 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1967 break;
1968 }
1969
1970 case Attr::ObjCException:
1971 case Attr::ObjCNSObject:
1972 case Attr::Overloadable:
1973 break;
1974
1975 case Attr::Packed:
1976 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1977 break;
1978
1979 case Attr::Pure:
1980 break;
1981
1982 case Attr::Regparm:
1983 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1984 break;
1985
1986 case Attr::Section:
1987 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1988 break;
1989
1990 case Attr::StdCall:
1991 case Attr::TransparentUnion:
1992 case Attr::Unavailable:
1993 case Attr::Unused:
1994 case Attr::Used:
1995 break;
1996
1997 case Attr::Visibility:
1998 // FIXME: stable encoding
1999 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2000 break;
2001
2002 case Attr::WarnUnusedResult:
2003 case Attr::Weak:
2004 case Attr::WeakImport:
2005 break;
2006 }
2007 }
2008
Douglas Gregorc9490c02009-04-16 22:23:12 +00002009 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002010}
2011
2012void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2013 Record.push_back(Str.size());
2014 Record.insert(Record.end(), Str.begin(), Str.end());
2015}
2016
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002017/// \brief Note that the identifier II occurs at the given offset
2018/// within the identifier table.
2019void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
2020 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
2021}
2022
Douglas Gregorc9490c02009-04-16 22:23:12 +00002023PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor37e26842009-04-21 23:56:24 +00002024 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00002025 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2026 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002027
Douglas Gregore7785042009-04-20 15:53:59 +00002028void PCHWriter::WritePCH(Sema &SemaRef) {
2029 ASTContext &Context = SemaRef.Context;
2030 Preprocessor &PP = SemaRef.PP;
2031
Douglas Gregor2cf26342009-04-09 22:27:44 +00002032 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002033 Stream.Emit((unsigned)'C', 8);
2034 Stream.Emit((unsigned)'P', 8);
2035 Stream.Emit((unsigned)'C', 8);
2036 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002037
2038 // The translation unit is the first declaration we'll emit.
2039 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2040 DeclsToEmit.push(Context.getTranslationUnitDecl());
2041
Douglas Gregor2deaea32009-04-22 18:49:13 +00002042 // Make sure that we emit IdentifierInfos (and any attached
2043 // declarations) for builtins.
2044 {
2045 IdentifierTable &Table = PP.getIdentifierTable();
2046 llvm::SmallVector<const char *, 32> BuiltinNames;
2047 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2048 Context.getLangOptions().NoBuiltin);
2049 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2050 getIdentifierRef(&Table.get(BuiltinNames[I]));
2051 }
2052
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002053 // Build a record containing all of the tentative definitions in
2054 // this header file. Generally, this record will be empty.
2055 RecordData TentativeDefinitions;
2056 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2057 TD = SemaRef.TentativeDefinitions.begin(),
2058 TDEnd = SemaRef.TentativeDefinitions.end();
2059 TD != TDEnd; ++TD)
2060 AddDeclRef(TD->second, TentativeDefinitions);
2061
Douglas Gregor14c22f22009-04-22 22:18:58 +00002062 // Build a record containing all of the locally-scoped external
2063 // declarations in this header file. Generally, this record will be
2064 // empty.
2065 RecordData LocallyScopedExternalDecls;
2066 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2067 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2068 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2069 TD != TDEnd; ++TD)
2070 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2071
Douglas Gregor2cf26342009-04-09 22:27:44 +00002072 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002073 RecordData Record;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002074 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00002075 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002076 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00002077 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00002078 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002079 WriteTypesBlock(Context);
2080 WriteDeclsBlock(Context);
Douglas Gregor37e26842009-04-21 23:56:24 +00002081 WriteIdentifierTable(PP);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002082 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
2083 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorad1de002009-04-18 05:55:16 +00002084
2085 // Write the record of special types.
2086 Record.clear();
2087 AddTypeRef(Context.getBuiltinVaListType(), Record);
2088 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2089
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002090 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002091 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002092 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002093
2094 // Write the record containing tentative definitions.
2095 if (!TentativeDefinitions.empty())
2096 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002097
2098 // Write the record containing locally-scoped external definitions.
2099 if (!LocallyScopedExternalDecls.empty())
2100 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2101 LocallyScopedExternalDecls);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002102
2103 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002104 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002105 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002106 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002107 Record.push_back(NumLexicalDeclContexts);
2108 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002109 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002110 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002111}
2112
2113void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2114 Record.push_back(Loc.getRawEncoding());
2115}
2116
2117void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2118 Record.push_back(Value.getBitWidth());
2119 unsigned N = Value.getNumWords();
2120 const uint64_t* Words = Value.getRawData();
2121 for (unsigned I = 0; I != N; ++I)
2122 Record.push_back(Words[I]);
2123}
2124
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002125void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2126 Record.push_back(Value.isUnsigned());
2127 AddAPInt(Value, Record);
2128}
2129
Douglas Gregor17fc2232009-04-14 21:55:33 +00002130void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2131 AddAPInt(Value.bitcastToAPInt(), Record);
2132}
2133
Douglas Gregor2cf26342009-04-09 22:27:44 +00002134void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002135 Record.push_back(getIdentifierRef(II));
2136}
2137
2138pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2139 if (II == 0)
2140 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002141
2142 pch::IdentID &ID = IdentifierIDs[II];
2143 if (ID == 0)
2144 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002145 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002146}
2147
2148void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2149 if (T.isNull()) {
2150 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2151 return;
2152 }
2153
2154 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002155 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002156 switch (BT->getKind()) {
2157 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2158 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2159 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2160 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2161 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2162 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2163 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2164 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2165 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2166 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2167 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2168 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2169 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2170 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2171 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2172 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2173 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2174 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2175 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2176 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2177 }
2178
2179 Record.push_back((ID << 3) | T.getCVRQualifiers());
2180 return;
2181 }
2182
Douglas Gregor8038d512009-04-10 17:25:41 +00002183 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002184 if (ID == 0) // we haven't seen this type before
2185 ID = NextTypeID++;
2186
2187 // Encode the type qualifiers in the type reference.
2188 Record.push_back((ID << 3) | T.getCVRQualifiers());
2189}
2190
2191void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2192 if (D == 0) {
2193 Record.push_back(0);
2194 return;
2195 }
2196
Douglas Gregor8038d512009-04-10 17:25:41 +00002197 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002198 if (ID == 0) {
2199 // We haven't seen this declaration before. Give it a new ID and
2200 // enqueue it in the list of declarations to emit.
2201 ID = DeclIDs.size();
2202 DeclsToEmit.push(const_cast<Decl *>(D));
2203 }
2204
2205 Record.push_back(ID);
2206}
2207
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002208pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2209 if (D == 0)
2210 return 0;
2211
2212 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2213 return DeclIDs[D];
2214}
2215
Douglas Gregor2cf26342009-04-09 22:27:44 +00002216void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2217 Record.push_back(Name.getNameKind());
2218 switch (Name.getNameKind()) {
2219 case DeclarationName::Identifier:
2220 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2221 break;
2222
2223 case DeclarationName::ObjCZeroArgSelector:
2224 case DeclarationName::ObjCOneArgSelector:
2225 case DeclarationName::ObjCMultiArgSelector:
2226 assert(false && "Serialization of Objective-C selectors unavailable");
2227 break;
2228
2229 case DeclarationName::CXXConstructorName:
2230 case DeclarationName::CXXDestructorName:
2231 case DeclarationName::CXXConversionFunctionName:
2232 AddTypeRef(Name.getCXXNameType(), Record);
2233 break;
2234
2235 case DeclarationName::CXXOperatorName:
2236 Record.push_back(Name.getCXXOverloadedOperator());
2237 break;
2238
2239 case DeclarationName::CXXUsingDirective:
2240 // No extra data to emit
2241 break;
2242 }
2243}
Douglas Gregor0b748912009-04-14 21:18:50 +00002244
Douglas Gregorc9490c02009-04-16 22:23:12 +00002245/// \brief Write the given substatement or subexpression to the
2246/// bitstream.
2247void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00002248 RecordData Record;
2249 PCHStmtWriter Writer(*this, Record);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002250 ++NumStatements;
Douglas Gregor087fd532009-04-14 23:32:43 +00002251
Douglas Gregorc9490c02009-04-16 22:23:12 +00002252 if (!S) {
2253 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002254 return;
2255 }
2256
Douglas Gregorc9490c02009-04-16 22:23:12 +00002257 Writer.Code = pch::STMT_NULL_PTR;
2258 Writer.Visit(S);
2259 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00002260 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002261 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002262}
2263
Douglas Gregorc9490c02009-04-16 22:23:12 +00002264/// \brief Flush all of the statements that have been added to the
2265/// queue via AddStmt().
2266void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00002267 RecordData Record;
2268 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002269
Douglas Gregorc9490c02009-04-16 22:23:12 +00002270 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor3e1af842009-04-17 22:13:46 +00002271 ++NumStatements;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002272 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00002273
Douglas Gregorc9490c02009-04-16 22:23:12 +00002274 if (!S) {
2275 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002276 continue;
2277 }
2278
Douglas Gregorc9490c02009-04-16 22:23:12 +00002279 Writer.Code = pch::STMT_NULL_PTR;
2280 Writer.Visit(S);
2281 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00002282 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002283 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002284
Douglas Gregorc9490c02009-04-16 22:23:12 +00002285 assert(N == StmtsToEmit.size() &&
2286 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00002287
2288 // Note that we are at the end of a full expression. Any
2289 // expression records that follow this one are part of a different
2290 // expression.
2291 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00002292 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002293 }
Douglas Gregor087fd532009-04-14 23:32:43 +00002294
Douglas Gregorc9490c02009-04-16 22:23:12 +00002295 StmtsToEmit.clear();
Douglas Gregor0de9d882009-04-17 16:34:57 +00002296 SwitchCaseIDs.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00002297}
Douglas Gregor025452f2009-04-17 00:04:06 +00002298
2299unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2300 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2301 "SwitchCase recorded twice");
2302 unsigned NextID = SwitchCaseIDs.size();
2303 SwitchCaseIDs[S] = NextID;
2304 return NextID;
2305}
2306
2307unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2308 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2309 "SwitchCase hasn't been seen yet");
2310 return SwitchCaseIDs[S];
2311}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002312
2313/// \brief Retrieve the ID for the given label statement, which may
2314/// or may not have been emitted yet.
2315unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2316 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2317 if (Pos != LabelIDs.end())
2318 return Pos->second;
2319
2320 unsigned NextID = LabelIDs.size();
2321 LabelIDs[S] = NextID;
2322 return NextID;
2323}