blob: 46db1428e31bc55d38576f9dd90ad5a297acf254 [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"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclContextInternals.h"
18#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000022#include "clang/Lex/MacroInfo.h"
23#include "clang/Lex/Preprocessor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000028#include "llvm/ADT/APFloat.h"
29#include "llvm/ADT/APInt.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000030#include "llvm/Bitcode/BitstreamWriter.h"
31#include "llvm/Support/Compiler.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "llvm/Support/MemoryBuffer.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000033#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Type serialization
38//===----------------------------------------------------------------------===//
39namespace {
40 class VISIBILITY_HIDDEN PCHTypeWriter {
41 PCHWriter &Writer;
42 PCHWriter::RecordData &Record;
43
44 public:
45 /// \brief Type code that corresponds to the record generated.
46 pch::TypeCode Code;
47
48 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
49 : Writer(Writer), Record(Record) { }
50
51 void VisitArrayType(const ArrayType *T);
52 void VisitFunctionType(const FunctionType *T);
53 void VisitTagType(const TagType *T);
54
55#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
56#define ABSTRACT_TYPE(Class, Base)
57#define DEPENDENT_TYPE(Class, Base)
58#include "clang/AST/TypeNodes.def"
59 };
60}
61
62void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
63 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
64 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
65 Record.push_back(T->getAddressSpace());
66 Code = pch::TYPE_EXT_QUAL;
67}
68
69void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
73void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
74 Record.push_back(T->getWidth());
75 Record.push_back(T->isSigned());
76 Code = pch::TYPE_FIXED_WIDTH_INT;
77}
78
79void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
80 Writer.AddTypeRef(T->getElementType(), Record);
81 Code = pch::TYPE_COMPLEX;
82}
83
84void PCHTypeWriter::VisitPointerType(const PointerType *T) {
85 Writer.AddTypeRef(T->getPointeeType(), Record);
86 Code = pch::TYPE_POINTER;
87}
88
89void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
90 Writer.AddTypeRef(T->getPointeeType(), Record);
91 Code = pch::TYPE_BLOCK_POINTER;
92}
93
94void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
95 Writer.AddTypeRef(T->getPointeeType(), Record);
96 Code = pch::TYPE_LVALUE_REFERENCE;
97}
98
99void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
100 Writer.AddTypeRef(T->getPointeeType(), Record);
101 Code = pch::TYPE_RVALUE_REFERENCE;
102}
103
104void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
105 Writer.AddTypeRef(T->getPointeeType(), Record);
106 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
107 Code = pch::TYPE_MEMBER_POINTER;
108}
109
110void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
111 Writer.AddTypeRef(T->getElementType(), Record);
112 Record.push_back(T->getSizeModifier()); // FIXME: stable values
113 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
114}
115
116void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
117 VisitArrayType(T);
118 Writer.AddAPInt(T->getSize(), Record);
119 Code = pch::TYPE_CONSTANT_ARRAY;
120}
121
122void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
123 VisitArrayType(T);
124 Code = pch::TYPE_INCOMPLETE_ARRAY;
125}
126
127void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
128 VisitArrayType(T);
Douglas Gregor0b748912009-04-14 21:18:50 +0000129 Writer.AddExpr(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 Code = pch::TYPE_VARIABLE_ARRAY;
131}
132
133void PCHTypeWriter::VisitVectorType(const VectorType *T) {
134 Writer.AddTypeRef(T->getElementType(), Record);
135 Record.push_back(T->getNumElements());
136 Code = pch::TYPE_VECTOR;
137}
138
139void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
140 VisitVectorType(T);
141 Code = pch::TYPE_EXT_VECTOR;
142}
143
144void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
145 Writer.AddTypeRef(T->getResultType(), Record);
146}
147
148void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
149 VisitFunctionType(T);
150 Code = pch::TYPE_FUNCTION_NO_PROTO;
151}
152
153void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
154 VisitFunctionType(T);
155 Record.push_back(T->getNumArgs());
156 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
157 Writer.AddTypeRef(T->getArgType(I), Record);
158 Record.push_back(T->isVariadic());
159 Record.push_back(T->getTypeQuals());
160 Code = pch::TYPE_FUNCTION_PROTO;
161}
162
163void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
164 Writer.AddDeclRef(T->getDecl(), Record);
165 Code = pch::TYPE_TYPEDEF;
166}
167
168void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000169 Writer.AddExpr(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000170 Code = pch::TYPE_TYPEOF_EXPR;
171}
172
173void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
174 Writer.AddTypeRef(T->getUnderlyingType(), Record);
175 Code = pch::TYPE_TYPEOF;
176}
177
178void PCHTypeWriter::VisitTagType(const TagType *T) {
179 Writer.AddDeclRef(T->getDecl(), Record);
180 assert(!T->isBeingDefined() &&
181 "Cannot serialize in the middle of a type definition");
182}
183
184void PCHTypeWriter::VisitRecordType(const RecordType *T) {
185 VisitTagType(T);
186 Code = pch::TYPE_RECORD;
187}
188
189void PCHTypeWriter::VisitEnumType(const EnumType *T) {
190 VisitTagType(T);
191 Code = pch::TYPE_ENUM;
192}
193
194void
195PCHTypeWriter::VisitTemplateSpecializationType(
196 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000197 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198 assert(false && "Cannot serialize template specialization types");
199}
200
201void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000202 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000203 assert(false && "Cannot serialize qualified name types");
204}
205
206void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
207 Writer.AddDeclRef(T->getDecl(), Record);
208 Code = pch::TYPE_OBJC_INTERFACE;
209}
210
211void
212PCHTypeWriter::VisitObjCQualifiedInterfaceType(
213 const ObjCQualifiedInterfaceType *T) {
214 VisitObjCInterfaceType(T);
215 Record.push_back(T->getNumProtocols());
216 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
217 Writer.AddDeclRef(T->getProtocol(I), Record);
218 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
219}
220
221void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
222 Record.push_back(T->getNumProtocols());
223 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
224 Writer.AddDeclRef(T->getProtocols(I), Record);
225 Code = pch::TYPE_OBJC_QUALIFIED_ID;
226}
227
228void
229PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) {
230 Record.push_back(T->getNumProtocols());
231 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
232 Writer.AddDeclRef(T->getProtocols(I), Record);
233 Code = pch::TYPE_OBJC_QUALIFIED_CLASS;
234}
235
236//===----------------------------------------------------------------------===//
237// Declaration serialization
238//===----------------------------------------------------------------------===//
239namespace {
240 class VISIBILITY_HIDDEN PCHDeclWriter
241 : public DeclVisitor<PCHDeclWriter, void> {
242
243 PCHWriter &Writer;
244 PCHWriter::RecordData &Record;
245
246 public:
247 pch::DeclCode Code;
248
249 PCHDeclWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
250 : Writer(Writer), Record(Record) { }
251
252 void VisitDecl(Decl *D);
253 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
254 void VisitNamedDecl(NamedDecl *D);
255 void VisitTypeDecl(TypeDecl *D);
256 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000257 void VisitTagDecl(TagDecl *D);
258 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000259 void VisitRecordDecl(RecordDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000260 void VisitValueDecl(ValueDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000261 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000262 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000263 void VisitFieldDecl(FieldDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264 void VisitVarDecl(VarDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000265 void VisitParmVarDecl(ParmVarDecl *D);
266 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor1028bc62009-04-13 22:49:25 +0000267 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
268 void VisitBlockDecl(BlockDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000269 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
270 uint64_t VisibleOffset);
271 };
272}
273
274void PCHDeclWriter::VisitDecl(Decl *D) {
275 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
276 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
277 Writer.AddSourceLocation(D->getLocation(), Record);
278 Record.push_back(D->isInvalidDecl());
279 // FIXME: hasAttrs
280 Record.push_back(D->isImplicit());
281 Record.push_back(D->getAccess());
282}
283
284void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
285 VisitDecl(D);
286 Code = pch::DECL_TRANSLATION_UNIT;
287}
288
289void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
290 VisitDecl(D);
291 Writer.AddDeclarationName(D->getDeclName(), Record);
292}
293
294void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
295 VisitNamedDecl(D);
296 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
297}
298
299void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
300 VisitTypeDecl(D);
301 Writer.AddTypeRef(D->getUnderlyingType(), Record);
302 Code = pch::DECL_TYPEDEF;
303}
304
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000305void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
306 VisitTypeDecl(D);
307 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
308 Record.push_back(D->isDefinition());
309 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
310}
311
312void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
313 VisitTagDecl(D);
314 Writer.AddTypeRef(D->getIntegerType(), Record);
315 Code = pch::DECL_ENUM;
316}
317
Douglas Gregor8c700062009-04-13 21:20:57 +0000318void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
319 VisitTagDecl(D);
320 Record.push_back(D->hasFlexibleArrayMember());
321 Record.push_back(D->isAnonymousStructOrUnion());
322 Code = pch::DECL_RECORD;
323}
324
Douglas Gregor2cf26342009-04-09 22:27:44 +0000325void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
326 VisitNamedDecl(D);
327 Writer.AddTypeRef(D->getType(), Record);
328}
329
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000330void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
331 VisitValueDecl(D);
Douglas Gregor0b748912009-04-14 21:18:50 +0000332 Record.push_back(D->getInitExpr()? 1 : 0);
333 if (D->getInitExpr())
334 Writer.AddExpr(D->getInitExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000335 Writer.AddAPSInt(D->getInitVal(), Record);
336 Code = pch::DECL_ENUM_CONSTANT;
337}
338
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000339void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
340 VisitValueDecl(D);
341 // FIXME: function body
342 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
343 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
344 Record.push_back(D->isInline());
345 Record.push_back(D->isVirtual());
346 Record.push_back(D->isPure());
347 Record.push_back(D->inheritedPrototype());
348 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
349 Record.push_back(D->isDeleted());
350 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
351 Record.push_back(D->param_size());
352 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
353 P != PEnd; ++P)
354 Writer.AddDeclRef(*P, Record);
355 Code = pch::DECL_FUNCTION;
356}
357
Douglas Gregor8c700062009-04-13 21:20:57 +0000358void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
359 VisitValueDecl(D);
360 Record.push_back(D->isMutable());
Douglas Gregor0b748912009-04-14 21:18:50 +0000361 Record.push_back(D->getBitWidth()? 1 : 0);
362 if (D->getBitWidth())
363 Writer.AddExpr(D->getBitWidth());
Douglas Gregor8c700062009-04-13 21:20:57 +0000364 Code = pch::DECL_FIELD;
365}
366
Douglas Gregor2cf26342009-04-09 22:27:44 +0000367void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
368 VisitValueDecl(D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000369 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor2cf26342009-04-09 22:27:44 +0000370 Record.push_back(D->isThreadSpecified());
371 Record.push_back(D->hasCXXDirectInitializer());
372 Record.push_back(D->isDeclaredInCondition());
373 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
374 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregor0b748912009-04-14 21:18:50 +0000375 Record.push_back(D->getInit()? 1 : 0);
376 if (D->getInit())
377 Writer.AddExpr(D->getInit());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000378 Code = pch::DECL_VAR;
379}
380
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000381void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
382 VisitVarDecl(D);
383 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000384 // FIXME: emit default argument (C++)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000385 // FIXME: why isn't the "default argument" just stored as the initializer
386 // in VarDecl?
387 Code = pch::DECL_PARM_VAR;
388}
389
390void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
391 VisitParmVarDecl(D);
392 Writer.AddTypeRef(D->getOriginalType(), Record);
393 Code = pch::DECL_ORIGINAL_PARM_VAR;
394}
395
Douglas Gregor1028bc62009-04-13 22:49:25 +0000396void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
397 VisitDecl(D);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000398 Writer.AddExpr(D->getAsmString());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000399 Code = pch::DECL_FILE_SCOPE_ASM;
400}
401
402void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
403 VisitDecl(D);
404 // FIXME: emit block body
405 Record.push_back(D->param_size());
406 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
407 P != PEnd; ++P)
408 Writer.AddDeclRef(*P, Record);
409 Code = pch::DECL_BLOCK;
410}
411
Douglas Gregor2cf26342009-04-09 22:27:44 +0000412/// \brief Emit the DeclContext part of a declaration context decl.
413///
414/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
415/// block for this declaration context is stored. May be 0 to indicate
416/// that there are no declarations stored within this context.
417///
418/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
419/// block for this declaration context is stored. May be 0 to indicate
420/// that there are no declarations visible from this context. Note
421/// that this value will not be emitted for non-primary declaration
422/// contexts.
423void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
424 uint64_t VisibleOffset) {
425 Record.push_back(LexicalOffset);
426 if (DC->getPrimaryContext() == DC)
427 Record.push_back(VisibleOffset);
428}
429
430//===----------------------------------------------------------------------===//
Douglas Gregor0b748912009-04-14 21:18:50 +0000431// Statement/expression serialization
432//===----------------------------------------------------------------------===//
433namespace {
434 class VISIBILITY_HIDDEN PCHStmtWriter
435 : public StmtVisitor<PCHStmtWriter, void> {
436
437 PCHWriter &Writer;
438 PCHWriter::RecordData &Record;
439
440 public:
441 pch::StmtCode Code;
442
443 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
444 : Writer(Writer), Record(Record) { }
445
446 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000447 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000448 void VisitDeclRefExpr(DeclRefExpr *E);
449 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000450 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000451 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000452 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000453 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000454 void VisitUnaryOperator(UnaryOperator *E);
455 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000456 void VisitCallExpr(CallExpr *E);
457 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000458 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000459 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000460 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000461 void VisitExplicitCastExpr(ExplicitCastExpr *E);
462 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000463 };
464}
465
466void PCHStmtWriter::VisitExpr(Expr *E) {
467 Writer.AddTypeRef(E->getType(), Record);
468 Record.push_back(E->isTypeDependent());
469 Record.push_back(E->isValueDependent());
470}
471
Douglas Gregor17fc2232009-04-14 21:55:33 +0000472void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
473 VisitExpr(E);
474 Writer.AddSourceLocation(E->getLocation(), Record);
475 Record.push_back(E->getIdentType()); // FIXME: stable encoding
476 Code = pch::EXPR_PREDEFINED;
477}
478
Douglas Gregor0b748912009-04-14 21:18:50 +0000479void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
480 VisitExpr(E);
481 Writer.AddDeclRef(E->getDecl(), Record);
482 Writer.AddSourceLocation(E->getLocation(), Record);
483 Code = pch::EXPR_DECL_REF;
484}
485
486void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
487 VisitExpr(E);
488 Writer.AddSourceLocation(E->getLocation(), Record);
489 Writer.AddAPInt(E->getValue(), Record);
490 Code = pch::EXPR_INTEGER_LITERAL;
491}
492
Douglas Gregor17fc2232009-04-14 21:55:33 +0000493void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
494 VisitExpr(E);
495 Writer.AddAPFloat(E->getValue(), Record);
496 Record.push_back(E->isExact());
497 Writer.AddSourceLocation(E->getLocation(), Record);
498 Code = pch::EXPR_FLOATING_LITERAL;
499}
500
Douglas Gregor673ecd62009-04-15 16:35:07 +0000501void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
502 VisitExpr(E);
503 Record.push_back(E->getByteLength());
504 Record.push_back(E->getNumConcatenated());
505 Record.push_back(E->isWide());
506 // FIXME: String data should be stored as a blob at the end of the
507 // StringLiteral. However, we can't do so now because we have no
508 // provision for coping with abbreviations when we're jumping around
509 // the PCH file during deserialization.
510 Record.insert(Record.end(),
511 E->getStrData(), E->getStrData() + E->getByteLength());
512 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
513 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
514 Code = pch::EXPR_STRING_LITERAL;
515}
516
Douglas Gregor0b748912009-04-14 21:18:50 +0000517void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
518 VisitExpr(E);
519 Record.push_back(E->getValue());
520 Writer.AddSourceLocation(E->getLoc(), Record);
521 Record.push_back(E->isWide());
522 Code = pch::EXPR_CHARACTER_LITERAL;
523}
524
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000525void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
526 VisitExpr(E);
527 Writer.AddSourceLocation(E->getLParen(), Record);
528 Writer.AddSourceLocation(E->getRParen(), Record);
529 Writer.WriteSubExpr(E->getSubExpr());
530 Code = pch::EXPR_PAREN;
531}
532
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000533void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
534 VisitExpr(E);
535 Writer.WriteSubExpr(E->getSubExpr());
536 Record.push_back(E->getOpcode()); // FIXME: stable encoding
537 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
538 Code = pch::EXPR_UNARY_OPERATOR;
539}
540
541void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
542 VisitExpr(E);
543 Record.push_back(E->isSizeOf());
544 if (E->isArgumentType())
545 Writer.AddTypeRef(E->getArgumentType(), Record);
546 else {
547 Record.push_back(0);
548 Writer.WriteSubExpr(E->getArgumentExpr());
549 }
550 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
551 Writer.AddSourceLocation(E->getRParenLoc(), Record);
552 Code = pch::EXPR_SIZEOF_ALIGN_OF;
553}
554
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000555void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
556 VisitExpr(E);
557 Record.push_back(E->getNumArgs());
558 Writer.AddSourceLocation(E->getRParenLoc(), Record);
559 Writer.WriteSubExpr(E->getCallee());
560 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
561 Arg != ArgEnd; ++Arg)
562 Writer.WriteSubExpr(*Arg);
563 Code = pch::EXPR_CALL;
564}
565
566void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
567 VisitExpr(E);
568 Writer.WriteSubExpr(E->getBase());
569 Writer.AddDeclRef(E->getMemberDecl(), Record);
570 Writer.AddSourceLocation(E->getMemberLoc(), Record);
571 Record.push_back(E->isArrow());
572 Code = pch::EXPR_MEMBER;
573}
574
Douglas Gregor087fd532009-04-14 23:32:43 +0000575void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
576 VisitExpr(E);
577 Writer.WriteSubExpr(E->getSubExpr());
578}
579
Douglas Gregordb600c32009-04-15 00:25:59 +0000580void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
581 VisitExpr(E);
582 Writer.WriteSubExpr(E->getLHS());
583 Writer.WriteSubExpr(E->getRHS());
584 Record.push_back(E->getOpcode()); // FIXME: stable encoding
585 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
586 Code = pch::EXPR_BINARY_OPERATOR;
587}
588
Douglas Gregor087fd532009-04-14 23:32:43 +0000589void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
590 VisitCastExpr(E);
591 Record.push_back(E->isLvalueCast());
592 Code = pch::EXPR_IMPLICIT_CAST;
593}
594
Douglas Gregordb600c32009-04-15 00:25:59 +0000595void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
596 VisitCastExpr(E);
597 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
598}
599
600void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
601 VisitExplicitCastExpr(E);
602 Writer.AddSourceLocation(E->getLParenLoc(), Record);
603 Writer.AddSourceLocation(E->getRParenLoc(), Record);
604 Code = pch::EXPR_CSTYLE_CAST;
605}
606
Douglas Gregor0b748912009-04-14 21:18:50 +0000607//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000608// PCHWriter Implementation
609//===----------------------------------------------------------------------===//
610
Douglas Gregor2bec0412009-04-10 21:16:55 +0000611/// \brief Write the target triple (e.g., i686-apple-darwin9).
612void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
613 using namespace llvm;
614 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
615 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
617 unsigned TripleAbbrev = S.EmitAbbrev(Abbrev);
618
619 RecordData Record;
620 Record.push_back(pch::TARGET_TRIPLE);
621 const char *Triple = Target.getTargetTriple();
622 S.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
623}
624
625/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000626void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
627 RecordData Record;
628 Record.push_back(LangOpts.Trigraphs);
629 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
630 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
631 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
632 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
633 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
634 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
635 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
636 Record.push_back(LangOpts.C99); // C99 Support
637 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
638 Record.push_back(LangOpts.CPlusPlus); // C++ Support
639 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
640 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
641 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
642
643 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
644 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
645 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
646
647 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
648 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
649 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
650 Record.push_back(LangOpts.LaxVectorConversions);
651 Record.push_back(LangOpts.Exceptions); // Support exception handling.
652
653 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
654 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
655 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
656
657 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
658 // by locks.
659 Record.push_back(LangOpts.Blocks); // block extension to C
660 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
661 // they are unused.
662 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
663 // (modulo the platform support).
664
665 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
666 // signed integer arithmetic overflows.
667
668 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
669 // may be ripped out at any time.
670
671 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
672 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
673 // defined.
674 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
675 // opposed to __DYNAMIC__).
676 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
677
678 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
679 // used (instead of C99 semantics).
680 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
681 Record.push_back(LangOpts.getGCMode());
682 Record.push_back(LangOpts.getVisibilityMode());
683 Record.push_back(LangOpts.InstantiationDepth);
684 S.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
685}
686
Douglas Gregor14f79002009-04-10 03:52:48 +0000687//===----------------------------------------------------------------------===//
688// Source Manager Serialization
689//===----------------------------------------------------------------------===//
690
691/// \brief Create an abbreviation for the SLocEntry that refers to a
692/// file.
693static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) {
694 using namespace llvm;
695 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
696 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
697 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
698 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
699 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
700 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000701 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
702 return S.EmitAbbrev(Abbrev);
703}
704
705/// \brief Create an abbreviation for the SLocEntry that refers to a
706/// buffer.
707static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &S) {
708 using namespace llvm;
709 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
710 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
714 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
715 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
716 return S.EmitAbbrev(Abbrev);
717}
718
719/// \brief Create an abbreviation for the SLocEntry that refers to a
720/// buffer's blob.
721static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &S) {
722 using namespace llvm;
723 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
724 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
725 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
726 return S.EmitAbbrev(Abbrev);
727}
728
729/// \brief Create an abbreviation for the SLocEntry that refers to an
730/// buffer.
731static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &S) {
732 using namespace llvm;
733 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
734 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor14f79002009-04-10 03:52:48 +0000740 return S.EmitAbbrev(Abbrev);
741}
742
743/// \brief Writes the block containing the serialized form of the
744/// source manager.
745///
746/// TODO: We should probably use an on-disk hash table (stored in a
747/// blob), indexed based on the file name, so that we only create
748/// entries for files that we actually need. In the common case (no
749/// errors), we probably won't have to create file entries for any of
750/// the files in the AST.
751void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +0000752 // Enter the source manager block.
Douglas Gregor14f79002009-04-10 03:52:48 +0000753 S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
754
755 // Abbreviations for the various kinds of source-location entries.
756 int SLocFileAbbrv = -1;
757 int SLocBufferAbbrv = -1;
758 int SLocBufferBlobAbbrv = -1;
759 int SLocInstantiationAbbrv = -1;
760
761 // Write out the source location entry table. We skip the first
762 // entry, which is always the same dummy entry.
763 RecordData Record;
764 for (SourceManager::sloc_entry_iterator
765 SLoc = SourceMgr.sloc_entry_begin() + 1,
766 SLocEnd = SourceMgr.sloc_entry_end();
767 SLoc != SLocEnd; ++SLoc) {
768 // Figure out which record code to use.
769 unsigned Code;
770 if (SLoc->isFile()) {
771 if (SLoc->getFile().getContentCache()->Entry)
772 Code = pch::SM_SLOC_FILE_ENTRY;
773 else
774 Code = pch::SM_SLOC_BUFFER_ENTRY;
775 } else
776 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
777 Record.push_back(Code);
778
779 Record.push_back(SLoc->getOffset());
780 if (SLoc->isFile()) {
781 const SrcMgr::FileInfo &File = SLoc->getFile();
782 Record.push_back(File.getIncludeLoc().getRawEncoding());
783 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +0000784 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +0000785
786 const SrcMgr::ContentCache *Content = File.getContentCache();
787 if (Content->Entry) {
788 // The source location entry is a file. The blob associated
789 // with this entry is the file name.
790 if (SLocFileAbbrv == -1)
791 SLocFileAbbrv = CreateSLocFileAbbrev(S);
792 S.EmitRecordWithBlob(SLocFileAbbrv, Record,
793 Content->Entry->getName(),
794 strlen(Content->Entry->getName()));
795 } else {
796 // The source location entry is a buffer. The blob associated
797 // with this entry contains the contents of the buffer.
798 if (SLocBufferAbbrv == -1) {
799 SLocBufferAbbrv = CreateSLocBufferAbbrev(S);
800 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S);
801 }
802
803 // We add one to the size so that we capture the trailing NULL
804 // that is required by llvm::MemoryBuffer::getMemBuffer (on
805 // the reader side).
806 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
807 const char *Name = Buffer->getBufferIdentifier();
808 S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
809 Record.clear();
810 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
811 S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
812 Buffer->getBufferStart(),
813 Buffer->getBufferSize() + 1);
814 }
815 } else {
816 // The source location entry is an instantiation.
817 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
818 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
819 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
820 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
821
Douglas Gregorf60e9912009-04-15 18:05:10 +0000822 // Compute the token length for this macro expansion.
823 unsigned NextOffset = SourceMgr.getNextOffset();
824 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
825 if (++NextSLoc != SLocEnd)
826 NextOffset = NextSLoc->getOffset();
827 Record.push_back(NextOffset - SLoc->getOffset() - 1);
828
Douglas Gregor14f79002009-04-10 03:52:48 +0000829 if (SLocInstantiationAbbrv == -1)
830 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S);
831 S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
832 }
833
834 Record.clear();
835 }
836
Douglas Gregorbd945002009-04-13 16:31:14 +0000837 // Write the line table.
838 if (SourceMgr.hasLineTable()) {
839 LineTableInfo &LineTable = SourceMgr.getLineTable();
840
841 // Emit the file names
842 Record.push_back(LineTable.getNumFilenames());
843 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
844 // Emit the file name
845 const char *Filename = LineTable.getFilename(I);
846 unsigned FilenameLen = Filename? strlen(Filename) : 0;
847 Record.push_back(FilenameLen);
848 if (FilenameLen)
849 Record.insert(Record.end(), Filename, Filename + FilenameLen);
850 }
851
852 // Emit the line entries
853 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
854 L != LEnd; ++L) {
855 // Emit the file ID
856 Record.push_back(L->first);
857
858 // Emit the line entries
859 Record.push_back(L->second.size());
860 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
861 LEEnd = L->second.end();
862 LE != LEEnd; ++LE) {
863 Record.push_back(LE->FileOffset);
864 Record.push_back(LE->LineNo);
865 Record.push_back(LE->FilenameID);
866 Record.push_back((unsigned)LE->FileKind);
867 Record.push_back(LE->IncludeOffset);
868 }
869 S.EmitRecord(pch::SM_LINE_TABLE, Record);
870 }
871 }
872
Douglas Gregor14f79002009-04-10 03:52:48 +0000873 S.ExitBlock();
874}
875
Chris Lattner0b1fb982009-04-10 17:15:23 +0000876/// \brief Writes the block containing the serialized form of the
877/// preprocessor.
878///
Chris Lattnerdf961c22009-04-10 18:08:30 +0000879void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +0000880 // Enter the preprocessor block.
881 S.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
882
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000883 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
884 // FIXME: use diagnostics subsystem for localization etc.
885 if (PP.SawDateOrTime())
886 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +0000887
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000888 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +0000889
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000890 // If the preprocessor __COUNTER__ value has been bumped, remember it.
891 if (PP.getCounterValue() != 0) {
892 Record.push_back(PP.getCounterValue());
893 S.EmitRecord(pch::PP_COUNTER_VALUE, Record);
894 Record.clear();
895 }
896
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000897 // Loop over all the macro definitions that are live at the end of the file,
898 // emitting each to the PP section.
899 // FIXME: Eventually we want to emit an index so that we can lazily load
900 // macros.
901 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
902 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +0000903 // FIXME: This emits macros in hash table order, we should do it in a stable
904 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000905 MacroInfo *MI = I->second;
906
907 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
908 // been redefined by the header (in which case they are not isBuiltinMacro).
909 if (MI->isBuiltinMacro())
910 continue;
911
Chris Lattner7356a312009-04-11 21:15:38 +0000912 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000913 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
914 Record.push_back(MI->isUsed());
915
916 unsigned Code;
917 if (MI->isObjectLike()) {
918 Code = pch::PP_MACRO_OBJECT_LIKE;
919 } else {
920 Code = pch::PP_MACRO_FUNCTION_LIKE;
921
922 Record.push_back(MI->isC99Varargs());
923 Record.push_back(MI->isGNUVarargs());
924 Record.push_back(MI->getNumArgs());
925 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
926 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +0000927 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000928 }
929 S.EmitRecord(Code, Record);
930 Record.clear();
931
Chris Lattnerdf961c22009-04-10 18:08:30 +0000932 // Emit the tokens array.
933 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
934 // Note that we know that the preprocessor does not have any annotation
935 // tokens in it because they are created by the parser, and thus can't be
936 // in a macro definition.
937 const Token &Tok = MI->getReplacementToken(TokNo);
938
939 Record.push_back(Tok.getLocation().getRawEncoding());
940 Record.push_back(Tok.getLength());
941
Chris Lattnerdf961c22009-04-10 18:08:30 +0000942 // FIXME: When reading literal tokens, reconstruct the literal pointer if
943 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +0000944 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +0000945
946 // FIXME: Should translate token kind to a stable encoding.
947 Record.push_back(Tok.getKind());
948 // FIXME: Should translate token flags to a stable encoding.
949 Record.push_back(Tok.getFlags());
950
951 S.EmitRecord(pch::PP_TOKEN, Record);
952 Record.clear();
953 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000954
955 }
956
Chris Lattnerf04ad692009-04-10 17:16:57 +0000957 S.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +0000958}
959
960
Douglas Gregor2cf26342009-04-09 22:27:44 +0000961/// \brief Write the representation of a type to the PCH stream.
962void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +0000963 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +0000964 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000965 ID = NextTypeID++;
966
967 // Record the offset for this type.
968 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
969 TypeOffsets.push_back(S.GetCurrentBitNo());
970 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
971 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
972 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo();
973 }
974
975 RecordData Record;
976
977 // Emit the type's representation.
978 PCHTypeWriter W(*this, Record);
979 switch (T->getTypeClass()) {
980 // For all of the concrete, non-dependent types, call the
981 // appropriate visitor function.
982#define TYPE(Class, Base) \
983 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
984#define ABSTRACT_TYPE(Class, Base)
985#define DEPENDENT_TYPE(Class, Base)
986#include "clang/AST/TypeNodes.def"
987
988 // For all of the dependent type nodes (which only occur in C++
989 // templates), produce an error.
990#define TYPE(Class, Base)
991#define DEPENDENT_TYPE(Class, Base) case Type::Class:
992#include "clang/AST/TypeNodes.def"
993 assert(false && "Cannot serialize dependent type nodes");
994 break;
995 }
996
997 // Emit the serialized record.
998 S.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +0000999
1000 // Flush any expressions that were written as part of this type.
1001 FlushExprs();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001002}
1003
1004/// \brief Write a block containing all of the types.
1005void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001006 // Enter the types block.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001007 S.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
1008
1009 // Emit all of the types in the ASTContext
1010 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1011 TEnd = Context.getTypes().end();
1012 T != TEnd; ++T) {
1013 // Builtin types are never serialized.
1014 if (isa<BuiltinType>(*T))
1015 continue;
1016
1017 WriteType(*T);
1018 }
1019
1020 // Exit the types block
1021 S.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001022}
1023
1024/// \brief Write the block containing all of the declaration IDs
1025/// lexically declared within the given DeclContext.
1026///
1027/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1028/// bistream, or 0 if no block was written.
1029uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1030 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001031 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001032 return 0;
1033
1034 uint64_t Offset = S.GetCurrentBitNo();
1035 RecordData Record;
1036 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1037 DEnd = DC->decls_end(Context);
1038 D != DEnd; ++D)
1039 AddDeclRef(*D, Record);
1040
1041 S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
1042 return Offset;
1043}
1044
1045/// \brief Write the block containing all of the declaration IDs
1046/// visible from the given DeclContext.
1047///
1048/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1049/// bistream, or 0 if no block was written.
1050uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1051 DeclContext *DC) {
1052 if (DC->getPrimaryContext() != DC)
1053 return 0;
1054
1055 // Force the DeclContext to build a its name-lookup table.
1056 DC->lookup(Context, DeclarationName());
1057
1058 // Serialize the contents of the mapping used for lookup. Note that,
1059 // although we have two very different code paths, the serialized
1060 // representation is the same for both cases: a declaration name,
1061 // followed by a size, followed by references to the visible
1062 // declarations that have that name.
1063 uint64_t Offset = S.GetCurrentBitNo();
1064 RecordData Record;
1065 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001066 if (!Map)
1067 return 0;
1068
Douglas Gregor2cf26342009-04-09 22:27:44 +00001069 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1070 D != DEnd; ++D) {
1071 AddDeclarationName(D->first, Record);
1072 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1073 Record.push_back(Result.second - Result.first);
1074 for(; Result.first != Result.second; ++Result.first)
1075 AddDeclRef(*Result.first, Record);
1076 }
1077
1078 if (Record.size() == 0)
1079 return 0;
1080
1081 S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
1082 return Offset;
1083}
1084
1085/// \brief Write a block containing all of the declarations.
1086void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001087 // Enter the declarations block.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001088 S.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
1089
1090 // Emit all of the declarations.
1091 RecordData Record;
1092 PCHDeclWriter W(*this, Record);
1093 while (!DeclsToEmit.empty()) {
1094 // Pull the next declaration off the queue
1095 Decl *D = DeclsToEmit.front();
1096 DeclsToEmit.pop();
1097
1098 // If this declaration is also a DeclContext, write blocks for the
1099 // declarations that lexically stored inside its context and those
1100 // declarations that are visible from its context. These blocks
1101 // are written before the declaration itself so that we can put
1102 // their offsets into the record for the declaration.
1103 uint64_t LexicalOffset = 0;
1104 uint64_t VisibleOffset = 0;
1105 DeclContext *DC = dyn_cast<DeclContext>(D);
1106 if (DC) {
1107 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1108 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1109 }
1110
1111 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001112 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001113 if (ID == 0)
1114 ID = DeclIDs.size();
1115
1116 unsigned Index = ID - 1;
1117
1118 // Record the offset for this declaration
1119 if (DeclOffsets.size() == Index)
1120 DeclOffsets.push_back(S.GetCurrentBitNo());
1121 else if (DeclOffsets.size() < Index) {
1122 DeclOffsets.resize(Index+1);
1123 DeclOffsets[Index] = S.GetCurrentBitNo();
1124 }
1125
1126 // Build and emit a record for this declaration
1127 Record.clear();
1128 W.Code = (pch::DeclCode)0;
1129 W.Visit(D);
1130 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001131 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregor2cf26342009-04-09 22:27:44 +00001132 S.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001133
Douglas Gregor0b748912009-04-14 21:18:50 +00001134 // Flush any expressions that were written as part of this declaration.
1135 FlushExprs();
1136
Douglas Gregorfdd01722009-04-14 00:24:19 +00001137 // Note external declarations so that we can add them to a record
1138 // in the PCH file later.
1139 if (isa<FileScopeAsmDecl>(D))
1140 ExternalDefinitions.push_back(ID);
1141 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1142 if (// Non-static file-scope variables with initializers or that
1143 // are tentative definitions.
1144 (Var->isFileVarDecl() &&
1145 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1146 // Out-of-line definitions of static data members (C++).
1147 (Var->getDeclContext()->isRecord() &&
1148 !Var->getLexicalDeclContext()->isRecord() &&
1149 Var->getStorageClass() == VarDecl::Static))
1150 ExternalDefinitions.push_back(ID);
1151 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1152 if (Func->isThisDeclarationADefinition() &&
1153 Func->getStorageClass() != FunctionDecl::Static &&
1154 !Func->isInline())
1155 ExternalDefinitions.push_back(ID);
1156 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001157 }
1158
1159 // Exit the declarations block
1160 S.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001161}
1162
Douglas Gregorafaf3082009-04-11 00:14:32 +00001163/// \brief Write the identifier table into the PCH file.
1164///
1165/// The identifier table consists of a blob containing string data
1166/// (the actual identifiers themselves) and a separate "offsets" index
1167/// that maps identifier IDs to locations within the blob.
1168void PCHWriter::WriteIdentifierTable() {
1169 using namespace llvm;
1170
1171 // Create and write out the blob that contains the identifier
1172 // strings.
1173 RecordData IdentOffsets;
1174 IdentOffsets.resize(IdentifierIDs.size());
1175 {
1176 // Create the identifier string data.
1177 std::vector<char> Data;
1178 Data.push_back(0); // Data must not be empty.
1179 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1180 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1181 ID != IDEnd; ++ID) {
1182 assert(ID->first && "NULL identifier in identifier table");
1183
1184 // Make sure we're starting on an odd byte. The PCH reader
1185 // expects the low bit to be set on all of the offsets.
1186 if ((Data.size() & 0x01) == 0)
1187 Data.push_back((char)0);
1188
1189 IdentOffsets[ID->second - 1] = Data.size();
1190 Data.insert(Data.end(),
1191 ID->first->getName(),
1192 ID->first->getName() + ID->first->getLength());
1193 Data.push_back((char)0);
1194 }
1195
1196 // Create a blob abbreviation
1197 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1198 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
1200 unsigned IDTableAbbrev = S.EmitAbbrev(Abbrev);
1201
1202 // Write the identifier table
1203 RecordData Record;
1204 Record.push_back(pch::IDENTIFIER_TABLE);
1205 S.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
1206 }
1207
1208 // Write the offsets table for identifier IDs.
1209 S.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
1210}
1211
Douglas Gregor2cf26342009-04-09 22:27:44 +00001212PCHWriter::PCHWriter(llvm::BitstreamWriter &S)
1213 : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
1214
Chris Lattnerdf961c22009-04-10 18:08:30 +00001215void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001216 // Emit the file header.
1217 S.Emit((unsigned)'C', 8);
1218 S.Emit((unsigned)'P', 8);
1219 S.Emit((unsigned)'C', 8);
1220 S.Emit((unsigned)'H', 8);
1221
1222 // The translation unit is the first declaration we'll emit.
1223 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1224 DeclsToEmit.push(Context.getTranslationUnitDecl());
1225
1226 // Write the remaining PCH contents.
Douglas Gregor2bec0412009-04-10 21:16:55 +00001227 S.EnterSubblock(pch::PCH_BLOCK_ID, 3);
1228 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001229 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001230 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001231 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001232 WriteTypesBlock(Context);
1233 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001234 WriteIdentifierTable();
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001235 S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1236 S.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001237 if (!ExternalDefinitions.empty())
1238 S.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001239 S.ExitBlock();
1240}
1241
1242void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1243 Record.push_back(Loc.getRawEncoding());
1244}
1245
1246void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1247 Record.push_back(Value.getBitWidth());
1248 unsigned N = Value.getNumWords();
1249 const uint64_t* Words = Value.getRawData();
1250 for (unsigned I = 0; I != N; ++I)
1251 Record.push_back(Words[I]);
1252}
1253
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001254void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1255 Record.push_back(Value.isUnsigned());
1256 AddAPInt(Value, Record);
1257}
1258
Douglas Gregor17fc2232009-04-14 21:55:33 +00001259void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1260 AddAPInt(Value.bitcastToAPInt(), Record);
1261}
1262
Douglas Gregor2cf26342009-04-09 22:27:44 +00001263void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001264 if (II == 0) {
1265 Record.push_back(0);
1266 return;
1267 }
1268
1269 pch::IdentID &ID = IdentifierIDs[II];
1270 if (ID == 0)
1271 ID = IdentifierIDs.size();
1272
1273 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001274}
1275
1276void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1277 if (T.isNull()) {
1278 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1279 return;
1280 }
1281
1282 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001283 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001284 switch (BT->getKind()) {
1285 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1286 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1287 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1288 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1289 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1290 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1291 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1292 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1293 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1294 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1295 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1296 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1297 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1298 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1299 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1300 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1301 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1302 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1303 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1304 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1305 }
1306
1307 Record.push_back((ID << 3) | T.getCVRQualifiers());
1308 return;
1309 }
1310
Douglas Gregor8038d512009-04-10 17:25:41 +00001311 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001312 if (ID == 0) // we haven't seen this type before
1313 ID = NextTypeID++;
1314
1315 // Encode the type qualifiers in the type reference.
1316 Record.push_back((ID << 3) | T.getCVRQualifiers());
1317}
1318
1319void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1320 if (D == 0) {
1321 Record.push_back(0);
1322 return;
1323 }
1324
Douglas Gregor8038d512009-04-10 17:25:41 +00001325 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001326 if (ID == 0) {
1327 // We haven't seen this declaration before. Give it a new ID and
1328 // enqueue it in the list of declarations to emit.
1329 ID = DeclIDs.size();
1330 DeclsToEmit.push(const_cast<Decl *>(D));
1331 }
1332
1333 Record.push_back(ID);
1334}
1335
1336void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1337 Record.push_back(Name.getNameKind());
1338 switch (Name.getNameKind()) {
1339 case DeclarationName::Identifier:
1340 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1341 break;
1342
1343 case DeclarationName::ObjCZeroArgSelector:
1344 case DeclarationName::ObjCOneArgSelector:
1345 case DeclarationName::ObjCMultiArgSelector:
1346 assert(false && "Serialization of Objective-C selectors unavailable");
1347 break;
1348
1349 case DeclarationName::CXXConstructorName:
1350 case DeclarationName::CXXDestructorName:
1351 case DeclarationName::CXXConversionFunctionName:
1352 AddTypeRef(Name.getCXXNameType(), Record);
1353 break;
1354
1355 case DeclarationName::CXXOperatorName:
1356 Record.push_back(Name.getCXXOverloadedOperator());
1357 break;
1358
1359 case DeclarationName::CXXUsingDirective:
1360 // No extra data to emit
1361 break;
1362 }
1363}
Douglas Gregor0b748912009-04-14 21:18:50 +00001364
Douglas Gregor087fd532009-04-14 23:32:43 +00001365/// \brief Write the given subexpression to the bitstream.
1366void PCHWriter::WriteSubExpr(Expr *E) {
1367 RecordData Record;
1368 PCHStmtWriter Writer(*this, Record);
1369
1370 if (!E) {
1371 S.EmitRecord(pch::EXPR_NULL, Record);
1372 return;
1373 }
1374
1375 Writer.Code = pch::EXPR_NULL;
1376 Writer.Visit(E);
1377 assert(Writer.Code != pch::EXPR_NULL &&
1378 "Unhandled expression writing PCH file");
1379 S.EmitRecord(Writer.Code, Record);
1380}
1381
Douglas Gregor0b748912009-04-14 21:18:50 +00001382/// \brief Flush all of the expressions that have been added to the
1383/// queue via AddExpr().
1384void PCHWriter::FlushExprs() {
1385 RecordData Record;
1386 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001387
Douglas Gregor087fd532009-04-14 23:32:43 +00001388 for (unsigned I = 0, N = ExprsToEmit.size(); I != N; ++I) {
1389 Expr *E = ExprsToEmit[I];
1390
Douglas Gregor0b748912009-04-14 21:18:50 +00001391 if (!E) {
1392 S.EmitRecord(pch::EXPR_NULL, Record);
1393 continue;
1394 }
1395
1396 Writer.Code = pch::EXPR_NULL;
1397 Writer.Visit(E);
1398 assert(Writer.Code != pch::EXPR_NULL &&
1399 "Unhandled expression writing PCH file");
1400 S.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001401
1402 assert(N == ExprsToEmit.size() &&
1403 "Subexpression writen via AddExpr rather than WriteSubExpr!");
1404
1405 // Note that we are at the end of a full expression. Any
1406 // expression records that follow this one are part of a different
1407 // expression.
1408 Record.clear();
1409 S.EmitRecord(pch::EXPR_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001410 }
Douglas Gregor087fd532009-04-14 23:32:43 +00001411
1412 ExprsToEmit.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00001413}