blob: 1efe70f270feaef5d7995eef2797a7e50b97b5d5 [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;
Douglas Gregor72971342009-04-18 00:02:19 +0000244 ASTContext &Context;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000245 PCHWriter::RecordData &Record;
246
247 public:
248 pch::DeclCode Code;
249
Douglas Gregor72971342009-04-18 00:02:19 +0000250 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
251 PCHWriter::RecordData &Record)
252 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253
254 void VisitDecl(Decl *D);
255 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
256 void VisitNamedDecl(NamedDecl *D);
257 void VisitTypeDecl(TypeDecl *D);
258 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000259 void VisitTagDecl(TagDecl *D);
260 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000261 void VisitRecordDecl(RecordDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000262 void VisitValueDecl(ValueDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000263 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000264 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000265 void VisitFieldDecl(FieldDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000266 void VisitVarDecl(VarDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000267 void VisitParmVarDecl(ParmVarDecl *D);
268 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor1028bc62009-04-13 22:49:25 +0000269 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
270 void VisitBlockDecl(BlockDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000271 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
272 uint64_t VisibleOffset);
273 };
274}
275
276void PCHDeclWriter::VisitDecl(Decl *D) {
277 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
278 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
279 Writer.AddSourceLocation(D->getLocation(), Record);
280 Record.push_back(D->isInvalidDecl());
Douglas Gregor68a2eb02009-04-15 21:30:51 +0000281 Record.push_back(D->hasAttrs());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000282 Record.push_back(D->isImplicit());
283 Record.push_back(D->getAccess());
284}
285
286void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
287 VisitDecl(D);
288 Code = pch::DECL_TRANSLATION_UNIT;
289}
290
291void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
292 VisitDecl(D);
293 Writer.AddDeclarationName(D->getDeclName(), Record);
294}
295
296void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
297 VisitNamedDecl(D);
298 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
299}
300
301void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
302 VisitTypeDecl(D);
303 Writer.AddTypeRef(D->getUnderlyingType(), Record);
304 Code = pch::DECL_TYPEDEF;
305}
306
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000307void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
308 VisitTypeDecl(D);
309 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
310 Record.push_back(D->isDefinition());
311 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
312}
313
314void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
315 VisitTagDecl(D);
316 Writer.AddTypeRef(D->getIntegerType(), Record);
317 Code = pch::DECL_ENUM;
318}
319
Douglas Gregor8c700062009-04-13 21:20:57 +0000320void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
321 VisitTagDecl(D);
322 Record.push_back(D->hasFlexibleArrayMember());
323 Record.push_back(D->isAnonymousStructOrUnion());
324 Code = pch::DECL_RECORD;
325}
326
Douglas Gregor2cf26342009-04-09 22:27:44 +0000327void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
328 VisitNamedDecl(D);
329 Writer.AddTypeRef(D->getType(), Record);
330}
331
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000332void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
333 VisitValueDecl(D);
Douglas Gregor0b748912009-04-14 21:18:50 +0000334 Record.push_back(D->getInitExpr()? 1 : 0);
335 if (D->getInitExpr())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000336 Writer.AddStmt(D->getInitExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000337 Writer.AddAPSInt(D->getInitVal(), Record);
338 Code = pch::DECL_ENUM_CONSTANT;
339}
340
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000341void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
342 VisitValueDecl(D);
Douglas Gregor025452f2009-04-17 00:04:06 +0000343 Record.push_back(D->isThisDeclarationADefinition());
344 if (D->isThisDeclarationADefinition())
Douglas Gregor72971342009-04-18 00:02:19 +0000345 Writer.AddStmt(D->getBody(Context));
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000346 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
347 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
348 Record.push_back(D->isInline());
349 Record.push_back(D->isVirtual());
350 Record.push_back(D->isPure());
351 Record.push_back(D->inheritedPrototype());
352 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
353 Record.push_back(D->isDeleted());
354 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
355 Record.push_back(D->param_size());
356 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
357 P != PEnd; ++P)
358 Writer.AddDeclRef(*P, Record);
359 Code = pch::DECL_FUNCTION;
360}
361
Douglas Gregor8c700062009-04-13 21:20:57 +0000362void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
363 VisitValueDecl(D);
364 Record.push_back(D->isMutable());
Douglas Gregor0b748912009-04-14 21:18:50 +0000365 Record.push_back(D->getBitWidth()? 1 : 0);
366 if (D->getBitWidth())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000367 Writer.AddStmt(D->getBitWidth());
Douglas Gregor8c700062009-04-13 21:20:57 +0000368 Code = pch::DECL_FIELD;
369}
370
Douglas Gregor2cf26342009-04-09 22:27:44 +0000371void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
372 VisitValueDecl(D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000373 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor2cf26342009-04-09 22:27:44 +0000374 Record.push_back(D->isThreadSpecified());
375 Record.push_back(D->hasCXXDirectInitializer());
376 Record.push_back(D->isDeclaredInCondition());
377 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
378 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregor0b748912009-04-14 21:18:50 +0000379 Record.push_back(D->getInit()? 1 : 0);
380 if (D->getInit())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000381 Writer.AddStmt(D->getInit());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000382 Code = pch::DECL_VAR;
383}
384
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000385void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
386 VisitVarDecl(D);
387 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000388 // FIXME: emit default argument (C++)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000389 // FIXME: why isn't the "default argument" just stored as the initializer
390 // in VarDecl?
391 Code = pch::DECL_PARM_VAR;
392}
393
394void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
395 VisitParmVarDecl(D);
396 Writer.AddTypeRef(D->getOriginalType(), Record);
397 Code = pch::DECL_ORIGINAL_PARM_VAR;
398}
399
Douglas Gregor1028bc62009-04-13 22:49:25 +0000400void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
401 VisitDecl(D);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000402 Writer.AddStmt(D->getAsmString());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000403 Code = pch::DECL_FILE_SCOPE_ASM;
404}
405
406void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
407 VisitDecl(D);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000408 Writer.AddStmt(D->getBody());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000409 Record.push_back(D->param_size());
410 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
411 P != PEnd; ++P)
412 Writer.AddDeclRef(*P, Record);
413 Code = pch::DECL_BLOCK;
414}
415
Douglas Gregor2cf26342009-04-09 22:27:44 +0000416/// \brief Emit the DeclContext part of a declaration context decl.
417///
418/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
419/// block for this declaration context is stored. May be 0 to indicate
420/// that there are no declarations stored within this context.
421///
422/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
423/// block for this declaration context is stored. May be 0 to indicate
424/// that there are no declarations visible from this context. Note
425/// that this value will not be emitted for non-primary declaration
426/// contexts.
427void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
428 uint64_t VisibleOffset) {
429 Record.push_back(LexicalOffset);
430 if (DC->getPrimaryContext() == DC)
431 Record.push_back(VisibleOffset);
432}
433
434//===----------------------------------------------------------------------===//
Douglas Gregor0b748912009-04-14 21:18:50 +0000435// Statement/expression serialization
436//===----------------------------------------------------------------------===//
437namespace {
438 class VISIBILITY_HIDDEN PCHStmtWriter
439 : public StmtVisitor<PCHStmtWriter, void> {
440
441 PCHWriter &Writer;
442 PCHWriter::RecordData &Record;
443
444 public:
445 pch::StmtCode Code;
446
447 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
448 : Writer(Writer), Record(Record) { }
449
Douglas Gregor025452f2009-04-17 00:04:06 +0000450 void VisitStmt(Stmt *S);
451 void VisitNullStmt(NullStmt *S);
452 void VisitCompoundStmt(CompoundStmt *S);
453 void VisitSwitchCase(SwitchCase *S);
454 void VisitCaseStmt(CaseStmt *S);
455 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000456 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000457 void VisitIfStmt(IfStmt *S);
458 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000459 void VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000460 void VisitDoStmt(DoStmt *S);
461 void VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000462 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000463 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000464 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000465 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000466 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000467 void VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000468 void VisitAsmStmt(AsmStmt *S);
Douglas Gregor0b748912009-04-14 21:18:50 +0000469 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000470 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000471 void VisitDeclRefExpr(DeclRefExpr *E);
472 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000473 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000474 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000475 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000476 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000477 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000478 void VisitUnaryOperator(UnaryOperator *E);
479 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000480 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000481 void VisitCallExpr(CallExpr *E);
482 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000483 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000484 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000485 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
486 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000487 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000488 void VisitExplicitCastExpr(ExplicitCastExpr *E);
489 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000490 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000491 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000492 void VisitInitListExpr(InitListExpr *E);
493 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
494 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000495 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000496 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000497 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000498 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
499 void VisitChooseExpr(ChooseExpr *E);
500 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000501 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000502 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000503 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000504 };
505}
506
Douglas Gregor025452f2009-04-17 00:04:06 +0000507void PCHStmtWriter::VisitStmt(Stmt *S) {
508}
509
510void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
511 VisitStmt(S);
512 Writer.AddSourceLocation(S->getSemiLoc(), Record);
513 Code = pch::STMT_NULL;
514}
515
516void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
517 VisitStmt(S);
518 Record.push_back(S->size());
519 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
520 CS != CSEnd; ++CS)
521 Writer.WriteSubStmt(*CS);
522 Writer.AddSourceLocation(S->getLBracLoc(), Record);
523 Writer.AddSourceLocation(S->getRBracLoc(), Record);
524 Code = pch::STMT_COMPOUND;
525}
526
527void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
528 VisitStmt(S);
529 Record.push_back(Writer.RecordSwitchCaseID(S));
530}
531
532void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
533 VisitSwitchCase(S);
534 Writer.WriteSubStmt(S->getLHS());
535 Writer.WriteSubStmt(S->getRHS());
536 Writer.WriteSubStmt(S->getSubStmt());
537 Writer.AddSourceLocation(S->getCaseLoc(), Record);
538 Code = pch::STMT_CASE;
539}
540
541void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
542 VisitSwitchCase(S);
543 Writer.WriteSubStmt(S->getSubStmt());
544 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
545 Code = pch::STMT_DEFAULT;
546}
547
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000548void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
549 VisitStmt(S);
550 Writer.AddIdentifierRef(S->getID(), Record);
551 Writer.WriteSubStmt(S->getSubStmt());
552 Writer.AddSourceLocation(S->getIdentLoc(), Record);
553 Record.push_back(Writer.GetLabelID(S));
554 Code = pch::STMT_LABEL;
555}
556
Douglas Gregor025452f2009-04-17 00:04:06 +0000557void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
558 VisitStmt(S);
559 Writer.WriteSubStmt(S->getCond());
560 Writer.WriteSubStmt(S->getThen());
561 Writer.WriteSubStmt(S->getElse());
562 Writer.AddSourceLocation(S->getIfLoc(), Record);
563 Code = pch::STMT_IF;
564}
565
566void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
567 VisitStmt(S);
568 Writer.WriteSubStmt(S->getCond());
569 Writer.WriteSubStmt(S->getBody());
570 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
571 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
572 SC = SC->getNextSwitchCase())
573 Record.push_back(Writer.getSwitchCaseID(SC));
574 Code = pch::STMT_SWITCH;
575}
576
Douglas Gregord921cf92009-04-17 00:16:09 +0000577void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
578 VisitStmt(S);
579 Writer.WriteSubStmt(S->getCond());
580 Writer.WriteSubStmt(S->getBody());
581 Writer.AddSourceLocation(S->getWhileLoc(), Record);
582 Code = pch::STMT_WHILE;
583}
584
Douglas Gregor67d82492009-04-17 00:29:51 +0000585void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
586 VisitStmt(S);
587 Writer.WriteSubStmt(S->getCond());
588 Writer.WriteSubStmt(S->getBody());
589 Writer.AddSourceLocation(S->getDoLoc(), Record);
590 Code = pch::STMT_DO;
591}
592
593void PCHStmtWriter::VisitForStmt(ForStmt *S) {
594 VisitStmt(S);
595 Writer.WriteSubStmt(S->getInit());
596 Writer.WriteSubStmt(S->getCond());
597 Writer.WriteSubStmt(S->getInc());
598 Writer.WriteSubStmt(S->getBody());
599 Writer.AddSourceLocation(S->getForLoc(), Record);
600 Code = pch::STMT_FOR;
601}
602
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000603void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
604 VisitStmt(S);
605 Record.push_back(Writer.GetLabelID(S->getLabel()));
606 Writer.AddSourceLocation(S->getGotoLoc(), Record);
607 Writer.AddSourceLocation(S->getLabelLoc(), Record);
608 Code = pch::STMT_GOTO;
609}
610
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000611void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
612 VisitStmt(S);
613 Writer.WriteSubStmt(S->getTarget());
614 Code = pch::STMT_INDIRECT_GOTO;
615}
616
Douglas Gregord921cf92009-04-17 00:16:09 +0000617void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
618 VisitStmt(S);
619 Writer.AddSourceLocation(S->getContinueLoc(), Record);
620 Code = pch::STMT_CONTINUE;
621}
622
Douglas Gregor025452f2009-04-17 00:04:06 +0000623void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
624 VisitStmt(S);
625 Writer.AddSourceLocation(S->getBreakLoc(), Record);
626 Code = pch::STMT_BREAK;
627}
628
Douglas Gregor0de9d882009-04-17 16:34:57 +0000629void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
630 VisitStmt(S);
631 Writer.WriteSubStmt(S->getRetValue());
632 Writer.AddSourceLocation(S->getReturnLoc(), Record);
633 Code = pch::STMT_RETURN;
634}
635
Douglas Gregor84f21702009-04-17 16:55:36 +0000636void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
637 VisitStmt(S);
638 Writer.AddSourceLocation(S->getStartLoc(), Record);
639 Writer.AddSourceLocation(S->getEndLoc(), Record);
640 DeclGroupRef DG = S->getDeclGroup();
641 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
642 Writer.AddDeclRef(*D, Record);
643 Code = pch::STMT_DECL;
644}
645
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000646void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
647 VisitStmt(S);
648 Record.push_back(S->getNumOutputs());
649 Record.push_back(S->getNumInputs());
650 Record.push_back(S->getNumClobbers());
651 Writer.AddSourceLocation(S->getAsmLoc(), Record);
652 Writer.AddSourceLocation(S->getRParenLoc(), Record);
653 Record.push_back(S->isVolatile());
654 Record.push_back(S->isSimple());
655 Writer.WriteSubStmt(S->getAsmString());
656
657 // Outputs
658 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
659 Writer.AddString(S->getOutputName(I), Record);
660 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
661 Writer.WriteSubStmt(S->getOutputExpr(I));
662 }
663
664 // Inputs
665 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
666 Writer.AddString(S->getInputName(I), Record);
667 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
668 Writer.WriteSubStmt(S->getInputExpr(I));
669 }
670
671 // Clobbers
672 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
673 Writer.WriteSubStmt(S->getClobber(I));
674
675 Code = pch::STMT_ASM;
676}
677
Douglas Gregor0b748912009-04-14 21:18:50 +0000678void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000679 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000680 Writer.AddTypeRef(E->getType(), Record);
681 Record.push_back(E->isTypeDependent());
682 Record.push_back(E->isValueDependent());
683}
684
Douglas Gregor17fc2232009-04-14 21:55:33 +0000685void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
686 VisitExpr(E);
687 Writer.AddSourceLocation(E->getLocation(), Record);
688 Record.push_back(E->getIdentType()); // FIXME: stable encoding
689 Code = pch::EXPR_PREDEFINED;
690}
691
Douglas Gregor0b748912009-04-14 21:18:50 +0000692void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
693 VisitExpr(E);
694 Writer.AddDeclRef(E->getDecl(), Record);
695 Writer.AddSourceLocation(E->getLocation(), Record);
696 Code = pch::EXPR_DECL_REF;
697}
698
699void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
700 VisitExpr(E);
701 Writer.AddSourceLocation(E->getLocation(), Record);
702 Writer.AddAPInt(E->getValue(), Record);
703 Code = pch::EXPR_INTEGER_LITERAL;
704}
705
Douglas Gregor17fc2232009-04-14 21:55:33 +0000706void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
707 VisitExpr(E);
708 Writer.AddAPFloat(E->getValue(), Record);
709 Record.push_back(E->isExact());
710 Writer.AddSourceLocation(E->getLocation(), Record);
711 Code = pch::EXPR_FLOATING_LITERAL;
712}
713
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000714void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
715 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000716 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000717 Code = pch::EXPR_IMAGINARY_LITERAL;
718}
719
Douglas Gregor673ecd62009-04-15 16:35:07 +0000720void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
721 VisitExpr(E);
722 Record.push_back(E->getByteLength());
723 Record.push_back(E->getNumConcatenated());
724 Record.push_back(E->isWide());
725 // FIXME: String data should be stored as a blob at the end of the
726 // StringLiteral. However, we can't do so now because we have no
727 // provision for coping with abbreviations when we're jumping around
728 // the PCH file during deserialization.
729 Record.insert(Record.end(),
730 E->getStrData(), E->getStrData() + E->getByteLength());
731 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
732 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
733 Code = pch::EXPR_STRING_LITERAL;
734}
735
Douglas Gregor0b748912009-04-14 21:18:50 +0000736void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
737 VisitExpr(E);
738 Record.push_back(E->getValue());
739 Writer.AddSourceLocation(E->getLoc(), Record);
740 Record.push_back(E->isWide());
741 Code = pch::EXPR_CHARACTER_LITERAL;
742}
743
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000744void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
745 VisitExpr(E);
746 Writer.AddSourceLocation(E->getLParen(), Record);
747 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000748 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000749 Code = pch::EXPR_PAREN;
750}
751
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000752void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
753 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000754 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000755 Record.push_back(E->getOpcode()); // FIXME: stable encoding
756 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
757 Code = pch::EXPR_UNARY_OPERATOR;
758}
759
760void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
761 VisitExpr(E);
762 Record.push_back(E->isSizeOf());
763 if (E->isArgumentType())
764 Writer.AddTypeRef(E->getArgumentType(), Record);
765 else {
766 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000767 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000768 }
769 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
770 Writer.AddSourceLocation(E->getRParenLoc(), Record);
771 Code = pch::EXPR_SIZEOF_ALIGN_OF;
772}
773
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000774void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
775 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000776 Writer.WriteSubStmt(E->getLHS());
777 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000778 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
779 Code = pch::EXPR_ARRAY_SUBSCRIPT;
780}
781
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000782void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
783 VisitExpr(E);
784 Record.push_back(E->getNumArgs());
785 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000786 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000787 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
788 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000789 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000790 Code = pch::EXPR_CALL;
791}
792
793void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
794 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000795 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000796 Writer.AddDeclRef(E->getMemberDecl(), Record);
797 Writer.AddSourceLocation(E->getMemberLoc(), Record);
798 Record.push_back(E->isArrow());
799 Code = pch::EXPR_MEMBER;
800}
801
Douglas Gregor087fd532009-04-14 23:32:43 +0000802void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
803 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000804 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000805}
806
Douglas Gregordb600c32009-04-15 00:25:59 +0000807void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
808 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000809 Writer.WriteSubStmt(E->getLHS());
810 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000811 Record.push_back(E->getOpcode()); // FIXME: stable encoding
812 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
813 Code = pch::EXPR_BINARY_OPERATOR;
814}
815
Douglas Gregorad90e962009-04-15 22:40:36 +0000816void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
817 VisitBinaryOperator(E);
818 Writer.AddTypeRef(E->getComputationLHSType(), Record);
819 Writer.AddTypeRef(E->getComputationResultType(), Record);
820 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
821}
822
823void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
824 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000825 Writer.WriteSubStmt(E->getCond());
826 Writer.WriteSubStmt(E->getLHS());
827 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000828 Code = pch::EXPR_CONDITIONAL_OPERATOR;
829}
830
Douglas Gregor087fd532009-04-14 23:32:43 +0000831void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
832 VisitCastExpr(E);
833 Record.push_back(E->isLvalueCast());
834 Code = pch::EXPR_IMPLICIT_CAST;
835}
836
Douglas Gregordb600c32009-04-15 00:25:59 +0000837void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
838 VisitCastExpr(E);
839 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
840}
841
842void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
843 VisitExplicitCastExpr(E);
844 Writer.AddSourceLocation(E->getLParenLoc(), Record);
845 Writer.AddSourceLocation(E->getRParenLoc(), Record);
846 Code = pch::EXPR_CSTYLE_CAST;
847}
848
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000849void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
850 VisitExpr(E);
851 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000852 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000853 Record.push_back(E->isFileScope());
854 Code = pch::EXPR_COMPOUND_LITERAL;
855}
856
Douglas Gregord3c98a02009-04-15 23:02:49 +0000857void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
858 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000859 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000860 Writer.AddIdentifierRef(&E->getAccessor(), Record);
861 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
862 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
863}
864
Douglas Gregord077d752009-04-16 00:55:48 +0000865void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
866 VisitExpr(E);
867 Record.push_back(E->getNumInits());
868 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000869 Writer.WriteSubStmt(E->getInit(I));
870 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +0000871 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
872 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
873 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
874 Record.push_back(E->hadArrayRangeDesignator());
875 Code = pch::EXPR_INIT_LIST;
876}
877
878void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *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->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +0000883 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
884 Record.push_back(E->usesGNUSyntax());
885 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
886 DEnd = E->designators_end();
887 D != DEnd; ++D) {
888 if (D->isFieldDesignator()) {
889 if (FieldDecl *Field = D->getField()) {
890 Record.push_back(pch::DESIG_FIELD_DECL);
891 Writer.AddDeclRef(Field, Record);
892 } else {
893 Record.push_back(pch::DESIG_FIELD_NAME);
894 Writer.AddIdentifierRef(D->getFieldName(), Record);
895 }
896 Writer.AddSourceLocation(D->getDotLoc(), Record);
897 Writer.AddSourceLocation(D->getFieldLoc(), Record);
898 } else if (D->isArrayDesignator()) {
899 Record.push_back(pch::DESIG_ARRAY);
900 Record.push_back(D->getFirstExprIndex());
901 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
902 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
903 } else {
904 assert(D->isArrayRangeDesignator() && "Unknown designator");
905 Record.push_back(pch::DESIG_ARRAY_RANGE);
906 Record.push_back(D->getFirstExprIndex());
907 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
908 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
909 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
910 }
911 }
912 Code = pch::EXPR_DESIGNATED_INIT;
913}
914
915void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
916 VisitExpr(E);
917 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
918}
919
Douglas Gregord3c98a02009-04-15 23:02:49 +0000920void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
921 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000922 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000923 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
924 Writer.AddSourceLocation(E->getRParenLoc(), Record);
925 Code = pch::EXPR_VA_ARG;
926}
927
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000928void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
929 VisitExpr(E);
930 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
931 Writer.AddSourceLocation(E->getLabelLoc(), Record);
932 Record.push_back(Writer.GetLabelID(E->getLabel()));
933 Code = pch::EXPR_ADDR_LABEL;
934}
935
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000936void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
937 VisitExpr(E);
938 Writer.WriteSubStmt(E->getSubStmt());
939 Writer.AddSourceLocation(E->getLParenLoc(), Record);
940 Writer.AddSourceLocation(E->getRParenLoc(), Record);
941 Code = pch::EXPR_STMT;
942}
943
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000944void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
945 VisitExpr(E);
946 Writer.AddTypeRef(E->getArgType1(), Record);
947 Writer.AddTypeRef(E->getArgType2(), Record);
948 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
949 Writer.AddSourceLocation(E->getRParenLoc(), Record);
950 Code = pch::EXPR_TYPES_COMPATIBLE;
951}
952
953void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
954 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000955 Writer.WriteSubStmt(E->getCond());
956 Writer.WriteSubStmt(E->getLHS());
957 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000958 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
959 Writer.AddSourceLocation(E->getRParenLoc(), Record);
960 Code = pch::EXPR_CHOOSE;
961}
962
963void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
964 VisitExpr(E);
965 Writer.AddSourceLocation(E->getTokenLocation(), Record);
966 Code = pch::EXPR_GNU_NULL;
967}
968
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000969void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
970 VisitExpr(E);
971 Record.push_back(E->getNumSubExprs());
972 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000973 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000974 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
975 Writer.AddSourceLocation(E->getRParenLoc(), Record);
976 Code = pch::EXPR_SHUFFLE_VECTOR;
977}
978
Douglas Gregor84af7c22009-04-17 19:21:43 +0000979void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
980 VisitExpr(E);
981 Writer.AddDeclRef(E->getBlockDecl(), Record);
982 Record.push_back(E->hasBlockDeclRefExprs());
983 Code = pch::EXPR_BLOCK;
984}
985
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000986void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
987 VisitExpr(E);
988 Writer.AddDeclRef(E->getDecl(), Record);
989 Writer.AddSourceLocation(E->getLocation(), Record);
990 Record.push_back(E->isByRef());
991 Code = pch::EXPR_BLOCK_DECL_REF;
992}
993
Douglas Gregor0b748912009-04-14 21:18:50 +0000994//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000995// PCHWriter Implementation
996//===----------------------------------------------------------------------===//
997
Douglas Gregor2bec0412009-04-10 21:16:55 +0000998/// \brief Write the target triple (e.g., i686-apple-darwin9).
999void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1000 using namespace llvm;
1001 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1002 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001004 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001005
1006 RecordData Record;
1007 Record.push_back(pch::TARGET_TRIPLE);
1008 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001009 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +00001010}
1011
1012/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001013void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1014 RecordData Record;
1015 Record.push_back(LangOpts.Trigraphs);
1016 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1017 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1018 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1019 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1020 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1021 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1022 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1023 Record.push_back(LangOpts.C99); // C99 Support
1024 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1025 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1026 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1027 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1028 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1029
1030 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1031 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1032 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1033
1034 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1035 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1036 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1037 Record.push_back(LangOpts.LaxVectorConversions);
1038 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1039
1040 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1041 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1042 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1043
1044 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1045 // by locks.
1046 Record.push_back(LangOpts.Blocks); // block extension to C
1047 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1048 // they are unused.
1049 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1050 // (modulo the platform support).
1051
1052 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1053 // signed integer arithmetic overflows.
1054
1055 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1056 // may be ripped out at any time.
1057
1058 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1059 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1060 // defined.
1061 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1062 // opposed to __DYNAMIC__).
1063 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1064
1065 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1066 // used (instead of C99 semantics).
1067 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1068 Record.push_back(LangOpts.getGCMode());
1069 Record.push_back(LangOpts.getVisibilityMode());
1070 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001071 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001072}
1073
Douglas Gregor14f79002009-04-10 03:52:48 +00001074//===----------------------------------------------------------------------===//
1075// Source Manager Serialization
1076//===----------------------------------------------------------------------===//
1077
1078/// \brief Create an abbreviation for the SLocEntry that refers to a
1079/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001080static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001081 using namespace llvm;
1082 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1083 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +00001088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001089 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001090}
1091
1092/// \brief Create an abbreviation for the SLocEntry that refers to a
1093/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001094static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001095 using namespace llvm;
1096 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1097 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1099 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1101 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1102 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001103 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001104}
1105
1106/// \brief Create an abbreviation for the SLocEntry that refers to a
1107/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001108static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001109 using namespace llvm;
1110 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1111 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001113 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001114}
1115
1116/// \brief Create an abbreviation for the SLocEntry that refers to an
1117/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001118static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001119 using namespace llvm;
1120 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1121 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001127 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001128}
1129
1130/// \brief Writes the block containing the serialized form of the
1131/// source manager.
1132///
1133/// TODO: We should probably use an on-disk hash table (stored in a
1134/// blob), indexed based on the file name, so that we only create
1135/// entries for files that we actually need. In the common case (no
1136/// errors), we probably won't have to create file entries for any of
1137/// the files in the AST.
1138void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001139 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001140 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001141
1142 // Abbreviations for the various kinds of source-location entries.
1143 int SLocFileAbbrv = -1;
1144 int SLocBufferAbbrv = -1;
1145 int SLocBufferBlobAbbrv = -1;
1146 int SLocInstantiationAbbrv = -1;
1147
1148 // Write out the source location entry table. We skip the first
1149 // entry, which is always the same dummy entry.
1150 RecordData Record;
1151 for (SourceManager::sloc_entry_iterator
1152 SLoc = SourceMgr.sloc_entry_begin() + 1,
1153 SLocEnd = SourceMgr.sloc_entry_end();
1154 SLoc != SLocEnd; ++SLoc) {
1155 // Figure out which record code to use.
1156 unsigned Code;
1157 if (SLoc->isFile()) {
1158 if (SLoc->getFile().getContentCache()->Entry)
1159 Code = pch::SM_SLOC_FILE_ENTRY;
1160 else
1161 Code = pch::SM_SLOC_BUFFER_ENTRY;
1162 } else
1163 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1164 Record.push_back(Code);
1165
1166 Record.push_back(SLoc->getOffset());
1167 if (SLoc->isFile()) {
1168 const SrcMgr::FileInfo &File = SLoc->getFile();
1169 Record.push_back(File.getIncludeLoc().getRawEncoding());
1170 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001171 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001172
1173 const SrcMgr::ContentCache *Content = File.getContentCache();
1174 if (Content->Entry) {
1175 // The source location entry is a file. The blob associated
1176 // with this entry is the file name.
1177 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001178 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1179 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001180 Content->Entry->getName(),
1181 strlen(Content->Entry->getName()));
1182 } else {
1183 // The source location entry is a buffer. The blob associated
1184 // with this entry contains the contents of the buffer.
1185 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001186 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1187 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001188 }
1189
1190 // We add one to the size so that we capture the trailing NULL
1191 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1192 // the reader side).
1193 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1194 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001195 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001196 Record.clear();
1197 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001198 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001199 Buffer->getBufferStart(),
1200 Buffer->getBufferSize() + 1);
1201 }
1202 } else {
1203 // The source location entry is an instantiation.
1204 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1205 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1206 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1207 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1208
Douglas Gregorf60e9912009-04-15 18:05:10 +00001209 // Compute the token length for this macro expansion.
1210 unsigned NextOffset = SourceMgr.getNextOffset();
1211 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1212 if (++NextSLoc != SLocEnd)
1213 NextOffset = NextSLoc->getOffset();
1214 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1215
Douglas Gregor14f79002009-04-10 03:52:48 +00001216 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001217 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1218 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001219 }
1220
1221 Record.clear();
1222 }
1223
Douglas Gregorbd945002009-04-13 16:31:14 +00001224 // Write the line table.
1225 if (SourceMgr.hasLineTable()) {
1226 LineTableInfo &LineTable = SourceMgr.getLineTable();
1227
1228 // Emit the file names
1229 Record.push_back(LineTable.getNumFilenames());
1230 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1231 // Emit the file name
1232 const char *Filename = LineTable.getFilename(I);
1233 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1234 Record.push_back(FilenameLen);
1235 if (FilenameLen)
1236 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1237 }
1238
1239 // Emit the line entries
1240 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1241 L != LEnd; ++L) {
1242 // Emit the file ID
1243 Record.push_back(L->first);
1244
1245 // Emit the line entries
1246 Record.push_back(L->second.size());
1247 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1248 LEEnd = L->second.end();
1249 LE != LEEnd; ++LE) {
1250 Record.push_back(LE->FileOffset);
1251 Record.push_back(LE->LineNo);
1252 Record.push_back(LE->FilenameID);
1253 Record.push_back((unsigned)LE->FileKind);
1254 Record.push_back(LE->IncludeOffset);
1255 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001256 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001257 }
1258 }
1259
Douglas Gregorc9490c02009-04-16 22:23:12 +00001260 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001261}
1262
Chris Lattner0b1fb982009-04-10 17:15:23 +00001263/// \brief Writes the block containing the serialized form of the
1264/// preprocessor.
1265///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001266void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001267 // Enter the preprocessor block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001268 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001269
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001270 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1271 // FIXME: use diagnostics subsystem for localization etc.
1272 if (PP.SawDateOrTime())
1273 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001274
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001275 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001276
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001277 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1278 if (PP.getCounterValue() != 0) {
1279 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001280 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001281 Record.clear();
1282 }
1283
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001284 // Loop over all the macro definitions that are live at the end of the file,
1285 // emitting each to the PP section.
1286 // FIXME: Eventually we want to emit an index so that we can lazily load
1287 // macros.
1288 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1289 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001290 // FIXME: This emits macros in hash table order, we should do it in a stable
1291 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001292 MacroInfo *MI = I->second;
1293
1294 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1295 // been redefined by the header (in which case they are not isBuiltinMacro).
1296 if (MI->isBuiltinMacro())
1297 continue;
1298
Chris Lattner7356a312009-04-11 21:15:38 +00001299 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001300 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1301 Record.push_back(MI->isUsed());
1302
1303 unsigned Code;
1304 if (MI->isObjectLike()) {
1305 Code = pch::PP_MACRO_OBJECT_LIKE;
1306 } else {
1307 Code = pch::PP_MACRO_FUNCTION_LIKE;
1308
1309 Record.push_back(MI->isC99Varargs());
1310 Record.push_back(MI->isGNUVarargs());
1311 Record.push_back(MI->getNumArgs());
1312 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1313 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001314 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001315 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001316 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001317 Record.clear();
1318
Chris Lattnerdf961c22009-04-10 18:08:30 +00001319 // Emit the tokens array.
1320 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1321 // Note that we know that the preprocessor does not have any annotation
1322 // tokens in it because they are created by the parser, and thus can't be
1323 // in a macro definition.
1324 const Token &Tok = MI->getReplacementToken(TokNo);
1325
1326 Record.push_back(Tok.getLocation().getRawEncoding());
1327 Record.push_back(Tok.getLength());
1328
Chris Lattnerdf961c22009-04-10 18:08:30 +00001329 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1330 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001331 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001332
1333 // FIXME: Should translate token kind to a stable encoding.
1334 Record.push_back(Tok.getKind());
1335 // FIXME: Should translate token flags to a stable encoding.
1336 Record.push_back(Tok.getFlags());
1337
Douglas Gregorc9490c02009-04-16 22:23:12 +00001338 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001339 Record.clear();
1340 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001341
1342 }
1343
Douglas Gregorc9490c02009-04-16 22:23:12 +00001344 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001345}
1346
1347
Douglas Gregor2cf26342009-04-09 22:27:44 +00001348/// \brief Write the representation of a type to the PCH stream.
1349void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001350 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001351 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001352 ID = NextTypeID++;
1353
1354 // Record the offset for this type.
1355 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001356 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001357 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1358 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001359 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001360 }
1361
1362 RecordData Record;
1363
1364 // Emit the type's representation.
1365 PCHTypeWriter W(*this, Record);
1366 switch (T->getTypeClass()) {
1367 // For all of the concrete, non-dependent types, call the
1368 // appropriate visitor function.
1369#define TYPE(Class, Base) \
1370 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1371#define ABSTRACT_TYPE(Class, Base)
1372#define DEPENDENT_TYPE(Class, Base)
1373#include "clang/AST/TypeNodes.def"
1374
1375 // For all of the dependent type nodes (which only occur in C++
1376 // templates), produce an error.
1377#define TYPE(Class, Base)
1378#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1379#include "clang/AST/TypeNodes.def"
1380 assert(false && "Cannot serialize dependent type nodes");
1381 break;
1382 }
1383
1384 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001385 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001386
1387 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001388 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001389}
1390
1391/// \brief Write a block containing all of the types.
1392void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001393 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001394 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001395
1396 // Emit all of the types in the ASTContext
1397 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1398 TEnd = Context.getTypes().end();
1399 T != TEnd; ++T) {
1400 // Builtin types are never serialized.
1401 if (isa<BuiltinType>(*T))
1402 continue;
1403
1404 WriteType(*T);
1405 }
1406
1407 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001408 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001409}
1410
1411/// \brief Write the block containing all of the declaration IDs
1412/// lexically declared within the given DeclContext.
1413///
1414/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1415/// bistream, or 0 if no block was written.
1416uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1417 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001418 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001419 return 0;
1420
Douglas Gregorc9490c02009-04-16 22:23:12 +00001421 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001422 RecordData Record;
1423 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1424 DEnd = DC->decls_end(Context);
1425 D != DEnd; ++D)
1426 AddDeclRef(*D, Record);
1427
Douglas Gregorc9490c02009-04-16 22:23:12 +00001428 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001429 return Offset;
1430}
1431
1432/// \brief Write the block containing all of the declaration IDs
1433/// visible from the given DeclContext.
1434///
1435/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1436/// bistream, or 0 if no block was written.
1437uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1438 DeclContext *DC) {
1439 if (DC->getPrimaryContext() != DC)
1440 return 0;
1441
Douglas Gregor58f06992009-04-18 15:49:20 +00001442 // Since there is no name lookup into functions or methods, don't
1443 // bother to build a visible-declarations table.
1444 if (DC->isFunctionOrMethod())
1445 return 0;
1446
Douglas Gregor2cf26342009-04-09 22:27:44 +00001447 // Force the DeclContext to build a its name-lookup table.
1448 DC->lookup(Context, DeclarationName());
1449
1450 // Serialize the contents of the mapping used for lookup. Note that,
1451 // although we have two very different code paths, the serialized
1452 // representation is the same for both cases: a declaration name,
1453 // followed by a size, followed by references to the visible
1454 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001455 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001456 RecordData Record;
1457 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001458 if (!Map)
1459 return 0;
1460
Douglas Gregor2cf26342009-04-09 22:27:44 +00001461 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1462 D != DEnd; ++D) {
1463 AddDeclarationName(D->first, Record);
1464 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1465 Record.push_back(Result.second - Result.first);
1466 for(; Result.first != Result.second; ++Result.first)
1467 AddDeclRef(*Result.first, Record);
1468 }
1469
1470 if (Record.size() == 0)
1471 return 0;
1472
Douglas Gregorc9490c02009-04-16 22:23:12 +00001473 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001474 return Offset;
1475}
1476
1477/// \brief Write a block containing all of the declarations.
1478void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001479 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001480 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001481
1482 // Emit all of the declarations.
1483 RecordData Record;
Douglas Gregor72971342009-04-18 00:02:19 +00001484 PCHDeclWriter W(*this, Context, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001485 while (!DeclsToEmit.empty()) {
1486 // Pull the next declaration off the queue
1487 Decl *D = DeclsToEmit.front();
1488 DeclsToEmit.pop();
1489
1490 // If this declaration is also a DeclContext, write blocks for the
1491 // declarations that lexically stored inside its context and those
1492 // declarations that are visible from its context. These blocks
1493 // are written before the declaration itself so that we can put
1494 // their offsets into the record for the declaration.
1495 uint64_t LexicalOffset = 0;
1496 uint64_t VisibleOffset = 0;
1497 DeclContext *DC = dyn_cast<DeclContext>(D);
1498 if (DC) {
1499 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1500 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1501 }
1502
1503 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001504 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001505 if (ID == 0)
1506 ID = DeclIDs.size();
1507
1508 unsigned Index = ID - 1;
1509
1510 // Record the offset for this declaration
1511 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001512 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001513 else if (DeclOffsets.size() < Index) {
1514 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001515 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001516 }
1517
1518 // Build and emit a record for this declaration
1519 Record.clear();
1520 W.Code = (pch::DeclCode)0;
1521 W.Visit(D);
1522 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001523 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001524 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001525
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001526 // If the declaration had any attributes, write them now.
1527 if (D->hasAttrs())
1528 WriteAttributeRecord(D->getAttrs());
1529
Douglas Gregor0b748912009-04-14 21:18:50 +00001530 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001531 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001532
Douglas Gregorfdd01722009-04-14 00:24:19 +00001533 // Note external declarations so that we can add them to a record
1534 // in the PCH file later.
1535 if (isa<FileScopeAsmDecl>(D))
1536 ExternalDefinitions.push_back(ID);
1537 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1538 if (// Non-static file-scope variables with initializers or that
1539 // are tentative definitions.
1540 (Var->isFileVarDecl() &&
1541 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1542 // Out-of-line definitions of static data members (C++).
1543 (Var->getDeclContext()->isRecord() &&
1544 !Var->getLexicalDeclContext()->isRecord() &&
1545 Var->getStorageClass() == VarDecl::Static))
1546 ExternalDefinitions.push_back(ID);
1547 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00001548 if (Func->isThisDeclarationADefinition())
Douglas Gregorfdd01722009-04-14 00:24:19 +00001549 ExternalDefinitions.push_back(ID);
1550 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001551 }
1552
1553 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001554 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001555}
1556
Douglas Gregorafaf3082009-04-11 00:14:32 +00001557/// \brief Write the identifier table into the PCH file.
1558///
1559/// The identifier table consists of a blob containing string data
1560/// (the actual identifiers themselves) and a separate "offsets" index
1561/// that maps identifier IDs to locations within the blob.
1562void PCHWriter::WriteIdentifierTable() {
1563 using namespace llvm;
1564
1565 // Create and write out the blob that contains the identifier
1566 // strings.
1567 RecordData IdentOffsets;
1568 IdentOffsets.resize(IdentifierIDs.size());
1569 {
1570 // Create the identifier string data.
1571 std::vector<char> Data;
1572 Data.push_back(0); // Data must not be empty.
1573 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1574 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1575 ID != IDEnd; ++ID) {
1576 assert(ID->first && "NULL identifier in identifier table");
1577
1578 // Make sure we're starting on an odd byte. The PCH reader
1579 // expects the low bit to be set on all of the offsets.
1580 if ((Data.size() & 0x01) == 0)
1581 Data.push_back((char)0);
1582
1583 IdentOffsets[ID->second - 1] = Data.size();
1584 Data.insert(Data.end(),
1585 ID->first->getName(),
1586 ID->first->getName() + ID->first->getLength());
1587 Data.push_back((char)0);
1588 }
1589
1590 // Create a blob abbreviation
1591 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1592 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001594 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001595
1596 // Write the identifier table
1597 RecordData Record;
1598 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001599 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001600 }
1601
1602 // Write the offsets table for identifier IDs.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001603 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001604}
1605
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001606/// \brief Write a record containing the given attributes.
1607void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1608 RecordData Record;
1609 for (; Attr; Attr = Attr->getNext()) {
1610 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1611 Record.push_back(Attr->isInherited());
1612 switch (Attr->getKind()) {
1613 case Attr::Alias:
1614 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1615 break;
1616
1617 case Attr::Aligned:
1618 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1619 break;
1620
1621 case Attr::AlwaysInline:
1622 break;
1623
1624 case Attr::AnalyzerNoReturn:
1625 break;
1626
1627 case Attr::Annotate:
1628 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1629 break;
1630
1631 case Attr::AsmLabel:
1632 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1633 break;
1634
1635 case Attr::Blocks:
1636 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1637 break;
1638
1639 case Attr::Cleanup:
1640 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1641 break;
1642
1643 case Attr::Const:
1644 break;
1645
1646 case Attr::Constructor:
1647 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1648 break;
1649
1650 case Attr::DLLExport:
1651 case Attr::DLLImport:
1652 case Attr::Deprecated:
1653 break;
1654
1655 case Attr::Destructor:
1656 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1657 break;
1658
1659 case Attr::FastCall:
1660 break;
1661
1662 case Attr::Format: {
1663 const FormatAttr *Format = cast<FormatAttr>(Attr);
1664 AddString(Format->getType(), Record);
1665 Record.push_back(Format->getFormatIdx());
1666 Record.push_back(Format->getFirstArg());
1667 break;
1668 }
1669
1670 case Attr::GNUCInline:
1671 case Attr::IBOutletKind:
1672 case Attr::NoReturn:
1673 case Attr::NoThrow:
1674 case Attr::Nodebug:
1675 case Attr::Noinline:
1676 break;
1677
1678 case Attr::NonNull: {
1679 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1680 Record.push_back(NonNull->size());
1681 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1682 break;
1683 }
1684
1685 case Attr::ObjCException:
1686 case Attr::ObjCNSObject:
1687 case Attr::Overloadable:
1688 break;
1689
1690 case Attr::Packed:
1691 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1692 break;
1693
1694 case Attr::Pure:
1695 break;
1696
1697 case Attr::Regparm:
1698 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1699 break;
1700
1701 case Attr::Section:
1702 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1703 break;
1704
1705 case Attr::StdCall:
1706 case Attr::TransparentUnion:
1707 case Attr::Unavailable:
1708 case Attr::Unused:
1709 case Attr::Used:
1710 break;
1711
1712 case Attr::Visibility:
1713 // FIXME: stable encoding
1714 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1715 break;
1716
1717 case Attr::WarnUnusedResult:
1718 case Attr::Weak:
1719 case Attr::WeakImport:
1720 break;
1721 }
1722 }
1723
Douglas Gregorc9490c02009-04-16 22:23:12 +00001724 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001725}
1726
1727void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1728 Record.push_back(Str.size());
1729 Record.insert(Record.end(), Str.begin(), Str.end());
1730}
1731
Douglas Gregorc9490c02009-04-16 22:23:12 +00001732PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor3e1af842009-04-17 22:13:46 +00001733 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), NumStatements(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001734
Chris Lattnerdf961c22009-04-10 18:08:30 +00001735void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001736 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001737 Stream.Emit((unsigned)'C', 8);
1738 Stream.Emit((unsigned)'P', 8);
1739 Stream.Emit((unsigned)'C', 8);
1740 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001741
1742 // The translation unit is the first declaration we'll emit.
1743 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1744 DeclsToEmit.push(Context.getTranslationUnitDecl());
1745
1746 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001747 RecordData Record;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001748 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001749 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001750 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001751 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001752 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001753 WriteTypesBlock(Context);
1754 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001755 WriteIdentifierTable();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001756 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1757 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorad1de002009-04-18 05:55:16 +00001758
1759 // Write the record of special types.
1760 Record.clear();
1761 AddTypeRef(Context.getBuiltinVaListType(), Record);
1762 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1763
Douglas Gregorfdd01722009-04-14 00:24:19 +00001764 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00001765 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001766
1767 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00001768 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00001769 Record.push_back(NumStatements);
1770 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001771 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001772}
1773
1774void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1775 Record.push_back(Loc.getRawEncoding());
1776}
1777
1778void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1779 Record.push_back(Value.getBitWidth());
1780 unsigned N = Value.getNumWords();
1781 const uint64_t* Words = Value.getRawData();
1782 for (unsigned I = 0; I != N; ++I)
1783 Record.push_back(Words[I]);
1784}
1785
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001786void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1787 Record.push_back(Value.isUnsigned());
1788 AddAPInt(Value, Record);
1789}
1790
Douglas Gregor17fc2232009-04-14 21:55:33 +00001791void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1792 AddAPInt(Value.bitcastToAPInt(), Record);
1793}
1794
Douglas Gregor2cf26342009-04-09 22:27:44 +00001795void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001796 if (II == 0) {
1797 Record.push_back(0);
1798 return;
1799 }
1800
1801 pch::IdentID &ID = IdentifierIDs[II];
1802 if (ID == 0)
1803 ID = IdentifierIDs.size();
1804
1805 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001806}
1807
1808void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1809 if (T.isNull()) {
1810 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1811 return;
1812 }
1813
1814 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001815 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001816 switch (BT->getKind()) {
1817 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1818 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1819 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1820 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1821 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1822 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1823 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1824 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1825 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1826 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1827 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1828 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1829 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1830 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1831 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1832 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1833 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1834 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1835 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1836 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1837 }
1838
1839 Record.push_back((ID << 3) | T.getCVRQualifiers());
1840 return;
1841 }
1842
Douglas Gregor8038d512009-04-10 17:25:41 +00001843 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001844 if (ID == 0) // we haven't seen this type before
1845 ID = NextTypeID++;
1846
1847 // Encode the type qualifiers in the type reference.
1848 Record.push_back((ID << 3) | T.getCVRQualifiers());
1849}
1850
1851void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1852 if (D == 0) {
1853 Record.push_back(0);
1854 return;
1855 }
1856
Douglas Gregor8038d512009-04-10 17:25:41 +00001857 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001858 if (ID == 0) {
1859 // We haven't seen this declaration before. Give it a new ID and
1860 // enqueue it in the list of declarations to emit.
1861 ID = DeclIDs.size();
1862 DeclsToEmit.push(const_cast<Decl *>(D));
1863 }
1864
1865 Record.push_back(ID);
1866}
1867
1868void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1869 Record.push_back(Name.getNameKind());
1870 switch (Name.getNameKind()) {
1871 case DeclarationName::Identifier:
1872 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1873 break;
1874
1875 case DeclarationName::ObjCZeroArgSelector:
1876 case DeclarationName::ObjCOneArgSelector:
1877 case DeclarationName::ObjCMultiArgSelector:
1878 assert(false && "Serialization of Objective-C selectors unavailable");
1879 break;
1880
1881 case DeclarationName::CXXConstructorName:
1882 case DeclarationName::CXXDestructorName:
1883 case DeclarationName::CXXConversionFunctionName:
1884 AddTypeRef(Name.getCXXNameType(), Record);
1885 break;
1886
1887 case DeclarationName::CXXOperatorName:
1888 Record.push_back(Name.getCXXOverloadedOperator());
1889 break;
1890
1891 case DeclarationName::CXXUsingDirective:
1892 // No extra data to emit
1893 break;
1894 }
1895}
Douglas Gregor0b748912009-04-14 21:18:50 +00001896
Douglas Gregorc9490c02009-04-16 22:23:12 +00001897/// \brief Write the given substatement or subexpression to the
1898/// bitstream.
1899void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00001900 RecordData Record;
1901 PCHStmtWriter Writer(*this, Record);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001902 ++NumStatements;
Douglas Gregor087fd532009-04-14 23:32:43 +00001903
Douglas Gregorc9490c02009-04-16 22:23:12 +00001904 if (!S) {
1905 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001906 return;
1907 }
1908
Douglas Gregorc9490c02009-04-16 22:23:12 +00001909 Writer.Code = pch::STMT_NULL_PTR;
1910 Writer.Visit(S);
1911 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00001912 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001913 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001914}
1915
Douglas Gregorc9490c02009-04-16 22:23:12 +00001916/// \brief Flush all of the statements that have been added to the
1917/// queue via AddStmt().
1918void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00001919 RecordData Record;
1920 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001921
Douglas Gregorc9490c02009-04-16 22:23:12 +00001922 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor3e1af842009-04-17 22:13:46 +00001923 ++NumStatements;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001924 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00001925
Douglas Gregorc9490c02009-04-16 22:23:12 +00001926 if (!S) {
1927 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001928 continue;
1929 }
1930
Douglas Gregorc9490c02009-04-16 22:23:12 +00001931 Writer.Code = pch::STMT_NULL_PTR;
1932 Writer.Visit(S);
1933 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00001934 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001935 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001936
Douglas Gregorc9490c02009-04-16 22:23:12 +00001937 assert(N == StmtsToEmit.size() &&
1938 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00001939
1940 // Note that we are at the end of a full expression. Any
1941 // expression records that follow this one are part of a different
1942 // expression.
1943 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001944 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001945 }
Douglas Gregor087fd532009-04-14 23:32:43 +00001946
Douglas Gregorc9490c02009-04-16 22:23:12 +00001947 StmtsToEmit.clear();
Douglas Gregor0de9d882009-04-17 16:34:57 +00001948 SwitchCaseIDs.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00001949}
Douglas Gregor025452f2009-04-17 00:04:06 +00001950
1951unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1952 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1953 "SwitchCase recorded twice");
1954 unsigned NextID = SwitchCaseIDs.size();
1955 SwitchCaseIDs[S] = NextID;
1956 return NextID;
1957}
1958
1959unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1960 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1961 "SwitchCase hasn't been seen yet");
1962 return SwitchCaseIDs[S];
1963}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00001964
1965/// \brief Retrieve the ID for the given label statement, which may
1966/// or may not have been emitted yet.
1967unsigned PCHWriter::GetLabelID(LabelStmt *S) {
1968 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
1969 if (Pos != LabelIDs.end())
1970 return Pos->second;
1971
1972 unsigned NextID = LabelIDs.size();
1973 LabelIDs[S] = NextID;
1974 return NextID;
1975}