blob: afe77aebf8f14160db5321cd4539e17e2ec4fe6b [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 Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
19#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
26#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000027#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000029#include "llvm/ADT/APFloat.h"
30#include "llvm/ADT/APInt.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000031#include "llvm/Bitcode/BitstreamWriter.h"
32#include "llvm/Support/Compiler.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "llvm/Support/MemoryBuffer.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000034#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000035using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Type serialization
39//===----------------------------------------------------------------------===//
40namespace {
41 class VISIBILITY_HIDDEN PCHTypeWriter {
42 PCHWriter &Writer;
43 PCHWriter::RecordData &Record;
44
45 public:
46 /// \brief Type code that corresponds to the record generated.
47 pch::TypeCode Code;
48
49 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
50 : Writer(Writer), Record(Record) { }
51
52 void VisitArrayType(const ArrayType *T);
53 void VisitFunctionType(const FunctionType *T);
54 void VisitTagType(const TagType *T);
55
56#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
57#define ABSTRACT_TYPE(Class, Base)
58#define DEPENDENT_TYPE(Class, Base)
59#include "clang/AST/TypeNodes.def"
60 };
61}
62
63void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
64 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
65 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
66 Record.push_back(T->getAddressSpace());
67 Code = pch::TYPE_EXT_QUAL;
68}
69
70void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
71 assert(false && "Built-in types are never serialized");
72}
73
74void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
75 Record.push_back(T->getWidth());
76 Record.push_back(T->isSigned());
77 Code = pch::TYPE_FIXED_WIDTH_INT;
78}
79
80void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
81 Writer.AddTypeRef(T->getElementType(), Record);
82 Code = pch::TYPE_COMPLEX;
83}
84
85void PCHTypeWriter::VisitPointerType(const PointerType *T) {
86 Writer.AddTypeRef(T->getPointeeType(), Record);
87 Code = pch::TYPE_POINTER;
88}
89
90void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
91 Writer.AddTypeRef(T->getPointeeType(), Record);
92 Code = pch::TYPE_BLOCK_POINTER;
93}
94
95void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
96 Writer.AddTypeRef(T->getPointeeType(), Record);
97 Code = pch::TYPE_LVALUE_REFERENCE;
98}
99
100void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
101 Writer.AddTypeRef(T->getPointeeType(), Record);
102 Code = pch::TYPE_RVALUE_REFERENCE;
103}
104
105void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
106 Writer.AddTypeRef(T->getPointeeType(), Record);
107 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
108 Code = pch::TYPE_MEMBER_POINTER;
109}
110
111void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
112 Writer.AddTypeRef(T->getElementType(), Record);
113 Record.push_back(T->getSizeModifier()); // FIXME: stable values
114 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
115}
116
117void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
118 VisitArrayType(T);
119 Writer.AddAPInt(T->getSize(), Record);
120 Code = pch::TYPE_CONSTANT_ARRAY;
121}
122
123void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
124 VisitArrayType(T);
125 Code = pch::TYPE_INCOMPLETE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
129 VisitArrayType(T);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000130 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131 Code = pch::TYPE_VARIABLE_ARRAY;
132}
133
134void PCHTypeWriter::VisitVectorType(const VectorType *T) {
135 Writer.AddTypeRef(T->getElementType(), Record);
136 Record.push_back(T->getNumElements());
137 Code = pch::TYPE_VECTOR;
138}
139
140void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
141 VisitVectorType(T);
142 Code = pch::TYPE_EXT_VECTOR;
143}
144
145void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
146 Writer.AddTypeRef(T->getResultType(), Record);
147}
148
149void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
150 VisitFunctionType(T);
151 Code = pch::TYPE_FUNCTION_NO_PROTO;
152}
153
154void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
155 VisitFunctionType(T);
156 Record.push_back(T->getNumArgs());
157 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
158 Writer.AddTypeRef(T->getArgType(I), Record);
159 Record.push_back(T->isVariadic());
160 Record.push_back(T->getTypeQuals());
161 Code = pch::TYPE_FUNCTION_PROTO;
162}
163
164void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
165 Writer.AddDeclRef(T->getDecl(), Record);
166 Code = pch::TYPE_TYPEDEF;
167}
168
169void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000170 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000171 Code = pch::TYPE_TYPEOF_EXPR;
172}
173
174void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
175 Writer.AddTypeRef(T->getUnderlyingType(), Record);
176 Code = pch::TYPE_TYPEOF;
177}
178
179void PCHTypeWriter::VisitTagType(const TagType *T) {
180 Writer.AddDeclRef(T->getDecl(), Record);
181 assert(!T->isBeingDefined() &&
182 "Cannot serialize in the middle of a type definition");
183}
184
185void PCHTypeWriter::VisitRecordType(const RecordType *T) {
186 VisitTagType(T);
187 Code = pch::TYPE_RECORD;
188}
189
190void PCHTypeWriter::VisitEnumType(const EnumType *T) {
191 VisitTagType(T);
192 Code = pch::TYPE_ENUM;
193}
194
195void
196PCHTypeWriter::VisitTemplateSpecializationType(
197 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000198 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199 assert(false && "Cannot serialize template specialization types");
200}
201
202void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000203 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000204 assert(false && "Cannot serialize qualified name types");
205}
206
207void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
208 Writer.AddDeclRef(T->getDecl(), Record);
209 Code = pch::TYPE_OBJC_INTERFACE;
210}
211
212void
213PCHTypeWriter::VisitObjCQualifiedInterfaceType(
214 const ObjCQualifiedInterfaceType *T) {
215 VisitObjCInterfaceType(T);
216 Record.push_back(T->getNumProtocols());
217 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
218 Writer.AddDeclRef(T->getProtocol(I), Record);
219 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
220}
221
222void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
223 Record.push_back(T->getNumProtocols());
224 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
225 Writer.AddDeclRef(T->getProtocols(I), Record);
226 Code = pch::TYPE_OBJC_QUALIFIED_ID;
227}
228
229void
230PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) {
231 Record.push_back(T->getNumProtocols());
232 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
233 Writer.AddDeclRef(T->getProtocols(I), Record);
234 Code = pch::TYPE_OBJC_QUALIFIED_CLASS;
235}
236
237//===----------------------------------------------------------------------===//
238// Declaration serialization
239//===----------------------------------------------------------------------===//
240namespace {
241 class VISIBILITY_HIDDEN PCHDeclWriter
242 : public DeclVisitor<PCHDeclWriter, void> {
243
244 PCHWriter &Writer;
Douglas Gregor72971342009-04-18 00:02:19 +0000245 ASTContext &Context;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000246 PCHWriter::RecordData &Record;
247
248 public:
249 pch::DeclCode Code;
250
Douglas Gregor72971342009-04-18 00:02:19 +0000251 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
252 PCHWriter::RecordData &Record)
253 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000254
255 void VisitDecl(Decl *D);
256 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
257 void VisitNamedDecl(NamedDecl *D);
258 void VisitTypeDecl(TypeDecl *D);
259 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000260 void VisitTagDecl(TagDecl *D);
261 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000262 void VisitRecordDecl(RecordDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263 void VisitValueDecl(ValueDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000264 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000265 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000266 void VisitFieldDecl(FieldDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000267 void VisitVarDecl(VarDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000268 void VisitParmVarDecl(ParmVarDecl *D);
269 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor1028bc62009-04-13 22:49:25 +0000270 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
271 void VisitBlockDecl(BlockDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000272 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
273 uint64_t VisibleOffset);
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000274 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000275 };
276}
277
278void PCHDeclWriter::VisitDecl(Decl *D) {
279 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
280 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
281 Writer.AddSourceLocation(D->getLocation(), Record);
282 Record.push_back(D->isInvalidDecl());
Douglas Gregor68a2eb02009-04-15 21:30:51 +0000283 Record.push_back(D->hasAttrs());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000284 Record.push_back(D->isImplicit());
285 Record.push_back(D->getAccess());
286}
287
288void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
289 VisitDecl(D);
290 Code = pch::DECL_TRANSLATION_UNIT;
291}
292
293void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
294 VisitDecl(D);
295 Writer.AddDeclarationName(D->getDeclName(), Record);
296}
297
298void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
299 VisitNamedDecl(D);
300 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
301}
302
303void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
304 VisitTypeDecl(D);
305 Writer.AddTypeRef(D->getUnderlyingType(), Record);
306 Code = pch::DECL_TYPEDEF;
307}
308
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000309void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
310 VisitTypeDecl(D);
311 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
312 Record.push_back(D->isDefinition());
313 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
314}
315
316void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
317 VisitTagDecl(D);
318 Writer.AddTypeRef(D->getIntegerType(), Record);
319 Code = pch::DECL_ENUM;
320}
321
Douglas Gregor8c700062009-04-13 21:20:57 +0000322void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
323 VisitTagDecl(D);
324 Record.push_back(D->hasFlexibleArrayMember());
325 Record.push_back(D->isAnonymousStructOrUnion());
326 Code = pch::DECL_RECORD;
327}
328
Douglas Gregor2cf26342009-04-09 22:27:44 +0000329void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
330 VisitNamedDecl(D);
331 Writer.AddTypeRef(D->getType(), Record);
332}
333
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000334void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
335 VisitValueDecl(D);
Douglas Gregor0b748912009-04-14 21:18:50 +0000336 Record.push_back(D->getInitExpr()? 1 : 0);
337 if (D->getInitExpr())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000338 Writer.AddStmt(D->getInitExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000339 Writer.AddAPSInt(D->getInitVal(), Record);
340 Code = pch::DECL_ENUM_CONSTANT;
341}
342
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000343void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
344 VisitValueDecl(D);
Douglas Gregor025452f2009-04-17 00:04:06 +0000345 Record.push_back(D->isThisDeclarationADefinition());
346 if (D->isThisDeclarationADefinition())
Douglas Gregor72971342009-04-18 00:02:19 +0000347 Writer.AddStmt(D->getBody(Context));
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000348 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
349 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
350 Record.push_back(D->isInline());
351 Record.push_back(D->isVirtual());
352 Record.push_back(D->isPure());
353 Record.push_back(D->inheritedPrototype());
354 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
355 Record.push_back(D->isDeleted());
356 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
357 Record.push_back(D->param_size());
358 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
359 P != PEnd; ++P)
360 Writer.AddDeclRef(*P, Record);
361 Code = pch::DECL_FUNCTION;
362}
363
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000364void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
365 VisitNamedDecl(D);
366 // FIXME: convert to LazyStmtPtr?
367 // Unlike C/C++, method bodies will never be in header files.
368 Record.push_back(D->getBody() != 0);
369 if (D->getBody() != 0) {
370 Writer.AddStmt(D->getBody(Context));
371 Writer.AddDeclRef(D->getSelfDecl(), Record);
372 Writer.AddDeclRef(D->getCmdDecl(), Record);
373 }
374 Record.push_back(D->isInstanceMethod());
375 Record.push_back(D->isVariadic());
376 Record.push_back(D->isSynthesized());
377 // FIXME: stable encoding for @required/@optional
378 Record.push_back(D->getImplementationControl());
379 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
380 Record.push_back(D->getObjCDeclQualifier());
381 Writer.AddTypeRef(D->getResultType(), Record);
382 Writer.AddSourceLocation(D->getLocEnd(), Record);
383 Record.push_back(D->param_size());
384 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
385 PEnd = D->param_end(); P != PEnd; ++P)
386 Writer.AddDeclRef(*P, Record);
387 Code = pch::DECL_OBJC_METHOD;
388}
389
Douglas Gregor8c700062009-04-13 21:20:57 +0000390void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
391 VisitValueDecl(D);
392 Record.push_back(D->isMutable());
Douglas Gregor0b748912009-04-14 21:18:50 +0000393 Record.push_back(D->getBitWidth()? 1 : 0);
394 if (D->getBitWidth())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000395 Writer.AddStmt(D->getBitWidth());
Douglas Gregor8c700062009-04-13 21:20:57 +0000396 Code = pch::DECL_FIELD;
397}
398
Douglas Gregor2cf26342009-04-09 22:27:44 +0000399void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
400 VisitValueDecl(D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000401 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor2cf26342009-04-09 22:27:44 +0000402 Record.push_back(D->isThreadSpecified());
403 Record.push_back(D->hasCXXDirectInitializer());
404 Record.push_back(D->isDeclaredInCondition());
405 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
406 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregor0b748912009-04-14 21:18:50 +0000407 Record.push_back(D->getInit()? 1 : 0);
408 if (D->getInit())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000409 Writer.AddStmt(D->getInit());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000410 Code = pch::DECL_VAR;
411}
412
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000413void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
414 VisitVarDecl(D);
415 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000416 // FIXME: emit default argument (C++)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000417 // FIXME: why isn't the "default argument" just stored as the initializer
418 // in VarDecl?
419 Code = pch::DECL_PARM_VAR;
420}
421
422void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
423 VisitParmVarDecl(D);
424 Writer.AddTypeRef(D->getOriginalType(), Record);
425 Code = pch::DECL_ORIGINAL_PARM_VAR;
426}
427
Douglas Gregor1028bc62009-04-13 22:49:25 +0000428void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
429 VisitDecl(D);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000430 Writer.AddStmt(D->getAsmString());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000431 Code = pch::DECL_FILE_SCOPE_ASM;
432}
433
434void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
435 VisitDecl(D);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000436 Writer.AddStmt(D->getBody());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000437 Record.push_back(D->param_size());
438 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
439 P != PEnd; ++P)
440 Writer.AddDeclRef(*P, Record);
441 Code = pch::DECL_BLOCK;
442}
443
Douglas Gregor2cf26342009-04-09 22:27:44 +0000444/// \brief Emit the DeclContext part of a declaration context decl.
445///
446/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
447/// block for this declaration context is stored. May be 0 to indicate
448/// that there are no declarations stored within this context.
449///
450/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
451/// block for this declaration context is stored. May be 0 to indicate
452/// that there are no declarations visible from this context. Note
453/// that this value will not be emitted for non-primary declaration
454/// contexts.
455void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
456 uint64_t VisibleOffset) {
457 Record.push_back(LexicalOffset);
458 if (DC->getPrimaryContext() == DC)
459 Record.push_back(VisibleOffset);
460}
461
462//===----------------------------------------------------------------------===//
Douglas Gregor0b748912009-04-14 21:18:50 +0000463// Statement/expression serialization
464//===----------------------------------------------------------------------===//
465namespace {
466 class VISIBILITY_HIDDEN PCHStmtWriter
467 : public StmtVisitor<PCHStmtWriter, void> {
468
469 PCHWriter &Writer;
470 PCHWriter::RecordData &Record;
471
472 public:
473 pch::StmtCode Code;
474
475 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
476 : Writer(Writer), Record(Record) { }
477
Douglas Gregor025452f2009-04-17 00:04:06 +0000478 void VisitStmt(Stmt *S);
479 void VisitNullStmt(NullStmt *S);
480 void VisitCompoundStmt(CompoundStmt *S);
481 void VisitSwitchCase(SwitchCase *S);
482 void VisitCaseStmt(CaseStmt *S);
483 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000484 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000485 void VisitIfStmt(IfStmt *S);
486 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000487 void VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000488 void VisitDoStmt(DoStmt *S);
489 void VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000490 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000491 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000492 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000493 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000494 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000495 void VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000496 void VisitAsmStmt(AsmStmt *S);
Douglas Gregor0b748912009-04-14 21:18:50 +0000497 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000498 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000499 void VisitDeclRefExpr(DeclRefExpr *E);
500 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000501 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000502 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000503 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000504 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000505 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000506 void VisitUnaryOperator(UnaryOperator *E);
507 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000508 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000509 void VisitCallExpr(CallExpr *E);
510 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000511 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000512 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000513 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
514 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000515 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000516 void VisitExplicitCastExpr(ExplicitCastExpr *E);
517 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000518 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000519 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000520 void VisitInitListExpr(InitListExpr *E);
521 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
522 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000523 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000524 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000525 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000526 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
527 void VisitChooseExpr(ChooseExpr *E);
528 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000529 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000530 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000531 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000532 };
533}
534
Douglas Gregor025452f2009-04-17 00:04:06 +0000535void PCHStmtWriter::VisitStmt(Stmt *S) {
536}
537
538void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
539 VisitStmt(S);
540 Writer.AddSourceLocation(S->getSemiLoc(), Record);
541 Code = pch::STMT_NULL;
542}
543
544void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
545 VisitStmt(S);
546 Record.push_back(S->size());
547 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
548 CS != CSEnd; ++CS)
549 Writer.WriteSubStmt(*CS);
550 Writer.AddSourceLocation(S->getLBracLoc(), Record);
551 Writer.AddSourceLocation(S->getRBracLoc(), Record);
552 Code = pch::STMT_COMPOUND;
553}
554
555void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
556 VisitStmt(S);
557 Record.push_back(Writer.RecordSwitchCaseID(S));
558}
559
560void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
561 VisitSwitchCase(S);
562 Writer.WriteSubStmt(S->getLHS());
563 Writer.WriteSubStmt(S->getRHS());
564 Writer.WriteSubStmt(S->getSubStmt());
565 Writer.AddSourceLocation(S->getCaseLoc(), Record);
566 Code = pch::STMT_CASE;
567}
568
569void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
570 VisitSwitchCase(S);
571 Writer.WriteSubStmt(S->getSubStmt());
572 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
573 Code = pch::STMT_DEFAULT;
574}
575
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000576void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
577 VisitStmt(S);
578 Writer.AddIdentifierRef(S->getID(), Record);
579 Writer.WriteSubStmt(S->getSubStmt());
580 Writer.AddSourceLocation(S->getIdentLoc(), Record);
581 Record.push_back(Writer.GetLabelID(S));
582 Code = pch::STMT_LABEL;
583}
584
Douglas Gregor025452f2009-04-17 00:04:06 +0000585void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
586 VisitStmt(S);
587 Writer.WriteSubStmt(S->getCond());
588 Writer.WriteSubStmt(S->getThen());
589 Writer.WriteSubStmt(S->getElse());
590 Writer.AddSourceLocation(S->getIfLoc(), Record);
591 Code = pch::STMT_IF;
592}
593
594void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
595 VisitStmt(S);
596 Writer.WriteSubStmt(S->getCond());
597 Writer.WriteSubStmt(S->getBody());
598 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
599 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
600 SC = SC->getNextSwitchCase())
601 Record.push_back(Writer.getSwitchCaseID(SC));
602 Code = pch::STMT_SWITCH;
603}
604
Douglas Gregord921cf92009-04-17 00:16:09 +0000605void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
606 VisitStmt(S);
607 Writer.WriteSubStmt(S->getCond());
608 Writer.WriteSubStmt(S->getBody());
609 Writer.AddSourceLocation(S->getWhileLoc(), Record);
610 Code = pch::STMT_WHILE;
611}
612
Douglas Gregor67d82492009-04-17 00:29:51 +0000613void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
614 VisitStmt(S);
615 Writer.WriteSubStmt(S->getCond());
616 Writer.WriteSubStmt(S->getBody());
617 Writer.AddSourceLocation(S->getDoLoc(), Record);
618 Code = pch::STMT_DO;
619}
620
621void PCHStmtWriter::VisitForStmt(ForStmt *S) {
622 VisitStmt(S);
623 Writer.WriteSubStmt(S->getInit());
624 Writer.WriteSubStmt(S->getCond());
625 Writer.WriteSubStmt(S->getInc());
626 Writer.WriteSubStmt(S->getBody());
627 Writer.AddSourceLocation(S->getForLoc(), Record);
628 Code = pch::STMT_FOR;
629}
630
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000631void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
632 VisitStmt(S);
633 Record.push_back(Writer.GetLabelID(S->getLabel()));
634 Writer.AddSourceLocation(S->getGotoLoc(), Record);
635 Writer.AddSourceLocation(S->getLabelLoc(), Record);
636 Code = pch::STMT_GOTO;
637}
638
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000639void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
640 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000641 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000642 Writer.WriteSubStmt(S->getTarget());
643 Code = pch::STMT_INDIRECT_GOTO;
644}
645
Douglas Gregord921cf92009-04-17 00:16:09 +0000646void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
647 VisitStmt(S);
648 Writer.AddSourceLocation(S->getContinueLoc(), Record);
649 Code = pch::STMT_CONTINUE;
650}
651
Douglas Gregor025452f2009-04-17 00:04:06 +0000652void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
653 VisitStmt(S);
654 Writer.AddSourceLocation(S->getBreakLoc(), Record);
655 Code = pch::STMT_BREAK;
656}
657
Douglas Gregor0de9d882009-04-17 16:34:57 +0000658void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
659 VisitStmt(S);
660 Writer.WriteSubStmt(S->getRetValue());
661 Writer.AddSourceLocation(S->getReturnLoc(), Record);
662 Code = pch::STMT_RETURN;
663}
664
Douglas Gregor84f21702009-04-17 16:55:36 +0000665void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
666 VisitStmt(S);
667 Writer.AddSourceLocation(S->getStartLoc(), Record);
668 Writer.AddSourceLocation(S->getEndLoc(), Record);
669 DeclGroupRef DG = S->getDeclGroup();
670 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
671 Writer.AddDeclRef(*D, Record);
672 Code = pch::STMT_DECL;
673}
674
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000675void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
676 VisitStmt(S);
677 Record.push_back(S->getNumOutputs());
678 Record.push_back(S->getNumInputs());
679 Record.push_back(S->getNumClobbers());
680 Writer.AddSourceLocation(S->getAsmLoc(), Record);
681 Writer.AddSourceLocation(S->getRParenLoc(), Record);
682 Record.push_back(S->isVolatile());
683 Record.push_back(S->isSimple());
684 Writer.WriteSubStmt(S->getAsmString());
685
686 // Outputs
687 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
688 Writer.AddString(S->getOutputName(I), Record);
689 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
690 Writer.WriteSubStmt(S->getOutputExpr(I));
691 }
692
693 // Inputs
694 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
695 Writer.AddString(S->getInputName(I), Record);
696 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
697 Writer.WriteSubStmt(S->getInputExpr(I));
698 }
699
700 // Clobbers
701 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
702 Writer.WriteSubStmt(S->getClobber(I));
703
704 Code = pch::STMT_ASM;
705}
706
Douglas Gregor0b748912009-04-14 21:18:50 +0000707void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000708 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000709 Writer.AddTypeRef(E->getType(), Record);
710 Record.push_back(E->isTypeDependent());
711 Record.push_back(E->isValueDependent());
712}
713
Douglas Gregor17fc2232009-04-14 21:55:33 +0000714void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
715 VisitExpr(E);
716 Writer.AddSourceLocation(E->getLocation(), Record);
717 Record.push_back(E->getIdentType()); // FIXME: stable encoding
718 Code = pch::EXPR_PREDEFINED;
719}
720
Douglas Gregor0b748912009-04-14 21:18:50 +0000721void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
722 VisitExpr(E);
723 Writer.AddDeclRef(E->getDecl(), Record);
724 Writer.AddSourceLocation(E->getLocation(), Record);
725 Code = pch::EXPR_DECL_REF;
726}
727
728void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
729 VisitExpr(E);
730 Writer.AddSourceLocation(E->getLocation(), Record);
731 Writer.AddAPInt(E->getValue(), Record);
732 Code = pch::EXPR_INTEGER_LITERAL;
733}
734
Douglas Gregor17fc2232009-04-14 21:55:33 +0000735void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
736 VisitExpr(E);
737 Writer.AddAPFloat(E->getValue(), Record);
738 Record.push_back(E->isExact());
739 Writer.AddSourceLocation(E->getLocation(), Record);
740 Code = pch::EXPR_FLOATING_LITERAL;
741}
742
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000743void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
744 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000745 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000746 Code = pch::EXPR_IMAGINARY_LITERAL;
747}
748
Douglas Gregor673ecd62009-04-15 16:35:07 +0000749void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
750 VisitExpr(E);
751 Record.push_back(E->getByteLength());
752 Record.push_back(E->getNumConcatenated());
753 Record.push_back(E->isWide());
754 // FIXME: String data should be stored as a blob at the end of the
755 // StringLiteral. However, we can't do so now because we have no
756 // provision for coping with abbreviations when we're jumping around
757 // the PCH file during deserialization.
758 Record.insert(Record.end(),
759 E->getStrData(), E->getStrData() + E->getByteLength());
760 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
761 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
762 Code = pch::EXPR_STRING_LITERAL;
763}
764
Douglas Gregor0b748912009-04-14 21:18:50 +0000765void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
766 VisitExpr(E);
767 Record.push_back(E->getValue());
768 Writer.AddSourceLocation(E->getLoc(), Record);
769 Record.push_back(E->isWide());
770 Code = pch::EXPR_CHARACTER_LITERAL;
771}
772
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000773void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
774 VisitExpr(E);
775 Writer.AddSourceLocation(E->getLParen(), Record);
776 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000777 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000778 Code = pch::EXPR_PAREN;
779}
780
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000781void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
782 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000783 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000784 Record.push_back(E->getOpcode()); // FIXME: stable encoding
785 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
786 Code = pch::EXPR_UNARY_OPERATOR;
787}
788
789void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
790 VisitExpr(E);
791 Record.push_back(E->isSizeOf());
792 if (E->isArgumentType())
793 Writer.AddTypeRef(E->getArgumentType(), Record);
794 else {
795 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000796 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000797 }
798 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
799 Writer.AddSourceLocation(E->getRParenLoc(), Record);
800 Code = pch::EXPR_SIZEOF_ALIGN_OF;
801}
802
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000803void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
804 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000805 Writer.WriteSubStmt(E->getLHS());
806 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000807 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
808 Code = pch::EXPR_ARRAY_SUBSCRIPT;
809}
810
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000811void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
812 VisitExpr(E);
813 Record.push_back(E->getNumArgs());
814 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000815 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000816 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
817 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000818 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000819 Code = pch::EXPR_CALL;
820}
821
822void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
823 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000824 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000825 Writer.AddDeclRef(E->getMemberDecl(), Record);
826 Writer.AddSourceLocation(E->getMemberLoc(), Record);
827 Record.push_back(E->isArrow());
828 Code = pch::EXPR_MEMBER;
829}
830
Douglas Gregor087fd532009-04-14 23:32:43 +0000831void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
832 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000833 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000834}
835
Douglas Gregordb600c32009-04-15 00:25:59 +0000836void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
837 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000838 Writer.WriteSubStmt(E->getLHS());
839 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000840 Record.push_back(E->getOpcode()); // FIXME: stable encoding
841 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
842 Code = pch::EXPR_BINARY_OPERATOR;
843}
844
Douglas Gregorad90e962009-04-15 22:40:36 +0000845void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
846 VisitBinaryOperator(E);
847 Writer.AddTypeRef(E->getComputationLHSType(), Record);
848 Writer.AddTypeRef(E->getComputationResultType(), Record);
849 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
850}
851
852void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
853 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000854 Writer.WriteSubStmt(E->getCond());
855 Writer.WriteSubStmt(E->getLHS());
856 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000857 Code = pch::EXPR_CONDITIONAL_OPERATOR;
858}
859
Douglas Gregor087fd532009-04-14 23:32:43 +0000860void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
861 VisitCastExpr(E);
862 Record.push_back(E->isLvalueCast());
863 Code = pch::EXPR_IMPLICIT_CAST;
864}
865
Douglas Gregordb600c32009-04-15 00:25:59 +0000866void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
867 VisitCastExpr(E);
868 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
869}
870
871void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
872 VisitExplicitCastExpr(E);
873 Writer.AddSourceLocation(E->getLParenLoc(), Record);
874 Writer.AddSourceLocation(E->getRParenLoc(), Record);
875 Code = pch::EXPR_CSTYLE_CAST;
876}
877
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000878void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
879 VisitExpr(E);
880 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000881 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000882 Record.push_back(E->isFileScope());
883 Code = pch::EXPR_COMPOUND_LITERAL;
884}
885
Douglas Gregord3c98a02009-04-15 23:02:49 +0000886void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
887 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000888 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000889 Writer.AddIdentifierRef(&E->getAccessor(), Record);
890 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
891 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
892}
893
Douglas Gregord077d752009-04-16 00:55:48 +0000894void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
895 VisitExpr(E);
896 Record.push_back(E->getNumInits());
897 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000898 Writer.WriteSubStmt(E->getInit(I));
899 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +0000900 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
901 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
902 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
903 Record.push_back(E->hadArrayRangeDesignator());
904 Code = pch::EXPR_INIT_LIST;
905}
906
907void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
908 VisitExpr(E);
909 Record.push_back(E->getNumSubExprs());
910 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000911 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +0000912 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
913 Record.push_back(E->usesGNUSyntax());
914 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
915 DEnd = E->designators_end();
916 D != DEnd; ++D) {
917 if (D->isFieldDesignator()) {
918 if (FieldDecl *Field = D->getField()) {
919 Record.push_back(pch::DESIG_FIELD_DECL);
920 Writer.AddDeclRef(Field, Record);
921 } else {
922 Record.push_back(pch::DESIG_FIELD_NAME);
923 Writer.AddIdentifierRef(D->getFieldName(), Record);
924 }
925 Writer.AddSourceLocation(D->getDotLoc(), Record);
926 Writer.AddSourceLocation(D->getFieldLoc(), Record);
927 } else if (D->isArrayDesignator()) {
928 Record.push_back(pch::DESIG_ARRAY);
929 Record.push_back(D->getFirstExprIndex());
930 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
931 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
932 } else {
933 assert(D->isArrayRangeDesignator() && "Unknown designator");
934 Record.push_back(pch::DESIG_ARRAY_RANGE);
935 Record.push_back(D->getFirstExprIndex());
936 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
937 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
938 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
939 }
940 }
941 Code = pch::EXPR_DESIGNATED_INIT;
942}
943
944void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
945 VisitExpr(E);
946 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
947}
948
Douglas Gregord3c98a02009-04-15 23:02:49 +0000949void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
950 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000951 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000952 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
953 Writer.AddSourceLocation(E->getRParenLoc(), Record);
954 Code = pch::EXPR_VA_ARG;
955}
956
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000957void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
958 VisitExpr(E);
959 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
960 Writer.AddSourceLocation(E->getLabelLoc(), Record);
961 Record.push_back(Writer.GetLabelID(E->getLabel()));
962 Code = pch::EXPR_ADDR_LABEL;
963}
964
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000965void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
966 VisitExpr(E);
967 Writer.WriteSubStmt(E->getSubStmt());
968 Writer.AddSourceLocation(E->getLParenLoc(), Record);
969 Writer.AddSourceLocation(E->getRParenLoc(), Record);
970 Code = pch::EXPR_STMT;
971}
972
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000973void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
974 VisitExpr(E);
975 Writer.AddTypeRef(E->getArgType1(), Record);
976 Writer.AddTypeRef(E->getArgType2(), Record);
977 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
978 Writer.AddSourceLocation(E->getRParenLoc(), Record);
979 Code = pch::EXPR_TYPES_COMPATIBLE;
980}
981
982void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
983 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000984 Writer.WriteSubStmt(E->getCond());
985 Writer.WriteSubStmt(E->getLHS());
986 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000987 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
988 Writer.AddSourceLocation(E->getRParenLoc(), Record);
989 Code = pch::EXPR_CHOOSE;
990}
991
992void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
993 VisitExpr(E);
994 Writer.AddSourceLocation(E->getTokenLocation(), Record);
995 Code = pch::EXPR_GNU_NULL;
996}
997
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000998void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
999 VisitExpr(E);
1000 Record.push_back(E->getNumSubExprs());
1001 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001002 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001003 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1004 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1005 Code = pch::EXPR_SHUFFLE_VECTOR;
1006}
1007
Douglas Gregor84af7c22009-04-17 19:21:43 +00001008void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1009 VisitExpr(E);
1010 Writer.AddDeclRef(E->getBlockDecl(), Record);
1011 Record.push_back(E->hasBlockDeclRefExprs());
1012 Code = pch::EXPR_BLOCK;
1013}
1014
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001015void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1016 VisitExpr(E);
1017 Writer.AddDeclRef(E->getDecl(), Record);
1018 Writer.AddSourceLocation(E->getLocation(), Record);
1019 Record.push_back(E->isByRef());
1020 Code = pch::EXPR_BLOCK_DECL_REF;
1021}
1022
Douglas Gregor0b748912009-04-14 21:18:50 +00001023//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00001024// PCHWriter Implementation
1025//===----------------------------------------------------------------------===//
1026
Douglas Gregor2bec0412009-04-10 21:16:55 +00001027/// \brief Write the target triple (e.g., i686-apple-darwin9).
1028void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1029 using namespace llvm;
1030 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1031 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001033 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001034
1035 RecordData Record;
1036 Record.push_back(pch::TARGET_TRIPLE);
1037 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001038 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +00001039}
1040
1041/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001042void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1043 RecordData Record;
1044 Record.push_back(LangOpts.Trigraphs);
1045 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1046 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1047 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1048 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1049 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1050 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1051 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1052 Record.push_back(LangOpts.C99); // C99 Support
1053 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1054 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1055 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1056 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1057 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1058
1059 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1060 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1061 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1062
1063 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1064 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1065 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1066 Record.push_back(LangOpts.LaxVectorConversions);
1067 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1068
1069 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1070 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1071 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1072
1073 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1074 // by locks.
1075 Record.push_back(LangOpts.Blocks); // block extension to C
1076 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1077 // they are unused.
1078 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1079 // (modulo the platform support).
1080
1081 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1082 // signed integer arithmetic overflows.
1083
1084 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1085 // may be ripped out at any time.
1086
1087 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1088 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1089 // defined.
1090 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1091 // opposed to __DYNAMIC__).
1092 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1093
1094 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1095 // used (instead of C99 semantics).
1096 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1097 Record.push_back(LangOpts.getGCMode());
1098 Record.push_back(LangOpts.getVisibilityMode());
1099 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001100 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001101}
1102
Douglas Gregor14f79002009-04-10 03:52:48 +00001103//===----------------------------------------------------------------------===//
1104// Source Manager Serialization
1105//===----------------------------------------------------------------------===//
1106
1107/// \brief Create an abbreviation for the SLocEntry that refers to a
1108/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001109static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001110 using namespace llvm;
1111 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1112 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1115 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +00001117 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001118 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001119}
1120
1121/// \brief Create an abbreviation for the SLocEntry that refers to a
1122/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001123static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001124 using namespace llvm;
1125 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1126 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1128 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1129 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1130 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1131 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001132 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001133}
1134
1135/// \brief Create an abbreviation for the SLocEntry that refers to a
1136/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001137static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001138 using namespace llvm;
1139 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1140 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1141 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001142 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001143}
1144
1145/// \brief Create an abbreviation for the SLocEntry that refers to an
1146/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001147static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001148 using namespace llvm;
1149 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1150 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001156 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001157}
1158
1159/// \brief Writes the block containing the serialized form of the
1160/// source manager.
1161///
1162/// TODO: We should probably use an on-disk hash table (stored in a
1163/// blob), indexed based on the file name, so that we only create
1164/// entries for files that we actually need. In the common case (no
1165/// errors), we probably won't have to create file entries for any of
1166/// the files in the AST.
1167void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001168 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001169 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001170
1171 // Abbreviations for the various kinds of source-location entries.
1172 int SLocFileAbbrv = -1;
1173 int SLocBufferAbbrv = -1;
1174 int SLocBufferBlobAbbrv = -1;
1175 int SLocInstantiationAbbrv = -1;
1176
1177 // Write out the source location entry table. We skip the first
1178 // entry, which is always the same dummy entry.
1179 RecordData Record;
1180 for (SourceManager::sloc_entry_iterator
1181 SLoc = SourceMgr.sloc_entry_begin() + 1,
1182 SLocEnd = SourceMgr.sloc_entry_end();
1183 SLoc != SLocEnd; ++SLoc) {
1184 // Figure out which record code to use.
1185 unsigned Code;
1186 if (SLoc->isFile()) {
1187 if (SLoc->getFile().getContentCache()->Entry)
1188 Code = pch::SM_SLOC_FILE_ENTRY;
1189 else
1190 Code = pch::SM_SLOC_BUFFER_ENTRY;
1191 } else
1192 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1193 Record.push_back(Code);
1194
1195 Record.push_back(SLoc->getOffset());
1196 if (SLoc->isFile()) {
1197 const SrcMgr::FileInfo &File = SLoc->getFile();
1198 Record.push_back(File.getIncludeLoc().getRawEncoding());
1199 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001200 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001201
1202 const SrcMgr::ContentCache *Content = File.getContentCache();
1203 if (Content->Entry) {
1204 // The source location entry is a file. The blob associated
1205 // with this entry is the file name.
1206 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001207 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1208 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001209 Content->Entry->getName(),
1210 strlen(Content->Entry->getName()));
1211 } else {
1212 // The source location entry is a buffer. The blob associated
1213 // with this entry contains the contents of the buffer.
1214 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001215 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1216 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001217 }
1218
1219 // We add one to the size so that we capture the trailing NULL
1220 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1221 // the reader side).
1222 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1223 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001224 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001225 Record.clear();
1226 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001227 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001228 Buffer->getBufferStart(),
1229 Buffer->getBufferSize() + 1);
1230 }
1231 } else {
1232 // The source location entry is an instantiation.
1233 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1234 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1235 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1236 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1237
Douglas Gregorf60e9912009-04-15 18:05:10 +00001238 // Compute the token length for this macro expansion.
1239 unsigned NextOffset = SourceMgr.getNextOffset();
1240 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1241 if (++NextSLoc != SLocEnd)
1242 NextOffset = NextSLoc->getOffset();
1243 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1244
Douglas Gregor14f79002009-04-10 03:52:48 +00001245 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001246 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1247 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001248 }
1249
1250 Record.clear();
1251 }
1252
Douglas Gregorbd945002009-04-13 16:31:14 +00001253 // Write the line table.
1254 if (SourceMgr.hasLineTable()) {
1255 LineTableInfo &LineTable = SourceMgr.getLineTable();
1256
1257 // Emit the file names
1258 Record.push_back(LineTable.getNumFilenames());
1259 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1260 // Emit the file name
1261 const char *Filename = LineTable.getFilename(I);
1262 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1263 Record.push_back(FilenameLen);
1264 if (FilenameLen)
1265 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1266 }
1267
1268 // Emit the line entries
1269 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1270 L != LEnd; ++L) {
1271 // Emit the file ID
1272 Record.push_back(L->first);
1273
1274 // Emit the line entries
1275 Record.push_back(L->second.size());
1276 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1277 LEEnd = L->second.end();
1278 LE != LEEnd; ++LE) {
1279 Record.push_back(LE->FileOffset);
1280 Record.push_back(LE->LineNo);
1281 Record.push_back(LE->FilenameID);
1282 Record.push_back((unsigned)LE->FileKind);
1283 Record.push_back(LE->IncludeOffset);
1284 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001285 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001286 }
1287 }
1288
Douglas Gregorc9490c02009-04-16 22:23:12 +00001289 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001290}
1291
Chris Lattner0b1fb982009-04-10 17:15:23 +00001292/// \brief Writes the block containing the serialized form of the
1293/// preprocessor.
1294///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001295void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001296 // Enter the preprocessor block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001297 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001298
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001299 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1300 // FIXME: use diagnostics subsystem for localization etc.
1301 if (PP.SawDateOrTime())
1302 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001303
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001304 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001305
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001306 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1307 if (PP.getCounterValue() != 0) {
1308 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001309 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001310 Record.clear();
1311 }
1312
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001313 // Loop over all the macro definitions that are live at the end of the file,
1314 // emitting each to the PP section.
1315 // FIXME: Eventually we want to emit an index so that we can lazily load
1316 // macros.
1317 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1318 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001319 // FIXME: This emits macros in hash table order, we should do it in a stable
1320 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001321 MacroInfo *MI = I->second;
1322
1323 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1324 // been redefined by the header (in which case they are not isBuiltinMacro).
1325 if (MI->isBuiltinMacro())
1326 continue;
1327
Chris Lattner7356a312009-04-11 21:15:38 +00001328 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001329 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1330 Record.push_back(MI->isUsed());
1331
1332 unsigned Code;
1333 if (MI->isObjectLike()) {
1334 Code = pch::PP_MACRO_OBJECT_LIKE;
1335 } else {
1336 Code = pch::PP_MACRO_FUNCTION_LIKE;
1337
1338 Record.push_back(MI->isC99Varargs());
1339 Record.push_back(MI->isGNUVarargs());
1340 Record.push_back(MI->getNumArgs());
1341 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1342 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001343 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001344 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001345 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001346 Record.clear();
1347
Chris Lattnerdf961c22009-04-10 18:08:30 +00001348 // Emit the tokens array.
1349 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1350 // Note that we know that the preprocessor does not have any annotation
1351 // tokens in it because they are created by the parser, and thus can't be
1352 // in a macro definition.
1353 const Token &Tok = MI->getReplacementToken(TokNo);
1354
1355 Record.push_back(Tok.getLocation().getRawEncoding());
1356 Record.push_back(Tok.getLength());
1357
Chris Lattnerdf961c22009-04-10 18:08:30 +00001358 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1359 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001360 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001361
1362 // FIXME: Should translate token kind to a stable encoding.
1363 Record.push_back(Tok.getKind());
1364 // FIXME: Should translate token flags to a stable encoding.
1365 Record.push_back(Tok.getFlags());
1366
Douglas Gregorc9490c02009-04-16 22:23:12 +00001367 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001368 Record.clear();
1369 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001370
1371 }
1372
Douglas Gregorc9490c02009-04-16 22:23:12 +00001373 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001374}
1375
1376
Douglas Gregor2cf26342009-04-09 22:27:44 +00001377/// \brief Write the representation of a type to the PCH stream.
1378void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001379 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001380 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001381 ID = NextTypeID++;
1382
1383 // Record the offset for this type.
1384 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001385 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001386 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1387 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001388 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001389 }
1390
1391 RecordData Record;
1392
1393 // Emit the type's representation.
1394 PCHTypeWriter W(*this, Record);
1395 switch (T->getTypeClass()) {
1396 // For all of the concrete, non-dependent types, call the
1397 // appropriate visitor function.
1398#define TYPE(Class, Base) \
1399 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1400#define ABSTRACT_TYPE(Class, Base)
1401#define DEPENDENT_TYPE(Class, Base)
1402#include "clang/AST/TypeNodes.def"
1403
1404 // For all of the dependent type nodes (which only occur in C++
1405 // templates), produce an error.
1406#define TYPE(Class, Base)
1407#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1408#include "clang/AST/TypeNodes.def"
1409 assert(false && "Cannot serialize dependent type nodes");
1410 break;
1411 }
1412
1413 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001414 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001415
1416 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001417 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001418}
1419
1420/// \brief Write a block containing all of the types.
1421void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001422 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001423 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001424
1425 // Emit all of the types in the ASTContext
1426 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1427 TEnd = Context.getTypes().end();
1428 T != TEnd; ++T) {
1429 // Builtin types are never serialized.
1430 if (isa<BuiltinType>(*T))
1431 continue;
1432
1433 WriteType(*T);
1434 }
1435
1436 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001437 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001438}
1439
1440/// \brief Write the block containing all of the declaration IDs
1441/// lexically declared within the given DeclContext.
1442///
1443/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1444/// bistream, or 0 if no block was written.
1445uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1446 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001447 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001448 return 0;
1449
Douglas Gregorc9490c02009-04-16 22:23:12 +00001450 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001451 RecordData Record;
1452 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1453 DEnd = DC->decls_end(Context);
1454 D != DEnd; ++D)
1455 AddDeclRef(*D, Record);
1456
Douglas Gregorc9490c02009-04-16 22:23:12 +00001457 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001458 return Offset;
1459}
1460
1461/// \brief Write the block containing all of the declaration IDs
1462/// visible from the given DeclContext.
1463///
1464/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1465/// bistream, or 0 if no block was written.
1466uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1467 DeclContext *DC) {
1468 if (DC->getPrimaryContext() != DC)
1469 return 0;
1470
Douglas Gregor58f06992009-04-18 15:49:20 +00001471 // Since there is no name lookup into functions or methods, don't
1472 // bother to build a visible-declarations table.
1473 if (DC->isFunctionOrMethod())
1474 return 0;
1475
Douglas Gregor2cf26342009-04-09 22:27:44 +00001476 // Force the DeclContext to build a its name-lookup table.
1477 DC->lookup(Context, DeclarationName());
1478
1479 // Serialize the contents of the mapping used for lookup. Note that,
1480 // although we have two very different code paths, the serialized
1481 // representation is the same for both cases: a declaration name,
1482 // followed by a size, followed by references to the visible
1483 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001484 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001485 RecordData Record;
1486 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001487 if (!Map)
1488 return 0;
1489
Douglas Gregor2cf26342009-04-09 22:27:44 +00001490 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1491 D != DEnd; ++D) {
1492 AddDeclarationName(D->first, Record);
1493 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1494 Record.push_back(Result.second - Result.first);
1495 for(; Result.first != Result.second; ++Result.first)
1496 AddDeclRef(*Result.first, Record);
1497 }
1498
1499 if (Record.size() == 0)
1500 return 0;
1501
Douglas Gregorc9490c02009-04-16 22:23:12 +00001502 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001503 return Offset;
1504}
1505
1506/// \brief Write a block containing all of the declarations.
1507void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001508 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001509 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001510
1511 // Emit all of the declarations.
1512 RecordData Record;
Douglas Gregor72971342009-04-18 00:02:19 +00001513 PCHDeclWriter W(*this, Context, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001514 while (!DeclsToEmit.empty()) {
1515 // Pull the next declaration off the queue
1516 Decl *D = DeclsToEmit.front();
1517 DeclsToEmit.pop();
1518
1519 // If this declaration is also a DeclContext, write blocks for the
1520 // declarations that lexically stored inside its context and those
1521 // declarations that are visible from its context. These blocks
1522 // are written before the declaration itself so that we can put
1523 // their offsets into the record for the declaration.
1524 uint64_t LexicalOffset = 0;
1525 uint64_t VisibleOffset = 0;
1526 DeclContext *DC = dyn_cast<DeclContext>(D);
1527 if (DC) {
1528 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1529 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1530 }
1531
1532 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001533 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001534 if (ID == 0)
1535 ID = DeclIDs.size();
1536
1537 unsigned Index = ID - 1;
1538
1539 // Record the offset for this declaration
1540 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001541 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001542 else if (DeclOffsets.size() < Index) {
1543 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001544 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001545 }
1546
1547 // Build and emit a record for this declaration
1548 Record.clear();
1549 W.Code = (pch::DeclCode)0;
1550 W.Visit(D);
1551 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001552 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001553 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001554
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001555 // If the declaration had any attributes, write them now.
1556 if (D->hasAttrs())
1557 WriteAttributeRecord(D->getAttrs());
1558
Douglas Gregor0b748912009-04-14 21:18:50 +00001559 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001560 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001561
Douglas Gregorfdd01722009-04-14 00:24:19 +00001562 // Note external declarations so that we can add them to a record
1563 // in the PCH file later.
1564 if (isa<FileScopeAsmDecl>(D))
1565 ExternalDefinitions.push_back(ID);
1566 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1567 if (// Non-static file-scope variables with initializers or that
1568 // are tentative definitions.
1569 (Var->isFileVarDecl() &&
1570 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1571 // Out-of-line definitions of static data members (C++).
1572 (Var->getDeclContext()->isRecord() &&
1573 !Var->getLexicalDeclContext()->isRecord() &&
1574 Var->getStorageClass() == VarDecl::Static))
1575 ExternalDefinitions.push_back(ID);
1576 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00001577 if (Func->isThisDeclarationADefinition())
Douglas Gregorfdd01722009-04-14 00:24:19 +00001578 ExternalDefinitions.push_back(ID);
1579 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001580 }
1581
1582 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001583 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001584}
1585
Douglas Gregorafaf3082009-04-11 00:14:32 +00001586/// \brief Write the identifier table into the PCH file.
1587///
1588/// The identifier table consists of a blob containing string data
1589/// (the actual identifiers themselves) and a separate "offsets" index
1590/// that maps identifier IDs to locations within the blob.
1591void PCHWriter::WriteIdentifierTable() {
1592 using namespace llvm;
1593
1594 // Create and write out the blob that contains the identifier
1595 // strings.
1596 RecordData IdentOffsets;
1597 IdentOffsets.resize(IdentifierIDs.size());
1598 {
1599 // Create the identifier string data.
1600 std::vector<char> Data;
1601 Data.push_back(0); // Data must not be empty.
1602 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1603 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1604 ID != IDEnd; ++ID) {
1605 assert(ID->first && "NULL identifier in identifier table");
1606
1607 // Make sure we're starting on an odd byte. The PCH reader
1608 // expects the low bit to be set on all of the offsets.
1609 if ((Data.size() & 0x01) == 0)
1610 Data.push_back((char)0);
1611
1612 IdentOffsets[ID->second - 1] = Data.size();
1613 Data.insert(Data.end(),
1614 ID->first->getName(),
1615 ID->first->getName() + ID->first->getLength());
1616 Data.push_back((char)0);
1617 }
1618
1619 // Create a blob abbreviation
1620 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1621 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001623 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001624
1625 // Write the identifier table
1626 RecordData Record;
1627 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001628 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001629 }
1630
1631 // Write the offsets table for identifier IDs.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001632 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001633}
1634
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001635/// \brief Write a record containing the given attributes.
1636void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1637 RecordData Record;
1638 for (; Attr; Attr = Attr->getNext()) {
1639 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1640 Record.push_back(Attr->isInherited());
1641 switch (Attr->getKind()) {
1642 case Attr::Alias:
1643 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1644 break;
1645
1646 case Attr::Aligned:
1647 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1648 break;
1649
1650 case Attr::AlwaysInline:
1651 break;
1652
1653 case Attr::AnalyzerNoReturn:
1654 break;
1655
1656 case Attr::Annotate:
1657 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1658 break;
1659
1660 case Attr::AsmLabel:
1661 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1662 break;
1663
1664 case Attr::Blocks:
1665 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1666 break;
1667
1668 case Attr::Cleanup:
1669 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1670 break;
1671
1672 case Attr::Const:
1673 break;
1674
1675 case Attr::Constructor:
1676 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1677 break;
1678
1679 case Attr::DLLExport:
1680 case Attr::DLLImport:
1681 case Attr::Deprecated:
1682 break;
1683
1684 case Attr::Destructor:
1685 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1686 break;
1687
1688 case Attr::FastCall:
1689 break;
1690
1691 case Attr::Format: {
1692 const FormatAttr *Format = cast<FormatAttr>(Attr);
1693 AddString(Format->getType(), Record);
1694 Record.push_back(Format->getFormatIdx());
1695 Record.push_back(Format->getFirstArg());
1696 break;
1697 }
1698
1699 case Attr::GNUCInline:
1700 case Attr::IBOutletKind:
1701 case Attr::NoReturn:
1702 case Attr::NoThrow:
1703 case Attr::Nodebug:
1704 case Attr::Noinline:
1705 break;
1706
1707 case Attr::NonNull: {
1708 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1709 Record.push_back(NonNull->size());
1710 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1711 break;
1712 }
1713
1714 case Attr::ObjCException:
1715 case Attr::ObjCNSObject:
1716 case Attr::Overloadable:
1717 break;
1718
1719 case Attr::Packed:
1720 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1721 break;
1722
1723 case Attr::Pure:
1724 break;
1725
1726 case Attr::Regparm:
1727 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1728 break;
1729
1730 case Attr::Section:
1731 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1732 break;
1733
1734 case Attr::StdCall:
1735 case Attr::TransparentUnion:
1736 case Attr::Unavailable:
1737 case Attr::Unused:
1738 case Attr::Used:
1739 break;
1740
1741 case Attr::Visibility:
1742 // FIXME: stable encoding
1743 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1744 break;
1745
1746 case Attr::WarnUnusedResult:
1747 case Attr::Weak:
1748 case Attr::WeakImport:
1749 break;
1750 }
1751 }
1752
Douglas Gregorc9490c02009-04-16 22:23:12 +00001753 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001754}
1755
1756void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1757 Record.push_back(Str.size());
1758 Record.insert(Record.end(), Str.begin(), Str.end());
1759}
1760
Douglas Gregorc9490c02009-04-16 22:23:12 +00001761PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor3e1af842009-04-17 22:13:46 +00001762 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), NumStatements(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001763
Douglas Gregore7785042009-04-20 15:53:59 +00001764void PCHWriter::WritePCH(Sema &SemaRef) {
1765 ASTContext &Context = SemaRef.Context;
1766 Preprocessor &PP = SemaRef.PP;
1767
Douglas Gregor2cf26342009-04-09 22:27:44 +00001768 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001769 Stream.Emit((unsigned)'C', 8);
1770 Stream.Emit((unsigned)'P', 8);
1771 Stream.Emit((unsigned)'C', 8);
1772 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001773
1774 // The translation unit is the first declaration we'll emit.
1775 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1776 DeclsToEmit.push(Context.getTranslationUnitDecl());
1777
1778 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001779 RecordData Record;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001780 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001781 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001782 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001783 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001784 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001785 WriteTypesBlock(Context);
1786 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001787 WriteIdentifierTable();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001788 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1789 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorad1de002009-04-18 05:55:16 +00001790
1791 // Write the record of special types.
1792 Record.clear();
1793 AddTypeRef(Context.getBuiltinVaListType(), Record);
1794 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1795
Douglas Gregorfdd01722009-04-14 00:24:19 +00001796 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00001797 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001798
1799 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00001800 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00001801 Record.push_back(NumStatements);
1802 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001803 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001804}
1805
1806void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1807 Record.push_back(Loc.getRawEncoding());
1808}
1809
1810void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1811 Record.push_back(Value.getBitWidth());
1812 unsigned N = Value.getNumWords();
1813 const uint64_t* Words = Value.getRawData();
1814 for (unsigned I = 0; I != N; ++I)
1815 Record.push_back(Words[I]);
1816}
1817
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001818void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1819 Record.push_back(Value.isUnsigned());
1820 AddAPInt(Value, Record);
1821}
1822
Douglas Gregor17fc2232009-04-14 21:55:33 +00001823void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1824 AddAPInt(Value.bitcastToAPInt(), Record);
1825}
1826
Douglas Gregor2cf26342009-04-09 22:27:44 +00001827void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001828 if (II == 0) {
1829 Record.push_back(0);
1830 return;
1831 }
1832
1833 pch::IdentID &ID = IdentifierIDs[II];
1834 if (ID == 0)
1835 ID = IdentifierIDs.size();
1836
1837 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001838}
1839
1840void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1841 if (T.isNull()) {
1842 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1843 return;
1844 }
1845
1846 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001847 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001848 switch (BT->getKind()) {
1849 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1850 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1851 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1852 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1853 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1854 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1855 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1856 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1857 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1858 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1859 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1860 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1861 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1862 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1863 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1864 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1865 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1866 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1867 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1868 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1869 }
1870
1871 Record.push_back((ID << 3) | T.getCVRQualifiers());
1872 return;
1873 }
1874
Douglas Gregor8038d512009-04-10 17:25:41 +00001875 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001876 if (ID == 0) // we haven't seen this type before
1877 ID = NextTypeID++;
1878
1879 // Encode the type qualifiers in the type reference.
1880 Record.push_back((ID << 3) | T.getCVRQualifiers());
1881}
1882
1883void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1884 if (D == 0) {
1885 Record.push_back(0);
1886 return;
1887 }
1888
Douglas Gregor8038d512009-04-10 17:25:41 +00001889 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001890 if (ID == 0) {
1891 // We haven't seen this declaration before. Give it a new ID and
1892 // enqueue it in the list of declarations to emit.
1893 ID = DeclIDs.size();
1894 DeclsToEmit.push(const_cast<Decl *>(D));
1895 }
1896
1897 Record.push_back(ID);
1898}
1899
1900void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1901 Record.push_back(Name.getNameKind());
1902 switch (Name.getNameKind()) {
1903 case DeclarationName::Identifier:
1904 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1905 break;
1906
1907 case DeclarationName::ObjCZeroArgSelector:
1908 case DeclarationName::ObjCOneArgSelector:
1909 case DeclarationName::ObjCMultiArgSelector:
1910 assert(false && "Serialization of Objective-C selectors unavailable");
1911 break;
1912
1913 case DeclarationName::CXXConstructorName:
1914 case DeclarationName::CXXDestructorName:
1915 case DeclarationName::CXXConversionFunctionName:
1916 AddTypeRef(Name.getCXXNameType(), Record);
1917 break;
1918
1919 case DeclarationName::CXXOperatorName:
1920 Record.push_back(Name.getCXXOverloadedOperator());
1921 break;
1922
1923 case DeclarationName::CXXUsingDirective:
1924 // No extra data to emit
1925 break;
1926 }
1927}
Douglas Gregor0b748912009-04-14 21:18:50 +00001928
Douglas Gregorc9490c02009-04-16 22:23:12 +00001929/// \brief Write the given substatement or subexpression to the
1930/// bitstream.
1931void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00001932 RecordData Record;
1933 PCHStmtWriter Writer(*this, Record);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001934 ++NumStatements;
Douglas Gregor087fd532009-04-14 23:32:43 +00001935
Douglas Gregorc9490c02009-04-16 22:23:12 +00001936 if (!S) {
1937 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001938 return;
1939 }
1940
Douglas Gregorc9490c02009-04-16 22:23:12 +00001941 Writer.Code = pch::STMT_NULL_PTR;
1942 Writer.Visit(S);
1943 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00001944 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001945 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001946}
1947
Douglas Gregorc9490c02009-04-16 22:23:12 +00001948/// \brief Flush all of the statements that have been added to the
1949/// queue via AddStmt().
1950void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00001951 RecordData Record;
1952 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001953
Douglas Gregorc9490c02009-04-16 22:23:12 +00001954 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor3e1af842009-04-17 22:13:46 +00001955 ++NumStatements;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001956 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00001957
Douglas Gregorc9490c02009-04-16 22:23:12 +00001958 if (!S) {
1959 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001960 continue;
1961 }
1962
Douglas Gregorc9490c02009-04-16 22:23:12 +00001963 Writer.Code = pch::STMT_NULL_PTR;
1964 Writer.Visit(S);
1965 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00001966 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001967 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001968
Douglas Gregorc9490c02009-04-16 22:23:12 +00001969 assert(N == StmtsToEmit.size() &&
1970 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00001971
1972 // Note that we are at the end of a full expression. Any
1973 // expression records that follow this one are part of a different
1974 // expression.
1975 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001976 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001977 }
Douglas Gregor087fd532009-04-14 23:32:43 +00001978
Douglas Gregorc9490c02009-04-16 22:23:12 +00001979 StmtsToEmit.clear();
Douglas Gregor0de9d882009-04-17 16:34:57 +00001980 SwitchCaseIDs.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00001981}
Douglas Gregor025452f2009-04-17 00:04:06 +00001982
1983unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1984 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1985 "SwitchCase recorded twice");
1986 unsigned NextID = SwitchCaseIDs.size();
1987 SwitchCaseIDs[S] = NextID;
1988 return NextID;
1989}
1990
1991unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1992 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1993 "SwitchCase hasn't been seen yet");
1994 return SwitchCaseIDs[S];
1995}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00001996
1997/// \brief Retrieve the ID for the given label statement, which may
1998/// or may not have been emitted yet.
1999unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2000 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2001 if (Pos != LabelIDs.end())
2002 return Pos->second;
2003
2004 unsigned NextID = LabelIDs.size();
2005 LabelIDs[S] = NextID;
2006 return NextID;
2007}