blob: 9f22f13134bf3d69877e6bb06e1a18390a9ca8ae [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);
Chris Lattnerad56d682009-04-19 01:04:21 +0000613 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000614 Writer.WriteSubStmt(S->getTarget());
615 Code = pch::STMT_INDIRECT_GOTO;
616}
617
Douglas Gregord921cf92009-04-17 00:16:09 +0000618void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
619 VisitStmt(S);
620 Writer.AddSourceLocation(S->getContinueLoc(), Record);
621 Code = pch::STMT_CONTINUE;
622}
623
Douglas Gregor025452f2009-04-17 00:04:06 +0000624void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
625 VisitStmt(S);
626 Writer.AddSourceLocation(S->getBreakLoc(), Record);
627 Code = pch::STMT_BREAK;
628}
629
Douglas Gregor0de9d882009-04-17 16:34:57 +0000630void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
631 VisitStmt(S);
632 Writer.WriteSubStmt(S->getRetValue());
633 Writer.AddSourceLocation(S->getReturnLoc(), Record);
634 Code = pch::STMT_RETURN;
635}
636
Douglas Gregor84f21702009-04-17 16:55:36 +0000637void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
638 VisitStmt(S);
639 Writer.AddSourceLocation(S->getStartLoc(), Record);
640 Writer.AddSourceLocation(S->getEndLoc(), Record);
641 DeclGroupRef DG = S->getDeclGroup();
642 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
643 Writer.AddDeclRef(*D, Record);
644 Code = pch::STMT_DECL;
645}
646
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000647void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
648 VisitStmt(S);
649 Record.push_back(S->getNumOutputs());
650 Record.push_back(S->getNumInputs());
651 Record.push_back(S->getNumClobbers());
652 Writer.AddSourceLocation(S->getAsmLoc(), Record);
653 Writer.AddSourceLocation(S->getRParenLoc(), Record);
654 Record.push_back(S->isVolatile());
655 Record.push_back(S->isSimple());
656 Writer.WriteSubStmt(S->getAsmString());
657
658 // Outputs
659 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
660 Writer.AddString(S->getOutputName(I), Record);
661 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
662 Writer.WriteSubStmt(S->getOutputExpr(I));
663 }
664
665 // Inputs
666 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
667 Writer.AddString(S->getInputName(I), Record);
668 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
669 Writer.WriteSubStmt(S->getInputExpr(I));
670 }
671
672 // Clobbers
673 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
674 Writer.WriteSubStmt(S->getClobber(I));
675
676 Code = pch::STMT_ASM;
677}
678
Douglas Gregor0b748912009-04-14 21:18:50 +0000679void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000680 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000681 Writer.AddTypeRef(E->getType(), Record);
682 Record.push_back(E->isTypeDependent());
683 Record.push_back(E->isValueDependent());
684}
685
Douglas Gregor17fc2232009-04-14 21:55:33 +0000686void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
687 VisitExpr(E);
688 Writer.AddSourceLocation(E->getLocation(), Record);
689 Record.push_back(E->getIdentType()); // FIXME: stable encoding
690 Code = pch::EXPR_PREDEFINED;
691}
692
Douglas Gregor0b748912009-04-14 21:18:50 +0000693void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
694 VisitExpr(E);
695 Writer.AddDeclRef(E->getDecl(), Record);
696 Writer.AddSourceLocation(E->getLocation(), Record);
697 Code = pch::EXPR_DECL_REF;
698}
699
700void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
701 VisitExpr(E);
702 Writer.AddSourceLocation(E->getLocation(), Record);
703 Writer.AddAPInt(E->getValue(), Record);
704 Code = pch::EXPR_INTEGER_LITERAL;
705}
706
Douglas Gregor17fc2232009-04-14 21:55:33 +0000707void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
708 VisitExpr(E);
709 Writer.AddAPFloat(E->getValue(), Record);
710 Record.push_back(E->isExact());
711 Writer.AddSourceLocation(E->getLocation(), Record);
712 Code = pch::EXPR_FLOATING_LITERAL;
713}
714
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000715void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
716 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000717 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000718 Code = pch::EXPR_IMAGINARY_LITERAL;
719}
720
Douglas Gregor673ecd62009-04-15 16:35:07 +0000721void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
722 VisitExpr(E);
723 Record.push_back(E->getByteLength());
724 Record.push_back(E->getNumConcatenated());
725 Record.push_back(E->isWide());
726 // FIXME: String data should be stored as a blob at the end of the
727 // StringLiteral. However, we can't do so now because we have no
728 // provision for coping with abbreviations when we're jumping around
729 // the PCH file during deserialization.
730 Record.insert(Record.end(),
731 E->getStrData(), E->getStrData() + E->getByteLength());
732 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
733 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
734 Code = pch::EXPR_STRING_LITERAL;
735}
736
Douglas Gregor0b748912009-04-14 21:18:50 +0000737void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
738 VisitExpr(E);
739 Record.push_back(E->getValue());
740 Writer.AddSourceLocation(E->getLoc(), Record);
741 Record.push_back(E->isWide());
742 Code = pch::EXPR_CHARACTER_LITERAL;
743}
744
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000745void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
746 VisitExpr(E);
747 Writer.AddSourceLocation(E->getLParen(), Record);
748 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000749 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000750 Code = pch::EXPR_PAREN;
751}
752
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000753void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
754 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000755 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000756 Record.push_back(E->getOpcode()); // FIXME: stable encoding
757 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
758 Code = pch::EXPR_UNARY_OPERATOR;
759}
760
761void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
762 VisitExpr(E);
763 Record.push_back(E->isSizeOf());
764 if (E->isArgumentType())
765 Writer.AddTypeRef(E->getArgumentType(), Record);
766 else {
767 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000768 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000769 }
770 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
771 Writer.AddSourceLocation(E->getRParenLoc(), Record);
772 Code = pch::EXPR_SIZEOF_ALIGN_OF;
773}
774
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000775void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
776 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000777 Writer.WriteSubStmt(E->getLHS());
778 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000779 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
780 Code = pch::EXPR_ARRAY_SUBSCRIPT;
781}
782
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000783void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
784 VisitExpr(E);
785 Record.push_back(E->getNumArgs());
786 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000787 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000788 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
789 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000790 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000791 Code = pch::EXPR_CALL;
792}
793
794void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
795 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000796 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000797 Writer.AddDeclRef(E->getMemberDecl(), Record);
798 Writer.AddSourceLocation(E->getMemberLoc(), Record);
799 Record.push_back(E->isArrow());
800 Code = pch::EXPR_MEMBER;
801}
802
Douglas Gregor087fd532009-04-14 23:32:43 +0000803void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
804 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000805 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000806}
807
Douglas Gregordb600c32009-04-15 00:25:59 +0000808void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
809 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000810 Writer.WriteSubStmt(E->getLHS());
811 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000812 Record.push_back(E->getOpcode()); // FIXME: stable encoding
813 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
814 Code = pch::EXPR_BINARY_OPERATOR;
815}
816
Douglas Gregorad90e962009-04-15 22:40:36 +0000817void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
818 VisitBinaryOperator(E);
819 Writer.AddTypeRef(E->getComputationLHSType(), Record);
820 Writer.AddTypeRef(E->getComputationResultType(), Record);
821 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
822}
823
824void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
825 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000826 Writer.WriteSubStmt(E->getCond());
827 Writer.WriteSubStmt(E->getLHS());
828 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000829 Code = pch::EXPR_CONDITIONAL_OPERATOR;
830}
831
Douglas Gregor087fd532009-04-14 23:32:43 +0000832void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
833 VisitCastExpr(E);
834 Record.push_back(E->isLvalueCast());
835 Code = pch::EXPR_IMPLICIT_CAST;
836}
837
Douglas Gregordb600c32009-04-15 00:25:59 +0000838void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
839 VisitCastExpr(E);
840 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
841}
842
843void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
844 VisitExplicitCastExpr(E);
845 Writer.AddSourceLocation(E->getLParenLoc(), Record);
846 Writer.AddSourceLocation(E->getRParenLoc(), Record);
847 Code = pch::EXPR_CSTYLE_CAST;
848}
849
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000850void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
851 VisitExpr(E);
852 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000853 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000854 Record.push_back(E->isFileScope());
855 Code = pch::EXPR_COMPOUND_LITERAL;
856}
857
Douglas Gregord3c98a02009-04-15 23:02:49 +0000858void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
859 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000860 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000861 Writer.AddIdentifierRef(&E->getAccessor(), Record);
862 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
863 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
864}
865
Douglas Gregord077d752009-04-16 00:55:48 +0000866void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
867 VisitExpr(E);
868 Record.push_back(E->getNumInits());
869 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000870 Writer.WriteSubStmt(E->getInit(I));
871 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +0000872 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
873 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
874 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
875 Record.push_back(E->hadArrayRangeDesignator());
876 Code = pch::EXPR_INIT_LIST;
877}
878
879void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
880 VisitExpr(E);
881 Record.push_back(E->getNumSubExprs());
882 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000883 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +0000884 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
885 Record.push_back(E->usesGNUSyntax());
886 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
887 DEnd = E->designators_end();
888 D != DEnd; ++D) {
889 if (D->isFieldDesignator()) {
890 if (FieldDecl *Field = D->getField()) {
891 Record.push_back(pch::DESIG_FIELD_DECL);
892 Writer.AddDeclRef(Field, Record);
893 } else {
894 Record.push_back(pch::DESIG_FIELD_NAME);
895 Writer.AddIdentifierRef(D->getFieldName(), Record);
896 }
897 Writer.AddSourceLocation(D->getDotLoc(), Record);
898 Writer.AddSourceLocation(D->getFieldLoc(), Record);
899 } else if (D->isArrayDesignator()) {
900 Record.push_back(pch::DESIG_ARRAY);
901 Record.push_back(D->getFirstExprIndex());
902 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
903 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
904 } else {
905 assert(D->isArrayRangeDesignator() && "Unknown designator");
906 Record.push_back(pch::DESIG_ARRAY_RANGE);
907 Record.push_back(D->getFirstExprIndex());
908 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
909 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
910 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
911 }
912 }
913 Code = pch::EXPR_DESIGNATED_INIT;
914}
915
916void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
917 VisitExpr(E);
918 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
919}
920
Douglas Gregord3c98a02009-04-15 23:02:49 +0000921void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
922 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000923 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000924 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
925 Writer.AddSourceLocation(E->getRParenLoc(), Record);
926 Code = pch::EXPR_VA_ARG;
927}
928
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000929void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
930 VisitExpr(E);
931 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
932 Writer.AddSourceLocation(E->getLabelLoc(), Record);
933 Record.push_back(Writer.GetLabelID(E->getLabel()));
934 Code = pch::EXPR_ADDR_LABEL;
935}
936
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000937void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
938 VisitExpr(E);
939 Writer.WriteSubStmt(E->getSubStmt());
940 Writer.AddSourceLocation(E->getLParenLoc(), Record);
941 Writer.AddSourceLocation(E->getRParenLoc(), Record);
942 Code = pch::EXPR_STMT;
943}
944
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000945void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
946 VisitExpr(E);
947 Writer.AddTypeRef(E->getArgType1(), Record);
948 Writer.AddTypeRef(E->getArgType2(), Record);
949 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
950 Writer.AddSourceLocation(E->getRParenLoc(), Record);
951 Code = pch::EXPR_TYPES_COMPATIBLE;
952}
953
954void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
955 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000956 Writer.WriteSubStmt(E->getCond());
957 Writer.WriteSubStmt(E->getLHS());
958 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000959 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
960 Writer.AddSourceLocation(E->getRParenLoc(), Record);
961 Code = pch::EXPR_CHOOSE;
962}
963
964void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
965 VisitExpr(E);
966 Writer.AddSourceLocation(E->getTokenLocation(), Record);
967 Code = pch::EXPR_GNU_NULL;
968}
969
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000970void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
971 VisitExpr(E);
972 Record.push_back(E->getNumSubExprs());
973 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000974 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000975 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
976 Writer.AddSourceLocation(E->getRParenLoc(), Record);
977 Code = pch::EXPR_SHUFFLE_VECTOR;
978}
979
Douglas Gregor84af7c22009-04-17 19:21:43 +0000980void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
981 VisitExpr(E);
982 Writer.AddDeclRef(E->getBlockDecl(), Record);
983 Record.push_back(E->hasBlockDeclRefExprs());
984 Code = pch::EXPR_BLOCK;
985}
986
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000987void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
988 VisitExpr(E);
989 Writer.AddDeclRef(E->getDecl(), Record);
990 Writer.AddSourceLocation(E->getLocation(), Record);
991 Record.push_back(E->isByRef());
992 Code = pch::EXPR_BLOCK_DECL_REF;
993}
994
Douglas Gregor0b748912009-04-14 21:18:50 +0000995//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000996// PCHWriter Implementation
997//===----------------------------------------------------------------------===//
998
Douglas Gregor2bec0412009-04-10 21:16:55 +0000999/// \brief Write the target triple (e.g., i686-apple-darwin9).
1000void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1001 using namespace llvm;
1002 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1003 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001005 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001006
1007 RecordData Record;
1008 Record.push_back(pch::TARGET_TRIPLE);
1009 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001010 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +00001011}
1012
1013/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001014void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1015 RecordData Record;
1016 Record.push_back(LangOpts.Trigraphs);
1017 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1018 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1019 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1020 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1021 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1022 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1023 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1024 Record.push_back(LangOpts.C99); // C99 Support
1025 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1026 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1027 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1028 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1029 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1030
1031 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1032 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1033 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1034
1035 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1036 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1037 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1038 Record.push_back(LangOpts.LaxVectorConversions);
1039 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1040
1041 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1042 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1043 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1044
1045 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1046 // by locks.
1047 Record.push_back(LangOpts.Blocks); // block extension to C
1048 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1049 // they are unused.
1050 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1051 // (modulo the platform support).
1052
1053 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1054 // signed integer arithmetic overflows.
1055
1056 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1057 // may be ripped out at any time.
1058
1059 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1060 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1061 // defined.
1062 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1063 // opposed to __DYNAMIC__).
1064 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1065
1066 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1067 // used (instead of C99 semantics).
1068 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1069 Record.push_back(LangOpts.getGCMode());
1070 Record.push_back(LangOpts.getVisibilityMode());
1071 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001072 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001073}
1074
Douglas Gregor14f79002009-04-10 03:52:48 +00001075//===----------------------------------------------------------------------===//
1076// Source Manager Serialization
1077//===----------------------------------------------------------------------===//
1078
1079/// \brief Create an abbreviation for the SLocEntry that refers to a
1080/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001081static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001082 using namespace llvm;
1083 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1084 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +00001089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001090 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001091}
1092
1093/// \brief Create an abbreviation for the SLocEntry that refers to a
1094/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001095static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001096 using namespace llvm;
1097 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1098 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1099 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1101 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1102 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001104 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001105}
1106
1107/// \brief Create an abbreviation for the SLocEntry that refers to a
1108/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001109static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001110 using namespace llvm;
1111 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1112 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001114 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001115}
1116
1117/// \brief Create an abbreviation for the SLocEntry that refers to an
1118/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001119static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001120 using namespace llvm;
1121 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1122 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001128 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001129}
1130
1131/// \brief Writes the block containing the serialized form of the
1132/// source manager.
1133///
1134/// TODO: We should probably use an on-disk hash table (stored in a
1135/// blob), indexed based on the file name, so that we only create
1136/// entries for files that we actually need. In the common case (no
1137/// errors), we probably won't have to create file entries for any of
1138/// the files in the AST.
1139void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001140 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001141 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001142
1143 // Abbreviations for the various kinds of source-location entries.
1144 int SLocFileAbbrv = -1;
1145 int SLocBufferAbbrv = -1;
1146 int SLocBufferBlobAbbrv = -1;
1147 int SLocInstantiationAbbrv = -1;
1148
1149 // Write out the source location entry table. We skip the first
1150 // entry, which is always the same dummy entry.
1151 RecordData Record;
1152 for (SourceManager::sloc_entry_iterator
1153 SLoc = SourceMgr.sloc_entry_begin() + 1,
1154 SLocEnd = SourceMgr.sloc_entry_end();
1155 SLoc != SLocEnd; ++SLoc) {
1156 // Figure out which record code to use.
1157 unsigned Code;
1158 if (SLoc->isFile()) {
1159 if (SLoc->getFile().getContentCache()->Entry)
1160 Code = pch::SM_SLOC_FILE_ENTRY;
1161 else
1162 Code = pch::SM_SLOC_BUFFER_ENTRY;
1163 } else
1164 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1165 Record.push_back(Code);
1166
1167 Record.push_back(SLoc->getOffset());
1168 if (SLoc->isFile()) {
1169 const SrcMgr::FileInfo &File = SLoc->getFile();
1170 Record.push_back(File.getIncludeLoc().getRawEncoding());
1171 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001172 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001173
1174 const SrcMgr::ContentCache *Content = File.getContentCache();
1175 if (Content->Entry) {
1176 // The source location entry is a file. The blob associated
1177 // with this entry is the file name.
1178 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001179 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1180 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001181 Content->Entry->getName(),
1182 strlen(Content->Entry->getName()));
1183 } else {
1184 // The source location entry is a buffer. The blob associated
1185 // with this entry contains the contents of the buffer.
1186 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001187 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1188 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001189 }
1190
1191 // We add one to the size so that we capture the trailing NULL
1192 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1193 // the reader side).
1194 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1195 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001196 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001197 Record.clear();
1198 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001199 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001200 Buffer->getBufferStart(),
1201 Buffer->getBufferSize() + 1);
1202 }
1203 } else {
1204 // The source location entry is an instantiation.
1205 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1206 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1207 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1208 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1209
Douglas Gregorf60e9912009-04-15 18:05:10 +00001210 // Compute the token length for this macro expansion.
1211 unsigned NextOffset = SourceMgr.getNextOffset();
1212 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1213 if (++NextSLoc != SLocEnd)
1214 NextOffset = NextSLoc->getOffset();
1215 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1216
Douglas Gregor14f79002009-04-10 03:52:48 +00001217 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001218 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1219 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001220 }
1221
1222 Record.clear();
1223 }
1224
Douglas Gregorbd945002009-04-13 16:31:14 +00001225 // Write the line table.
1226 if (SourceMgr.hasLineTable()) {
1227 LineTableInfo &LineTable = SourceMgr.getLineTable();
1228
1229 // Emit the file names
1230 Record.push_back(LineTable.getNumFilenames());
1231 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1232 // Emit the file name
1233 const char *Filename = LineTable.getFilename(I);
1234 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1235 Record.push_back(FilenameLen);
1236 if (FilenameLen)
1237 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1238 }
1239
1240 // Emit the line entries
1241 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1242 L != LEnd; ++L) {
1243 // Emit the file ID
1244 Record.push_back(L->first);
1245
1246 // Emit the line entries
1247 Record.push_back(L->second.size());
1248 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1249 LEEnd = L->second.end();
1250 LE != LEEnd; ++LE) {
1251 Record.push_back(LE->FileOffset);
1252 Record.push_back(LE->LineNo);
1253 Record.push_back(LE->FilenameID);
1254 Record.push_back((unsigned)LE->FileKind);
1255 Record.push_back(LE->IncludeOffset);
1256 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001257 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001258 }
1259 }
1260
Douglas Gregorc9490c02009-04-16 22:23:12 +00001261 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001262}
1263
Chris Lattner0b1fb982009-04-10 17:15:23 +00001264/// \brief Writes the block containing the serialized form of the
1265/// preprocessor.
1266///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001267void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001268 // Enter the preprocessor block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001269 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001270
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001271 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1272 // FIXME: use diagnostics subsystem for localization etc.
1273 if (PP.SawDateOrTime())
1274 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001275
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001276 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001277
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001278 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1279 if (PP.getCounterValue() != 0) {
1280 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001281 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001282 Record.clear();
1283 }
1284
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001285 // Loop over all the macro definitions that are live at the end of the file,
1286 // emitting each to the PP section.
1287 // FIXME: Eventually we want to emit an index so that we can lazily load
1288 // macros.
1289 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1290 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001291 // FIXME: This emits macros in hash table order, we should do it in a stable
1292 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001293 MacroInfo *MI = I->second;
1294
1295 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1296 // been redefined by the header (in which case they are not isBuiltinMacro).
1297 if (MI->isBuiltinMacro())
1298 continue;
1299
Chris Lattner7356a312009-04-11 21:15:38 +00001300 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001301 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1302 Record.push_back(MI->isUsed());
1303
1304 unsigned Code;
1305 if (MI->isObjectLike()) {
1306 Code = pch::PP_MACRO_OBJECT_LIKE;
1307 } else {
1308 Code = pch::PP_MACRO_FUNCTION_LIKE;
1309
1310 Record.push_back(MI->isC99Varargs());
1311 Record.push_back(MI->isGNUVarargs());
1312 Record.push_back(MI->getNumArgs());
1313 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1314 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001315 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001316 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001317 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001318 Record.clear();
1319
Chris Lattnerdf961c22009-04-10 18:08:30 +00001320 // Emit the tokens array.
1321 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1322 // Note that we know that the preprocessor does not have any annotation
1323 // tokens in it because they are created by the parser, and thus can't be
1324 // in a macro definition.
1325 const Token &Tok = MI->getReplacementToken(TokNo);
1326
1327 Record.push_back(Tok.getLocation().getRawEncoding());
1328 Record.push_back(Tok.getLength());
1329
Chris Lattnerdf961c22009-04-10 18:08:30 +00001330 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1331 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001332 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001333
1334 // FIXME: Should translate token kind to a stable encoding.
1335 Record.push_back(Tok.getKind());
1336 // FIXME: Should translate token flags to a stable encoding.
1337 Record.push_back(Tok.getFlags());
1338
Douglas Gregorc9490c02009-04-16 22:23:12 +00001339 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001340 Record.clear();
1341 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001342
1343 }
1344
Douglas Gregorc9490c02009-04-16 22:23:12 +00001345 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001346}
1347
1348
Douglas Gregor2cf26342009-04-09 22:27:44 +00001349/// \brief Write the representation of a type to the PCH stream.
1350void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001351 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001352 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001353 ID = NextTypeID++;
1354
1355 // Record the offset for this type.
1356 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001357 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001358 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1359 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001360 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001361 }
1362
1363 RecordData Record;
1364
1365 // Emit the type's representation.
1366 PCHTypeWriter W(*this, Record);
1367 switch (T->getTypeClass()) {
1368 // For all of the concrete, non-dependent types, call the
1369 // appropriate visitor function.
1370#define TYPE(Class, Base) \
1371 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1372#define ABSTRACT_TYPE(Class, Base)
1373#define DEPENDENT_TYPE(Class, Base)
1374#include "clang/AST/TypeNodes.def"
1375
1376 // For all of the dependent type nodes (which only occur in C++
1377 // templates), produce an error.
1378#define TYPE(Class, Base)
1379#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1380#include "clang/AST/TypeNodes.def"
1381 assert(false && "Cannot serialize dependent type nodes");
1382 break;
1383 }
1384
1385 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001386 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001387
1388 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001389 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001390}
1391
1392/// \brief Write a block containing all of the types.
1393void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001394 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001395 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001396
1397 // Emit all of the types in the ASTContext
1398 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1399 TEnd = Context.getTypes().end();
1400 T != TEnd; ++T) {
1401 // Builtin types are never serialized.
1402 if (isa<BuiltinType>(*T))
1403 continue;
1404
1405 WriteType(*T);
1406 }
1407
1408 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001409 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001410}
1411
1412/// \brief Write the block containing all of the declaration IDs
1413/// lexically declared within the given DeclContext.
1414///
1415/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1416/// bistream, or 0 if no block was written.
1417uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1418 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001419 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001420 return 0;
1421
Douglas Gregorc9490c02009-04-16 22:23:12 +00001422 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001423 RecordData Record;
1424 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1425 DEnd = DC->decls_end(Context);
1426 D != DEnd; ++D)
1427 AddDeclRef(*D, Record);
1428
Douglas Gregorc9490c02009-04-16 22:23:12 +00001429 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001430 return Offset;
1431}
1432
1433/// \brief Write the block containing all of the declaration IDs
1434/// visible from the given DeclContext.
1435///
1436/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1437/// bistream, or 0 if no block was written.
1438uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1439 DeclContext *DC) {
1440 if (DC->getPrimaryContext() != DC)
1441 return 0;
1442
Douglas Gregor58f06992009-04-18 15:49:20 +00001443 // Since there is no name lookup into functions or methods, don't
1444 // bother to build a visible-declarations table.
1445 if (DC->isFunctionOrMethod())
1446 return 0;
1447
Douglas Gregor2cf26342009-04-09 22:27:44 +00001448 // Force the DeclContext to build a its name-lookup table.
1449 DC->lookup(Context, DeclarationName());
1450
1451 // Serialize the contents of the mapping used for lookup. Note that,
1452 // although we have two very different code paths, the serialized
1453 // representation is the same for both cases: a declaration name,
1454 // followed by a size, followed by references to the visible
1455 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001456 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001457 RecordData Record;
1458 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001459 if (!Map)
1460 return 0;
1461
Douglas Gregor2cf26342009-04-09 22:27:44 +00001462 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1463 D != DEnd; ++D) {
1464 AddDeclarationName(D->first, Record);
1465 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1466 Record.push_back(Result.second - Result.first);
1467 for(; Result.first != Result.second; ++Result.first)
1468 AddDeclRef(*Result.first, Record);
1469 }
1470
1471 if (Record.size() == 0)
1472 return 0;
1473
Douglas Gregorc9490c02009-04-16 22:23:12 +00001474 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001475 return Offset;
1476}
1477
1478/// \brief Write a block containing all of the declarations.
1479void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001480 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001481 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001482
1483 // Emit all of the declarations.
1484 RecordData Record;
Douglas Gregor72971342009-04-18 00:02:19 +00001485 PCHDeclWriter W(*this, Context, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001486 while (!DeclsToEmit.empty()) {
1487 // Pull the next declaration off the queue
1488 Decl *D = DeclsToEmit.front();
1489 DeclsToEmit.pop();
1490
1491 // If this declaration is also a DeclContext, write blocks for the
1492 // declarations that lexically stored inside its context and those
1493 // declarations that are visible from its context. These blocks
1494 // are written before the declaration itself so that we can put
1495 // their offsets into the record for the declaration.
1496 uint64_t LexicalOffset = 0;
1497 uint64_t VisibleOffset = 0;
1498 DeclContext *DC = dyn_cast<DeclContext>(D);
1499 if (DC) {
1500 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1501 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1502 }
1503
1504 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001505 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001506 if (ID == 0)
1507 ID = DeclIDs.size();
1508
1509 unsigned Index = ID - 1;
1510
1511 // Record the offset for this declaration
1512 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001513 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001514 else if (DeclOffsets.size() < Index) {
1515 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001516 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001517 }
1518
1519 // Build and emit a record for this declaration
1520 Record.clear();
1521 W.Code = (pch::DeclCode)0;
1522 W.Visit(D);
1523 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001524 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001525 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001526
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001527 // If the declaration had any attributes, write them now.
1528 if (D->hasAttrs())
1529 WriteAttributeRecord(D->getAttrs());
1530
Douglas Gregor0b748912009-04-14 21:18:50 +00001531 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001532 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001533
Douglas Gregorfdd01722009-04-14 00:24:19 +00001534 // Note external declarations so that we can add them to a record
1535 // in the PCH file later.
1536 if (isa<FileScopeAsmDecl>(D))
1537 ExternalDefinitions.push_back(ID);
1538 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1539 if (// Non-static file-scope variables with initializers or that
1540 // are tentative definitions.
1541 (Var->isFileVarDecl() &&
1542 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1543 // Out-of-line definitions of static data members (C++).
1544 (Var->getDeclContext()->isRecord() &&
1545 !Var->getLexicalDeclContext()->isRecord() &&
1546 Var->getStorageClass() == VarDecl::Static))
1547 ExternalDefinitions.push_back(ID);
1548 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00001549 if (Func->isThisDeclarationADefinition())
Douglas Gregorfdd01722009-04-14 00:24:19 +00001550 ExternalDefinitions.push_back(ID);
1551 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001552 }
1553
1554 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001555 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001556}
1557
Douglas Gregorafaf3082009-04-11 00:14:32 +00001558/// \brief Write the identifier table into the PCH file.
1559///
1560/// The identifier table consists of a blob containing string data
1561/// (the actual identifiers themselves) and a separate "offsets" index
1562/// that maps identifier IDs to locations within the blob.
1563void PCHWriter::WriteIdentifierTable() {
1564 using namespace llvm;
1565
1566 // Create and write out the blob that contains the identifier
1567 // strings.
1568 RecordData IdentOffsets;
1569 IdentOffsets.resize(IdentifierIDs.size());
1570 {
1571 // Create the identifier string data.
1572 std::vector<char> Data;
1573 Data.push_back(0); // Data must not be empty.
1574 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1575 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1576 ID != IDEnd; ++ID) {
1577 assert(ID->first && "NULL identifier in identifier table");
1578
1579 // Make sure we're starting on an odd byte. The PCH reader
1580 // expects the low bit to be set on all of the offsets.
1581 if ((Data.size() & 0x01) == 0)
1582 Data.push_back((char)0);
1583
1584 IdentOffsets[ID->second - 1] = Data.size();
1585 Data.insert(Data.end(),
1586 ID->first->getName(),
1587 ID->first->getName() + ID->first->getLength());
1588 Data.push_back((char)0);
1589 }
1590
1591 // Create a blob abbreviation
1592 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1593 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1594 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001595 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001596
1597 // Write the identifier table
1598 RecordData Record;
1599 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001600 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001601 }
1602
1603 // Write the offsets table for identifier IDs.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001604 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001605}
1606
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001607/// \brief Write a record containing the given attributes.
1608void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1609 RecordData Record;
1610 for (; Attr; Attr = Attr->getNext()) {
1611 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1612 Record.push_back(Attr->isInherited());
1613 switch (Attr->getKind()) {
1614 case Attr::Alias:
1615 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1616 break;
1617
1618 case Attr::Aligned:
1619 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1620 break;
1621
1622 case Attr::AlwaysInline:
1623 break;
1624
1625 case Attr::AnalyzerNoReturn:
1626 break;
1627
1628 case Attr::Annotate:
1629 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1630 break;
1631
1632 case Attr::AsmLabel:
1633 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1634 break;
1635
1636 case Attr::Blocks:
1637 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1638 break;
1639
1640 case Attr::Cleanup:
1641 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1642 break;
1643
1644 case Attr::Const:
1645 break;
1646
1647 case Attr::Constructor:
1648 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1649 break;
1650
1651 case Attr::DLLExport:
1652 case Attr::DLLImport:
1653 case Attr::Deprecated:
1654 break;
1655
1656 case Attr::Destructor:
1657 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1658 break;
1659
1660 case Attr::FastCall:
1661 break;
1662
1663 case Attr::Format: {
1664 const FormatAttr *Format = cast<FormatAttr>(Attr);
1665 AddString(Format->getType(), Record);
1666 Record.push_back(Format->getFormatIdx());
1667 Record.push_back(Format->getFirstArg());
1668 break;
1669 }
1670
1671 case Attr::GNUCInline:
1672 case Attr::IBOutletKind:
1673 case Attr::NoReturn:
1674 case Attr::NoThrow:
1675 case Attr::Nodebug:
1676 case Attr::Noinline:
1677 break;
1678
1679 case Attr::NonNull: {
1680 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1681 Record.push_back(NonNull->size());
1682 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1683 break;
1684 }
1685
1686 case Attr::ObjCException:
1687 case Attr::ObjCNSObject:
1688 case Attr::Overloadable:
1689 break;
1690
1691 case Attr::Packed:
1692 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1693 break;
1694
1695 case Attr::Pure:
1696 break;
1697
1698 case Attr::Regparm:
1699 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1700 break;
1701
1702 case Attr::Section:
1703 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1704 break;
1705
1706 case Attr::StdCall:
1707 case Attr::TransparentUnion:
1708 case Attr::Unavailable:
1709 case Attr::Unused:
1710 case Attr::Used:
1711 break;
1712
1713 case Attr::Visibility:
1714 // FIXME: stable encoding
1715 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1716 break;
1717
1718 case Attr::WarnUnusedResult:
1719 case Attr::Weak:
1720 case Attr::WeakImport:
1721 break;
1722 }
1723 }
1724
Douglas Gregorc9490c02009-04-16 22:23:12 +00001725 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001726}
1727
1728void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1729 Record.push_back(Str.size());
1730 Record.insert(Record.end(), Str.begin(), Str.end());
1731}
1732
Douglas Gregorc9490c02009-04-16 22:23:12 +00001733PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor3e1af842009-04-17 22:13:46 +00001734 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), NumStatements(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001735
Chris Lattnerdf961c22009-04-10 18:08:30 +00001736void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001737 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001738 Stream.Emit((unsigned)'C', 8);
1739 Stream.Emit((unsigned)'P', 8);
1740 Stream.Emit((unsigned)'C', 8);
1741 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001742
1743 // The translation unit is the first declaration we'll emit.
1744 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1745 DeclsToEmit.push(Context.getTranslationUnitDecl());
1746
1747 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001748 RecordData Record;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001749 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001750 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001751 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001752 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001753 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001754 WriteTypesBlock(Context);
1755 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001756 WriteIdentifierTable();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001757 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1758 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorad1de002009-04-18 05:55:16 +00001759
1760 // Write the record of special types.
1761 Record.clear();
1762 AddTypeRef(Context.getBuiltinVaListType(), Record);
1763 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1764
Douglas Gregorfdd01722009-04-14 00:24:19 +00001765 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00001766 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001767
1768 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00001769 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00001770 Record.push_back(NumStatements);
1771 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001772 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001773}
1774
1775void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1776 Record.push_back(Loc.getRawEncoding());
1777}
1778
1779void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1780 Record.push_back(Value.getBitWidth());
1781 unsigned N = Value.getNumWords();
1782 const uint64_t* Words = Value.getRawData();
1783 for (unsigned I = 0; I != N; ++I)
1784 Record.push_back(Words[I]);
1785}
1786
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001787void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1788 Record.push_back(Value.isUnsigned());
1789 AddAPInt(Value, Record);
1790}
1791
Douglas Gregor17fc2232009-04-14 21:55:33 +00001792void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1793 AddAPInt(Value.bitcastToAPInt(), Record);
1794}
1795
Douglas Gregor2cf26342009-04-09 22:27:44 +00001796void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001797 if (II == 0) {
1798 Record.push_back(0);
1799 return;
1800 }
1801
1802 pch::IdentID &ID = IdentifierIDs[II];
1803 if (ID == 0)
1804 ID = IdentifierIDs.size();
1805
1806 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001807}
1808
1809void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1810 if (T.isNull()) {
1811 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1812 return;
1813 }
1814
1815 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001816 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001817 switch (BT->getKind()) {
1818 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1819 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1820 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1821 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1822 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1823 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1824 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1825 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1826 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1827 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1828 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1829 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1830 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1831 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1832 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1833 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1834 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1835 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1836 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1837 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1838 }
1839
1840 Record.push_back((ID << 3) | T.getCVRQualifiers());
1841 return;
1842 }
1843
Douglas Gregor8038d512009-04-10 17:25:41 +00001844 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001845 if (ID == 0) // we haven't seen this type before
1846 ID = NextTypeID++;
1847
1848 // Encode the type qualifiers in the type reference.
1849 Record.push_back((ID << 3) | T.getCVRQualifiers());
1850}
1851
1852void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1853 if (D == 0) {
1854 Record.push_back(0);
1855 return;
1856 }
1857
Douglas Gregor8038d512009-04-10 17:25:41 +00001858 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001859 if (ID == 0) {
1860 // We haven't seen this declaration before. Give it a new ID and
1861 // enqueue it in the list of declarations to emit.
1862 ID = DeclIDs.size();
1863 DeclsToEmit.push(const_cast<Decl *>(D));
1864 }
1865
1866 Record.push_back(ID);
1867}
1868
1869void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1870 Record.push_back(Name.getNameKind());
1871 switch (Name.getNameKind()) {
1872 case DeclarationName::Identifier:
1873 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1874 break;
1875
1876 case DeclarationName::ObjCZeroArgSelector:
1877 case DeclarationName::ObjCOneArgSelector:
1878 case DeclarationName::ObjCMultiArgSelector:
1879 assert(false && "Serialization of Objective-C selectors unavailable");
1880 break;
1881
1882 case DeclarationName::CXXConstructorName:
1883 case DeclarationName::CXXDestructorName:
1884 case DeclarationName::CXXConversionFunctionName:
1885 AddTypeRef(Name.getCXXNameType(), Record);
1886 break;
1887
1888 case DeclarationName::CXXOperatorName:
1889 Record.push_back(Name.getCXXOverloadedOperator());
1890 break;
1891
1892 case DeclarationName::CXXUsingDirective:
1893 // No extra data to emit
1894 break;
1895 }
1896}
Douglas Gregor0b748912009-04-14 21:18:50 +00001897
Douglas Gregorc9490c02009-04-16 22:23:12 +00001898/// \brief Write the given substatement or subexpression to the
1899/// bitstream.
1900void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00001901 RecordData Record;
1902 PCHStmtWriter Writer(*this, Record);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001903 ++NumStatements;
Douglas Gregor087fd532009-04-14 23:32:43 +00001904
Douglas Gregorc9490c02009-04-16 22:23:12 +00001905 if (!S) {
1906 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001907 return;
1908 }
1909
Douglas Gregorc9490c02009-04-16 22:23:12 +00001910 Writer.Code = pch::STMT_NULL_PTR;
1911 Writer.Visit(S);
1912 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00001913 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001914 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001915}
1916
Douglas Gregorc9490c02009-04-16 22:23:12 +00001917/// \brief Flush all of the statements that have been added to the
1918/// queue via AddStmt().
1919void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00001920 RecordData Record;
1921 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001922
Douglas Gregorc9490c02009-04-16 22:23:12 +00001923 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor3e1af842009-04-17 22:13:46 +00001924 ++NumStatements;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001925 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00001926
Douglas Gregorc9490c02009-04-16 22:23:12 +00001927 if (!S) {
1928 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001929 continue;
1930 }
1931
Douglas Gregorc9490c02009-04-16 22:23:12 +00001932 Writer.Code = pch::STMT_NULL_PTR;
1933 Writer.Visit(S);
1934 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00001935 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001936 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001937
Douglas Gregorc9490c02009-04-16 22:23:12 +00001938 assert(N == StmtsToEmit.size() &&
1939 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00001940
1941 // Note that we are at the end of a full expression. Any
1942 // expression records that follow this one are part of a different
1943 // expression.
1944 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001945 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001946 }
Douglas Gregor087fd532009-04-14 23:32:43 +00001947
Douglas Gregorc9490c02009-04-16 22:23:12 +00001948 StmtsToEmit.clear();
Douglas Gregor0de9d882009-04-17 16:34:57 +00001949 SwitchCaseIDs.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00001950}
Douglas Gregor025452f2009-04-17 00:04:06 +00001951
1952unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1953 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1954 "SwitchCase recorded twice");
1955 unsigned NextID = SwitchCaseIDs.size();
1956 SwitchCaseIDs[S] = NextID;
1957 return NextID;
1958}
1959
1960unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1961 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1962 "SwitchCase hasn't been seen yet");
1963 return SwitchCaseIDs[S];
1964}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00001965
1966/// \brief Retrieve the ID for the given label statement, which may
1967/// or may not have been emitted yet.
1968unsigned PCHWriter::GetLabelID(LabelStmt *S) {
1969 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
1970 if (Pos != LabelIDs.end())
1971 return Pos->second;
1972
1973 unsigned NextID = LabelIDs.size();
1974 LabelIDs[S] = NextID;
1975 return NextID;
1976}