blob: 29c9eb81f444e16db757232cc1735632a63fb4c1 [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);
Douglas Gregor67d82492009-04-17 00:29:51 +0000457 void VisitDoStmt(DoStmt *S);
458 void VisitForStmt(ForStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000459 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000460 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor0b748912009-04-14 21:18:50 +0000461 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000462 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000463 void VisitDeclRefExpr(DeclRefExpr *E);
464 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000465 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000466 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000467 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000468 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000469 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000470 void VisitUnaryOperator(UnaryOperator *E);
471 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000472 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000473 void VisitCallExpr(CallExpr *E);
474 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000475 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000476 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000477 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
478 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000479 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000480 void VisitExplicitCastExpr(ExplicitCastExpr *E);
481 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000482 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000483 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000484 void VisitInitListExpr(InitListExpr *E);
485 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
486 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000487 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000488 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
489 void VisitChooseExpr(ChooseExpr *E);
490 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000491 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
492 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000493 };
494}
495
Douglas Gregor025452f2009-04-17 00:04:06 +0000496void PCHStmtWriter::VisitStmt(Stmt *S) {
497}
498
499void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
500 VisitStmt(S);
501 Writer.AddSourceLocation(S->getSemiLoc(), Record);
502 Code = pch::STMT_NULL;
503}
504
505void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
506 VisitStmt(S);
507 Record.push_back(S->size());
508 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
509 CS != CSEnd; ++CS)
510 Writer.WriteSubStmt(*CS);
511 Writer.AddSourceLocation(S->getLBracLoc(), Record);
512 Writer.AddSourceLocation(S->getRBracLoc(), Record);
513 Code = pch::STMT_COMPOUND;
514}
515
516void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
517 VisitStmt(S);
518 Record.push_back(Writer.RecordSwitchCaseID(S));
519}
520
521void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
522 VisitSwitchCase(S);
523 Writer.WriteSubStmt(S->getLHS());
524 Writer.WriteSubStmt(S->getRHS());
525 Writer.WriteSubStmt(S->getSubStmt());
526 Writer.AddSourceLocation(S->getCaseLoc(), Record);
527 Code = pch::STMT_CASE;
528}
529
530void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
531 VisitSwitchCase(S);
532 Writer.WriteSubStmt(S->getSubStmt());
533 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
534 Code = pch::STMT_DEFAULT;
535}
536
537void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
538 VisitStmt(S);
539 Writer.WriteSubStmt(S->getCond());
540 Writer.WriteSubStmt(S->getThen());
541 Writer.WriteSubStmt(S->getElse());
542 Writer.AddSourceLocation(S->getIfLoc(), Record);
543 Code = pch::STMT_IF;
544}
545
546void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
547 VisitStmt(S);
548 Writer.WriteSubStmt(S->getCond());
549 Writer.WriteSubStmt(S->getBody());
550 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
551 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
552 SC = SC->getNextSwitchCase())
553 Record.push_back(Writer.getSwitchCaseID(SC));
554 Code = pch::STMT_SWITCH;
555}
556
Douglas Gregord921cf92009-04-17 00:16:09 +0000557void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
558 VisitStmt(S);
559 Writer.WriteSubStmt(S->getCond());
560 Writer.WriteSubStmt(S->getBody());
561 Writer.AddSourceLocation(S->getWhileLoc(), Record);
562 Code = pch::STMT_WHILE;
563}
564
Douglas Gregor67d82492009-04-17 00:29:51 +0000565void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
566 VisitStmt(S);
567 Writer.WriteSubStmt(S->getCond());
568 Writer.WriteSubStmt(S->getBody());
569 Writer.AddSourceLocation(S->getDoLoc(), Record);
570 Code = pch::STMT_DO;
571}
572
573void PCHStmtWriter::VisitForStmt(ForStmt *S) {
574 VisitStmt(S);
575 Writer.WriteSubStmt(S->getInit());
576 Writer.WriteSubStmt(S->getCond());
577 Writer.WriteSubStmt(S->getInc());
578 Writer.WriteSubStmt(S->getBody());
579 Writer.AddSourceLocation(S->getForLoc(), Record);
580 Code = pch::STMT_FOR;
581}
582
Douglas Gregord921cf92009-04-17 00:16:09 +0000583void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
584 VisitStmt(S);
585 Writer.AddSourceLocation(S->getContinueLoc(), Record);
586 Code = pch::STMT_CONTINUE;
587}
588
Douglas Gregor025452f2009-04-17 00:04:06 +0000589void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
590 VisitStmt(S);
591 Writer.AddSourceLocation(S->getBreakLoc(), Record);
592 Code = pch::STMT_BREAK;
593}
594
Douglas Gregor0b748912009-04-14 21:18:50 +0000595void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000596 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000597 Writer.AddTypeRef(E->getType(), Record);
598 Record.push_back(E->isTypeDependent());
599 Record.push_back(E->isValueDependent());
600}
601
Douglas Gregor17fc2232009-04-14 21:55:33 +0000602void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
603 VisitExpr(E);
604 Writer.AddSourceLocation(E->getLocation(), Record);
605 Record.push_back(E->getIdentType()); // FIXME: stable encoding
606 Code = pch::EXPR_PREDEFINED;
607}
608
Douglas Gregor0b748912009-04-14 21:18:50 +0000609void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
610 VisitExpr(E);
611 Writer.AddDeclRef(E->getDecl(), Record);
612 Writer.AddSourceLocation(E->getLocation(), Record);
613 Code = pch::EXPR_DECL_REF;
614}
615
616void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
617 VisitExpr(E);
618 Writer.AddSourceLocation(E->getLocation(), Record);
619 Writer.AddAPInt(E->getValue(), Record);
620 Code = pch::EXPR_INTEGER_LITERAL;
621}
622
Douglas Gregor17fc2232009-04-14 21:55:33 +0000623void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
624 VisitExpr(E);
625 Writer.AddAPFloat(E->getValue(), Record);
626 Record.push_back(E->isExact());
627 Writer.AddSourceLocation(E->getLocation(), Record);
628 Code = pch::EXPR_FLOATING_LITERAL;
629}
630
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000631void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
632 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000633 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000634 Code = pch::EXPR_IMAGINARY_LITERAL;
635}
636
Douglas Gregor673ecd62009-04-15 16:35:07 +0000637void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
638 VisitExpr(E);
639 Record.push_back(E->getByteLength());
640 Record.push_back(E->getNumConcatenated());
641 Record.push_back(E->isWide());
642 // FIXME: String data should be stored as a blob at the end of the
643 // StringLiteral. However, we can't do so now because we have no
644 // provision for coping with abbreviations when we're jumping around
645 // the PCH file during deserialization.
646 Record.insert(Record.end(),
647 E->getStrData(), E->getStrData() + E->getByteLength());
648 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
649 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
650 Code = pch::EXPR_STRING_LITERAL;
651}
652
Douglas Gregor0b748912009-04-14 21:18:50 +0000653void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
654 VisitExpr(E);
655 Record.push_back(E->getValue());
656 Writer.AddSourceLocation(E->getLoc(), Record);
657 Record.push_back(E->isWide());
658 Code = pch::EXPR_CHARACTER_LITERAL;
659}
660
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000661void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
662 VisitExpr(E);
663 Writer.AddSourceLocation(E->getLParen(), Record);
664 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000665 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000666 Code = pch::EXPR_PAREN;
667}
668
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000669void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
670 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000671 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000672 Record.push_back(E->getOpcode()); // FIXME: stable encoding
673 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
674 Code = pch::EXPR_UNARY_OPERATOR;
675}
676
677void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
678 VisitExpr(E);
679 Record.push_back(E->isSizeOf());
680 if (E->isArgumentType())
681 Writer.AddTypeRef(E->getArgumentType(), Record);
682 else {
683 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000684 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000685 }
686 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
687 Writer.AddSourceLocation(E->getRParenLoc(), Record);
688 Code = pch::EXPR_SIZEOF_ALIGN_OF;
689}
690
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000691void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
692 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000693 Writer.WriteSubStmt(E->getLHS());
694 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000695 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
696 Code = pch::EXPR_ARRAY_SUBSCRIPT;
697}
698
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000699void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
700 VisitExpr(E);
701 Record.push_back(E->getNumArgs());
702 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000703 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000704 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
705 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000706 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000707 Code = pch::EXPR_CALL;
708}
709
710void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
711 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000712 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000713 Writer.AddDeclRef(E->getMemberDecl(), Record);
714 Writer.AddSourceLocation(E->getMemberLoc(), Record);
715 Record.push_back(E->isArrow());
716 Code = pch::EXPR_MEMBER;
717}
718
Douglas Gregor087fd532009-04-14 23:32:43 +0000719void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
720 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000721 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000722}
723
Douglas Gregordb600c32009-04-15 00:25:59 +0000724void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
725 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000726 Writer.WriteSubStmt(E->getLHS());
727 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000728 Record.push_back(E->getOpcode()); // FIXME: stable encoding
729 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
730 Code = pch::EXPR_BINARY_OPERATOR;
731}
732
Douglas Gregorad90e962009-04-15 22:40:36 +0000733void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
734 VisitBinaryOperator(E);
735 Writer.AddTypeRef(E->getComputationLHSType(), Record);
736 Writer.AddTypeRef(E->getComputationResultType(), Record);
737 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
738}
739
740void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
741 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000742 Writer.WriteSubStmt(E->getCond());
743 Writer.WriteSubStmt(E->getLHS());
744 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000745 Code = pch::EXPR_CONDITIONAL_OPERATOR;
746}
747
Douglas Gregor087fd532009-04-14 23:32:43 +0000748void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
749 VisitCastExpr(E);
750 Record.push_back(E->isLvalueCast());
751 Code = pch::EXPR_IMPLICIT_CAST;
752}
753
Douglas Gregordb600c32009-04-15 00:25:59 +0000754void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
755 VisitCastExpr(E);
756 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
757}
758
759void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
760 VisitExplicitCastExpr(E);
761 Writer.AddSourceLocation(E->getLParenLoc(), Record);
762 Writer.AddSourceLocation(E->getRParenLoc(), Record);
763 Code = pch::EXPR_CSTYLE_CAST;
764}
765
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000766void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
767 VisitExpr(E);
768 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000769 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000770 Record.push_back(E->isFileScope());
771 Code = pch::EXPR_COMPOUND_LITERAL;
772}
773
Douglas Gregord3c98a02009-04-15 23:02:49 +0000774void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
775 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000776 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000777 Writer.AddIdentifierRef(&E->getAccessor(), Record);
778 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
779 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
780}
781
Douglas Gregord077d752009-04-16 00:55:48 +0000782void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
783 VisitExpr(E);
784 Record.push_back(E->getNumInits());
785 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000786 Writer.WriteSubStmt(E->getInit(I));
787 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +0000788 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
789 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
790 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
791 Record.push_back(E->hadArrayRangeDesignator());
792 Code = pch::EXPR_INIT_LIST;
793}
794
795void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
796 VisitExpr(E);
797 Record.push_back(E->getNumSubExprs());
798 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000799 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +0000800 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
801 Record.push_back(E->usesGNUSyntax());
802 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
803 DEnd = E->designators_end();
804 D != DEnd; ++D) {
805 if (D->isFieldDesignator()) {
806 if (FieldDecl *Field = D->getField()) {
807 Record.push_back(pch::DESIG_FIELD_DECL);
808 Writer.AddDeclRef(Field, Record);
809 } else {
810 Record.push_back(pch::DESIG_FIELD_NAME);
811 Writer.AddIdentifierRef(D->getFieldName(), Record);
812 }
813 Writer.AddSourceLocation(D->getDotLoc(), Record);
814 Writer.AddSourceLocation(D->getFieldLoc(), Record);
815 } else if (D->isArrayDesignator()) {
816 Record.push_back(pch::DESIG_ARRAY);
817 Record.push_back(D->getFirstExprIndex());
818 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
819 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
820 } else {
821 assert(D->isArrayRangeDesignator() && "Unknown designator");
822 Record.push_back(pch::DESIG_ARRAY_RANGE);
823 Record.push_back(D->getFirstExprIndex());
824 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
825 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
826 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
827 }
828 }
829 Code = pch::EXPR_DESIGNATED_INIT;
830}
831
832void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
833 VisitExpr(E);
834 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
835}
836
Douglas Gregord3c98a02009-04-15 23:02:49 +0000837void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
838 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000839 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000840 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
841 Writer.AddSourceLocation(E->getRParenLoc(), Record);
842 Code = pch::EXPR_VA_ARG;
843}
844
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000845void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
846 VisitExpr(E);
847 Writer.AddTypeRef(E->getArgType1(), Record);
848 Writer.AddTypeRef(E->getArgType2(), Record);
849 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
850 Writer.AddSourceLocation(E->getRParenLoc(), Record);
851 Code = pch::EXPR_TYPES_COMPATIBLE;
852}
853
854void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
855 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000856 Writer.WriteSubStmt(E->getCond());
857 Writer.WriteSubStmt(E->getLHS());
858 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000859 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
860 Writer.AddSourceLocation(E->getRParenLoc(), Record);
861 Code = pch::EXPR_CHOOSE;
862}
863
864void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
865 VisitExpr(E);
866 Writer.AddSourceLocation(E->getTokenLocation(), Record);
867 Code = pch::EXPR_GNU_NULL;
868}
869
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000870void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
871 VisitExpr(E);
872 Record.push_back(E->getNumSubExprs());
873 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000874 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000875 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
876 Writer.AddSourceLocation(E->getRParenLoc(), Record);
877 Code = pch::EXPR_SHUFFLE_VECTOR;
878}
879
880void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
881 VisitExpr(E);
882 Writer.AddDeclRef(E->getDecl(), Record);
883 Writer.AddSourceLocation(E->getLocation(), Record);
884 Record.push_back(E->isByRef());
885 Code = pch::EXPR_BLOCK_DECL_REF;
886}
887
Douglas Gregor0b748912009-04-14 21:18:50 +0000888//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000889// PCHWriter Implementation
890//===----------------------------------------------------------------------===//
891
Douglas Gregor2bec0412009-04-10 21:16:55 +0000892/// \brief Write the target triple (e.g., i686-apple-darwin9).
893void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
894 using namespace llvm;
895 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
896 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000898 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +0000899
900 RecordData Record;
901 Record.push_back(pch::TARGET_TRIPLE);
902 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +0000903 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +0000904}
905
906/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000907void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
908 RecordData Record;
909 Record.push_back(LangOpts.Trigraphs);
910 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
911 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
912 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
913 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
914 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
915 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
916 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
917 Record.push_back(LangOpts.C99); // C99 Support
918 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
919 Record.push_back(LangOpts.CPlusPlus); // C++ Support
920 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
921 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
922 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
923
924 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
925 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
926 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
927
928 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
929 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
930 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
931 Record.push_back(LangOpts.LaxVectorConversions);
932 Record.push_back(LangOpts.Exceptions); // Support exception handling.
933
934 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
935 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
936 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
937
938 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
939 // by locks.
940 Record.push_back(LangOpts.Blocks); // block extension to C
941 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
942 // they are unused.
943 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
944 // (modulo the platform support).
945
946 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
947 // signed integer arithmetic overflows.
948
949 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
950 // may be ripped out at any time.
951
952 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
953 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
954 // defined.
955 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
956 // opposed to __DYNAMIC__).
957 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
958
959 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
960 // used (instead of C99 semantics).
961 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
962 Record.push_back(LangOpts.getGCMode());
963 Record.push_back(LangOpts.getVisibilityMode());
964 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000965 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000966}
967
Douglas Gregor14f79002009-04-10 03:52:48 +0000968//===----------------------------------------------------------------------===//
969// Source Manager Serialization
970//===----------------------------------------------------------------------===//
971
972/// \brief Create an abbreviation for the SLocEntry that refers to a
973/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000974static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000975 using namespace llvm;
976 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
977 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
981 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000983 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000984}
985
986/// \brief Create an abbreviation for the SLocEntry that refers to a
987/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000988static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000989 using namespace llvm;
990 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
991 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000997 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000998}
999
1000/// \brief Create an abbreviation for the SLocEntry that refers to a
1001/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001002static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001003 using namespace llvm;
1004 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1005 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001007 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001008}
1009
1010/// \brief Create an abbreviation for the SLocEntry that refers to an
1011/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001012static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001013 using namespace llvm;
1014 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1015 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001021 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001022}
1023
1024/// \brief Writes the block containing the serialized form of the
1025/// source manager.
1026///
1027/// TODO: We should probably use an on-disk hash table (stored in a
1028/// blob), indexed based on the file name, so that we only create
1029/// entries for files that we actually need. In the common case (no
1030/// errors), we probably won't have to create file entries for any of
1031/// the files in the AST.
1032void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001033 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001034 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001035
1036 // Abbreviations for the various kinds of source-location entries.
1037 int SLocFileAbbrv = -1;
1038 int SLocBufferAbbrv = -1;
1039 int SLocBufferBlobAbbrv = -1;
1040 int SLocInstantiationAbbrv = -1;
1041
1042 // Write out the source location entry table. We skip the first
1043 // entry, which is always the same dummy entry.
1044 RecordData Record;
1045 for (SourceManager::sloc_entry_iterator
1046 SLoc = SourceMgr.sloc_entry_begin() + 1,
1047 SLocEnd = SourceMgr.sloc_entry_end();
1048 SLoc != SLocEnd; ++SLoc) {
1049 // Figure out which record code to use.
1050 unsigned Code;
1051 if (SLoc->isFile()) {
1052 if (SLoc->getFile().getContentCache()->Entry)
1053 Code = pch::SM_SLOC_FILE_ENTRY;
1054 else
1055 Code = pch::SM_SLOC_BUFFER_ENTRY;
1056 } else
1057 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1058 Record.push_back(Code);
1059
1060 Record.push_back(SLoc->getOffset());
1061 if (SLoc->isFile()) {
1062 const SrcMgr::FileInfo &File = SLoc->getFile();
1063 Record.push_back(File.getIncludeLoc().getRawEncoding());
1064 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001065 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001066
1067 const SrcMgr::ContentCache *Content = File.getContentCache();
1068 if (Content->Entry) {
1069 // The source location entry is a file. The blob associated
1070 // with this entry is the file name.
1071 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001072 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1073 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001074 Content->Entry->getName(),
1075 strlen(Content->Entry->getName()));
1076 } else {
1077 // The source location entry is a buffer. The blob associated
1078 // with this entry contains the contents of the buffer.
1079 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001080 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1081 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001082 }
1083
1084 // We add one to the size so that we capture the trailing NULL
1085 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1086 // the reader side).
1087 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1088 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001089 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001090 Record.clear();
1091 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001092 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001093 Buffer->getBufferStart(),
1094 Buffer->getBufferSize() + 1);
1095 }
1096 } else {
1097 // The source location entry is an instantiation.
1098 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1099 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1100 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1101 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1102
Douglas Gregorf60e9912009-04-15 18:05:10 +00001103 // Compute the token length for this macro expansion.
1104 unsigned NextOffset = SourceMgr.getNextOffset();
1105 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1106 if (++NextSLoc != SLocEnd)
1107 NextOffset = NextSLoc->getOffset();
1108 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1109
Douglas Gregor14f79002009-04-10 03:52:48 +00001110 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001111 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1112 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001113 }
1114
1115 Record.clear();
1116 }
1117
Douglas Gregorbd945002009-04-13 16:31:14 +00001118 // Write the line table.
1119 if (SourceMgr.hasLineTable()) {
1120 LineTableInfo &LineTable = SourceMgr.getLineTable();
1121
1122 // Emit the file names
1123 Record.push_back(LineTable.getNumFilenames());
1124 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1125 // Emit the file name
1126 const char *Filename = LineTable.getFilename(I);
1127 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1128 Record.push_back(FilenameLen);
1129 if (FilenameLen)
1130 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1131 }
1132
1133 // Emit the line entries
1134 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1135 L != LEnd; ++L) {
1136 // Emit the file ID
1137 Record.push_back(L->first);
1138
1139 // Emit the line entries
1140 Record.push_back(L->second.size());
1141 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1142 LEEnd = L->second.end();
1143 LE != LEEnd; ++LE) {
1144 Record.push_back(LE->FileOffset);
1145 Record.push_back(LE->LineNo);
1146 Record.push_back(LE->FilenameID);
1147 Record.push_back((unsigned)LE->FileKind);
1148 Record.push_back(LE->IncludeOffset);
1149 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001150 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001151 }
1152 }
1153
Douglas Gregorc9490c02009-04-16 22:23:12 +00001154 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001155}
1156
Chris Lattner0b1fb982009-04-10 17:15:23 +00001157/// \brief Writes the block containing the serialized form of the
1158/// preprocessor.
1159///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001160void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001161 // Enter the preprocessor block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001162 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001163
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001164 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1165 // FIXME: use diagnostics subsystem for localization etc.
1166 if (PP.SawDateOrTime())
1167 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001168
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001169 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001170
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001171 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1172 if (PP.getCounterValue() != 0) {
1173 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001174 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001175 Record.clear();
1176 }
1177
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001178 // Loop over all the macro definitions that are live at the end of the file,
1179 // emitting each to the PP section.
1180 // FIXME: Eventually we want to emit an index so that we can lazily load
1181 // macros.
1182 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1183 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001184 // FIXME: This emits macros in hash table order, we should do it in a stable
1185 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001186 MacroInfo *MI = I->second;
1187
1188 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1189 // been redefined by the header (in which case they are not isBuiltinMacro).
1190 if (MI->isBuiltinMacro())
1191 continue;
1192
Chris Lattner7356a312009-04-11 21:15:38 +00001193 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001194 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1195 Record.push_back(MI->isUsed());
1196
1197 unsigned Code;
1198 if (MI->isObjectLike()) {
1199 Code = pch::PP_MACRO_OBJECT_LIKE;
1200 } else {
1201 Code = pch::PP_MACRO_FUNCTION_LIKE;
1202
1203 Record.push_back(MI->isC99Varargs());
1204 Record.push_back(MI->isGNUVarargs());
1205 Record.push_back(MI->getNumArgs());
1206 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1207 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001208 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001209 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001210 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001211 Record.clear();
1212
Chris Lattnerdf961c22009-04-10 18:08:30 +00001213 // Emit the tokens array.
1214 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1215 // Note that we know that the preprocessor does not have any annotation
1216 // tokens in it because they are created by the parser, and thus can't be
1217 // in a macro definition.
1218 const Token &Tok = MI->getReplacementToken(TokNo);
1219
1220 Record.push_back(Tok.getLocation().getRawEncoding());
1221 Record.push_back(Tok.getLength());
1222
Chris Lattnerdf961c22009-04-10 18:08:30 +00001223 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1224 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001225 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001226
1227 // FIXME: Should translate token kind to a stable encoding.
1228 Record.push_back(Tok.getKind());
1229 // FIXME: Should translate token flags to a stable encoding.
1230 Record.push_back(Tok.getFlags());
1231
Douglas Gregorc9490c02009-04-16 22:23:12 +00001232 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001233 Record.clear();
1234 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001235
1236 }
1237
Douglas Gregorc9490c02009-04-16 22:23:12 +00001238 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001239}
1240
1241
Douglas Gregor2cf26342009-04-09 22:27:44 +00001242/// \brief Write the representation of a type to the PCH stream.
1243void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001244 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001245 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001246 ID = NextTypeID++;
1247
1248 // Record the offset for this type.
1249 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001250 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001251 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1252 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001253 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001254 }
1255
1256 RecordData Record;
1257
1258 // Emit the type's representation.
1259 PCHTypeWriter W(*this, Record);
1260 switch (T->getTypeClass()) {
1261 // For all of the concrete, non-dependent types, call the
1262 // appropriate visitor function.
1263#define TYPE(Class, Base) \
1264 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1265#define ABSTRACT_TYPE(Class, Base)
1266#define DEPENDENT_TYPE(Class, Base)
1267#include "clang/AST/TypeNodes.def"
1268
1269 // For all of the dependent type nodes (which only occur in C++
1270 // templates), produce an error.
1271#define TYPE(Class, Base)
1272#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1273#include "clang/AST/TypeNodes.def"
1274 assert(false && "Cannot serialize dependent type nodes");
1275 break;
1276 }
1277
1278 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001279 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001280
1281 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001282 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001283}
1284
1285/// \brief Write a block containing all of the types.
1286void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001287 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001288 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001289
1290 // Emit all of the types in the ASTContext
1291 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1292 TEnd = Context.getTypes().end();
1293 T != TEnd; ++T) {
1294 // Builtin types are never serialized.
1295 if (isa<BuiltinType>(*T))
1296 continue;
1297
1298 WriteType(*T);
1299 }
1300
1301 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001302 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001303}
1304
1305/// \brief Write the block containing all of the declaration IDs
1306/// lexically declared within the given DeclContext.
1307///
1308/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1309/// bistream, or 0 if no block was written.
1310uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1311 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001312 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001313 return 0;
1314
Douglas Gregorc9490c02009-04-16 22:23:12 +00001315 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001316 RecordData Record;
1317 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1318 DEnd = DC->decls_end(Context);
1319 D != DEnd; ++D)
1320 AddDeclRef(*D, Record);
1321
Douglas Gregorc9490c02009-04-16 22:23:12 +00001322 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001323 return Offset;
1324}
1325
1326/// \brief Write the block containing all of the declaration IDs
1327/// visible from the given DeclContext.
1328///
1329/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1330/// bistream, or 0 if no block was written.
1331uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1332 DeclContext *DC) {
1333 if (DC->getPrimaryContext() != DC)
1334 return 0;
1335
1336 // Force the DeclContext to build a its name-lookup table.
1337 DC->lookup(Context, DeclarationName());
1338
1339 // Serialize the contents of the mapping used for lookup. Note that,
1340 // although we have two very different code paths, the serialized
1341 // representation is the same for both cases: a declaration name,
1342 // followed by a size, followed by references to the visible
1343 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001344 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001345 RecordData Record;
1346 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001347 if (!Map)
1348 return 0;
1349
Douglas Gregor2cf26342009-04-09 22:27:44 +00001350 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1351 D != DEnd; ++D) {
1352 AddDeclarationName(D->first, Record);
1353 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1354 Record.push_back(Result.second - Result.first);
1355 for(; Result.first != Result.second; ++Result.first)
1356 AddDeclRef(*Result.first, Record);
1357 }
1358
1359 if (Record.size() == 0)
1360 return 0;
1361
Douglas Gregorc9490c02009-04-16 22:23:12 +00001362 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001363 return Offset;
1364}
1365
1366/// \brief Write a block containing all of the declarations.
1367void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001368 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001369 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001370
1371 // Emit all of the declarations.
1372 RecordData Record;
1373 PCHDeclWriter W(*this, Record);
1374 while (!DeclsToEmit.empty()) {
1375 // Pull the next declaration off the queue
1376 Decl *D = DeclsToEmit.front();
1377 DeclsToEmit.pop();
1378
1379 // If this declaration is also a DeclContext, write blocks for the
1380 // declarations that lexically stored inside its context and those
1381 // declarations that are visible from its context. These blocks
1382 // are written before the declaration itself so that we can put
1383 // their offsets into the record for the declaration.
1384 uint64_t LexicalOffset = 0;
1385 uint64_t VisibleOffset = 0;
1386 DeclContext *DC = dyn_cast<DeclContext>(D);
1387 if (DC) {
1388 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1389 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1390 }
1391
1392 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001393 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001394 if (ID == 0)
1395 ID = DeclIDs.size();
1396
1397 unsigned Index = ID - 1;
1398
1399 // Record the offset for this declaration
1400 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001401 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001402 else if (DeclOffsets.size() < Index) {
1403 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001404 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001405 }
1406
1407 // Build and emit a record for this declaration
1408 Record.clear();
1409 W.Code = (pch::DeclCode)0;
1410 W.Visit(D);
1411 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001412 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001413 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001414
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001415 // If the declaration had any attributes, write them now.
1416 if (D->hasAttrs())
1417 WriteAttributeRecord(D->getAttrs());
1418
Douglas Gregor0b748912009-04-14 21:18:50 +00001419 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001420 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001421
Douglas Gregorfdd01722009-04-14 00:24:19 +00001422 // Note external declarations so that we can add them to a record
1423 // in the PCH file later.
1424 if (isa<FileScopeAsmDecl>(D))
1425 ExternalDefinitions.push_back(ID);
1426 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1427 if (// Non-static file-scope variables with initializers or that
1428 // are tentative definitions.
1429 (Var->isFileVarDecl() &&
1430 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1431 // Out-of-line definitions of static data members (C++).
1432 (Var->getDeclContext()->isRecord() &&
1433 !Var->getLexicalDeclContext()->isRecord() &&
1434 Var->getStorageClass() == VarDecl::Static))
1435 ExternalDefinitions.push_back(ID);
1436 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1437 if (Func->isThisDeclarationADefinition() &&
1438 Func->getStorageClass() != FunctionDecl::Static &&
1439 !Func->isInline())
1440 ExternalDefinitions.push_back(ID);
1441 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001442 }
1443
1444 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001445 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001446}
1447
Douglas Gregorafaf3082009-04-11 00:14:32 +00001448/// \brief Write the identifier table into the PCH file.
1449///
1450/// The identifier table consists of a blob containing string data
1451/// (the actual identifiers themselves) and a separate "offsets" index
1452/// that maps identifier IDs to locations within the blob.
1453void PCHWriter::WriteIdentifierTable() {
1454 using namespace llvm;
1455
1456 // Create and write out the blob that contains the identifier
1457 // strings.
1458 RecordData IdentOffsets;
1459 IdentOffsets.resize(IdentifierIDs.size());
1460 {
1461 // Create the identifier string data.
1462 std::vector<char> Data;
1463 Data.push_back(0); // Data must not be empty.
1464 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1465 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1466 ID != IDEnd; ++ID) {
1467 assert(ID->first && "NULL identifier in identifier table");
1468
1469 // Make sure we're starting on an odd byte. The PCH reader
1470 // expects the low bit to be set on all of the offsets.
1471 if ((Data.size() & 0x01) == 0)
1472 Data.push_back((char)0);
1473
1474 IdentOffsets[ID->second - 1] = Data.size();
1475 Data.insert(Data.end(),
1476 ID->first->getName(),
1477 ID->first->getName() + ID->first->getLength());
1478 Data.push_back((char)0);
1479 }
1480
1481 // Create a blob abbreviation
1482 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1483 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1484 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001485 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001486
1487 // Write the identifier table
1488 RecordData Record;
1489 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001490 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001491 }
1492
1493 // Write the offsets table for identifier IDs.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001494 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001495}
1496
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001497/// \brief Write a record containing the given attributes.
1498void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1499 RecordData Record;
1500 for (; Attr; Attr = Attr->getNext()) {
1501 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1502 Record.push_back(Attr->isInherited());
1503 switch (Attr->getKind()) {
1504 case Attr::Alias:
1505 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1506 break;
1507
1508 case Attr::Aligned:
1509 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1510 break;
1511
1512 case Attr::AlwaysInline:
1513 break;
1514
1515 case Attr::AnalyzerNoReturn:
1516 break;
1517
1518 case Attr::Annotate:
1519 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1520 break;
1521
1522 case Attr::AsmLabel:
1523 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1524 break;
1525
1526 case Attr::Blocks:
1527 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1528 break;
1529
1530 case Attr::Cleanup:
1531 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1532 break;
1533
1534 case Attr::Const:
1535 break;
1536
1537 case Attr::Constructor:
1538 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1539 break;
1540
1541 case Attr::DLLExport:
1542 case Attr::DLLImport:
1543 case Attr::Deprecated:
1544 break;
1545
1546 case Attr::Destructor:
1547 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1548 break;
1549
1550 case Attr::FastCall:
1551 break;
1552
1553 case Attr::Format: {
1554 const FormatAttr *Format = cast<FormatAttr>(Attr);
1555 AddString(Format->getType(), Record);
1556 Record.push_back(Format->getFormatIdx());
1557 Record.push_back(Format->getFirstArg());
1558 break;
1559 }
1560
1561 case Attr::GNUCInline:
1562 case Attr::IBOutletKind:
1563 case Attr::NoReturn:
1564 case Attr::NoThrow:
1565 case Attr::Nodebug:
1566 case Attr::Noinline:
1567 break;
1568
1569 case Attr::NonNull: {
1570 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1571 Record.push_back(NonNull->size());
1572 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1573 break;
1574 }
1575
1576 case Attr::ObjCException:
1577 case Attr::ObjCNSObject:
1578 case Attr::Overloadable:
1579 break;
1580
1581 case Attr::Packed:
1582 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1583 break;
1584
1585 case Attr::Pure:
1586 break;
1587
1588 case Attr::Regparm:
1589 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1590 break;
1591
1592 case Attr::Section:
1593 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1594 break;
1595
1596 case Attr::StdCall:
1597 case Attr::TransparentUnion:
1598 case Attr::Unavailable:
1599 case Attr::Unused:
1600 case Attr::Used:
1601 break;
1602
1603 case Attr::Visibility:
1604 // FIXME: stable encoding
1605 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1606 break;
1607
1608 case Attr::WarnUnusedResult:
1609 case Attr::Weak:
1610 case Attr::WeakImport:
1611 break;
1612 }
1613 }
1614
Douglas Gregorc9490c02009-04-16 22:23:12 +00001615 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001616}
1617
1618void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1619 Record.push_back(Str.size());
1620 Record.insert(Record.end(), Str.begin(), Str.end());
1621}
1622
Douglas Gregorc9490c02009-04-16 22:23:12 +00001623PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1624 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001625
Chris Lattnerdf961c22009-04-10 18:08:30 +00001626void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001627 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001628 Stream.Emit((unsigned)'C', 8);
1629 Stream.Emit((unsigned)'P', 8);
1630 Stream.Emit((unsigned)'C', 8);
1631 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001632
1633 // The translation unit is the first declaration we'll emit.
1634 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1635 DeclsToEmit.push(Context.getTranslationUnitDecl());
1636
1637 // Write the remaining PCH contents.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001638 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001639 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001640 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001641 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001642 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001643 WriteTypesBlock(Context);
1644 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001645 WriteIdentifierTable();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001646 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1647 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001648 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00001649 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
1650 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001651}
1652
1653void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1654 Record.push_back(Loc.getRawEncoding());
1655}
1656
1657void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1658 Record.push_back(Value.getBitWidth());
1659 unsigned N = Value.getNumWords();
1660 const uint64_t* Words = Value.getRawData();
1661 for (unsigned I = 0; I != N; ++I)
1662 Record.push_back(Words[I]);
1663}
1664
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001665void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1666 Record.push_back(Value.isUnsigned());
1667 AddAPInt(Value, Record);
1668}
1669
Douglas Gregor17fc2232009-04-14 21:55:33 +00001670void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1671 AddAPInt(Value.bitcastToAPInt(), Record);
1672}
1673
Douglas Gregor2cf26342009-04-09 22:27:44 +00001674void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001675 if (II == 0) {
1676 Record.push_back(0);
1677 return;
1678 }
1679
1680 pch::IdentID &ID = IdentifierIDs[II];
1681 if (ID == 0)
1682 ID = IdentifierIDs.size();
1683
1684 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001685}
1686
1687void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1688 if (T.isNull()) {
1689 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1690 return;
1691 }
1692
1693 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001694 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001695 switch (BT->getKind()) {
1696 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1697 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1698 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1699 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1700 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1701 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1702 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1703 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1704 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1705 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1706 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1707 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1708 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1709 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1710 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1711 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1712 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1713 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1714 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1715 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1716 }
1717
1718 Record.push_back((ID << 3) | T.getCVRQualifiers());
1719 return;
1720 }
1721
Douglas Gregor8038d512009-04-10 17:25:41 +00001722 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001723 if (ID == 0) // we haven't seen this type before
1724 ID = NextTypeID++;
1725
1726 // Encode the type qualifiers in the type reference.
1727 Record.push_back((ID << 3) | T.getCVRQualifiers());
1728}
1729
1730void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1731 if (D == 0) {
1732 Record.push_back(0);
1733 return;
1734 }
1735
Douglas Gregor8038d512009-04-10 17:25:41 +00001736 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001737 if (ID == 0) {
1738 // We haven't seen this declaration before. Give it a new ID and
1739 // enqueue it in the list of declarations to emit.
1740 ID = DeclIDs.size();
1741 DeclsToEmit.push(const_cast<Decl *>(D));
1742 }
1743
1744 Record.push_back(ID);
1745}
1746
1747void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1748 Record.push_back(Name.getNameKind());
1749 switch (Name.getNameKind()) {
1750 case DeclarationName::Identifier:
1751 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1752 break;
1753
1754 case DeclarationName::ObjCZeroArgSelector:
1755 case DeclarationName::ObjCOneArgSelector:
1756 case DeclarationName::ObjCMultiArgSelector:
1757 assert(false && "Serialization of Objective-C selectors unavailable");
1758 break;
1759
1760 case DeclarationName::CXXConstructorName:
1761 case DeclarationName::CXXDestructorName:
1762 case DeclarationName::CXXConversionFunctionName:
1763 AddTypeRef(Name.getCXXNameType(), Record);
1764 break;
1765
1766 case DeclarationName::CXXOperatorName:
1767 Record.push_back(Name.getCXXOverloadedOperator());
1768 break;
1769
1770 case DeclarationName::CXXUsingDirective:
1771 // No extra data to emit
1772 break;
1773 }
1774}
Douglas Gregor0b748912009-04-14 21:18:50 +00001775
Douglas Gregorc9490c02009-04-16 22:23:12 +00001776/// \brief Write the given substatement or subexpression to the
1777/// bitstream.
1778void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00001779 RecordData Record;
1780 PCHStmtWriter Writer(*this, Record);
1781
Douglas Gregorc9490c02009-04-16 22:23:12 +00001782 if (!S) {
1783 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001784 return;
1785 }
1786
Douglas Gregorc9490c02009-04-16 22:23:12 +00001787 Writer.Code = pch::STMT_NULL_PTR;
1788 Writer.Visit(S);
1789 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00001790 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001791 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001792}
1793
Douglas Gregorc9490c02009-04-16 22:23:12 +00001794/// \brief Flush all of the statements that have been added to the
1795/// queue via AddStmt().
1796void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00001797 RecordData Record;
1798 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001799
Douglas Gregorc9490c02009-04-16 22:23:12 +00001800 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1801 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00001802
Douglas Gregorc9490c02009-04-16 22:23:12 +00001803 if (!S) {
1804 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001805 continue;
1806 }
1807
Douglas Gregorc9490c02009-04-16 22:23:12 +00001808 Writer.Code = pch::STMT_NULL_PTR;
1809 Writer.Visit(S);
1810 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00001811 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001812 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001813
Douglas Gregorc9490c02009-04-16 22:23:12 +00001814 assert(N == StmtsToEmit.size() &&
1815 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00001816
1817 // Note that we are at the end of a full expression. Any
1818 // expression records that follow this one are part of a different
1819 // expression.
1820 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001821 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001822 }
Douglas Gregor087fd532009-04-14 23:32:43 +00001823
Douglas Gregorc9490c02009-04-16 22:23:12 +00001824 StmtsToEmit.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00001825}
Douglas Gregor025452f2009-04-17 00:04:06 +00001826
1827unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1828 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1829 "SwitchCase recorded twice");
1830 unsigned NextID = SwitchCaseIDs.size();
1831 SwitchCaseIDs[S] = NextID;
1832 return NextID;
1833}
1834
1835unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1836 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1837 "SwitchCase hasn't been seen yet");
1838 return SwitchCaseIDs[S];
1839}