blob: 0d514016692c52699c322d8c758b34c179a7796a [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 Gregorc9490c02009-04-16 22:23:12 +0000129 Writer.AddStmt(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 Gregorc9490c02009-04-16 22:23:12 +0000169 Writer.AddStmt(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());
Douglas Gregor68a2eb02009-04-15 21:30:51 +0000279 Record.push_back(D->hasAttrs());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000280 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())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000334 Writer.AddStmt(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);
Douglas Gregor025452f2009-04-17 00:04:06 +0000341 Record.push_back(D->isThisDeclarationADefinition());
342 if (D->isThisDeclarationADefinition())
343 Writer.AddStmt(D->getBody());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000344 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
345 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
346 Record.push_back(D->isInline());
347 Record.push_back(D->isVirtual());
348 Record.push_back(D->isPure());
349 Record.push_back(D->inheritedPrototype());
350 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
351 Record.push_back(D->isDeleted());
352 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
353 Record.push_back(D->param_size());
354 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
355 P != PEnd; ++P)
356 Writer.AddDeclRef(*P, Record);
357 Code = pch::DECL_FUNCTION;
358}
359
Douglas Gregor8c700062009-04-13 21:20:57 +0000360void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
361 VisitValueDecl(D);
362 Record.push_back(D->isMutable());
Douglas Gregor0b748912009-04-14 21:18:50 +0000363 Record.push_back(D->getBitWidth()? 1 : 0);
364 if (D->getBitWidth())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000365 Writer.AddStmt(D->getBitWidth());
Douglas Gregor8c700062009-04-13 21:20:57 +0000366 Code = pch::DECL_FIELD;
367}
368
Douglas Gregor2cf26342009-04-09 22:27:44 +0000369void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
370 VisitValueDecl(D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000371 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor2cf26342009-04-09 22:27:44 +0000372 Record.push_back(D->isThreadSpecified());
373 Record.push_back(D->hasCXXDirectInitializer());
374 Record.push_back(D->isDeclaredInCondition());
375 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
376 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregor0b748912009-04-14 21:18:50 +0000377 Record.push_back(D->getInit()? 1 : 0);
378 if (D->getInit())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000379 Writer.AddStmt(D->getInit());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000380 Code = pch::DECL_VAR;
381}
382
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000383void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
384 VisitVarDecl(D);
385 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000386 // FIXME: emit default argument (C++)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000387 // FIXME: why isn't the "default argument" just stored as the initializer
388 // in VarDecl?
389 Code = pch::DECL_PARM_VAR;
390}
391
392void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
393 VisitParmVarDecl(D);
394 Writer.AddTypeRef(D->getOriginalType(), Record);
395 Code = pch::DECL_ORIGINAL_PARM_VAR;
396}
397
Douglas Gregor1028bc62009-04-13 22:49:25 +0000398void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
399 VisitDecl(D);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000400 Writer.AddStmt(D->getAsmString());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000401 Code = pch::DECL_FILE_SCOPE_ASM;
402}
403
404void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
405 VisitDecl(D);
406 // FIXME: emit block body
407 Record.push_back(D->param_size());
408 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
409 P != PEnd; ++P)
410 Writer.AddDeclRef(*P, Record);
411 Code = pch::DECL_BLOCK;
412}
413
Douglas Gregor2cf26342009-04-09 22:27:44 +0000414/// \brief Emit the DeclContext part of a declaration context decl.
415///
416/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
417/// block for this declaration context is stored. May be 0 to indicate
418/// that there are no declarations stored within this context.
419///
420/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
421/// block for this declaration context is stored. May be 0 to indicate
422/// that there are no declarations visible from this context. Note
423/// that this value will not be emitted for non-primary declaration
424/// contexts.
425void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
426 uint64_t VisibleOffset) {
427 Record.push_back(LexicalOffset);
428 if (DC->getPrimaryContext() == DC)
429 Record.push_back(VisibleOffset);
430}
431
432//===----------------------------------------------------------------------===//
Douglas Gregor0b748912009-04-14 21:18:50 +0000433// Statement/expression serialization
434//===----------------------------------------------------------------------===//
435namespace {
436 class VISIBILITY_HIDDEN PCHStmtWriter
437 : public StmtVisitor<PCHStmtWriter, void> {
438
439 PCHWriter &Writer;
440 PCHWriter::RecordData &Record;
441
442 public:
443 pch::StmtCode Code;
444
445 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
446 : Writer(Writer), Record(Record) { }
447
Douglas Gregor025452f2009-04-17 00:04:06 +0000448 void VisitStmt(Stmt *S);
449 void VisitNullStmt(NullStmt *S);
450 void VisitCompoundStmt(CompoundStmt *S);
451 void VisitSwitchCase(SwitchCase *S);
452 void VisitCaseStmt(CaseStmt *S);
453 void VisitDefaultStmt(DefaultStmt *S);
454 void VisitIfStmt(IfStmt *S);
455 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000456 void VisitWhileStmt(WhileStmt *S);
457 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000458 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor0b748912009-04-14 21:18:50 +0000459 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000460 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000461 void VisitDeclRefExpr(DeclRefExpr *E);
462 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000463 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000464 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000465 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000466 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000467 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000468 void VisitUnaryOperator(UnaryOperator *E);
469 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000470 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000471 void VisitCallExpr(CallExpr *E);
472 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000473 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000474 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000475 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
476 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000477 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000478 void VisitExplicitCastExpr(ExplicitCastExpr *E);
479 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000480 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000481 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000482 void VisitInitListExpr(InitListExpr *E);
483 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
484 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000485 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000486 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
487 void VisitChooseExpr(ChooseExpr *E);
488 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000489 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
490 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000491 };
492}
493
Douglas Gregor025452f2009-04-17 00:04:06 +0000494void PCHStmtWriter::VisitStmt(Stmt *S) {
495}
496
497void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
498 VisitStmt(S);
499 Writer.AddSourceLocation(S->getSemiLoc(), Record);
500 Code = pch::STMT_NULL;
501}
502
503void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
504 VisitStmt(S);
505 Record.push_back(S->size());
506 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
507 CS != CSEnd; ++CS)
508 Writer.WriteSubStmt(*CS);
509 Writer.AddSourceLocation(S->getLBracLoc(), Record);
510 Writer.AddSourceLocation(S->getRBracLoc(), Record);
511 Code = pch::STMT_COMPOUND;
512}
513
514void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
515 VisitStmt(S);
516 Record.push_back(Writer.RecordSwitchCaseID(S));
517}
518
519void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
520 VisitSwitchCase(S);
521 Writer.WriteSubStmt(S->getLHS());
522 Writer.WriteSubStmt(S->getRHS());
523 Writer.WriteSubStmt(S->getSubStmt());
524 Writer.AddSourceLocation(S->getCaseLoc(), Record);
525 Code = pch::STMT_CASE;
526}
527
528void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
529 VisitSwitchCase(S);
530 Writer.WriteSubStmt(S->getSubStmt());
531 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
532 Code = pch::STMT_DEFAULT;
533}
534
535void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
536 VisitStmt(S);
537 Writer.WriteSubStmt(S->getCond());
538 Writer.WriteSubStmt(S->getThen());
539 Writer.WriteSubStmt(S->getElse());
540 Writer.AddSourceLocation(S->getIfLoc(), Record);
541 Code = pch::STMT_IF;
542}
543
544void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
545 VisitStmt(S);
546 Writer.WriteSubStmt(S->getCond());
547 Writer.WriteSubStmt(S->getBody());
548 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
549 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
550 SC = SC->getNextSwitchCase())
551 Record.push_back(Writer.getSwitchCaseID(SC));
552 Code = pch::STMT_SWITCH;
553}
554
Douglas Gregord921cf92009-04-17 00:16:09 +0000555void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
556 VisitStmt(S);
557 Writer.WriteSubStmt(S->getCond());
558 Writer.WriteSubStmt(S->getBody());
559 Writer.AddSourceLocation(S->getWhileLoc(), Record);
560 Code = pch::STMT_WHILE;
561}
562
563void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
564 VisitStmt(S);
565 Writer.AddSourceLocation(S->getContinueLoc(), Record);
566 Code = pch::STMT_CONTINUE;
567}
568
Douglas Gregor025452f2009-04-17 00:04:06 +0000569void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
570 VisitStmt(S);
571 Writer.AddSourceLocation(S->getBreakLoc(), Record);
572 Code = pch::STMT_BREAK;
573}
574
Douglas Gregor0b748912009-04-14 21:18:50 +0000575void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000576 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000577 Writer.AddTypeRef(E->getType(), Record);
578 Record.push_back(E->isTypeDependent());
579 Record.push_back(E->isValueDependent());
580}
581
Douglas Gregor17fc2232009-04-14 21:55:33 +0000582void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
583 VisitExpr(E);
584 Writer.AddSourceLocation(E->getLocation(), Record);
585 Record.push_back(E->getIdentType()); // FIXME: stable encoding
586 Code = pch::EXPR_PREDEFINED;
587}
588
Douglas Gregor0b748912009-04-14 21:18:50 +0000589void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
590 VisitExpr(E);
591 Writer.AddDeclRef(E->getDecl(), Record);
592 Writer.AddSourceLocation(E->getLocation(), Record);
593 Code = pch::EXPR_DECL_REF;
594}
595
596void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
597 VisitExpr(E);
598 Writer.AddSourceLocation(E->getLocation(), Record);
599 Writer.AddAPInt(E->getValue(), Record);
600 Code = pch::EXPR_INTEGER_LITERAL;
601}
602
Douglas Gregor17fc2232009-04-14 21:55:33 +0000603void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
604 VisitExpr(E);
605 Writer.AddAPFloat(E->getValue(), Record);
606 Record.push_back(E->isExact());
607 Writer.AddSourceLocation(E->getLocation(), Record);
608 Code = pch::EXPR_FLOATING_LITERAL;
609}
610
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000611void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
612 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000613 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000614 Code = pch::EXPR_IMAGINARY_LITERAL;
615}
616
Douglas Gregor673ecd62009-04-15 16:35:07 +0000617void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
618 VisitExpr(E);
619 Record.push_back(E->getByteLength());
620 Record.push_back(E->getNumConcatenated());
621 Record.push_back(E->isWide());
622 // FIXME: String data should be stored as a blob at the end of the
623 // StringLiteral. However, we can't do so now because we have no
624 // provision for coping with abbreviations when we're jumping around
625 // the PCH file during deserialization.
626 Record.insert(Record.end(),
627 E->getStrData(), E->getStrData() + E->getByteLength());
628 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
629 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
630 Code = pch::EXPR_STRING_LITERAL;
631}
632
Douglas Gregor0b748912009-04-14 21:18:50 +0000633void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
634 VisitExpr(E);
635 Record.push_back(E->getValue());
636 Writer.AddSourceLocation(E->getLoc(), Record);
637 Record.push_back(E->isWide());
638 Code = pch::EXPR_CHARACTER_LITERAL;
639}
640
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000641void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
642 VisitExpr(E);
643 Writer.AddSourceLocation(E->getLParen(), Record);
644 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000645 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000646 Code = pch::EXPR_PAREN;
647}
648
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000649void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
650 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000651 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000652 Record.push_back(E->getOpcode()); // FIXME: stable encoding
653 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
654 Code = pch::EXPR_UNARY_OPERATOR;
655}
656
657void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
658 VisitExpr(E);
659 Record.push_back(E->isSizeOf());
660 if (E->isArgumentType())
661 Writer.AddTypeRef(E->getArgumentType(), Record);
662 else {
663 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000664 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000665 }
666 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
667 Writer.AddSourceLocation(E->getRParenLoc(), Record);
668 Code = pch::EXPR_SIZEOF_ALIGN_OF;
669}
670
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000671void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
672 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000673 Writer.WriteSubStmt(E->getLHS());
674 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000675 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
676 Code = pch::EXPR_ARRAY_SUBSCRIPT;
677}
678
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000679void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
680 VisitExpr(E);
681 Record.push_back(E->getNumArgs());
682 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000683 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000684 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
685 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000686 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000687 Code = pch::EXPR_CALL;
688}
689
690void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
691 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000692 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000693 Writer.AddDeclRef(E->getMemberDecl(), Record);
694 Writer.AddSourceLocation(E->getMemberLoc(), Record);
695 Record.push_back(E->isArrow());
696 Code = pch::EXPR_MEMBER;
697}
698
Douglas Gregor087fd532009-04-14 23:32:43 +0000699void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
700 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000701 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000702}
703
Douglas Gregordb600c32009-04-15 00:25:59 +0000704void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
705 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000706 Writer.WriteSubStmt(E->getLHS());
707 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000708 Record.push_back(E->getOpcode()); // FIXME: stable encoding
709 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
710 Code = pch::EXPR_BINARY_OPERATOR;
711}
712
Douglas Gregorad90e962009-04-15 22:40:36 +0000713void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
714 VisitBinaryOperator(E);
715 Writer.AddTypeRef(E->getComputationLHSType(), Record);
716 Writer.AddTypeRef(E->getComputationResultType(), Record);
717 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
718}
719
720void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
721 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000722 Writer.WriteSubStmt(E->getCond());
723 Writer.WriteSubStmt(E->getLHS());
724 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000725 Code = pch::EXPR_CONDITIONAL_OPERATOR;
726}
727
Douglas Gregor087fd532009-04-14 23:32:43 +0000728void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
729 VisitCastExpr(E);
730 Record.push_back(E->isLvalueCast());
731 Code = pch::EXPR_IMPLICIT_CAST;
732}
733
Douglas Gregordb600c32009-04-15 00:25:59 +0000734void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
735 VisitCastExpr(E);
736 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
737}
738
739void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
740 VisitExplicitCastExpr(E);
741 Writer.AddSourceLocation(E->getLParenLoc(), Record);
742 Writer.AddSourceLocation(E->getRParenLoc(), Record);
743 Code = pch::EXPR_CSTYLE_CAST;
744}
745
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000746void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
747 VisitExpr(E);
748 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000749 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000750 Record.push_back(E->isFileScope());
751 Code = pch::EXPR_COMPOUND_LITERAL;
752}
753
Douglas Gregord3c98a02009-04-15 23:02:49 +0000754void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
755 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000756 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000757 Writer.AddIdentifierRef(&E->getAccessor(), Record);
758 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
759 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
760}
761
Douglas Gregord077d752009-04-16 00:55:48 +0000762void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
763 VisitExpr(E);
764 Record.push_back(E->getNumInits());
765 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000766 Writer.WriteSubStmt(E->getInit(I));
767 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +0000768 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
769 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
770 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
771 Record.push_back(E->hadArrayRangeDesignator());
772 Code = pch::EXPR_INIT_LIST;
773}
774
775void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
776 VisitExpr(E);
777 Record.push_back(E->getNumSubExprs());
778 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000779 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +0000780 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
781 Record.push_back(E->usesGNUSyntax());
782 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
783 DEnd = E->designators_end();
784 D != DEnd; ++D) {
785 if (D->isFieldDesignator()) {
786 if (FieldDecl *Field = D->getField()) {
787 Record.push_back(pch::DESIG_FIELD_DECL);
788 Writer.AddDeclRef(Field, Record);
789 } else {
790 Record.push_back(pch::DESIG_FIELD_NAME);
791 Writer.AddIdentifierRef(D->getFieldName(), Record);
792 }
793 Writer.AddSourceLocation(D->getDotLoc(), Record);
794 Writer.AddSourceLocation(D->getFieldLoc(), Record);
795 } else if (D->isArrayDesignator()) {
796 Record.push_back(pch::DESIG_ARRAY);
797 Record.push_back(D->getFirstExprIndex());
798 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
799 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
800 } else {
801 assert(D->isArrayRangeDesignator() && "Unknown designator");
802 Record.push_back(pch::DESIG_ARRAY_RANGE);
803 Record.push_back(D->getFirstExprIndex());
804 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
805 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
806 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
807 }
808 }
809 Code = pch::EXPR_DESIGNATED_INIT;
810}
811
812void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
813 VisitExpr(E);
814 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
815}
816
Douglas Gregord3c98a02009-04-15 23:02:49 +0000817void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
818 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000819 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000820 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
821 Writer.AddSourceLocation(E->getRParenLoc(), Record);
822 Code = pch::EXPR_VA_ARG;
823}
824
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000825void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
826 VisitExpr(E);
827 Writer.AddTypeRef(E->getArgType1(), Record);
828 Writer.AddTypeRef(E->getArgType2(), Record);
829 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
830 Writer.AddSourceLocation(E->getRParenLoc(), Record);
831 Code = pch::EXPR_TYPES_COMPATIBLE;
832}
833
834void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
835 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000836 Writer.WriteSubStmt(E->getCond());
837 Writer.WriteSubStmt(E->getLHS());
838 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000839 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
840 Writer.AddSourceLocation(E->getRParenLoc(), Record);
841 Code = pch::EXPR_CHOOSE;
842}
843
844void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
845 VisitExpr(E);
846 Writer.AddSourceLocation(E->getTokenLocation(), Record);
847 Code = pch::EXPR_GNU_NULL;
848}
849
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000850void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
851 VisitExpr(E);
852 Record.push_back(E->getNumSubExprs());
853 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000854 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000855 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
856 Writer.AddSourceLocation(E->getRParenLoc(), Record);
857 Code = pch::EXPR_SHUFFLE_VECTOR;
858}
859
860void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
861 VisitExpr(E);
862 Writer.AddDeclRef(E->getDecl(), Record);
863 Writer.AddSourceLocation(E->getLocation(), Record);
864 Record.push_back(E->isByRef());
865 Code = pch::EXPR_BLOCK_DECL_REF;
866}
867
Douglas Gregor0b748912009-04-14 21:18:50 +0000868//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000869// PCHWriter Implementation
870//===----------------------------------------------------------------------===//
871
Douglas Gregor2bec0412009-04-10 21:16:55 +0000872/// \brief Write the target triple (e.g., i686-apple-darwin9).
873void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
874 using namespace llvm;
875 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
876 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000878 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +0000879
880 RecordData Record;
881 Record.push_back(pch::TARGET_TRIPLE);
882 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +0000883 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +0000884}
885
886/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000887void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
888 RecordData Record;
889 Record.push_back(LangOpts.Trigraphs);
890 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
891 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
892 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
893 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
894 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
895 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
896 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
897 Record.push_back(LangOpts.C99); // C99 Support
898 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
899 Record.push_back(LangOpts.CPlusPlus); // C++ Support
900 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
901 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
902 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
903
904 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
905 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
906 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
907
908 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
909 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
910 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
911 Record.push_back(LangOpts.LaxVectorConversions);
912 Record.push_back(LangOpts.Exceptions); // Support exception handling.
913
914 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
915 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
916 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
917
918 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
919 // by locks.
920 Record.push_back(LangOpts.Blocks); // block extension to C
921 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
922 // they are unused.
923 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
924 // (modulo the platform support).
925
926 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
927 // signed integer arithmetic overflows.
928
929 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
930 // may be ripped out at any time.
931
932 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
933 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
934 // defined.
935 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
936 // opposed to __DYNAMIC__).
937 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
938
939 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
940 // used (instead of C99 semantics).
941 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
942 Record.push_back(LangOpts.getGCMode());
943 Record.push_back(LangOpts.getVisibilityMode());
944 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000945 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000946}
947
Douglas Gregor14f79002009-04-10 03:52:48 +0000948//===----------------------------------------------------------------------===//
949// Source Manager Serialization
950//===----------------------------------------------------------------------===//
951
952/// \brief Create an abbreviation for the SLocEntry that refers to a
953/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000954static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000955 using namespace llvm;
956 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
957 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000963 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000964}
965
966/// \brief Create an abbreviation for the SLocEntry that refers to a
967/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000968static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000969 using namespace llvm;
970 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
971 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000977 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000978}
979
980/// \brief Create an abbreviation for the SLocEntry that refers to a
981/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000982static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000983 using namespace llvm;
984 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
985 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
986 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000987 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000988}
989
990/// \brief Create an abbreviation for the SLocEntry that refers to an
991/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000992static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000993 using namespace llvm;
994 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
995 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001001 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001002}
1003
1004/// \brief Writes the block containing the serialized form of the
1005/// source manager.
1006///
1007/// TODO: We should probably use an on-disk hash table (stored in a
1008/// blob), indexed based on the file name, so that we only create
1009/// entries for files that we actually need. In the common case (no
1010/// errors), we probably won't have to create file entries for any of
1011/// the files in the AST.
1012void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001013 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001014 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001015
1016 // Abbreviations for the various kinds of source-location entries.
1017 int SLocFileAbbrv = -1;
1018 int SLocBufferAbbrv = -1;
1019 int SLocBufferBlobAbbrv = -1;
1020 int SLocInstantiationAbbrv = -1;
1021
1022 // Write out the source location entry table. We skip the first
1023 // entry, which is always the same dummy entry.
1024 RecordData Record;
1025 for (SourceManager::sloc_entry_iterator
1026 SLoc = SourceMgr.sloc_entry_begin() + 1,
1027 SLocEnd = SourceMgr.sloc_entry_end();
1028 SLoc != SLocEnd; ++SLoc) {
1029 // Figure out which record code to use.
1030 unsigned Code;
1031 if (SLoc->isFile()) {
1032 if (SLoc->getFile().getContentCache()->Entry)
1033 Code = pch::SM_SLOC_FILE_ENTRY;
1034 else
1035 Code = pch::SM_SLOC_BUFFER_ENTRY;
1036 } else
1037 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1038 Record.push_back(Code);
1039
1040 Record.push_back(SLoc->getOffset());
1041 if (SLoc->isFile()) {
1042 const SrcMgr::FileInfo &File = SLoc->getFile();
1043 Record.push_back(File.getIncludeLoc().getRawEncoding());
1044 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001045 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001046
1047 const SrcMgr::ContentCache *Content = File.getContentCache();
1048 if (Content->Entry) {
1049 // The source location entry is a file. The blob associated
1050 // with this entry is the file name.
1051 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001052 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1053 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001054 Content->Entry->getName(),
1055 strlen(Content->Entry->getName()));
1056 } else {
1057 // The source location entry is a buffer. The blob associated
1058 // with this entry contains the contents of the buffer.
1059 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001060 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1061 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001062 }
1063
1064 // We add one to the size so that we capture the trailing NULL
1065 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1066 // the reader side).
1067 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1068 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001069 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001070 Record.clear();
1071 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001072 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001073 Buffer->getBufferStart(),
1074 Buffer->getBufferSize() + 1);
1075 }
1076 } else {
1077 // The source location entry is an instantiation.
1078 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1079 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1080 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1081 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1082
Douglas Gregorf60e9912009-04-15 18:05:10 +00001083 // Compute the token length for this macro expansion.
1084 unsigned NextOffset = SourceMgr.getNextOffset();
1085 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1086 if (++NextSLoc != SLocEnd)
1087 NextOffset = NextSLoc->getOffset();
1088 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1089
Douglas Gregor14f79002009-04-10 03:52:48 +00001090 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001091 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1092 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001093 }
1094
1095 Record.clear();
1096 }
1097
Douglas Gregorbd945002009-04-13 16:31:14 +00001098 // Write the line table.
1099 if (SourceMgr.hasLineTable()) {
1100 LineTableInfo &LineTable = SourceMgr.getLineTable();
1101
1102 // Emit the file names
1103 Record.push_back(LineTable.getNumFilenames());
1104 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1105 // Emit the file name
1106 const char *Filename = LineTable.getFilename(I);
1107 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1108 Record.push_back(FilenameLen);
1109 if (FilenameLen)
1110 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1111 }
1112
1113 // Emit the line entries
1114 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1115 L != LEnd; ++L) {
1116 // Emit the file ID
1117 Record.push_back(L->first);
1118
1119 // Emit the line entries
1120 Record.push_back(L->second.size());
1121 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1122 LEEnd = L->second.end();
1123 LE != LEEnd; ++LE) {
1124 Record.push_back(LE->FileOffset);
1125 Record.push_back(LE->LineNo);
1126 Record.push_back(LE->FilenameID);
1127 Record.push_back((unsigned)LE->FileKind);
1128 Record.push_back(LE->IncludeOffset);
1129 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001130 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001131 }
1132 }
1133
Douglas Gregorc9490c02009-04-16 22:23:12 +00001134 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001135}
1136
Chris Lattner0b1fb982009-04-10 17:15:23 +00001137/// \brief Writes the block containing the serialized form of the
1138/// preprocessor.
1139///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001140void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001141 // Enter the preprocessor block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001142 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001143
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001144 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1145 // FIXME: use diagnostics subsystem for localization etc.
1146 if (PP.SawDateOrTime())
1147 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001148
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001149 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001150
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001151 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1152 if (PP.getCounterValue() != 0) {
1153 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001154 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001155 Record.clear();
1156 }
1157
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001158 // Loop over all the macro definitions that are live at the end of the file,
1159 // emitting each to the PP section.
1160 // FIXME: Eventually we want to emit an index so that we can lazily load
1161 // macros.
1162 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1163 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001164 // FIXME: This emits macros in hash table order, we should do it in a stable
1165 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001166 MacroInfo *MI = I->second;
1167
1168 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1169 // been redefined by the header (in which case they are not isBuiltinMacro).
1170 if (MI->isBuiltinMacro())
1171 continue;
1172
Chris Lattner7356a312009-04-11 21:15:38 +00001173 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001174 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1175 Record.push_back(MI->isUsed());
1176
1177 unsigned Code;
1178 if (MI->isObjectLike()) {
1179 Code = pch::PP_MACRO_OBJECT_LIKE;
1180 } else {
1181 Code = pch::PP_MACRO_FUNCTION_LIKE;
1182
1183 Record.push_back(MI->isC99Varargs());
1184 Record.push_back(MI->isGNUVarargs());
1185 Record.push_back(MI->getNumArgs());
1186 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1187 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001188 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001189 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001190 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001191 Record.clear();
1192
Chris Lattnerdf961c22009-04-10 18:08:30 +00001193 // Emit the tokens array.
1194 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1195 // Note that we know that the preprocessor does not have any annotation
1196 // tokens in it because they are created by the parser, and thus can't be
1197 // in a macro definition.
1198 const Token &Tok = MI->getReplacementToken(TokNo);
1199
1200 Record.push_back(Tok.getLocation().getRawEncoding());
1201 Record.push_back(Tok.getLength());
1202
Chris Lattnerdf961c22009-04-10 18:08:30 +00001203 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1204 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001205 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001206
1207 // FIXME: Should translate token kind to a stable encoding.
1208 Record.push_back(Tok.getKind());
1209 // FIXME: Should translate token flags to a stable encoding.
1210 Record.push_back(Tok.getFlags());
1211
Douglas Gregorc9490c02009-04-16 22:23:12 +00001212 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001213 Record.clear();
1214 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001215
1216 }
1217
Douglas Gregorc9490c02009-04-16 22:23:12 +00001218 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001219}
1220
1221
Douglas Gregor2cf26342009-04-09 22:27:44 +00001222/// \brief Write the representation of a type to the PCH stream.
1223void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001224 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001225 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001226 ID = NextTypeID++;
1227
1228 // Record the offset for this type.
1229 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001230 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001231 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1232 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001233 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001234 }
1235
1236 RecordData Record;
1237
1238 // Emit the type's representation.
1239 PCHTypeWriter W(*this, Record);
1240 switch (T->getTypeClass()) {
1241 // For all of the concrete, non-dependent types, call the
1242 // appropriate visitor function.
1243#define TYPE(Class, Base) \
1244 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1245#define ABSTRACT_TYPE(Class, Base)
1246#define DEPENDENT_TYPE(Class, Base)
1247#include "clang/AST/TypeNodes.def"
1248
1249 // For all of the dependent type nodes (which only occur in C++
1250 // templates), produce an error.
1251#define TYPE(Class, Base)
1252#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1253#include "clang/AST/TypeNodes.def"
1254 assert(false && "Cannot serialize dependent type nodes");
1255 break;
1256 }
1257
1258 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001259 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001260
1261 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001262 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001263}
1264
1265/// \brief Write a block containing all of the types.
1266void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001267 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001268 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001269
1270 // Emit all of the types in the ASTContext
1271 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1272 TEnd = Context.getTypes().end();
1273 T != TEnd; ++T) {
1274 // Builtin types are never serialized.
1275 if (isa<BuiltinType>(*T))
1276 continue;
1277
1278 WriteType(*T);
1279 }
1280
1281 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001282 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001283}
1284
1285/// \brief Write the block containing all of the declaration IDs
1286/// lexically declared within the given DeclContext.
1287///
1288/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1289/// bistream, or 0 if no block was written.
1290uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1291 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001292 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001293 return 0;
1294
Douglas Gregorc9490c02009-04-16 22:23:12 +00001295 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001296 RecordData Record;
1297 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1298 DEnd = DC->decls_end(Context);
1299 D != DEnd; ++D)
1300 AddDeclRef(*D, Record);
1301
Douglas Gregorc9490c02009-04-16 22:23:12 +00001302 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001303 return Offset;
1304}
1305
1306/// \brief Write the block containing all of the declaration IDs
1307/// visible from the given DeclContext.
1308///
1309/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1310/// bistream, or 0 if no block was written.
1311uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1312 DeclContext *DC) {
1313 if (DC->getPrimaryContext() != DC)
1314 return 0;
1315
1316 // Force the DeclContext to build a its name-lookup table.
1317 DC->lookup(Context, DeclarationName());
1318
1319 // Serialize the contents of the mapping used for lookup. Note that,
1320 // although we have two very different code paths, the serialized
1321 // representation is the same for both cases: a declaration name,
1322 // followed by a size, followed by references to the visible
1323 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001324 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001325 RecordData Record;
1326 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001327 if (!Map)
1328 return 0;
1329
Douglas Gregor2cf26342009-04-09 22:27:44 +00001330 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1331 D != DEnd; ++D) {
1332 AddDeclarationName(D->first, Record);
1333 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1334 Record.push_back(Result.second - Result.first);
1335 for(; Result.first != Result.second; ++Result.first)
1336 AddDeclRef(*Result.first, Record);
1337 }
1338
1339 if (Record.size() == 0)
1340 return 0;
1341
Douglas Gregorc9490c02009-04-16 22:23:12 +00001342 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001343 return Offset;
1344}
1345
1346/// \brief Write a block containing all of the declarations.
1347void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001348 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001349 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001350
1351 // Emit all of the declarations.
1352 RecordData Record;
1353 PCHDeclWriter W(*this, Record);
1354 while (!DeclsToEmit.empty()) {
1355 // Pull the next declaration off the queue
1356 Decl *D = DeclsToEmit.front();
1357 DeclsToEmit.pop();
1358
1359 // If this declaration is also a DeclContext, write blocks for the
1360 // declarations that lexically stored inside its context and those
1361 // declarations that are visible from its context. These blocks
1362 // are written before the declaration itself so that we can put
1363 // their offsets into the record for the declaration.
1364 uint64_t LexicalOffset = 0;
1365 uint64_t VisibleOffset = 0;
1366 DeclContext *DC = dyn_cast<DeclContext>(D);
1367 if (DC) {
1368 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1369 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1370 }
1371
1372 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001373 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001374 if (ID == 0)
1375 ID = DeclIDs.size();
1376
1377 unsigned Index = ID - 1;
1378
1379 // Record the offset for this declaration
1380 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001381 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001382 else if (DeclOffsets.size() < Index) {
1383 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001384 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001385 }
1386
1387 // Build and emit a record for this declaration
1388 Record.clear();
1389 W.Code = (pch::DeclCode)0;
1390 W.Visit(D);
1391 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001392 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001393 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001394
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001395 // If the declaration had any attributes, write them now.
1396 if (D->hasAttrs())
1397 WriteAttributeRecord(D->getAttrs());
1398
Douglas Gregor0b748912009-04-14 21:18:50 +00001399 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001400 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001401
Douglas Gregorfdd01722009-04-14 00:24:19 +00001402 // Note external declarations so that we can add them to a record
1403 // in the PCH file later.
1404 if (isa<FileScopeAsmDecl>(D))
1405 ExternalDefinitions.push_back(ID);
1406 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1407 if (// Non-static file-scope variables with initializers or that
1408 // are tentative definitions.
1409 (Var->isFileVarDecl() &&
1410 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1411 // Out-of-line definitions of static data members (C++).
1412 (Var->getDeclContext()->isRecord() &&
1413 !Var->getLexicalDeclContext()->isRecord() &&
1414 Var->getStorageClass() == VarDecl::Static))
1415 ExternalDefinitions.push_back(ID);
1416 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1417 if (Func->isThisDeclarationADefinition() &&
1418 Func->getStorageClass() != FunctionDecl::Static &&
1419 !Func->isInline())
1420 ExternalDefinitions.push_back(ID);
1421 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001422 }
1423
1424 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001425 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001426}
1427
Douglas Gregorafaf3082009-04-11 00:14:32 +00001428/// \brief Write the identifier table into the PCH file.
1429///
1430/// The identifier table consists of a blob containing string data
1431/// (the actual identifiers themselves) and a separate "offsets" index
1432/// that maps identifier IDs to locations within the blob.
1433void PCHWriter::WriteIdentifierTable() {
1434 using namespace llvm;
1435
1436 // Create and write out the blob that contains the identifier
1437 // strings.
1438 RecordData IdentOffsets;
1439 IdentOffsets.resize(IdentifierIDs.size());
1440 {
1441 // Create the identifier string data.
1442 std::vector<char> Data;
1443 Data.push_back(0); // Data must not be empty.
1444 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1445 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1446 ID != IDEnd; ++ID) {
1447 assert(ID->first && "NULL identifier in identifier table");
1448
1449 // Make sure we're starting on an odd byte. The PCH reader
1450 // expects the low bit to be set on all of the offsets.
1451 if ((Data.size() & 0x01) == 0)
1452 Data.push_back((char)0);
1453
1454 IdentOffsets[ID->second - 1] = Data.size();
1455 Data.insert(Data.end(),
1456 ID->first->getName(),
1457 ID->first->getName() + ID->first->getLength());
1458 Data.push_back((char)0);
1459 }
1460
1461 // Create a blob abbreviation
1462 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1463 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1464 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001465 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001466
1467 // Write the identifier table
1468 RecordData Record;
1469 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001470 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001471 }
1472
1473 // Write the offsets table for identifier IDs.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001474 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001475}
1476
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001477/// \brief Write a record containing the given attributes.
1478void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1479 RecordData Record;
1480 for (; Attr; Attr = Attr->getNext()) {
1481 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1482 Record.push_back(Attr->isInherited());
1483 switch (Attr->getKind()) {
1484 case Attr::Alias:
1485 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1486 break;
1487
1488 case Attr::Aligned:
1489 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1490 break;
1491
1492 case Attr::AlwaysInline:
1493 break;
1494
1495 case Attr::AnalyzerNoReturn:
1496 break;
1497
1498 case Attr::Annotate:
1499 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1500 break;
1501
1502 case Attr::AsmLabel:
1503 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1504 break;
1505
1506 case Attr::Blocks:
1507 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1508 break;
1509
1510 case Attr::Cleanup:
1511 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1512 break;
1513
1514 case Attr::Const:
1515 break;
1516
1517 case Attr::Constructor:
1518 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1519 break;
1520
1521 case Attr::DLLExport:
1522 case Attr::DLLImport:
1523 case Attr::Deprecated:
1524 break;
1525
1526 case Attr::Destructor:
1527 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1528 break;
1529
1530 case Attr::FastCall:
1531 break;
1532
1533 case Attr::Format: {
1534 const FormatAttr *Format = cast<FormatAttr>(Attr);
1535 AddString(Format->getType(), Record);
1536 Record.push_back(Format->getFormatIdx());
1537 Record.push_back(Format->getFirstArg());
1538 break;
1539 }
1540
1541 case Attr::GNUCInline:
1542 case Attr::IBOutletKind:
1543 case Attr::NoReturn:
1544 case Attr::NoThrow:
1545 case Attr::Nodebug:
1546 case Attr::Noinline:
1547 break;
1548
1549 case Attr::NonNull: {
1550 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1551 Record.push_back(NonNull->size());
1552 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1553 break;
1554 }
1555
1556 case Attr::ObjCException:
1557 case Attr::ObjCNSObject:
1558 case Attr::Overloadable:
1559 break;
1560
1561 case Attr::Packed:
1562 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1563 break;
1564
1565 case Attr::Pure:
1566 break;
1567
1568 case Attr::Regparm:
1569 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1570 break;
1571
1572 case Attr::Section:
1573 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1574 break;
1575
1576 case Attr::StdCall:
1577 case Attr::TransparentUnion:
1578 case Attr::Unavailable:
1579 case Attr::Unused:
1580 case Attr::Used:
1581 break;
1582
1583 case Attr::Visibility:
1584 // FIXME: stable encoding
1585 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1586 break;
1587
1588 case Attr::WarnUnusedResult:
1589 case Attr::Weak:
1590 case Attr::WeakImport:
1591 break;
1592 }
1593 }
1594
Douglas Gregorc9490c02009-04-16 22:23:12 +00001595 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001596}
1597
1598void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1599 Record.push_back(Str.size());
1600 Record.insert(Record.end(), Str.begin(), Str.end());
1601}
1602
Douglas Gregorc9490c02009-04-16 22:23:12 +00001603PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1604 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001605
Chris Lattnerdf961c22009-04-10 18:08:30 +00001606void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001607 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001608 Stream.Emit((unsigned)'C', 8);
1609 Stream.Emit((unsigned)'P', 8);
1610 Stream.Emit((unsigned)'C', 8);
1611 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001612
1613 // The translation unit is the first declaration we'll emit.
1614 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1615 DeclsToEmit.push(Context.getTranslationUnitDecl());
1616
1617 // Write the remaining PCH contents.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001618 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001619 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001620 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001621 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001622 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001623 WriteTypesBlock(Context);
1624 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001625 WriteIdentifierTable();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001626 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1627 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001628 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00001629 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
1630 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001631}
1632
1633void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1634 Record.push_back(Loc.getRawEncoding());
1635}
1636
1637void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1638 Record.push_back(Value.getBitWidth());
1639 unsigned N = Value.getNumWords();
1640 const uint64_t* Words = Value.getRawData();
1641 for (unsigned I = 0; I != N; ++I)
1642 Record.push_back(Words[I]);
1643}
1644
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001645void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1646 Record.push_back(Value.isUnsigned());
1647 AddAPInt(Value, Record);
1648}
1649
Douglas Gregor17fc2232009-04-14 21:55:33 +00001650void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1651 AddAPInt(Value.bitcastToAPInt(), Record);
1652}
1653
Douglas Gregor2cf26342009-04-09 22:27:44 +00001654void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001655 if (II == 0) {
1656 Record.push_back(0);
1657 return;
1658 }
1659
1660 pch::IdentID &ID = IdentifierIDs[II];
1661 if (ID == 0)
1662 ID = IdentifierIDs.size();
1663
1664 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001665}
1666
1667void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1668 if (T.isNull()) {
1669 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1670 return;
1671 }
1672
1673 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001674 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001675 switch (BT->getKind()) {
1676 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1677 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1678 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1679 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1680 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1681 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1682 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1683 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1684 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1685 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1686 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1687 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1688 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1689 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1690 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1691 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1692 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1693 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1694 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1695 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1696 }
1697
1698 Record.push_back((ID << 3) | T.getCVRQualifiers());
1699 return;
1700 }
1701
Douglas Gregor8038d512009-04-10 17:25:41 +00001702 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001703 if (ID == 0) // we haven't seen this type before
1704 ID = NextTypeID++;
1705
1706 // Encode the type qualifiers in the type reference.
1707 Record.push_back((ID << 3) | T.getCVRQualifiers());
1708}
1709
1710void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1711 if (D == 0) {
1712 Record.push_back(0);
1713 return;
1714 }
1715
Douglas Gregor8038d512009-04-10 17:25:41 +00001716 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001717 if (ID == 0) {
1718 // We haven't seen this declaration before. Give it a new ID and
1719 // enqueue it in the list of declarations to emit.
1720 ID = DeclIDs.size();
1721 DeclsToEmit.push(const_cast<Decl *>(D));
1722 }
1723
1724 Record.push_back(ID);
1725}
1726
1727void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1728 Record.push_back(Name.getNameKind());
1729 switch (Name.getNameKind()) {
1730 case DeclarationName::Identifier:
1731 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1732 break;
1733
1734 case DeclarationName::ObjCZeroArgSelector:
1735 case DeclarationName::ObjCOneArgSelector:
1736 case DeclarationName::ObjCMultiArgSelector:
1737 assert(false && "Serialization of Objective-C selectors unavailable");
1738 break;
1739
1740 case DeclarationName::CXXConstructorName:
1741 case DeclarationName::CXXDestructorName:
1742 case DeclarationName::CXXConversionFunctionName:
1743 AddTypeRef(Name.getCXXNameType(), Record);
1744 break;
1745
1746 case DeclarationName::CXXOperatorName:
1747 Record.push_back(Name.getCXXOverloadedOperator());
1748 break;
1749
1750 case DeclarationName::CXXUsingDirective:
1751 // No extra data to emit
1752 break;
1753 }
1754}
Douglas Gregor0b748912009-04-14 21:18:50 +00001755
Douglas Gregorc9490c02009-04-16 22:23:12 +00001756/// \brief Write the given substatement or subexpression to the
1757/// bitstream.
1758void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00001759 RecordData Record;
1760 PCHStmtWriter Writer(*this, Record);
1761
Douglas Gregorc9490c02009-04-16 22:23:12 +00001762 if (!S) {
1763 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001764 return;
1765 }
1766
Douglas Gregorc9490c02009-04-16 22:23:12 +00001767 Writer.Code = pch::STMT_NULL_PTR;
1768 Writer.Visit(S);
1769 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00001770 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001771 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001772}
1773
Douglas Gregorc9490c02009-04-16 22:23:12 +00001774/// \brief Flush all of the statements that have been added to the
1775/// queue via AddStmt().
1776void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00001777 RecordData Record;
1778 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001779
Douglas Gregorc9490c02009-04-16 22:23:12 +00001780 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1781 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00001782
Douglas Gregorc9490c02009-04-16 22:23:12 +00001783 if (!S) {
1784 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001785 continue;
1786 }
1787
Douglas Gregorc9490c02009-04-16 22:23:12 +00001788 Writer.Code = pch::STMT_NULL_PTR;
1789 Writer.Visit(S);
1790 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00001791 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001792 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001793
Douglas Gregorc9490c02009-04-16 22:23:12 +00001794 assert(N == StmtsToEmit.size() &&
1795 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00001796
1797 // Note that we are at the end of a full expression. Any
1798 // expression records that follow this one are part of a different
1799 // expression.
1800 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001801 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001802 }
Douglas Gregor087fd532009-04-14 23:32:43 +00001803
Douglas Gregorc9490c02009-04-16 22:23:12 +00001804 StmtsToEmit.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00001805}
Douglas Gregor025452f2009-04-17 00:04:06 +00001806
1807unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1808 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1809 "SwitchCase recorded twice");
1810 unsigned NextID = SwitchCaseIDs.size();
1811 SwitchCaseIDs[S] = NextID;
1812 return NextID;
1813}
1814
1815unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1816 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1817 "SwitchCase hasn't been seen yet");
1818 return SwitchCaseIDs[S];
1819}