blob: 91490549eaba38cf864e90e742a218c3608a6a66 [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 Gregor0de9d882009-04-17 16:34:57 +0000461 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor0b748912009-04-14 21:18:50 +0000462 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000463 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000464 void VisitDeclRefExpr(DeclRefExpr *E);
465 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000466 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000467 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000468 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000469 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000470 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000471 void VisitUnaryOperator(UnaryOperator *E);
472 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000473 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000474 void VisitCallExpr(CallExpr *E);
475 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000476 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000477 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000478 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
479 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000480 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000481 void VisitExplicitCastExpr(ExplicitCastExpr *E);
482 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000483 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000484 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000485 void VisitInitListExpr(InitListExpr *E);
486 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
487 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000488 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000489 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
490 void VisitChooseExpr(ChooseExpr *E);
491 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000492 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
493 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000494 };
495}
496
Douglas Gregor025452f2009-04-17 00:04:06 +0000497void PCHStmtWriter::VisitStmt(Stmt *S) {
498}
499
500void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
501 VisitStmt(S);
502 Writer.AddSourceLocation(S->getSemiLoc(), Record);
503 Code = pch::STMT_NULL;
504}
505
506void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
507 VisitStmt(S);
508 Record.push_back(S->size());
509 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
510 CS != CSEnd; ++CS)
511 Writer.WriteSubStmt(*CS);
512 Writer.AddSourceLocation(S->getLBracLoc(), Record);
513 Writer.AddSourceLocation(S->getRBracLoc(), Record);
514 Code = pch::STMT_COMPOUND;
515}
516
517void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
518 VisitStmt(S);
519 Record.push_back(Writer.RecordSwitchCaseID(S));
520}
521
522void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
523 VisitSwitchCase(S);
524 Writer.WriteSubStmt(S->getLHS());
525 Writer.WriteSubStmt(S->getRHS());
526 Writer.WriteSubStmt(S->getSubStmt());
527 Writer.AddSourceLocation(S->getCaseLoc(), Record);
528 Code = pch::STMT_CASE;
529}
530
531void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
532 VisitSwitchCase(S);
533 Writer.WriteSubStmt(S->getSubStmt());
534 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
535 Code = pch::STMT_DEFAULT;
536}
537
538void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
539 VisitStmt(S);
540 Writer.WriteSubStmt(S->getCond());
541 Writer.WriteSubStmt(S->getThen());
542 Writer.WriteSubStmt(S->getElse());
543 Writer.AddSourceLocation(S->getIfLoc(), Record);
544 Code = pch::STMT_IF;
545}
546
547void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
548 VisitStmt(S);
549 Writer.WriteSubStmt(S->getCond());
550 Writer.WriteSubStmt(S->getBody());
551 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
552 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
553 SC = SC->getNextSwitchCase())
554 Record.push_back(Writer.getSwitchCaseID(SC));
555 Code = pch::STMT_SWITCH;
556}
557
Douglas Gregord921cf92009-04-17 00:16:09 +0000558void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
559 VisitStmt(S);
560 Writer.WriteSubStmt(S->getCond());
561 Writer.WriteSubStmt(S->getBody());
562 Writer.AddSourceLocation(S->getWhileLoc(), Record);
563 Code = pch::STMT_WHILE;
564}
565
Douglas Gregor67d82492009-04-17 00:29:51 +0000566void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
567 VisitStmt(S);
568 Writer.WriteSubStmt(S->getCond());
569 Writer.WriteSubStmt(S->getBody());
570 Writer.AddSourceLocation(S->getDoLoc(), Record);
571 Code = pch::STMT_DO;
572}
573
574void PCHStmtWriter::VisitForStmt(ForStmt *S) {
575 VisitStmt(S);
576 Writer.WriteSubStmt(S->getInit());
577 Writer.WriteSubStmt(S->getCond());
578 Writer.WriteSubStmt(S->getInc());
579 Writer.WriteSubStmt(S->getBody());
580 Writer.AddSourceLocation(S->getForLoc(), Record);
581 Code = pch::STMT_FOR;
582}
583
Douglas Gregord921cf92009-04-17 00:16:09 +0000584void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
585 VisitStmt(S);
586 Writer.AddSourceLocation(S->getContinueLoc(), Record);
587 Code = pch::STMT_CONTINUE;
588}
589
Douglas Gregor025452f2009-04-17 00:04:06 +0000590void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
591 VisitStmt(S);
592 Writer.AddSourceLocation(S->getBreakLoc(), Record);
593 Code = pch::STMT_BREAK;
594}
595
Douglas Gregor0de9d882009-04-17 16:34:57 +0000596void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
597 VisitStmt(S);
598 Writer.WriteSubStmt(S->getRetValue());
599 Writer.AddSourceLocation(S->getReturnLoc(), Record);
600 Code = pch::STMT_RETURN;
601}
602
Douglas Gregor0b748912009-04-14 21:18:50 +0000603void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000604 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000605 Writer.AddTypeRef(E->getType(), Record);
606 Record.push_back(E->isTypeDependent());
607 Record.push_back(E->isValueDependent());
608}
609
Douglas Gregor17fc2232009-04-14 21:55:33 +0000610void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
611 VisitExpr(E);
612 Writer.AddSourceLocation(E->getLocation(), Record);
613 Record.push_back(E->getIdentType()); // FIXME: stable encoding
614 Code = pch::EXPR_PREDEFINED;
615}
616
Douglas Gregor0b748912009-04-14 21:18:50 +0000617void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
618 VisitExpr(E);
619 Writer.AddDeclRef(E->getDecl(), Record);
620 Writer.AddSourceLocation(E->getLocation(), Record);
621 Code = pch::EXPR_DECL_REF;
622}
623
624void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
625 VisitExpr(E);
626 Writer.AddSourceLocation(E->getLocation(), Record);
627 Writer.AddAPInt(E->getValue(), Record);
628 Code = pch::EXPR_INTEGER_LITERAL;
629}
630
Douglas Gregor17fc2232009-04-14 21:55:33 +0000631void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
632 VisitExpr(E);
633 Writer.AddAPFloat(E->getValue(), Record);
634 Record.push_back(E->isExact());
635 Writer.AddSourceLocation(E->getLocation(), Record);
636 Code = pch::EXPR_FLOATING_LITERAL;
637}
638
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000639void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
640 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000641 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000642 Code = pch::EXPR_IMAGINARY_LITERAL;
643}
644
Douglas Gregor673ecd62009-04-15 16:35:07 +0000645void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
646 VisitExpr(E);
647 Record.push_back(E->getByteLength());
648 Record.push_back(E->getNumConcatenated());
649 Record.push_back(E->isWide());
650 // FIXME: String data should be stored as a blob at the end of the
651 // StringLiteral. However, we can't do so now because we have no
652 // provision for coping with abbreviations when we're jumping around
653 // the PCH file during deserialization.
654 Record.insert(Record.end(),
655 E->getStrData(), E->getStrData() + E->getByteLength());
656 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
657 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
658 Code = pch::EXPR_STRING_LITERAL;
659}
660
Douglas Gregor0b748912009-04-14 21:18:50 +0000661void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
662 VisitExpr(E);
663 Record.push_back(E->getValue());
664 Writer.AddSourceLocation(E->getLoc(), Record);
665 Record.push_back(E->isWide());
666 Code = pch::EXPR_CHARACTER_LITERAL;
667}
668
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000669void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
670 VisitExpr(E);
671 Writer.AddSourceLocation(E->getLParen(), Record);
672 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000673 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000674 Code = pch::EXPR_PAREN;
675}
676
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000677void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
678 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000679 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000680 Record.push_back(E->getOpcode()); // FIXME: stable encoding
681 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
682 Code = pch::EXPR_UNARY_OPERATOR;
683}
684
685void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
686 VisitExpr(E);
687 Record.push_back(E->isSizeOf());
688 if (E->isArgumentType())
689 Writer.AddTypeRef(E->getArgumentType(), Record);
690 else {
691 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000692 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000693 }
694 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
695 Writer.AddSourceLocation(E->getRParenLoc(), Record);
696 Code = pch::EXPR_SIZEOF_ALIGN_OF;
697}
698
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000699void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
700 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000701 Writer.WriteSubStmt(E->getLHS());
702 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000703 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
704 Code = pch::EXPR_ARRAY_SUBSCRIPT;
705}
706
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000707void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
708 VisitExpr(E);
709 Record.push_back(E->getNumArgs());
710 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000711 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000712 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
713 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000714 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000715 Code = pch::EXPR_CALL;
716}
717
718void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
719 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000720 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000721 Writer.AddDeclRef(E->getMemberDecl(), Record);
722 Writer.AddSourceLocation(E->getMemberLoc(), Record);
723 Record.push_back(E->isArrow());
724 Code = pch::EXPR_MEMBER;
725}
726
Douglas Gregor087fd532009-04-14 23:32:43 +0000727void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
728 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000729 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000730}
731
Douglas Gregordb600c32009-04-15 00:25:59 +0000732void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
733 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000734 Writer.WriteSubStmt(E->getLHS());
735 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000736 Record.push_back(E->getOpcode()); // FIXME: stable encoding
737 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
738 Code = pch::EXPR_BINARY_OPERATOR;
739}
740
Douglas Gregorad90e962009-04-15 22:40:36 +0000741void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
742 VisitBinaryOperator(E);
743 Writer.AddTypeRef(E->getComputationLHSType(), Record);
744 Writer.AddTypeRef(E->getComputationResultType(), Record);
745 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
746}
747
748void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
749 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000750 Writer.WriteSubStmt(E->getCond());
751 Writer.WriteSubStmt(E->getLHS());
752 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000753 Code = pch::EXPR_CONDITIONAL_OPERATOR;
754}
755
Douglas Gregor087fd532009-04-14 23:32:43 +0000756void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
757 VisitCastExpr(E);
758 Record.push_back(E->isLvalueCast());
759 Code = pch::EXPR_IMPLICIT_CAST;
760}
761
Douglas Gregordb600c32009-04-15 00:25:59 +0000762void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
763 VisitCastExpr(E);
764 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
765}
766
767void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
768 VisitExplicitCastExpr(E);
769 Writer.AddSourceLocation(E->getLParenLoc(), Record);
770 Writer.AddSourceLocation(E->getRParenLoc(), Record);
771 Code = pch::EXPR_CSTYLE_CAST;
772}
773
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000774void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
775 VisitExpr(E);
776 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000777 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000778 Record.push_back(E->isFileScope());
779 Code = pch::EXPR_COMPOUND_LITERAL;
780}
781
Douglas Gregord3c98a02009-04-15 23:02:49 +0000782void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
783 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000784 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000785 Writer.AddIdentifierRef(&E->getAccessor(), Record);
786 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
787 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
788}
789
Douglas Gregord077d752009-04-16 00:55:48 +0000790void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
791 VisitExpr(E);
792 Record.push_back(E->getNumInits());
793 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000794 Writer.WriteSubStmt(E->getInit(I));
795 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +0000796 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
797 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
798 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
799 Record.push_back(E->hadArrayRangeDesignator());
800 Code = pch::EXPR_INIT_LIST;
801}
802
803void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
804 VisitExpr(E);
805 Record.push_back(E->getNumSubExprs());
806 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000807 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +0000808 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
809 Record.push_back(E->usesGNUSyntax());
810 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
811 DEnd = E->designators_end();
812 D != DEnd; ++D) {
813 if (D->isFieldDesignator()) {
814 if (FieldDecl *Field = D->getField()) {
815 Record.push_back(pch::DESIG_FIELD_DECL);
816 Writer.AddDeclRef(Field, Record);
817 } else {
818 Record.push_back(pch::DESIG_FIELD_NAME);
819 Writer.AddIdentifierRef(D->getFieldName(), Record);
820 }
821 Writer.AddSourceLocation(D->getDotLoc(), Record);
822 Writer.AddSourceLocation(D->getFieldLoc(), Record);
823 } else if (D->isArrayDesignator()) {
824 Record.push_back(pch::DESIG_ARRAY);
825 Record.push_back(D->getFirstExprIndex());
826 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
827 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
828 } else {
829 assert(D->isArrayRangeDesignator() && "Unknown designator");
830 Record.push_back(pch::DESIG_ARRAY_RANGE);
831 Record.push_back(D->getFirstExprIndex());
832 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
833 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
834 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
835 }
836 }
837 Code = pch::EXPR_DESIGNATED_INIT;
838}
839
840void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
841 VisitExpr(E);
842 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
843}
844
Douglas Gregord3c98a02009-04-15 23:02:49 +0000845void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
846 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000847 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000848 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
849 Writer.AddSourceLocation(E->getRParenLoc(), Record);
850 Code = pch::EXPR_VA_ARG;
851}
852
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000853void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
854 VisitExpr(E);
855 Writer.AddTypeRef(E->getArgType1(), Record);
856 Writer.AddTypeRef(E->getArgType2(), Record);
857 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
858 Writer.AddSourceLocation(E->getRParenLoc(), Record);
859 Code = pch::EXPR_TYPES_COMPATIBLE;
860}
861
862void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
863 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000864 Writer.WriteSubStmt(E->getCond());
865 Writer.WriteSubStmt(E->getLHS());
866 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000867 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
868 Writer.AddSourceLocation(E->getRParenLoc(), Record);
869 Code = pch::EXPR_CHOOSE;
870}
871
872void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
873 VisitExpr(E);
874 Writer.AddSourceLocation(E->getTokenLocation(), Record);
875 Code = pch::EXPR_GNU_NULL;
876}
877
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000878void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
879 VisitExpr(E);
880 Record.push_back(E->getNumSubExprs());
881 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000882 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000883 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
884 Writer.AddSourceLocation(E->getRParenLoc(), Record);
885 Code = pch::EXPR_SHUFFLE_VECTOR;
886}
887
888void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
889 VisitExpr(E);
890 Writer.AddDeclRef(E->getDecl(), Record);
891 Writer.AddSourceLocation(E->getLocation(), Record);
892 Record.push_back(E->isByRef());
893 Code = pch::EXPR_BLOCK_DECL_REF;
894}
895
Douglas Gregor0b748912009-04-14 21:18:50 +0000896//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000897// PCHWriter Implementation
898//===----------------------------------------------------------------------===//
899
Douglas Gregor2bec0412009-04-10 21:16:55 +0000900/// \brief Write the target triple (e.g., i686-apple-darwin9).
901void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
902 using namespace llvm;
903 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
904 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000906 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +0000907
908 RecordData Record;
909 Record.push_back(pch::TARGET_TRIPLE);
910 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +0000911 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +0000912}
913
914/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000915void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
916 RecordData Record;
917 Record.push_back(LangOpts.Trigraphs);
918 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
919 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
920 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
921 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
922 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
923 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
924 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
925 Record.push_back(LangOpts.C99); // C99 Support
926 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
927 Record.push_back(LangOpts.CPlusPlus); // C++ Support
928 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
929 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
930 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
931
932 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
933 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
934 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
935
936 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
937 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
938 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
939 Record.push_back(LangOpts.LaxVectorConversions);
940 Record.push_back(LangOpts.Exceptions); // Support exception handling.
941
942 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
943 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
944 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
945
946 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
947 // by locks.
948 Record.push_back(LangOpts.Blocks); // block extension to C
949 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
950 // they are unused.
951 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
952 // (modulo the platform support).
953
954 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
955 // signed integer arithmetic overflows.
956
957 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
958 // may be ripped out at any time.
959
960 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
961 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
962 // defined.
963 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
964 // opposed to __DYNAMIC__).
965 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
966
967 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
968 // used (instead of C99 semantics).
969 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
970 Record.push_back(LangOpts.getGCMode());
971 Record.push_back(LangOpts.getVisibilityMode());
972 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000973 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000974}
975
Douglas Gregor14f79002009-04-10 03:52:48 +0000976//===----------------------------------------------------------------------===//
977// Source Manager Serialization
978//===----------------------------------------------------------------------===//
979
980/// \brief Create an abbreviation for the SLocEntry that refers to a
981/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000982static unsigned CreateSLocFileAbbrev(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_FILE_ENTRY));
986 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
987 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000991 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000992}
993
994/// \brief Create an abbreviation for the SLocEntry that refers to a
995/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000996static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000997 using namespace llvm;
998 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
999 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001005 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001006}
1007
1008/// \brief Create an abbreviation for the SLocEntry that refers to a
1009/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001010static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001011 using namespace llvm;
1012 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1013 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001015 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001016}
1017
1018/// \brief Create an abbreviation for the SLocEntry that refers to an
1019/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001020static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001021 using namespace llvm;
1022 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1023 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001029 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001030}
1031
1032/// \brief Writes the block containing the serialized form of the
1033/// source manager.
1034///
1035/// TODO: We should probably use an on-disk hash table (stored in a
1036/// blob), indexed based on the file name, so that we only create
1037/// entries for files that we actually need. In the common case (no
1038/// errors), we probably won't have to create file entries for any of
1039/// the files in the AST.
1040void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001041 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001042 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001043
1044 // Abbreviations for the various kinds of source-location entries.
1045 int SLocFileAbbrv = -1;
1046 int SLocBufferAbbrv = -1;
1047 int SLocBufferBlobAbbrv = -1;
1048 int SLocInstantiationAbbrv = -1;
1049
1050 // Write out the source location entry table. We skip the first
1051 // entry, which is always the same dummy entry.
1052 RecordData Record;
1053 for (SourceManager::sloc_entry_iterator
1054 SLoc = SourceMgr.sloc_entry_begin() + 1,
1055 SLocEnd = SourceMgr.sloc_entry_end();
1056 SLoc != SLocEnd; ++SLoc) {
1057 // Figure out which record code to use.
1058 unsigned Code;
1059 if (SLoc->isFile()) {
1060 if (SLoc->getFile().getContentCache()->Entry)
1061 Code = pch::SM_SLOC_FILE_ENTRY;
1062 else
1063 Code = pch::SM_SLOC_BUFFER_ENTRY;
1064 } else
1065 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1066 Record.push_back(Code);
1067
1068 Record.push_back(SLoc->getOffset());
1069 if (SLoc->isFile()) {
1070 const SrcMgr::FileInfo &File = SLoc->getFile();
1071 Record.push_back(File.getIncludeLoc().getRawEncoding());
1072 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001073 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001074
1075 const SrcMgr::ContentCache *Content = File.getContentCache();
1076 if (Content->Entry) {
1077 // The source location entry is a file. The blob associated
1078 // with this entry is the file name.
1079 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001080 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1081 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001082 Content->Entry->getName(),
1083 strlen(Content->Entry->getName()));
1084 } else {
1085 // The source location entry is a buffer. The blob associated
1086 // with this entry contains the contents of the buffer.
1087 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001088 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1089 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001090 }
1091
1092 // We add one to the size so that we capture the trailing NULL
1093 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1094 // the reader side).
1095 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1096 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001097 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001098 Record.clear();
1099 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001100 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001101 Buffer->getBufferStart(),
1102 Buffer->getBufferSize() + 1);
1103 }
1104 } else {
1105 // The source location entry is an instantiation.
1106 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1107 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1108 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1109 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1110
Douglas Gregorf60e9912009-04-15 18:05:10 +00001111 // Compute the token length for this macro expansion.
1112 unsigned NextOffset = SourceMgr.getNextOffset();
1113 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1114 if (++NextSLoc != SLocEnd)
1115 NextOffset = NextSLoc->getOffset();
1116 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1117
Douglas Gregor14f79002009-04-10 03:52:48 +00001118 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001119 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1120 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001121 }
1122
1123 Record.clear();
1124 }
1125
Douglas Gregorbd945002009-04-13 16:31:14 +00001126 // Write the line table.
1127 if (SourceMgr.hasLineTable()) {
1128 LineTableInfo &LineTable = SourceMgr.getLineTable();
1129
1130 // Emit the file names
1131 Record.push_back(LineTable.getNumFilenames());
1132 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1133 // Emit the file name
1134 const char *Filename = LineTable.getFilename(I);
1135 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1136 Record.push_back(FilenameLen);
1137 if (FilenameLen)
1138 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1139 }
1140
1141 // Emit the line entries
1142 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1143 L != LEnd; ++L) {
1144 // Emit the file ID
1145 Record.push_back(L->first);
1146
1147 // Emit the line entries
1148 Record.push_back(L->second.size());
1149 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1150 LEEnd = L->second.end();
1151 LE != LEEnd; ++LE) {
1152 Record.push_back(LE->FileOffset);
1153 Record.push_back(LE->LineNo);
1154 Record.push_back(LE->FilenameID);
1155 Record.push_back((unsigned)LE->FileKind);
1156 Record.push_back(LE->IncludeOffset);
1157 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001158 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001159 }
1160 }
1161
Douglas Gregorc9490c02009-04-16 22:23:12 +00001162 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001163}
1164
Chris Lattner0b1fb982009-04-10 17:15:23 +00001165/// \brief Writes the block containing the serialized form of the
1166/// preprocessor.
1167///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001168void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001169 // Enter the preprocessor block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001170 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001171
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001172 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1173 // FIXME: use diagnostics subsystem for localization etc.
1174 if (PP.SawDateOrTime())
1175 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001176
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001177 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001178
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001179 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1180 if (PP.getCounterValue() != 0) {
1181 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001182 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001183 Record.clear();
1184 }
1185
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001186 // Loop over all the macro definitions that are live at the end of the file,
1187 // emitting each to the PP section.
1188 // FIXME: Eventually we want to emit an index so that we can lazily load
1189 // macros.
1190 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1191 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001192 // FIXME: This emits macros in hash table order, we should do it in a stable
1193 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001194 MacroInfo *MI = I->second;
1195
1196 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1197 // been redefined by the header (in which case they are not isBuiltinMacro).
1198 if (MI->isBuiltinMacro())
1199 continue;
1200
Chris Lattner7356a312009-04-11 21:15:38 +00001201 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001202 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1203 Record.push_back(MI->isUsed());
1204
1205 unsigned Code;
1206 if (MI->isObjectLike()) {
1207 Code = pch::PP_MACRO_OBJECT_LIKE;
1208 } else {
1209 Code = pch::PP_MACRO_FUNCTION_LIKE;
1210
1211 Record.push_back(MI->isC99Varargs());
1212 Record.push_back(MI->isGNUVarargs());
1213 Record.push_back(MI->getNumArgs());
1214 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1215 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001216 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001217 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001218 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001219 Record.clear();
1220
Chris Lattnerdf961c22009-04-10 18:08:30 +00001221 // Emit the tokens array.
1222 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1223 // Note that we know that the preprocessor does not have any annotation
1224 // tokens in it because they are created by the parser, and thus can't be
1225 // in a macro definition.
1226 const Token &Tok = MI->getReplacementToken(TokNo);
1227
1228 Record.push_back(Tok.getLocation().getRawEncoding());
1229 Record.push_back(Tok.getLength());
1230
Chris Lattnerdf961c22009-04-10 18:08:30 +00001231 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1232 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001233 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001234
1235 // FIXME: Should translate token kind to a stable encoding.
1236 Record.push_back(Tok.getKind());
1237 // FIXME: Should translate token flags to a stable encoding.
1238 Record.push_back(Tok.getFlags());
1239
Douglas Gregorc9490c02009-04-16 22:23:12 +00001240 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001241 Record.clear();
1242 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001243
1244 }
1245
Douglas Gregorc9490c02009-04-16 22:23:12 +00001246 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001247}
1248
1249
Douglas Gregor2cf26342009-04-09 22:27:44 +00001250/// \brief Write the representation of a type to the PCH stream.
1251void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001252 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001253 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001254 ID = NextTypeID++;
1255
1256 // Record the offset for this type.
1257 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001258 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001259 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1260 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001261 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001262 }
1263
1264 RecordData Record;
1265
1266 // Emit the type's representation.
1267 PCHTypeWriter W(*this, Record);
1268 switch (T->getTypeClass()) {
1269 // For all of the concrete, non-dependent types, call the
1270 // appropriate visitor function.
1271#define TYPE(Class, Base) \
1272 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1273#define ABSTRACT_TYPE(Class, Base)
1274#define DEPENDENT_TYPE(Class, Base)
1275#include "clang/AST/TypeNodes.def"
1276
1277 // For all of the dependent type nodes (which only occur in C++
1278 // templates), produce an error.
1279#define TYPE(Class, Base)
1280#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1281#include "clang/AST/TypeNodes.def"
1282 assert(false && "Cannot serialize dependent type nodes");
1283 break;
1284 }
1285
1286 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001287 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001288
1289 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001290 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001291}
1292
1293/// \brief Write a block containing all of the types.
1294void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001295 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001296 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001297
1298 // Emit all of the types in the ASTContext
1299 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1300 TEnd = Context.getTypes().end();
1301 T != TEnd; ++T) {
1302 // Builtin types are never serialized.
1303 if (isa<BuiltinType>(*T))
1304 continue;
1305
1306 WriteType(*T);
1307 }
1308
1309 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001310 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001311}
1312
1313/// \brief Write the block containing all of the declaration IDs
1314/// lexically declared within the given DeclContext.
1315///
1316/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1317/// bistream, or 0 if no block was written.
1318uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1319 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001320 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001321 return 0;
1322
Douglas Gregorc9490c02009-04-16 22:23:12 +00001323 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001324 RecordData Record;
1325 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1326 DEnd = DC->decls_end(Context);
1327 D != DEnd; ++D)
1328 AddDeclRef(*D, Record);
1329
Douglas Gregorc9490c02009-04-16 22:23:12 +00001330 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001331 return Offset;
1332}
1333
1334/// \brief Write the block containing all of the declaration IDs
1335/// visible from the given DeclContext.
1336///
1337/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1338/// bistream, or 0 if no block was written.
1339uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1340 DeclContext *DC) {
1341 if (DC->getPrimaryContext() != DC)
1342 return 0;
1343
1344 // Force the DeclContext to build a its name-lookup table.
1345 DC->lookup(Context, DeclarationName());
1346
1347 // Serialize the contents of the mapping used for lookup. Note that,
1348 // although we have two very different code paths, the serialized
1349 // representation is the same for both cases: a declaration name,
1350 // followed by a size, followed by references to the visible
1351 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001352 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001353 RecordData Record;
1354 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001355 if (!Map)
1356 return 0;
1357
Douglas Gregor2cf26342009-04-09 22:27:44 +00001358 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1359 D != DEnd; ++D) {
1360 AddDeclarationName(D->first, Record);
1361 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1362 Record.push_back(Result.second - Result.first);
1363 for(; Result.first != Result.second; ++Result.first)
1364 AddDeclRef(*Result.first, Record);
1365 }
1366
1367 if (Record.size() == 0)
1368 return 0;
1369
Douglas Gregorc9490c02009-04-16 22:23:12 +00001370 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001371 return Offset;
1372}
1373
1374/// \brief Write a block containing all of the declarations.
1375void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001376 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001377 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001378
1379 // Emit all of the declarations.
1380 RecordData Record;
1381 PCHDeclWriter W(*this, Record);
1382 while (!DeclsToEmit.empty()) {
1383 // Pull the next declaration off the queue
1384 Decl *D = DeclsToEmit.front();
1385 DeclsToEmit.pop();
1386
1387 // If this declaration is also a DeclContext, write blocks for the
1388 // declarations that lexically stored inside its context and those
1389 // declarations that are visible from its context. These blocks
1390 // are written before the declaration itself so that we can put
1391 // their offsets into the record for the declaration.
1392 uint64_t LexicalOffset = 0;
1393 uint64_t VisibleOffset = 0;
1394 DeclContext *DC = dyn_cast<DeclContext>(D);
1395 if (DC) {
1396 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1397 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1398 }
1399
1400 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001401 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001402 if (ID == 0)
1403 ID = DeclIDs.size();
1404
1405 unsigned Index = ID - 1;
1406
1407 // Record the offset for this declaration
1408 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001409 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001410 else if (DeclOffsets.size() < Index) {
1411 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001412 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001413 }
1414
1415 // Build and emit a record for this declaration
1416 Record.clear();
1417 W.Code = (pch::DeclCode)0;
1418 W.Visit(D);
1419 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001420 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001421 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001422
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001423 // If the declaration had any attributes, write them now.
1424 if (D->hasAttrs())
1425 WriteAttributeRecord(D->getAttrs());
1426
Douglas Gregor0b748912009-04-14 21:18:50 +00001427 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001428 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001429
Douglas Gregorfdd01722009-04-14 00:24:19 +00001430 // Note external declarations so that we can add them to a record
1431 // in the PCH file later.
1432 if (isa<FileScopeAsmDecl>(D))
1433 ExternalDefinitions.push_back(ID);
1434 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1435 if (// Non-static file-scope variables with initializers or that
1436 // are tentative definitions.
1437 (Var->isFileVarDecl() &&
1438 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1439 // Out-of-line definitions of static data members (C++).
1440 (Var->getDeclContext()->isRecord() &&
1441 !Var->getLexicalDeclContext()->isRecord() &&
1442 Var->getStorageClass() == VarDecl::Static))
1443 ExternalDefinitions.push_back(ID);
1444 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1445 if (Func->isThisDeclarationADefinition() &&
1446 Func->getStorageClass() != FunctionDecl::Static &&
1447 !Func->isInline())
1448 ExternalDefinitions.push_back(ID);
1449 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001450 }
1451
1452 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001453 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001454}
1455
Douglas Gregorafaf3082009-04-11 00:14:32 +00001456/// \brief Write the identifier table into the PCH file.
1457///
1458/// The identifier table consists of a blob containing string data
1459/// (the actual identifiers themselves) and a separate "offsets" index
1460/// that maps identifier IDs to locations within the blob.
1461void PCHWriter::WriteIdentifierTable() {
1462 using namespace llvm;
1463
1464 // Create and write out the blob that contains the identifier
1465 // strings.
1466 RecordData IdentOffsets;
1467 IdentOffsets.resize(IdentifierIDs.size());
1468 {
1469 // Create the identifier string data.
1470 std::vector<char> Data;
1471 Data.push_back(0); // Data must not be empty.
1472 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1473 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1474 ID != IDEnd; ++ID) {
1475 assert(ID->first && "NULL identifier in identifier table");
1476
1477 // Make sure we're starting on an odd byte. The PCH reader
1478 // expects the low bit to be set on all of the offsets.
1479 if ((Data.size() & 0x01) == 0)
1480 Data.push_back((char)0);
1481
1482 IdentOffsets[ID->second - 1] = Data.size();
1483 Data.insert(Data.end(),
1484 ID->first->getName(),
1485 ID->first->getName() + ID->first->getLength());
1486 Data.push_back((char)0);
1487 }
1488
1489 // Create a blob abbreviation
1490 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1491 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1492 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001493 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001494
1495 // Write the identifier table
1496 RecordData Record;
1497 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001498 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001499 }
1500
1501 // Write the offsets table for identifier IDs.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001502 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001503}
1504
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001505/// \brief Write a record containing the given attributes.
1506void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1507 RecordData Record;
1508 for (; Attr; Attr = Attr->getNext()) {
1509 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1510 Record.push_back(Attr->isInherited());
1511 switch (Attr->getKind()) {
1512 case Attr::Alias:
1513 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1514 break;
1515
1516 case Attr::Aligned:
1517 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1518 break;
1519
1520 case Attr::AlwaysInline:
1521 break;
1522
1523 case Attr::AnalyzerNoReturn:
1524 break;
1525
1526 case Attr::Annotate:
1527 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1528 break;
1529
1530 case Attr::AsmLabel:
1531 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1532 break;
1533
1534 case Attr::Blocks:
1535 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1536 break;
1537
1538 case Attr::Cleanup:
1539 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1540 break;
1541
1542 case Attr::Const:
1543 break;
1544
1545 case Attr::Constructor:
1546 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1547 break;
1548
1549 case Attr::DLLExport:
1550 case Attr::DLLImport:
1551 case Attr::Deprecated:
1552 break;
1553
1554 case Attr::Destructor:
1555 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1556 break;
1557
1558 case Attr::FastCall:
1559 break;
1560
1561 case Attr::Format: {
1562 const FormatAttr *Format = cast<FormatAttr>(Attr);
1563 AddString(Format->getType(), Record);
1564 Record.push_back(Format->getFormatIdx());
1565 Record.push_back(Format->getFirstArg());
1566 break;
1567 }
1568
1569 case Attr::GNUCInline:
1570 case Attr::IBOutletKind:
1571 case Attr::NoReturn:
1572 case Attr::NoThrow:
1573 case Attr::Nodebug:
1574 case Attr::Noinline:
1575 break;
1576
1577 case Attr::NonNull: {
1578 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1579 Record.push_back(NonNull->size());
1580 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1581 break;
1582 }
1583
1584 case Attr::ObjCException:
1585 case Attr::ObjCNSObject:
1586 case Attr::Overloadable:
1587 break;
1588
1589 case Attr::Packed:
1590 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1591 break;
1592
1593 case Attr::Pure:
1594 break;
1595
1596 case Attr::Regparm:
1597 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1598 break;
1599
1600 case Attr::Section:
1601 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1602 break;
1603
1604 case Attr::StdCall:
1605 case Attr::TransparentUnion:
1606 case Attr::Unavailable:
1607 case Attr::Unused:
1608 case Attr::Used:
1609 break;
1610
1611 case Attr::Visibility:
1612 // FIXME: stable encoding
1613 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1614 break;
1615
1616 case Attr::WarnUnusedResult:
1617 case Attr::Weak:
1618 case Attr::WeakImport:
1619 break;
1620 }
1621 }
1622
Douglas Gregorc9490c02009-04-16 22:23:12 +00001623 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001624}
1625
1626void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1627 Record.push_back(Str.size());
1628 Record.insert(Record.end(), Str.begin(), Str.end());
1629}
1630
Douglas Gregorc9490c02009-04-16 22:23:12 +00001631PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1632 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001633
Chris Lattnerdf961c22009-04-10 18:08:30 +00001634void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001635 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001636 Stream.Emit((unsigned)'C', 8);
1637 Stream.Emit((unsigned)'P', 8);
1638 Stream.Emit((unsigned)'C', 8);
1639 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001640
1641 // The translation unit is the first declaration we'll emit.
1642 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1643 DeclsToEmit.push(Context.getTranslationUnitDecl());
1644
1645 // Write the remaining PCH contents.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001646 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001647 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001648 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001649 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001650 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001651 WriteTypesBlock(Context);
1652 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001653 WriteIdentifierTable();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001654 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1655 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001656 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00001657 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
1658 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001659}
1660
1661void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1662 Record.push_back(Loc.getRawEncoding());
1663}
1664
1665void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1666 Record.push_back(Value.getBitWidth());
1667 unsigned N = Value.getNumWords();
1668 const uint64_t* Words = Value.getRawData();
1669 for (unsigned I = 0; I != N; ++I)
1670 Record.push_back(Words[I]);
1671}
1672
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001673void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1674 Record.push_back(Value.isUnsigned());
1675 AddAPInt(Value, Record);
1676}
1677
Douglas Gregor17fc2232009-04-14 21:55:33 +00001678void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1679 AddAPInt(Value.bitcastToAPInt(), Record);
1680}
1681
Douglas Gregor2cf26342009-04-09 22:27:44 +00001682void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001683 if (II == 0) {
1684 Record.push_back(0);
1685 return;
1686 }
1687
1688 pch::IdentID &ID = IdentifierIDs[II];
1689 if (ID == 0)
1690 ID = IdentifierIDs.size();
1691
1692 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001693}
1694
1695void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1696 if (T.isNull()) {
1697 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1698 return;
1699 }
1700
1701 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001702 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001703 switch (BT->getKind()) {
1704 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1705 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1706 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1707 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1708 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1709 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1710 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1711 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1712 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1713 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1714 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1715 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1716 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1717 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1718 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1719 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1720 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1721 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1722 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1723 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1724 }
1725
1726 Record.push_back((ID << 3) | T.getCVRQualifiers());
1727 return;
1728 }
1729
Douglas Gregor8038d512009-04-10 17:25:41 +00001730 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001731 if (ID == 0) // we haven't seen this type before
1732 ID = NextTypeID++;
1733
1734 // Encode the type qualifiers in the type reference.
1735 Record.push_back((ID << 3) | T.getCVRQualifiers());
1736}
1737
1738void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1739 if (D == 0) {
1740 Record.push_back(0);
1741 return;
1742 }
1743
Douglas Gregor8038d512009-04-10 17:25:41 +00001744 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001745 if (ID == 0) {
1746 // We haven't seen this declaration before. Give it a new ID and
1747 // enqueue it in the list of declarations to emit.
1748 ID = DeclIDs.size();
1749 DeclsToEmit.push(const_cast<Decl *>(D));
1750 }
1751
1752 Record.push_back(ID);
1753}
1754
1755void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1756 Record.push_back(Name.getNameKind());
1757 switch (Name.getNameKind()) {
1758 case DeclarationName::Identifier:
1759 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1760 break;
1761
1762 case DeclarationName::ObjCZeroArgSelector:
1763 case DeclarationName::ObjCOneArgSelector:
1764 case DeclarationName::ObjCMultiArgSelector:
1765 assert(false && "Serialization of Objective-C selectors unavailable");
1766 break;
1767
1768 case DeclarationName::CXXConstructorName:
1769 case DeclarationName::CXXDestructorName:
1770 case DeclarationName::CXXConversionFunctionName:
1771 AddTypeRef(Name.getCXXNameType(), Record);
1772 break;
1773
1774 case DeclarationName::CXXOperatorName:
1775 Record.push_back(Name.getCXXOverloadedOperator());
1776 break;
1777
1778 case DeclarationName::CXXUsingDirective:
1779 // No extra data to emit
1780 break;
1781 }
1782}
Douglas Gregor0b748912009-04-14 21:18:50 +00001783
Douglas Gregorc9490c02009-04-16 22:23:12 +00001784/// \brief Write the given substatement or subexpression to the
1785/// bitstream.
1786void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00001787 RecordData Record;
1788 PCHStmtWriter Writer(*this, Record);
1789
Douglas Gregorc9490c02009-04-16 22:23:12 +00001790 if (!S) {
1791 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001792 return;
1793 }
1794
Douglas Gregorc9490c02009-04-16 22:23:12 +00001795 Writer.Code = pch::STMT_NULL_PTR;
1796 Writer.Visit(S);
1797 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00001798 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001799 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001800}
1801
Douglas Gregorc9490c02009-04-16 22:23:12 +00001802/// \brief Flush all of the statements that have been added to the
1803/// queue via AddStmt().
1804void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00001805 RecordData Record;
1806 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001807
Douglas Gregorc9490c02009-04-16 22:23:12 +00001808 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1809 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00001810
Douglas Gregorc9490c02009-04-16 22:23:12 +00001811 if (!S) {
1812 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001813 continue;
1814 }
1815
Douglas Gregorc9490c02009-04-16 22:23:12 +00001816 Writer.Code = pch::STMT_NULL_PTR;
1817 Writer.Visit(S);
1818 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00001819 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001820 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001821
Douglas Gregorc9490c02009-04-16 22:23:12 +00001822 assert(N == StmtsToEmit.size() &&
1823 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00001824
1825 // Note that we are at the end of a full expression. Any
1826 // expression records that follow this one are part of a different
1827 // expression.
1828 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001829 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001830 }
Douglas Gregor087fd532009-04-14 23:32:43 +00001831
Douglas Gregorc9490c02009-04-16 22:23:12 +00001832 StmtsToEmit.clear();
Douglas Gregor0de9d882009-04-17 16:34:57 +00001833 SwitchCaseIDs.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00001834}
Douglas Gregor025452f2009-04-17 00:04:06 +00001835
1836unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1837 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1838 "SwitchCase recorded twice");
1839 unsigned NextID = SwitchCaseIDs.size();
1840 SwitchCaseIDs[S] = NextID;
1841 return NextID;
1842}
1843
1844unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1845 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1846 "SwitchCase hasn't been seen yet");
1847 return SwitchCaseIDs[S];
1848}