blob: 14e979f2bc7f4cfc8641bfe424a527cf3a52bbc6 [file] [log] [blame]
Douglas Gregorc34897d2009-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 Gregorc10f86f2009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000022#include "clang/Lex/MacroInfo.h"
23#include "clang/Lex/Preprocessor.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000028#include "llvm/ADT/APFloat.h"
29#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000030#include "llvm/Bitcode/BitstreamWriter.h"
31#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000032#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000033#include <cstdio>
Douglas Gregorc34897d2009-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 Gregorc72f6c82009-04-16 22:23:12 +0000129 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-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 Gregorc72f6c82009-04-16 22:23:12 +0000169 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-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 Gregor3c8ff3e2009-04-15 18:43:11 +0000197 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000198 assert(false && "Cannot serialize template specialization types");
199}
200
201void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000202 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000203 assert(false && "Cannot serialize qualified name types");
204}
205
206void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
207 Writer.AddDeclRef(T->getDecl(), Record);
208 Code = pch::TYPE_OBJC_INTERFACE;
209}
210
211void
212PCHTypeWriter::VisitObjCQualifiedInterfaceType(
213 const ObjCQualifiedInterfaceType *T) {
214 VisitObjCInterfaceType(T);
215 Record.push_back(T->getNumProtocols());
216 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
217 Writer.AddDeclRef(T->getProtocol(I), Record);
218 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
219}
220
221void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
222 Record.push_back(T->getNumProtocols());
223 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
224 Writer.AddDeclRef(T->getProtocols(I), Record);
225 Code = pch::TYPE_OBJC_QUALIFIED_ID;
226}
227
228void
229PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) {
230 Record.push_back(T->getNumProtocols());
231 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
232 Writer.AddDeclRef(T->getProtocols(I), Record);
233 Code = pch::TYPE_OBJC_QUALIFIED_CLASS;
234}
235
236//===----------------------------------------------------------------------===//
237// Declaration serialization
238//===----------------------------------------------------------------------===//
239namespace {
240 class VISIBILITY_HIDDEN PCHDeclWriter
241 : public DeclVisitor<PCHDeclWriter, void> {
242
243 PCHWriter &Writer;
244 PCHWriter::RecordData &Record;
245
246 public:
247 pch::DeclCode Code;
248
249 PCHDeclWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
250 : Writer(Writer), Record(Record) { }
251
252 void VisitDecl(Decl *D);
253 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
254 void VisitNamedDecl(NamedDecl *D);
255 void VisitTypeDecl(TypeDecl *D);
256 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000257 void VisitTagDecl(TagDecl *D);
258 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000259 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000260 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000261 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000262 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000263 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000264 void VisitVarDecl(VarDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000265 void VisitParmVarDecl(ParmVarDecl *D);
266 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000267 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
268 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000269 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
270 uint64_t VisibleOffset);
271 };
272}
273
274void PCHDeclWriter::VisitDecl(Decl *D) {
275 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
276 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
277 Writer.AddSourceLocation(D->getLocation(), Record);
278 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000279 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000280 Record.push_back(D->isImplicit());
281 Record.push_back(D->getAccess());
282}
283
284void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
285 VisitDecl(D);
286 Code = pch::DECL_TRANSLATION_UNIT;
287}
288
289void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
290 VisitDecl(D);
291 Writer.AddDeclarationName(D->getDeclName(), Record);
292}
293
294void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
295 VisitNamedDecl(D);
296 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
297}
298
299void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
300 VisitTypeDecl(D);
301 Writer.AddTypeRef(D->getUnderlyingType(), Record);
302 Code = pch::DECL_TYPEDEF;
303}
304
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000305void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
306 VisitTypeDecl(D);
307 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
308 Record.push_back(D->isDefinition());
309 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
310}
311
312void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
313 VisitTagDecl(D);
314 Writer.AddTypeRef(D->getIntegerType(), Record);
315 Code = pch::DECL_ENUM;
316}
317
Douglas Gregor982365e2009-04-13 21:20:57 +0000318void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
319 VisitTagDecl(D);
320 Record.push_back(D->hasFlexibleArrayMember());
321 Record.push_back(D->isAnonymousStructOrUnion());
322 Code = pch::DECL_RECORD;
323}
324
Douglas Gregorc34897d2009-04-09 22:27:44 +0000325void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
326 VisitNamedDecl(D);
327 Writer.AddTypeRef(D->getType(), Record);
328}
329
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000330void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
331 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000332 Record.push_back(D->getInitExpr()? 1 : 0);
333 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000334 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000335 Writer.AddAPSInt(D->getInitVal(), Record);
336 Code = pch::DECL_ENUM_CONSTANT;
337}
338
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000339void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
340 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000341 Record.push_back(D->isThisDeclarationADefinition());
342 if (D->isThisDeclarationADefinition())
343 Writer.AddStmt(D->getBody());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000344 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
345 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
346 Record.push_back(D->isInline());
347 Record.push_back(D->isVirtual());
348 Record.push_back(D->isPure());
349 Record.push_back(D->inheritedPrototype());
350 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
351 Record.push_back(D->isDeleted());
352 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
353 Record.push_back(D->param_size());
354 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
355 P != PEnd; ++P)
356 Writer.AddDeclRef(*P, Record);
357 Code = pch::DECL_FUNCTION;
358}
359
Douglas Gregor982365e2009-04-13 21:20:57 +0000360void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
361 VisitValueDecl(D);
362 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000363 Record.push_back(D->getBitWidth()? 1 : 0);
364 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000365 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000366 Code = pch::DECL_FIELD;
367}
368
Douglas Gregorc34897d2009-04-09 22:27:44 +0000369void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
370 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000371 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000372 Record.push_back(D->isThreadSpecified());
373 Record.push_back(D->hasCXXDirectInitializer());
374 Record.push_back(D->isDeclaredInCondition());
375 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
376 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000377 Record.push_back(D->getInit()? 1 : 0);
378 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000379 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000380 Code = pch::DECL_VAR;
381}
382
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000383void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
384 VisitVarDecl(D);
385 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000386 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000387 // FIXME: why isn't the "default argument" just stored as the initializer
388 // in VarDecl?
389 Code = pch::DECL_PARM_VAR;
390}
391
392void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
393 VisitParmVarDecl(D);
394 Writer.AddTypeRef(D->getOriginalType(), Record);
395 Code = pch::DECL_ORIGINAL_PARM_VAR;
396}
397
Douglas Gregor2a491792009-04-13 22:49:25 +0000398void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
399 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000400 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000401 Code = pch::DECL_FILE_SCOPE_ASM;
402}
403
404void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
405 VisitDecl(D);
406 // FIXME: emit block body
407 Record.push_back(D->param_size());
408 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
409 P != PEnd; ++P)
410 Writer.AddDeclRef(*P, Record);
411 Code = pch::DECL_BLOCK;
412}
413
Douglas Gregorc34897d2009-04-09 22:27:44 +0000414/// \brief Emit the DeclContext part of a declaration context decl.
415///
416/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
417/// block for this declaration context is stored. May be 0 to indicate
418/// that there are no declarations stored within this context.
419///
420/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
421/// block for this declaration context is stored. May be 0 to indicate
422/// that there are no declarations visible from this context. Note
423/// that this value will not be emitted for non-primary declaration
424/// contexts.
425void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
426 uint64_t VisibleOffset) {
427 Record.push_back(LexicalOffset);
428 if (DC->getPrimaryContext() == DC)
429 Record.push_back(VisibleOffset);
430}
431
432//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000433// Statement/expression serialization
434//===----------------------------------------------------------------------===//
435namespace {
436 class VISIBILITY_HIDDEN PCHStmtWriter
437 : public StmtVisitor<PCHStmtWriter, void> {
438
439 PCHWriter &Writer;
440 PCHWriter::RecordData &Record;
441
442 public:
443 pch::StmtCode Code;
444
445 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
446 : Writer(Writer), Record(Record) { }
447
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000448 void VisitStmt(Stmt *S);
449 void VisitNullStmt(NullStmt *S);
450 void VisitCompoundStmt(CompoundStmt *S);
451 void VisitSwitchCase(SwitchCase *S);
452 void VisitCaseStmt(CaseStmt *S);
453 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000454 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000455 void VisitIfStmt(IfStmt *S);
456 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000457 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000458 void VisitDoStmt(DoStmt *S);
459 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000460 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000461 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000462 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000463 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000464 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000465 void VisitDeclStmt(DeclStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000466 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000467 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000468 void VisitDeclRefExpr(DeclRefExpr *E);
469 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000470 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000471 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000472 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000473 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000474 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000475 void VisitUnaryOperator(UnaryOperator *E);
476 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000477 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000478 void VisitCallExpr(CallExpr *E);
479 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000480 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000481 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000482 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
483 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000484 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000485 void VisitExplicitCastExpr(ExplicitCastExpr *E);
486 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000487 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000488 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000489 void VisitInitListExpr(InitListExpr *E);
490 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
491 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000492 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000493 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000494 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000495 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
496 void VisitChooseExpr(ChooseExpr *E);
497 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000498 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
499 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000500 };
501}
502
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000503void PCHStmtWriter::VisitStmt(Stmt *S) {
504}
505
506void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
507 VisitStmt(S);
508 Writer.AddSourceLocation(S->getSemiLoc(), Record);
509 Code = pch::STMT_NULL;
510}
511
512void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
513 VisitStmt(S);
514 Record.push_back(S->size());
515 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
516 CS != CSEnd; ++CS)
517 Writer.WriteSubStmt(*CS);
518 Writer.AddSourceLocation(S->getLBracLoc(), Record);
519 Writer.AddSourceLocation(S->getRBracLoc(), Record);
520 Code = pch::STMT_COMPOUND;
521}
522
523void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
524 VisitStmt(S);
525 Record.push_back(Writer.RecordSwitchCaseID(S));
526}
527
528void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
529 VisitSwitchCase(S);
530 Writer.WriteSubStmt(S->getLHS());
531 Writer.WriteSubStmt(S->getRHS());
532 Writer.WriteSubStmt(S->getSubStmt());
533 Writer.AddSourceLocation(S->getCaseLoc(), Record);
534 Code = pch::STMT_CASE;
535}
536
537void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
538 VisitSwitchCase(S);
539 Writer.WriteSubStmt(S->getSubStmt());
540 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
541 Code = pch::STMT_DEFAULT;
542}
543
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000544void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
545 VisitStmt(S);
546 Writer.AddIdentifierRef(S->getID(), Record);
547 Writer.WriteSubStmt(S->getSubStmt());
548 Writer.AddSourceLocation(S->getIdentLoc(), Record);
549 Record.push_back(Writer.GetLabelID(S));
550 Code = pch::STMT_LABEL;
551}
552
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000553void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
554 VisitStmt(S);
555 Writer.WriteSubStmt(S->getCond());
556 Writer.WriteSubStmt(S->getThen());
557 Writer.WriteSubStmt(S->getElse());
558 Writer.AddSourceLocation(S->getIfLoc(), Record);
559 Code = pch::STMT_IF;
560}
561
562void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
563 VisitStmt(S);
564 Writer.WriteSubStmt(S->getCond());
565 Writer.WriteSubStmt(S->getBody());
566 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
567 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
568 SC = SC->getNextSwitchCase())
569 Record.push_back(Writer.getSwitchCaseID(SC));
570 Code = pch::STMT_SWITCH;
571}
572
Douglas Gregora6b503f2009-04-17 00:16:09 +0000573void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
574 VisitStmt(S);
575 Writer.WriteSubStmt(S->getCond());
576 Writer.WriteSubStmt(S->getBody());
577 Writer.AddSourceLocation(S->getWhileLoc(), Record);
578 Code = pch::STMT_WHILE;
579}
580
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000581void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
582 VisitStmt(S);
583 Writer.WriteSubStmt(S->getCond());
584 Writer.WriteSubStmt(S->getBody());
585 Writer.AddSourceLocation(S->getDoLoc(), Record);
586 Code = pch::STMT_DO;
587}
588
589void PCHStmtWriter::VisitForStmt(ForStmt *S) {
590 VisitStmt(S);
591 Writer.WriteSubStmt(S->getInit());
592 Writer.WriteSubStmt(S->getCond());
593 Writer.WriteSubStmt(S->getInc());
594 Writer.WriteSubStmt(S->getBody());
595 Writer.AddSourceLocation(S->getForLoc(), Record);
596 Code = pch::STMT_FOR;
597}
598
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000599void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
600 VisitStmt(S);
601 Record.push_back(Writer.GetLabelID(S->getLabel()));
602 Writer.AddSourceLocation(S->getGotoLoc(), Record);
603 Writer.AddSourceLocation(S->getLabelLoc(), Record);
604 Code = pch::STMT_GOTO;
605}
606
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000607void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
608 VisitStmt(S);
609 Writer.WriteSubStmt(S->getTarget());
610 Code = pch::STMT_INDIRECT_GOTO;
611}
612
Douglas Gregora6b503f2009-04-17 00:16:09 +0000613void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
614 VisitStmt(S);
615 Writer.AddSourceLocation(S->getContinueLoc(), Record);
616 Code = pch::STMT_CONTINUE;
617}
618
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000619void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
620 VisitStmt(S);
621 Writer.AddSourceLocation(S->getBreakLoc(), Record);
622 Code = pch::STMT_BREAK;
623}
624
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000625void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
626 VisitStmt(S);
627 Writer.WriteSubStmt(S->getRetValue());
628 Writer.AddSourceLocation(S->getReturnLoc(), Record);
629 Code = pch::STMT_RETURN;
630}
631
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000632void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
633 VisitStmt(S);
634 Writer.AddSourceLocation(S->getStartLoc(), Record);
635 Writer.AddSourceLocation(S->getEndLoc(), Record);
636 DeclGroupRef DG = S->getDeclGroup();
637 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
638 Writer.AddDeclRef(*D, Record);
639 Code = pch::STMT_DECL;
640}
641
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000642void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000643 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000644 Writer.AddTypeRef(E->getType(), Record);
645 Record.push_back(E->isTypeDependent());
646 Record.push_back(E->isValueDependent());
647}
648
Douglas Gregore2f37202009-04-14 21:55:33 +0000649void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
650 VisitExpr(E);
651 Writer.AddSourceLocation(E->getLocation(), Record);
652 Record.push_back(E->getIdentType()); // FIXME: stable encoding
653 Code = pch::EXPR_PREDEFINED;
654}
655
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000656void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
657 VisitExpr(E);
658 Writer.AddDeclRef(E->getDecl(), Record);
659 Writer.AddSourceLocation(E->getLocation(), Record);
660 Code = pch::EXPR_DECL_REF;
661}
662
663void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
664 VisitExpr(E);
665 Writer.AddSourceLocation(E->getLocation(), Record);
666 Writer.AddAPInt(E->getValue(), Record);
667 Code = pch::EXPR_INTEGER_LITERAL;
668}
669
Douglas Gregore2f37202009-04-14 21:55:33 +0000670void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
671 VisitExpr(E);
672 Writer.AddAPFloat(E->getValue(), Record);
673 Record.push_back(E->isExact());
674 Writer.AddSourceLocation(E->getLocation(), Record);
675 Code = pch::EXPR_FLOATING_LITERAL;
676}
677
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000678void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
679 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000680 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000681 Code = pch::EXPR_IMAGINARY_LITERAL;
682}
683
Douglas Gregor596e0932009-04-15 16:35:07 +0000684void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
685 VisitExpr(E);
686 Record.push_back(E->getByteLength());
687 Record.push_back(E->getNumConcatenated());
688 Record.push_back(E->isWide());
689 // FIXME: String data should be stored as a blob at the end of the
690 // StringLiteral. However, we can't do so now because we have no
691 // provision for coping with abbreviations when we're jumping around
692 // the PCH file during deserialization.
693 Record.insert(Record.end(),
694 E->getStrData(), E->getStrData() + E->getByteLength());
695 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
696 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
697 Code = pch::EXPR_STRING_LITERAL;
698}
699
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000700void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
701 VisitExpr(E);
702 Record.push_back(E->getValue());
703 Writer.AddSourceLocation(E->getLoc(), Record);
704 Record.push_back(E->isWide());
705 Code = pch::EXPR_CHARACTER_LITERAL;
706}
707
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000708void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
709 VisitExpr(E);
710 Writer.AddSourceLocation(E->getLParen(), Record);
711 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000712 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000713 Code = pch::EXPR_PAREN;
714}
715
Douglas Gregor12d74052009-04-15 15:58:59 +0000716void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
717 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000718 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000719 Record.push_back(E->getOpcode()); // FIXME: stable encoding
720 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
721 Code = pch::EXPR_UNARY_OPERATOR;
722}
723
724void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
725 VisitExpr(E);
726 Record.push_back(E->isSizeOf());
727 if (E->isArgumentType())
728 Writer.AddTypeRef(E->getArgumentType(), Record);
729 else {
730 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000731 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000732 }
733 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
734 Writer.AddSourceLocation(E->getRParenLoc(), Record);
735 Code = pch::EXPR_SIZEOF_ALIGN_OF;
736}
737
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000738void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
739 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000740 Writer.WriteSubStmt(E->getLHS());
741 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000742 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
743 Code = pch::EXPR_ARRAY_SUBSCRIPT;
744}
745
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000746void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
747 VisitExpr(E);
748 Record.push_back(E->getNumArgs());
749 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000750 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000751 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
752 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000753 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000754 Code = pch::EXPR_CALL;
755}
756
757void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
758 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000759 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000760 Writer.AddDeclRef(E->getMemberDecl(), Record);
761 Writer.AddSourceLocation(E->getMemberLoc(), Record);
762 Record.push_back(E->isArrow());
763 Code = pch::EXPR_MEMBER;
764}
765
Douglas Gregora151ba42009-04-14 23:32:43 +0000766void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
767 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000768 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000769}
770
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000771void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
772 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000773 Writer.WriteSubStmt(E->getLHS());
774 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000775 Record.push_back(E->getOpcode()); // FIXME: stable encoding
776 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
777 Code = pch::EXPR_BINARY_OPERATOR;
778}
779
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000780void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
781 VisitBinaryOperator(E);
782 Writer.AddTypeRef(E->getComputationLHSType(), Record);
783 Writer.AddTypeRef(E->getComputationResultType(), Record);
784 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
785}
786
787void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
788 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000789 Writer.WriteSubStmt(E->getCond());
790 Writer.WriteSubStmt(E->getLHS());
791 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000792 Code = pch::EXPR_CONDITIONAL_OPERATOR;
793}
794
Douglas Gregora151ba42009-04-14 23:32:43 +0000795void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
796 VisitCastExpr(E);
797 Record.push_back(E->isLvalueCast());
798 Code = pch::EXPR_IMPLICIT_CAST;
799}
800
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000801void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
802 VisitCastExpr(E);
803 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
804}
805
806void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
807 VisitExplicitCastExpr(E);
808 Writer.AddSourceLocation(E->getLParenLoc(), Record);
809 Writer.AddSourceLocation(E->getRParenLoc(), Record);
810 Code = pch::EXPR_CSTYLE_CAST;
811}
812
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000813void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
814 VisitExpr(E);
815 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000816 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000817 Record.push_back(E->isFileScope());
818 Code = pch::EXPR_COMPOUND_LITERAL;
819}
820
Douglas Gregorec0b8292009-04-15 23:02:49 +0000821void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
822 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000823 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000824 Writer.AddIdentifierRef(&E->getAccessor(), Record);
825 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
826 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
827}
828
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000829void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
830 VisitExpr(E);
831 Record.push_back(E->getNumInits());
832 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000833 Writer.WriteSubStmt(E->getInit(I));
834 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000835 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
836 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
837 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
838 Record.push_back(E->hadArrayRangeDesignator());
839 Code = pch::EXPR_INIT_LIST;
840}
841
842void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
843 VisitExpr(E);
844 Record.push_back(E->getNumSubExprs());
845 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000846 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000847 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
848 Record.push_back(E->usesGNUSyntax());
849 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
850 DEnd = E->designators_end();
851 D != DEnd; ++D) {
852 if (D->isFieldDesignator()) {
853 if (FieldDecl *Field = D->getField()) {
854 Record.push_back(pch::DESIG_FIELD_DECL);
855 Writer.AddDeclRef(Field, Record);
856 } else {
857 Record.push_back(pch::DESIG_FIELD_NAME);
858 Writer.AddIdentifierRef(D->getFieldName(), Record);
859 }
860 Writer.AddSourceLocation(D->getDotLoc(), Record);
861 Writer.AddSourceLocation(D->getFieldLoc(), Record);
862 } else if (D->isArrayDesignator()) {
863 Record.push_back(pch::DESIG_ARRAY);
864 Record.push_back(D->getFirstExprIndex());
865 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
866 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
867 } else {
868 assert(D->isArrayRangeDesignator() && "Unknown designator");
869 Record.push_back(pch::DESIG_ARRAY_RANGE);
870 Record.push_back(D->getFirstExprIndex());
871 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
872 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
873 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
874 }
875 }
876 Code = pch::EXPR_DESIGNATED_INIT;
877}
878
879void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
880 VisitExpr(E);
881 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
882}
883
Douglas Gregorec0b8292009-04-15 23:02:49 +0000884void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
885 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000886 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000887 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
888 Writer.AddSourceLocation(E->getRParenLoc(), Record);
889 Code = pch::EXPR_VA_ARG;
890}
891
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000892void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
893 VisitExpr(E);
894 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
895 Writer.AddSourceLocation(E->getLabelLoc(), Record);
896 Record.push_back(Writer.GetLabelID(E->getLabel()));
897 Code = pch::EXPR_ADDR_LABEL;
898}
899
Douglas Gregoreca12f62009-04-17 19:05:30 +0000900void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
901 VisitExpr(E);
902 Writer.WriteSubStmt(E->getSubStmt());
903 Writer.AddSourceLocation(E->getLParenLoc(), Record);
904 Writer.AddSourceLocation(E->getRParenLoc(), Record);
905 Code = pch::EXPR_STMT;
906}
907
Douglas Gregor209d4622009-04-15 23:33:31 +0000908void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
909 VisitExpr(E);
910 Writer.AddTypeRef(E->getArgType1(), Record);
911 Writer.AddTypeRef(E->getArgType2(), Record);
912 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
913 Writer.AddSourceLocation(E->getRParenLoc(), Record);
914 Code = pch::EXPR_TYPES_COMPATIBLE;
915}
916
917void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
918 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000919 Writer.WriteSubStmt(E->getCond());
920 Writer.WriteSubStmt(E->getLHS());
921 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +0000922 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
923 Writer.AddSourceLocation(E->getRParenLoc(), Record);
924 Code = pch::EXPR_CHOOSE;
925}
926
927void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
928 VisitExpr(E);
929 Writer.AddSourceLocation(E->getTokenLocation(), Record);
930 Code = pch::EXPR_GNU_NULL;
931}
932
Douglas Gregor725e94b2009-04-16 00:01:45 +0000933void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
934 VisitExpr(E);
935 Record.push_back(E->getNumSubExprs());
936 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000937 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +0000938 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
939 Writer.AddSourceLocation(E->getRParenLoc(), Record);
940 Code = pch::EXPR_SHUFFLE_VECTOR;
941}
942
943void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
944 VisitExpr(E);
945 Writer.AddDeclRef(E->getDecl(), Record);
946 Writer.AddSourceLocation(E->getLocation(), Record);
947 Record.push_back(E->isByRef());
948 Code = pch::EXPR_BLOCK_DECL_REF;
949}
950
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000951//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000952// PCHWriter Implementation
953//===----------------------------------------------------------------------===//
954
Douglas Gregorb5887f32009-04-10 21:16:55 +0000955/// \brief Write the target triple (e.g., i686-apple-darwin9).
956void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
957 using namespace llvm;
958 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
959 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000961 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +0000962
963 RecordData Record;
964 Record.push_back(pch::TARGET_TRIPLE);
965 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000966 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +0000967}
968
969/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000970void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
971 RecordData Record;
972 Record.push_back(LangOpts.Trigraphs);
973 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
974 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
975 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
976 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
977 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
978 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
979 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
980 Record.push_back(LangOpts.C99); // C99 Support
981 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
982 Record.push_back(LangOpts.CPlusPlus); // C++ Support
983 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
984 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
985 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
986
987 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
988 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
989 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
990
991 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
992 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
993 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
994 Record.push_back(LangOpts.LaxVectorConversions);
995 Record.push_back(LangOpts.Exceptions); // Support exception handling.
996
997 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
998 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
999 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1000
1001 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1002 // by locks.
1003 Record.push_back(LangOpts.Blocks); // block extension to C
1004 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1005 // they are unused.
1006 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1007 // (modulo the platform support).
1008
1009 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1010 // signed integer arithmetic overflows.
1011
1012 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1013 // may be ripped out at any time.
1014
1015 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1016 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1017 // defined.
1018 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1019 // opposed to __DYNAMIC__).
1020 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1021
1022 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1023 // used (instead of C99 semantics).
1024 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1025 Record.push_back(LangOpts.getGCMode());
1026 Record.push_back(LangOpts.getVisibilityMode());
1027 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001028 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001029}
1030
Douglas Gregorab1cef72009-04-10 03:52:48 +00001031//===----------------------------------------------------------------------===//
1032// Source Manager Serialization
1033//===----------------------------------------------------------------------===//
1034
1035/// \brief Create an abbreviation for the SLocEntry that refers to a
1036/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001037static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001038 using namespace llvm;
1039 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1040 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001046 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001047}
1048
1049/// \brief Create an abbreviation for the SLocEntry that refers to a
1050/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001051static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001052 using namespace llvm;
1053 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1054 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001060 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001061}
1062
1063/// \brief Create an abbreviation for the SLocEntry that refers to a
1064/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001065static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001066 using namespace llvm;
1067 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1068 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1069 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001070 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001071}
1072
1073/// \brief Create an abbreviation for the SLocEntry that refers to an
1074/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001075static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001076 using namespace llvm;
1077 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1078 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1079 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1080 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1081 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001084 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001085}
1086
1087/// \brief Writes the block containing the serialized form of the
1088/// source manager.
1089///
1090/// TODO: We should probably use an on-disk hash table (stored in a
1091/// blob), indexed based on the file name, so that we only create
1092/// entries for files that we actually need. In the common case (no
1093/// errors), we probably won't have to create file entries for any of
1094/// the files in the AST.
1095void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001096 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001097 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001098
1099 // Abbreviations for the various kinds of source-location entries.
1100 int SLocFileAbbrv = -1;
1101 int SLocBufferAbbrv = -1;
1102 int SLocBufferBlobAbbrv = -1;
1103 int SLocInstantiationAbbrv = -1;
1104
1105 // Write out the source location entry table. We skip the first
1106 // entry, which is always the same dummy entry.
1107 RecordData Record;
1108 for (SourceManager::sloc_entry_iterator
1109 SLoc = SourceMgr.sloc_entry_begin() + 1,
1110 SLocEnd = SourceMgr.sloc_entry_end();
1111 SLoc != SLocEnd; ++SLoc) {
1112 // Figure out which record code to use.
1113 unsigned Code;
1114 if (SLoc->isFile()) {
1115 if (SLoc->getFile().getContentCache()->Entry)
1116 Code = pch::SM_SLOC_FILE_ENTRY;
1117 else
1118 Code = pch::SM_SLOC_BUFFER_ENTRY;
1119 } else
1120 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1121 Record.push_back(Code);
1122
1123 Record.push_back(SLoc->getOffset());
1124 if (SLoc->isFile()) {
1125 const SrcMgr::FileInfo &File = SLoc->getFile();
1126 Record.push_back(File.getIncludeLoc().getRawEncoding());
1127 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001128 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001129
1130 const SrcMgr::ContentCache *Content = File.getContentCache();
1131 if (Content->Entry) {
1132 // The source location entry is a file. The blob associated
1133 // with this entry is the file name.
1134 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001135 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1136 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001137 Content->Entry->getName(),
1138 strlen(Content->Entry->getName()));
1139 } else {
1140 // The source location entry is a buffer. The blob associated
1141 // with this entry contains the contents of the buffer.
1142 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001143 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1144 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001145 }
1146
1147 // We add one to the size so that we capture the trailing NULL
1148 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1149 // the reader side).
1150 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1151 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001152 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001153 Record.clear();
1154 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001155 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001156 Buffer->getBufferStart(),
1157 Buffer->getBufferSize() + 1);
1158 }
1159 } else {
1160 // The source location entry is an instantiation.
1161 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1162 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1163 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1164 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1165
Douglas Gregor364e5802009-04-15 18:05:10 +00001166 // Compute the token length for this macro expansion.
1167 unsigned NextOffset = SourceMgr.getNextOffset();
1168 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1169 if (++NextSLoc != SLocEnd)
1170 NextOffset = NextSLoc->getOffset();
1171 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1172
Douglas Gregorab1cef72009-04-10 03:52:48 +00001173 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001174 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1175 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001176 }
1177
1178 Record.clear();
1179 }
1180
Douglas Gregor635f97f2009-04-13 16:31:14 +00001181 // Write the line table.
1182 if (SourceMgr.hasLineTable()) {
1183 LineTableInfo &LineTable = SourceMgr.getLineTable();
1184
1185 // Emit the file names
1186 Record.push_back(LineTable.getNumFilenames());
1187 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1188 // Emit the file name
1189 const char *Filename = LineTable.getFilename(I);
1190 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1191 Record.push_back(FilenameLen);
1192 if (FilenameLen)
1193 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1194 }
1195
1196 // Emit the line entries
1197 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1198 L != LEnd; ++L) {
1199 // Emit the file ID
1200 Record.push_back(L->first);
1201
1202 // Emit the line entries
1203 Record.push_back(L->second.size());
1204 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1205 LEEnd = L->second.end();
1206 LE != LEEnd; ++LE) {
1207 Record.push_back(LE->FileOffset);
1208 Record.push_back(LE->LineNo);
1209 Record.push_back(LE->FilenameID);
1210 Record.push_back((unsigned)LE->FileKind);
1211 Record.push_back(LE->IncludeOffset);
1212 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001213 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001214 }
1215 }
1216
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001217 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001218}
1219
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001220/// \brief Writes the block containing the serialized form of the
1221/// preprocessor.
1222///
Chris Lattner850eabd2009-04-10 18:08:30 +00001223void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001224 // Enter the preprocessor block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001225 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattner84b04f12009-04-10 17:16:57 +00001226
Chris Lattner1b094952009-04-10 18:00:12 +00001227 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1228 // FIXME: use diagnostics subsystem for localization etc.
1229 if (PP.SawDateOrTime())
1230 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001231
Chris Lattner1b094952009-04-10 18:00:12 +00001232 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001233
Chris Lattner4b21c202009-04-13 01:29:17 +00001234 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1235 if (PP.getCounterValue() != 0) {
1236 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001237 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001238 Record.clear();
1239 }
1240
Chris Lattner1b094952009-04-10 18:00:12 +00001241 // Loop over all the macro definitions that are live at the end of the file,
1242 // emitting each to the PP section.
1243 // FIXME: Eventually we want to emit an index so that we can lazily load
1244 // macros.
1245 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1246 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001247 // FIXME: This emits macros in hash table order, we should do it in a stable
1248 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001249 MacroInfo *MI = I->second;
1250
1251 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1252 // been redefined by the header (in which case they are not isBuiltinMacro).
1253 if (MI->isBuiltinMacro())
1254 continue;
1255
Chris Lattner29241862009-04-11 21:15:38 +00001256 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001257 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1258 Record.push_back(MI->isUsed());
1259
1260 unsigned Code;
1261 if (MI->isObjectLike()) {
1262 Code = pch::PP_MACRO_OBJECT_LIKE;
1263 } else {
1264 Code = pch::PP_MACRO_FUNCTION_LIKE;
1265
1266 Record.push_back(MI->isC99Varargs());
1267 Record.push_back(MI->isGNUVarargs());
1268 Record.push_back(MI->getNumArgs());
1269 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1270 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001271 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001272 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001273 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001274 Record.clear();
1275
Chris Lattner850eabd2009-04-10 18:08:30 +00001276 // Emit the tokens array.
1277 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1278 // Note that we know that the preprocessor does not have any annotation
1279 // tokens in it because they are created by the parser, and thus can't be
1280 // in a macro definition.
1281 const Token &Tok = MI->getReplacementToken(TokNo);
1282
1283 Record.push_back(Tok.getLocation().getRawEncoding());
1284 Record.push_back(Tok.getLength());
1285
Chris Lattner850eabd2009-04-10 18:08:30 +00001286 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1287 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001288 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001289
1290 // FIXME: Should translate token kind to a stable encoding.
1291 Record.push_back(Tok.getKind());
1292 // FIXME: Should translate token flags to a stable encoding.
1293 Record.push_back(Tok.getFlags());
1294
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001295 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001296 Record.clear();
1297 }
Chris Lattner1b094952009-04-10 18:00:12 +00001298
1299 }
1300
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001301 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001302}
1303
1304
Douglas Gregorc34897d2009-04-09 22:27:44 +00001305/// \brief Write the representation of a type to the PCH stream.
1306void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001307 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001308 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001309 ID = NextTypeID++;
1310
1311 // Record the offset for this type.
1312 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001313 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001314 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1315 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001316 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001317 }
1318
1319 RecordData Record;
1320
1321 // Emit the type's representation.
1322 PCHTypeWriter W(*this, Record);
1323 switch (T->getTypeClass()) {
1324 // For all of the concrete, non-dependent types, call the
1325 // appropriate visitor function.
1326#define TYPE(Class, Base) \
1327 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1328#define ABSTRACT_TYPE(Class, Base)
1329#define DEPENDENT_TYPE(Class, Base)
1330#include "clang/AST/TypeNodes.def"
1331
1332 // For all of the dependent type nodes (which only occur in C++
1333 // templates), produce an error.
1334#define TYPE(Class, Base)
1335#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1336#include "clang/AST/TypeNodes.def"
1337 assert(false && "Cannot serialize dependent type nodes");
1338 break;
1339 }
1340
1341 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001342 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001343
1344 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001345 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001346}
1347
1348/// \brief Write a block containing all of the types.
1349void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001350 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001351 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001352
1353 // Emit all of the types in the ASTContext
1354 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1355 TEnd = Context.getTypes().end();
1356 T != TEnd; ++T) {
1357 // Builtin types are never serialized.
1358 if (isa<BuiltinType>(*T))
1359 continue;
1360
1361 WriteType(*T);
1362 }
1363
1364 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001365 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001366}
1367
1368/// \brief Write the block containing all of the declaration IDs
1369/// lexically declared within the given DeclContext.
1370///
1371/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1372/// bistream, or 0 if no block was written.
1373uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1374 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001375 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001376 return 0;
1377
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001378 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001379 RecordData Record;
1380 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1381 DEnd = DC->decls_end(Context);
1382 D != DEnd; ++D)
1383 AddDeclRef(*D, Record);
1384
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001385 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001386 return Offset;
1387}
1388
1389/// \brief Write the block containing all of the declaration IDs
1390/// visible from the given DeclContext.
1391///
1392/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1393/// bistream, or 0 if no block was written.
1394uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1395 DeclContext *DC) {
1396 if (DC->getPrimaryContext() != DC)
1397 return 0;
1398
1399 // Force the DeclContext to build a its name-lookup table.
1400 DC->lookup(Context, DeclarationName());
1401
1402 // Serialize the contents of the mapping used for lookup. Note that,
1403 // although we have two very different code paths, the serialized
1404 // representation is the same for both cases: a declaration name,
1405 // followed by a size, followed by references to the visible
1406 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001407 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001408 RecordData Record;
1409 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001410 if (!Map)
1411 return 0;
1412
Douglas Gregorc34897d2009-04-09 22:27:44 +00001413 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1414 D != DEnd; ++D) {
1415 AddDeclarationName(D->first, Record);
1416 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1417 Record.push_back(Result.second - Result.first);
1418 for(; Result.first != Result.second; ++Result.first)
1419 AddDeclRef(*Result.first, Record);
1420 }
1421
1422 if (Record.size() == 0)
1423 return 0;
1424
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001425 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001426 return Offset;
1427}
1428
1429/// \brief Write a block containing all of the declarations.
1430void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001431 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001432 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001433
1434 // Emit all of the declarations.
1435 RecordData Record;
1436 PCHDeclWriter W(*this, Record);
1437 while (!DeclsToEmit.empty()) {
1438 // Pull the next declaration off the queue
1439 Decl *D = DeclsToEmit.front();
1440 DeclsToEmit.pop();
1441
1442 // If this declaration is also a DeclContext, write blocks for the
1443 // declarations that lexically stored inside its context and those
1444 // declarations that are visible from its context. These blocks
1445 // are written before the declaration itself so that we can put
1446 // their offsets into the record for the declaration.
1447 uint64_t LexicalOffset = 0;
1448 uint64_t VisibleOffset = 0;
1449 DeclContext *DC = dyn_cast<DeclContext>(D);
1450 if (DC) {
1451 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1452 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1453 }
1454
1455 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001456 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001457 if (ID == 0)
1458 ID = DeclIDs.size();
1459
1460 unsigned Index = ID - 1;
1461
1462 // Record the offset for this declaration
1463 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001464 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001465 else if (DeclOffsets.size() < Index) {
1466 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001467 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001468 }
1469
1470 // Build and emit a record for this declaration
1471 Record.clear();
1472 W.Code = (pch::DeclCode)0;
1473 W.Visit(D);
1474 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001475 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001476 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001477
Douglas Gregor1c507882009-04-15 21:30:51 +00001478 // If the declaration had any attributes, write them now.
1479 if (D->hasAttrs())
1480 WriteAttributeRecord(D->getAttrs());
1481
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001482 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001483 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001484
Douglas Gregor631f6c62009-04-14 00:24:19 +00001485 // Note external declarations so that we can add them to a record
1486 // in the PCH file later.
1487 if (isa<FileScopeAsmDecl>(D))
1488 ExternalDefinitions.push_back(ID);
1489 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1490 if (// Non-static file-scope variables with initializers or that
1491 // are tentative definitions.
1492 (Var->isFileVarDecl() &&
1493 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1494 // Out-of-line definitions of static data members (C++).
1495 (Var->getDeclContext()->isRecord() &&
1496 !Var->getLexicalDeclContext()->isRecord() &&
1497 Var->getStorageClass() == VarDecl::Static))
1498 ExternalDefinitions.push_back(ID);
1499 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1500 if (Func->isThisDeclarationADefinition() &&
1501 Func->getStorageClass() != FunctionDecl::Static &&
1502 !Func->isInline())
1503 ExternalDefinitions.push_back(ID);
1504 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001505 }
1506
1507 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001508 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001509}
1510
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001511/// \brief Write the identifier table into the PCH file.
1512///
1513/// The identifier table consists of a blob containing string data
1514/// (the actual identifiers themselves) and a separate "offsets" index
1515/// that maps identifier IDs to locations within the blob.
1516void PCHWriter::WriteIdentifierTable() {
1517 using namespace llvm;
1518
1519 // Create and write out the blob that contains the identifier
1520 // strings.
1521 RecordData IdentOffsets;
1522 IdentOffsets.resize(IdentifierIDs.size());
1523 {
1524 // Create the identifier string data.
1525 std::vector<char> Data;
1526 Data.push_back(0); // Data must not be empty.
1527 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1528 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1529 ID != IDEnd; ++ID) {
1530 assert(ID->first && "NULL identifier in identifier table");
1531
1532 // Make sure we're starting on an odd byte. The PCH reader
1533 // expects the low bit to be set on all of the offsets.
1534 if ((Data.size() & 0x01) == 0)
1535 Data.push_back((char)0);
1536
1537 IdentOffsets[ID->second - 1] = Data.size();
1538 Data.insert(Data.end(),
1539 ID->first->getName(),
1540 ID->first->getName() + ID->first->getLength());
1541 Data.push_back((char)0);
1542 }
1543
1544 // Create a blob abbreviation
1545 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1546 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001548 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001549
1550 // Write the identifier table
1551 RecordData Record;
1552 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001553 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001554 }
1555
1556 // Write the offsets table for identifier IDs.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001557 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001558}
1559
Douglas Gregor1c507882009-04-15 21:30:51 +00001560/// \brief Write a record containing the given attributes.
1561void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1562 RecordData Record;
1563 for (; Attr; Attr = Attr->getNext()) {
1564 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1565 Record.push_back(Attr->isInherited());
1566 switch (Attr->getKind()) {
1567 case Attr::Alias:
1568 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1569 break;
1570
1571 case Attr::Aligned:
1572 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1573 break;
1574
1575 case Attr::AlwaysInline:
1576 break;
1577
1578 case Attr::AnalyzerNoReturn:
1579 break;
1580
1581 case Attr::Annotate:
1582 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1583 break;
1584
1585 case Attr::AsmLabel:
1586 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1587 break;
1588
1589 case Attr::Blocks:
1590 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1591 break;
1592
1593 case Attr::Cleanup:
1594 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1595 break;
1596
1597 case Attr::Const:
1598 break;
1599
1600 case Attr::Constructor:
1601 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1602 break;
1603
1604 case Attr::DLLExport:
1605 case Attr::DLLImport:
1606 case Attr::Deprecated:
1607 break;
1608
1609 case Attr::Destructor:
1610 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1611 break;
1612
1613 case Attr::FastCall:
1614 break;
1615
1616 case Attr::Format: {
1617 const FormatAttr *Format = cast<FormatAttr>(Attr);
1618 AddString(Format->getType(), Record);
1619 Record.push_back(Format->getFormatIdx());
1620 Record.push_back(Format->getFirstArg());
1621 break;
1622 }
1623
1624 case Attr::GNUCInline:
1625 case Attr::IBOutletKind:
1626 case Attr::NoReturn:
1627 case Attr::NoThrow:
1628 case Attr::Nodebug:
1629 case Attr::Noinline:
1630 break;
1631
1632 case Attr::NonNull: {
1633 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1634 Record.push_back(NonNull->size());
1635 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1636 break;
1637 }
1638
1639 case Attr::ObjCException:
1640 case Attr::ObjCNSObject:
1641 case Attr::Overloadable:
1642 break;
1643
1644 case Attr::Packed:
1645 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1646 break;
1647
1648 case Attr::Pure:
1649 break;
1650
1651 case Attr::Regparm:
1652 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1653 break;
1654
1655 case Attr::Section:
1656 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1657 break;
1658
1659 case Attr::StdCall:
1660 case Attr::TransparentUnion:
1661 case Attr::Unavailable:
1662 case Attr::Unused:
1663 case Attr::Used:
1664 break;
1665
1666 case Attr::Visibility:
1667 // FIXME: stable encoding
1668 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1669 break;
1670
1671 case Attr::WarnUnusedResult:
1672 case Attr::Weak:
1673 case Attr::WeakImport:
1674 break;
1675 }
1676 }
1677
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001678 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001679}
1680
1681void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1682 Record.push_back(Str.size());
1683 Record.insert(Record.end(), Str.begin(), Str.end());
1684}
1685
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001686PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1687 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001688
Chris Lattner850eabd2009-04-10 18:08:30 +00001689void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001690 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001691 Stream.Emit((unsigned)'C', 8);
1692 Stream.Emit((unsigned)'P', 8);
1693 Stream.Emit((unsigned)'C', 8);
1694 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001695
1696 // The translation unit is the first declaration we'll emit.
1697 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1698 DeclsToEmit.push(Context.getTranslationUnitDecl());
1699
1700 // Write the remaining PCH contents.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001701 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001702 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001703 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001704 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001705 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001706 WriteTypesBlock(Context);
1707 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001708 WriteIdentifierTable();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001709 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1710 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001711 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001712 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
1713 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001714}
1715
1716void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1717 Record.push_back(Loc.getRawEncoding());
1718}
1719
1720void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1721 Record.push_back(Value.getBitWidth());
1722 unsigned N = Value.getNumWords();
1723 const uint64_t* Words = Value.getRawData();
1724 for (unsigned I = 0; I != N; ++I)
1725 Record.push_back(Words[I]);
1726}
1727
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001728void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1729 Record.push_back(Value.isUnsigned());
1730 AddAPInt(Value, Record);
1731}
1732
Douglas Gregore2f37202009-04-14 21:55:33 +00001733void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1734 AddAPInt(Value.bitcastToAPInt(), Record);
1735}
1736
Douglas Gregorc34897d2009-04-09 22:27:44 +00001737void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001738 if (II == 0) {
1739 Record.push_back(0);
1740 return;
1741 }
1742
1743 pch::IdentID &ID = IdentifierIDs[II];
1744 if (ID == 0)
1745 ID = IdentifierIDs.size();
1746
1747 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001748}
1749
1750void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1751 if (T.isNull()) {
1752 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1753 return;
1754 }
1755
1756 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001757 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001758 switch (BT->getKind()) {
1759 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1760 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1761 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1762 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1763 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1764 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1765 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1766 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1767 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1768 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1769 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1770 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1771 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1772 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1773 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1774 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1775 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1776 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1777 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1778 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1779 }
1780
1781 Record.push_back((ID << 3) | T.getCVRQualifiers());
1782 return;
1783 }
1784
Douglas Gregorac8f2802009-04-10 17:25:41 +00001785 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001786 if (ID == 0) // we haven't seen this type before
1787 ID = NextTypeID++;
1788
1789 // Encode the type qualifiers in the type reference.
1790 Record.push_back((ID << 3) | T.getCVRQualifiers());
1791}
1792
1793void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1794 if (D == 0) {
1795 Record.push_back(0);
1796 return;
1797 }
1798
Douglas Gregorac8f2802009-04-10 17:25:41 +00001799 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001800 if (ID == 0) {
1801 // We haven't seen this declaration before. Give it a new ID and
1802 // enqueue it in the list of declarations to emit.
1803 ID = DeclIDs.size();
1804 DeclsToEmit.push(const_cast<Decl *>(D));
1805 }
1806
1807 Record.push_back(ID);
1808}
1809
1810void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1811 Record.push_back(Name.getNameKind());
1812 switch (Name.getNameKind()) {
1813 case DeclarationName::Identifier:
1814 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1815 break;
1816
1817 case DeclarationName::ObjCZeroArgSelector:
1818 case DeclarationName::ObjCOneArgSelector:
1819 case DeclarationName::ObjCMultiArgSelector:
1820 assert(false && "Serialization of Objective-C selectors unavailable");
1821 break;
1822
1823 case DeclarationName::CXXConstructorName:
1824 case DeclarationName::CXXDestructorName:
1825 case DeclarationName::CXXConversionFunctionName:
1826 AddTypeRef(Name.getCXXNameType(), Record);
1827 break;
1828
1829 case DeclarationName::CXXOperatorName:
1830 Record.push_back(Name.getCXXOverloadedOperator());
1831 break;
1832
1833 case DeclarationName::CXXUsingDirective:
1834 // No extra data to emit
1835 break;
1836 }
1837}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001838
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001839/// \brief Write the given substatement or subexpression to the
1840/// bitstream.
1841void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00001842 RecordData Record;
1843 PCHStmtWriter Writer(*this, Record);
1844
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001845 if (!S) {
1846 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001847 return;
1848 }
1849
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001850 Writer.Code = pch::STMT_NULL_PTR;
1851 Writer.Visit(S);
1852 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00001853 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001854 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001855}
1856
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001857/// \brief Flush all of the statements that have been added to the
1858/// queue via AddStmt().
1859void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001860 RecordData Record;
1861 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001862
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001863 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1864 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00001865
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001866 if (!S) {
1867 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001868 continue;
1869 }
1870
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001871 Writer.Code = pch::STMT_NULL_PTR;
1872 Writer.Visit(S);
1873 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001874 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001875 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001876
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001877 assert(N == StmtsToEmit.size() &&
1878 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00001879
1880 // Note that we are at the end of a full expression. Any
1881 // expression records that follow this one are part of a different
1882 // expression.
1883 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001884 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001885 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001886
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001887 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00001888 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001889}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00001890
1891unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1892 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1893 "SwitchCase recorded twice");
1894 unsigned NextID = SwitchCaseIDs.size();
1895 SwitchCaseIDs[S] = NextID;
1896 return NextID;
1897}
1898
1899unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1900 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1901 "SwitchCase hasn't been seen yet");
1902 return SwitchCaseIDs[S];
1903}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00001904
1905/// \brief Retrieve the ID for the given label statement, which may
1906/// or may not have been emitted yet.
1907unsigned PCHWriter::GetLabelID(LabelStmt *S) {
1908 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
1909 if (Pos != LabelIDs.end())
1910 return Pos->second;
1911
1912 unsigned NextID = LabelIDs.size();
1913 LabelIDs[S] = NextID;
1914 return NextID;
1915}