blob: bde859665f1f4355b09b366177a38c7ce4d1db2b [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 Gregora6b503f2009-04-17 00:16:09 +0000461 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000462 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000463 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000464 void VisitDeclStmt(DeclStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000465 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000466 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000467 void VisitDeclRefExpr(DeclRefExpr *E);
468 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000469 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000470 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000471 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000472 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000473 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000474 void VisitUnaryOperator(UnaryOperator *E);
475 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000476 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000477 void VisitCallExpr(CallExpr *E);
478 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000479 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000480 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000481 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
482 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000483 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000484 void VisitExplicitCastExpr(ExplicitCastExpr *E);
485 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000486 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000487 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000488 void VisitInitListExpr(InitListExpr *E);
489 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
490 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000491 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000492 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
493 void VisitChooseExpr(ChooseExpr *E);
494 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000495 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
496 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000497 };
498}
499
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000500void PCHStmtWriter::VisitStmt(Stmt *S) {
501}
502
503void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
504 VisitStmt(S);
505 Writer.AddSourceLocation(S->getSemiLoc(), Record);
506 Code = pch::STMT_NULL;
507}
508
509void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
510 VisitStmt(S);
511 Record.push_back(S->size());
512 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
513 CS != CSEnd; ++CS)
514 Writer.WriteSubStmt(*CS);
515 Writer.AddSourceLocation(S->getLBracLoc(), Record);
516 Writer.AddSourceLocation(S->getRBracLoc(), Record);
517 Code = pch::STMT_COMPOUND;
518}
519
520void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
521 VisitStmt(S);
522 Record.push_back(Writer.RecordSwitchCaseID(S));
523}
524
525void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
526 VisitSwitchCase(S);
527 Writer.WriteSubStmt(S->getLHS());
528 Writer.WriteSubStmt(S->getRHS());
529 Writer.WriteSubStmt(S->getSubStmt());
530 Writer.AddSourceLocation(S->getCaseLoc(), Record);
531 Code = pch::STMT_CASE;
532}
533
534void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
535 VisitSwitchCase(S);
536 Writer.WriteSubStmt(S->getSubStmt());
537 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
538 Code = pch::STMT_DEFAULT;
539}
540
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000541void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
542 VisitStmt(S);
543 Writer.AddIdentifierRef(S->getID(), Record);
544 Writer.WriteSubStmt(S->getSubStmt());
545 Writer.AddSourceLocation(S->getIdentLoc(), Record);
546 Record.push_back(Writer.GetLabelID(S));
547 Code = pch::STMT_LABEL;
548}
549
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000550void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
551 VisitStmt(S);
552 Writer.WriteSubStmt(S->getCond());
553 Writer.WriteSubStmt(S->getThen());
554 Writer.WriteSubStmt(S->getElse());
555 Writer.AddSourceLocation(S->getIfLoc(), Record);
556 Code = pch::STMT_IF;
557}
558
559void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
560 VisitStmt(S);
561 Writer.WriteSubStmt(S->getCond());
562 Writer.WriteSubStmt(S->getBody());
563 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
564 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
565 SC = SC->getNextSwitchCase())
566 Record.push_back(Writer.getSwitchCaseID(SC));
567 Code = pch::STMT_SWITCH;
568}
569
Douglas Gregora6b503f2009-04-17 00:16:09 +0000570void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
571 VisitStmt(S);
572 Writer.WriteSubStmt(S->getCond());
573 Writer.WriteSubStmt(S->getBody());
574 Writer.AddSourceLocation(S->getWhileLoc(), Record);
575 Code = pch::STMT_WHILE;
576}
577
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000578void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
579 VisitStmt(S);
580 Writer.WriteSubStmt(S->getCond());
581 Writer.WriteSubStmt(S->getBody());
582 Writer.AddSourceLocation(S->getDoLoc(), Record);
583 Code = pch::STMT_DO;
584}
585
586void PCHStmtWriter::VisitForStmt(ForStmt *S) {
587 VisitStmt(S);
588 Writer.WriteSubStmt(S->getInit());
589 Writer.WriteSubStmt(S->getCond());
590 Writer.WriteSubStmt(S->getInc());
591 Writer.WriteSubStmt(S->getBody());
592 Writer.AddSourceLocation(S->getForLoc(), Record);
593 Code = pch::STMT_FOR;
594}
595
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000596void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
597 VisitStmt(S);
598 Record.push_back(Writer.GetLabelID(S->getLabel()));
599 Writer.AddSourceLocation(S->getGotoLoc(), Record);
600 Writer.AddSourceLocation(S->getLabelLoc(), Record);
601 Code = pch::STMT_GOTO;
602}
603
Douglas Gregora6b503f2009-04-17 00:16:09 +0000604void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
605 VisitStmt(S);
606 Writer.AddSourceLocation(S->getContinueLoc(), Record);
607 Code = pch::STMT_CONTINUE;
608}
609
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000610void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
611 VisitStmt(S);
612 Writer.AddSourceLocation(S->getBreakLoc(), Record);
613 Code = pch::STMT_BREAK;
614}
615
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000616void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
617 VisitStmt(S);
618 Writer.WriteSubStmt(S->getRetValue());
619 Writer.AddSourceLocation(S->getReturnLoc(), Record);
620 Code = pch::STMT_RETURN;
621}
622
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000623void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
624 VisitStmt(S);
625 Writer.AddSourceLocation(S->getStartLoc(), Record);
626 Writer.AddSourceLocation(S->getEndLoc(), Record);
627 DeclGroupRef DG = S->getDeclGroup();
628 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
629 Writer.AddDeclRef(*D, Record);
630 Code = pch::STMT_DECL;
631}
632
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000633void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000634 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000635 Writer.AddTypeRef(E->getType(), Record);
636 Record.push_back(E->isTypeDependent());
637 Record.push_back(E->isValueDependent());
638}
639
Douglas Gregore2f37202009-04-14 21:55:33 +0000640void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
641 VisitExpr(E);
642 Writer.AddSourceLocation(E->getLocation(), Record);
643 Record.push_back(E->getIdentType()); // FIXME: stable encoding
644 Code = pch::EXPR_PREDEFINED;
645}
646
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000647void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
648 VisitExpr(E);
649 Writer.AddDeclRef(E->getDecl(), Record);
650 Writer.AddSourceLocation(E->getLocation(), Record);
651 Code = pch::EXPR_DECL_REF;
652}
653
654void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
655 VisitExpr(E);
656 Writer.AddSourceLocation(E->getLocation(), Record);
657 Writer.AddAPInt(E->getValue(), Record);
658 Code = pch::EXPR_INTEGER_LITERAL;
659}
660
Douglas Gregore2f37202009-04-14 21:55:33 +0000661void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
662 VisitExpr(E);
663 Writer.AddAPFloat(E->getValue(), Record);
664 Record.push_back(E->isExact());
665 Writer.AddSourceLocation(E->getLocation(), Record);
666 Code = pch::EXPR_FLOATING_LITERAL;
667}
668
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000669void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
670 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000671 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000672 Code = pch::EXPR_IMAGINARY_LITERAL;
673}
674
Douglas Gregor596e0932009-04-15 16:35:07 +0000675void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
676 VisitExpr(E);
677 Record.push_back(E->getByteLength());
678 Record.push_back(E->getNumConcatenated());
679 Record.push_back(E->isWide());
680 // FIXME: String data should be stored as a blob at the end of the
681 // StringLiteral. However, we can't do so now because we have no
682 // provision for coping with abbreviations when we're jumping around
683 // the PCH file during deserialization.
684 Record.insert(Record.end(),
685 E->getStrData(), E->getStrData() + E->getByteLength());
686 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
687 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
688 Code = pch::EXPR_STRING_LITERAL;
689}
690
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000691void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
692 VisitExpr(E);
693 Record.push_back(E->getValue());
694 Writer.AddSourceLocation(E->getLoc(), Record);
695 Record.push_back(E->isWide());
696 Code = pch::EXPR_CHARACTER_LITERAL;
697}
698
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000699void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
700 VisitExpr(E);
701 Writer.AddSourceLocation(E->getLParen(), Record);
702 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000703 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000704 Code = pch::EXPR_PAREN;
705}
706
Douglas Gregor12d74052009-04-15 15:58:59 +0000707void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
708 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000709 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000710 Record.push_back(E->getOpcode()); // FIXME: stable encoding
711 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
712 Code = pch::EXPR_UNARY_OPERATOR;
713}
714
715void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
716 VisitExpr(E);
717 Record.push_back(E->isSizeOf());
718 if (E->isArgumentType())
719 Writer.AddTypeRef(E->getArgumentType(), Record);
720 else {
721 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000722 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000723 }
724 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
725 Writer.AddSourceLocation(E->getRParenLoc(), Record);
726 Code = pch::EXPR_SIZEOF_ALIGN_OF;
727}
728
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000729void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
730 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000731 Writer.WriteSubStmt(E->getLHS());
732 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000733 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
734 Code = pch::EXPR_ARRAY_SUBSCRIPT;
735}
736
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000737void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
738 VisitExpr(E);
739 Record.push_back(E->getNumArgs());
740 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000741 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000742 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
743 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000744 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000745 Code = pch::EXPR_CALL;
746}
747
748void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
749 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000750 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000751 Writer.AddDeclRef(E->getMemberDecl(), Record);
752 Writer.AddSourceLocation(E->getMemberLoc(), Record);
753 Record.push_back(E->isArrow());
754 Code = pch::EXPR_MEMBER;
755}
756
Douglas Gregora151ba42009-04-14 23:32:43 +0000757void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
758 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000759 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000760}
761
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000762void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
763 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000764 Writer.WriteSubStmt(E->getLHS());
765 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000766 Record.push_back(E->getOpcode()); // FIXME: stable encoding
767 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
768 Code = pch::EXPR_BINARY_OPERATOR;
769}
770
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000771void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
772 VisitBinaryOperator(E);
773 Writer.AddTypeRef(E->getComputationLHSType(), Record);
774 Writer.AddTypeRef(E->getComputationResultType(), Record);
775 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
776}
777
778void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
779 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000780 Writer.WriteSubStmt(E->getCond());
781 Writer.WriteSubStmt(E->getLHS());
782 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000783 Code = pch::EXPR_CONDITIONAL_OPERATOR;
784}
785
Douglas Gregora151ba42009-04-14 23:32:43 +0000786void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
787 VisitCastExpr(E);
788 Record.push_back(E->isLvalueCast());
789 Code = pch::EXPR_IMPLICIT_CAST;
790}
791
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000792void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
793 VisitCastExpr(E);
794 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
795}
796
797void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
798 VisitExplicitCastExpr(E);
799 Writer.AddSourceLocation(E->getLParenLoc(), Record);
800 Writer.AddSourceLocation(E->getRParenLoc(), Record);
801 Code = pch::EXPR_CSTYLE_CAST;
802}
803
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000804void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
805 VisitExpr(E);
806 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000807 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000808 Record.push_back(E->isFileScope());
809 Code = pch::EXPR_COMPOUND_LITERAL;
810}
811
Douglas Gregorec0b8292009-04-15 23:02:49 +0000812void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
813 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000814 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000815 Writer.AddIdentifierRef(&E->getAccessor(), Record);
816 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
817 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
818}
819
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000820void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
821 VisitExpr(E);
822 Record.push_back(E->getNumInits());
823 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000824 Writer.WriteSubStmt(E->getInit(I));
825 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000826 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
827 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
828 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
829 Record.push_back(E->hadArrayRangeDesignator());
830 Code = pch::EXPR_INIT_LIST;
831}
832
833void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
834 VisitExpr(E);
835 Record.push_back(E->getNumSubExprs());
836 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000837 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000838 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
839 Record.push_back(E->usesGNUSyntax());
840 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
841 DEnd = E->designators_end();
842 D != DEnd; ++D) {
843 if (D->isFieldDesignator()) {
844 if (FieldDecl *Field = D->getField()) {
845 Record.push_back(pch::DESIG_FIELD_DECL);
846 Writer.AddDeclRef(Field, Record);
847 } else {
848 Record.push_back(pch::DESIG_FIELD_NAME);
849 Writer.AddIdentifierRef(D->getFieldName(), Record);
850 }
851 Writer.AddSourceLocation(D->getDotLoc(), Record);
852 Writer.AddSourceLocation(D->getFieldLoc(), Record);
853 } else if (D->isArrayDesignator()) {
854 Record.push_back(pch::DESIG_ARRAY);
855 Record.push_back(D->getFirstExprIndex());
856 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
857 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
858 } else {
859 assert(D->isArrayRangeDesignator() && "Unknown designator");
860 Record.push_back(pch::DESIG_ARRAY_RANGE);
861 Record.push_back(D->getFirstExprIndex());
862 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
863 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
864 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
865 }
866 }
867 Code = pch::EXPR_DESIGNATED_INIT;
868}
869
870void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
871 VisitExpr(E);
872 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
873}
874
Douglas Gregorec0b8292009-04-15 23:02:49 +0000875void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
876 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000877 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000878 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
879 Writer.AddSourceLocation(E->getRParenLoc(), Record);
880 Code = pch::EXPR_VA_ARG;
881}
882
Douglas Gregor209d4622009-04-15 23:33:31 +0000883void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
884 VisitExpr(E);
885 Writer.AddTypeRef(E->getArgType1(), Record);
886 Writer.AddTypeRef(E->getArgType2(), Record);
887 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
888 Writer.AddSourceLocation(E->getRParenLoc(), Record);
889 Code = pch::EXPR_TYPES_COMPATIBLE;
890}
891
892void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
893 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000894 Writer.WriteSubStmt(E->getCond());
895 Writer.WriteSubStmt(E->getLHS());
896 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +0000897 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
898 Writer.AddSourceLocation(E->getRParenLoc(), Record);
899 Code = pch::EXPR_CHOOSE;
900}
901
902void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
903 VisitExpr(E);
904 Writer.AddSourceLocation(E->getTokenLocation(), Record);
905 Code = pch::EXPR_GNU_NULL;
906}
907
Douglas Gregor725e94b2009-04-16 00:01:45 +0000908void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
909 VisitExpr(E);
910 Record.push_back(E->getNumSubExprs());
911 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000912 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +0000913 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
914 Writer.AddSourceLocation(E->getRParenLoc(), Record);
915 Code = pch::EXPR_SHUFFLE_VECTOR;
916}
917
918void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
919 VisitExpr(E);
920 Writer.AddDeclRef(E->getDecl(), Record);
921 Writer.AddSourceLocation(E->getLocation(), Record);
922 Record.push_back(E->isByRef());
923 Code = pch::EXPR_BLOCK_DECL_REF;
924}
925
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000926//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000927// PCHWriter Implementation
928//===----------------------------------------------------------------------===//
929
Douglas Gregorb5887f32009-04-10 21:16:55 +0000930/// \brief Write the target triple (e.g., i686-apple-darwin9).
931void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
932 using namespace llvm;
933 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
934 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000936 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +0000937
938 RecordData Record;
939 Record.push_back(pch::TARGET_TRIPLE);
940 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000941 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +0000942}
943
944/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000945void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
946 RecordData Record;
947 Record.push_back(LangOpts.Trigraphs);
948 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
949 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
950 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
951 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
952 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
953 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
954 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
955 Record.push_back(LangOpts.C99); // C99 Support
956 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
957 Record.push_back(LangOpts.CPlusPlus); // C++ Support
958 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
959 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
960 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
961
962 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
963 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
964 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
965
966 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
967 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
968 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
969 Record.push_back(LangOpts.LaxVectorConversions);
970 Record.push_back(LangOpts.Exceptions); // Support exception handling.
971
972 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
973 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
974 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
975
976 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
977 // by locks.
978 Record.push_back(LangOpts.Blocks); // block extension to C
979 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
980 // they are unused.
981 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
982 // (modulo the platform support).
983
984 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
985 // signed integer arithmetic overflows.
986
987 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
988 // may be ripped out at any time.
989
990 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
991 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
992 // defined.
993 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
994 // opposed to __DYNAMIC__).
995 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
996
997 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
998 // used (instead of C99 semantics).
999 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1000 Record.push_back(LangOpts.getGCMode());
1001 Record.push_back(LangOpts.getVisibilityMode());
1002 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001003 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001004}
1005
Douglas Gregorab1cef72009-04-10 03:52:48 +00001006//===----------------------------------------------------------------------===//
1007// Source Manager Serialization
1008//===----------------------------------------------------------------------===//
1009
1010/// \brief Create an abbreviation for the SLocEntry that refers to a
1011/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001012static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001013 using namespace llvm;
1014 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1015 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001021 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001022}
1023
1024/// \brief Create an abbreviation for the SLocEntry that refers to a
1025/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001026static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001027 using namespace llvm;
1028 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1029 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001035 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001036}
1037
1038/// \brief Create an abbreviation for the SLocEntry that refers to a
1039/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001040static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001041 using namespace llvm;
1042 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1043 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001045 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001046}
1047
1048/// \brief Create an abbreviation for the SLocEntry that refers to an
1049/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001050static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001051 using namespace llvm;
1052 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1053 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001059 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001060}
1061
1062/// \brief Writes the block containing the serialized form of the
1063/// source manager.
1064///
1065/// TODO: We should probably use an on-disk hash table (stored in a
1066/// blob), indexed based on the file name, so that we only create
1067/// entries for files that we actually need. In the common case (no
1068/// errors), we probably won't have to create file entries for any of
1069/// the files in the AST.
1070void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001071 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001072 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001073
1074 // Abbreviations for the various kinds of source-location entries.
1075 int SLocFileAbbrv = -1;
1076 int SLocBufferAbbrv = -1;
1077 int SLocBufferBlobAbbrv = -1;
1078 int SLocInstantiationAbbrv = -1;
1079
1080 // Write out the source location entry table. We skip the first
1081 // entry, which is always the same dummy entry.
1082 RecordData Record;
1083 for (SourceManager::sloc_entry_iterator
1084 SLoc = SourceMgr.sloc_entry_begin() + 1,
1085 SLocEnd = SourceMgr.sloc_entry_end();
1086 SLoc != SLocEnd; ++SLoc) {
1087 // Figure out which record code to use.
1088 unsigned Code;
1089 if (SLoc->isFile()) {
1090 if (SLoc->getFile().getContentCache()->Entry)
1091 Code = pch::SM_SLOC_FILE_ENTRY;
1092 else
1093 Code = pch::SM_SLOC_BUFFER_ENTRY;
1094 } else
1095 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1096 Record.push_back(Code);
1097
1098 Record.push_back(SLoc->getOffset());
1099 if (SLoc->isFile()) {
1100 const SrcMgr::FileInfo &File = SLoc->getFile();
1101 Record.push_back(File.getIncludeLoc().getRawEncoding());
1102 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001103 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001104
1105 const SrcMgr::ContentCache *Content = File.getContentCache();
1106 if (Content->Entry) {
1107 // The source location entry is a file. The blob associated
1108 // with this entry is the file name.
1109 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001110 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1111 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001112 Content->Entry->getName(),
1113 strlen(Content->Entry->getName()));
1114 } else {
1115 // The source location entry is a buffer. The blob associated
1116 // with this entry contains the contents of the buffer.
1117 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001118 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1119 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001120 }
1121
1122 // We add one to the size so that we capture the trailing NULL
1123 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1124 // the reader side).
1125 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1126 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001127 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001128 Record.clear();
1129 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001130 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001131 Buffer->getBufferStart(),
1132 Buffer->getBufferSize() + 1);
1133 }
1134 } else {
1135 // The source location entry is an instantiation.
1136 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1137 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1138 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1139 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1140
Douglas Gregor364e5802009-04-15 18:05:10 +00001141 // Compute the token length for this macro expansion.
1142 unsigned NextOffset = SourceMgr.getNextOffset();
1143 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1144 if (++NextSLoc != SLocEnd)
1145 NextOffset = NextSLoc->getOffset();
1146 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1147
Douglas Gregorab1cef72009-04-10 03:52:48 +00001148 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001149 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1150 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001151 }
1152
1153 Record.clear();
1154 }
1155
Douglas Gregor635f97f2009-04-13 16:31:14 +00001156 // Write the line table.
1157 if (SourceMgr.hasLineTable()) {
1158 LineTableInfo &LineTable = SourceMgr.getLineTable();
1159
1160 // Emit the file names
1161 Record.push_back(LineTable.getNumFilenames());
1162 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1163 // Emit the file name
1164 const char *Filename = LineTable.getFilename(I);
1165 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1166 Record.push_back(FilenameLen);
1167 if (FilenameLen)
1168 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1169 }
1170
1171 // Emit the line entries
1172 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1173 L != LEnd; ++L) {
1174 // Emit the file ID
1175 Record.push_back(L->first);
1176
1177 // Emit the line entries
1178 Record.push_back(L->second.size());
1179 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1180 LEEnd = L->second.end();
1181 LE != LEEnd; ++LE) {
1182 Record.push_back(LE->FileOffset);
1183 Record.push_back(LE->LineNo);
1184 Record.push_back(LE->FilenameID);
1185 Record.push_back((unsigned)LE->FileKind);
1186 Record.push_back(LE->IncludeOffset);
1187 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001188 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001189 }
1190 }
1191
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001192 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001193}
1194
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001195/// \brief Writes the block containing the serialized form of the
1196/// preprocessor.
1197///
Chris Lattner850eabd2009-04-10 18:08:30 +00001198void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001199 // Enter the preprocessor block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001200 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattner84b04f12009-04-10 17:16:57 +00001201
Chris Lattner1b094952009-04-10 18:00:12 +00001202 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1203 // FIXME: use diagnostics subsystem for localization etc.
1204 if (PP.SawDateOrTime())
1205 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001206
Chris Lattner1b094952009-04-10 18:00:12 +00001207 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001208
Chris Lattner4b21c202009-04-13 01:29:17 +00001209 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1210 if (PP.getCounterValue() != 0) {
1211 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001212 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001213 Record.clear();
1214 }
1215
Chris Lattner1b094952009-04-10 18:00:12 +00001216 // Loop over all the macro definitions that are live at the end of the file,
1217 // emitting each to the PP section.
1218 // FIXME: Eventually we want to emit an index so that we can lazily load
1219 // macros.
1220 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1221 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001222 // FIXME: This emits macros in hash table order, we should do it in a stable
1223 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001224 MacroInfo *MI = I->second;
1225
1226 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1227 // been redefined by the header (in which case they are not isBuiltinMacro).
1228 if (MI->isBuiltinMacro())
1229 continue;
1230
Chris Lattner29241862009-04-11 21:15:38 +00001231 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001232 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1233 Record.push_back(MI->isUsed());
1234
1235 unsigned Code;
1236 if (MI->isObjectLike()) {
1237 Code = pch::PP_MACRO_OBJECT_LIKE;
1238 } else {
1239 Code = pch::PP_MACRO_FUNCTION_LIKE;
1240
1241 Record.push_back(MI->isC99Varargs());
1242 Record.push_back(MI->isGNUVarargs());
1243 Record.push_back(MI->getNumArgs());
1244 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1245 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001246 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001247 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001248 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001249 Record.clear();
1250
Chris Lattner850eabd2009-04-10 18:08:30 +00001251 // Emit the tokens array.
1252 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1253 // Note that we know that the preprocessor does not have any annotation
1254 // tokens in it because they are created by the parser, and thus can't be
1255 // in a macro definition.
1256 const Token &Tok = MI->getReplacementToken(TokNo);
1257
1258 Record.push_back(Tok.getLocation().getRawEncoding());
1259 Record.push_back(Tok.getLength());
1260
Chris Lattner850eabd2009-04-10 18:08:30 +00001261 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1262 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001263 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001264
1265 // FIXME: Should translate token kind to a stable encoding.
1266 Record.push_back(Tok.getKind());
1267 // FIXME: Should translate token flags to a stable encoding.
1268 Record.push_back(Tok.getFlags());
1269
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001270 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001271 Record.clear();
1272 }
Chris Lattner1b094952009-04-10 18:00:12 +00001273
1274 }
1275
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001276 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001277}
1278
1279
Douglas Gregorc34897d2009-04-09 22:27:44 +00001280/// \brief Write the representation of a type to the PCH stream.
1281void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001282 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001283 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001284 ID = NextTypeID++;
1285
1286 // Record the offset for this type.
1287 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001288 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001289 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1290 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001291 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001292 }
1293
1294 RecordData Record;
1295
1296 // Emit the type's representation.
1297 PCHTypeWriter W(*this, Record);
1298 switch (T->getTypeClass()) {
1299 // For all of the concrete, non-dependent types, call the
1300 // appropriate visitor function.
1301#define TYPE(Class, Base) \
1302 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1303#define ABSTRACT_TYPE(Class, Base)
1304#define DEPENDENT_TYPE(Class, Base)
1305#include "clang/AST/TypeNodes.def"
1306
1307 // For all of the dependent type nodes (which only occur in C++
1308 // templates), produce an error.
1309#define TYPE(Class, Base)
1310#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1311#include "clang/AST/TypeNodes.def"
1312 assert(false && "Cannot serialize dependent type nodes");
1313 break;
1314 }
1315
1316 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001317 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001318
1319 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001320 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001321}
1322
1323/// \brief Write a block containing all of the types.
1324void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001325 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001326 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001327
1328 // Emit all of the types in the ASTContext
1329 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1330 TEnd = Context.getTypes().end();
1331 T != TEnd; ++T) {
1332 // Builtin types are never serialized.
1333 if (isa<BuiltinType>(*T))
1334 continue;
1335
1336 WriteType(*T);
1337 }
1338
1339 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001340 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001341}
1342
1343/// \brief Write the block containing all of the declaration IDs
1344/// lexically declared within the given DeclContext.
1345///
1346/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1347/// bistream, or 0 if no block was written.
1348uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1349 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001350 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001351 return 0;
1352
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001353 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001354 RecordData Record;
1355 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1356 DEnd = DC->decls_end(Context);
1357 D != DEnd; ++D)
1358 AddDeclRef(*D, Record);
1359
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001360 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001361 return Offset;
1362}
1363
1364/// \brief Write the block containing all of the declaration IDs
1365/// visible from the given DeclContext.
1366///
1367/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1368/// bistream, or 0 if no block was written.
1369uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1370 DeclContext *DC) {
1371 if (DC->getPrimaryContext() != DC)
1372 return 0;
1373
1374 // Force the DeclContext to build a its name-lookup table.
1375 DC->lookup(Context, DeclarationName());
1376
1377 // Serialize the contents of the mapping used for lookup. Note that,
1378 // although we have two very different code paths, the serialized
1379 // representation is the same for both cases: a declaration name,
1380 // followed by a size, followed by references to the visible
1381 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001382 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001383 RecordData Record;
1384 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001385 if (!Map)
1386 return 0;
1387
Douglas Gregorc34897d2009-04-09 22:27:44 +00001388 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1389 D != DEnd; ++D) {
1390 AddDeclarationName(D->first, Record);
1391 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1392 Record.push_back(Result.second - Result.first);
1393 for(; Result.first != Result.second; ++Result.first)
1394 AddDeclRef(*Result.first, Record);
1395 }
1396
1397 if (Record.size() == 0)
1398 return 0;
1399
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001400 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001401 return Offset;
1402}
1403
1404/// \brief Write a block containing all of the declarations.
1405void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001406 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001407 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001408
1409 // Emit all of the declarations.
1410 RecordData Record;
1411 PCHDeclWriter W(*this, Record);
1412 while (!DeclsToEmit.empty()) {
1413 // Pull the next declaration off the queue
1414 Decl *D = DeclsToEmit.front();
1415 DeclsToEmit.pop();
1416
1417 // If this declaration is also a DeclContext, write blocks for the
1418 // declarations that lexically stored inside its context and those
1419 // declarations that are visible from its context. These blocks
1420 // are written before the declaration itself so that we can put
1421 // their offsets into the record for the declaration.
1422 uint64_t LexicalOffset = 0;
1423 uint64_t VisibleOffset = 0;
1424 DeclContext *DC = dyn_cast<DeclContext>(D);
1425 if (DC) {
1426 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1427 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1428 }
1429
1430 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001431 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001432 if (ID == 0)
1433 ID = DeclIDs.size();
1434
1435 unsigned Index = ID - 1;
1436
1437 // Record the offset for this declaration
1438 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001439 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001440 else if (DeclOffsets.size() < Index) {
1441 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001442 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001443 }
1444
1445 // Build and emit a record for this declaration
1446 Record.clear();
1447 W.Code = (pch::DeclCode)0;
1448 W.Visit(D);
1449 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001450 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001451 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001452
Douglas Gregor1c507882009-04-15 21:30:51 +00001453 // If the declaration had any attributes, write them now.
1454 if (D->hasAttrs())
1455 WriteAttributeRecord(D->getAttrs());
1456
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001457 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001458 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001459
Douglas Gregor631f6c62009-04-14 00:24:19 +00001460 // Note external declarations so that we can add them to a record
1461 // in the PCH file later.
1462 if (isa<FileScopeAsmDecl>(D))
1463 ExternalDefinitions.push_back(ID);
1464 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1465 if (// Non-static file-scope variables with initializers or that
1466 // are tentative definitions.
1467 (Var->isFileVarDecl() &&
1468 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1469 // Out-of-line definitions of static data members (C++).
1470 (Var->getDeclContext()->isRecord() &&
1471 !Var->getLexicalDeclContext()->isRecord() &&
1472 Var->getStorageClass() == VarDecl::Static))
1473 ExternalDefinitions.push_back(ID);
1474 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1475 if (Func->isThisDeclarationADefinition() &&
1476 Func->getStorageClass() != FunctionDecl::Static &&
1477 !Func->isInline())
1478 ExternalDefinitions.push_back(ID);
1479 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001480 }
1481
1482 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001483 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001484}
1485
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001486/// \brief Write the identifier table into the PCH file.
1487///
1488/// The identifier table consists of a blob containing string data
1489/// (the actual identifiers themselves) and a separate "offsets" index
1490/// that maps identifier IDs to locations within the blob.
1491void PCHWriter::WriteIdentifierTable() {
1492 using namespace llvm;
1493
1494 // Create and write out the blob that contains the identifier
1495 // strings.
1496 RecordData IdentOffsets;
1497 IdentOffsets.resize(IdentifierIDs.size());
1498 {
1499 // Create the identifier string data.
1500 std::vector<char> Data;
1501 Data.push_back(0); // Data must not be empty.
1502 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1503 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1504 ID != IDEnd; ++ID) {
1505 assert(ID->first && "NULL identifier in identifier table");
1506
1507 // Make sure we're starting on an odd byte. The PCH reader
1508 // expects the low bit to be set on all of the offsets.
1509 if ((Data.size() & 0x01) == 0)
1510 Data.push_back((char)0);
1511
1512 IdentOffsets[ID->second - 1] = Data.size();
1513 Data.insert(Data.end(),
1514 ID->first->getName(),
1515 ID->first->getName() + ID->first->getLength());
1516 Data.push_back((char)0);
1517 }
1518
1519 // Create a blob abbreviation
1520 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1521 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1522 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001523 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001524
1525 // Write the identifier table
1526 RecordData Record;
1527 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001528 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001529 }
1530
1531 // Write the offsets table for identifier IDs.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001532 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001533}
1534
Douglas Gregor1c507882009-04-15 21:30:51 +00001535/// \brief Write a record containing the given attributes.
1536void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1537 RecordData Record;
1538 for (; Attr; Attr = Attr->getNext()) {
1539 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1540 Record.push_back(Attr->isInherited());
1541 switch (Attr->getKind()) {
1542 case Attr::Alias:
1543 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1544 break;
1545
1546 case Attr::Aligned:
1547 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1548 break;
1549
1550 case Attr::AlwaysInline:
1551 break;
1552
1553 case Attr::AnalyzerNoReturn:
1554 break;
1555
1556 case Attr::Annotate:
1557 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1558 break;
1559
1560 case Attr::AsmLabel:
1561 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1562 break;
1563
1564 case Attr::Blocks:
1565 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1566 break;
1567
1568 case Attr::Cleanup:
1569 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1570 break;
1571
1572 case Attr::Const:
1573 break;
1574
1575 case Attr::Constructor:
1576 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1577 break;
1578
1579 case Attr::DLLExport:
1580 case Attr::DLLImport:
1581 case Attr::Deprecated:
1582 break;
1583
1584 case Attr::Destructor:
1585 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1586 break;
1587
1588 case Attr::FastCall:
1589 break;
1590
1591 case Attr::Format: {
1592 const FormatAttr *Format = cast<FormatAttr>(Attr);
1593 AddString(Format->getType(), Record);
1594 Record.push_back(Format->getFormatIdx());
1595 Record.push_back(Format->getFirstArg());
1596 break;
1597 }
1598
1599 case Attr::GNUCInline:
1600 case Attr::IBOutletKind:
1601 case Attr::NoReturn:
1602 case Attr::NoThrow:
1603 case Attr::Nodebug:
1604 case Attr::Noinline:
1605 break;
1606
1607 case Attr::NonNull: {
1608 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1609 Record.push_back(NonNull->size());
1610 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1611 break;
1612 }
1613
1614 case Attr::ObjCException:
1615 case Attr::ObjCNSObject:
1616 case Attr::Overloadable:
1617 break;
1618
1619 case Attr::Packed:
1620 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1621 break;
1622
1623 case Attr::Pure:
1624 break;
1625
1626 case Attr::Regparm:
1627 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1628 break;
1629
1630 case Attr::Section:
1631 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1632 break;
1633
1634 case Attr::StdCall:
1635 case Attr::TransparentUnion:
1636 case Attr::Unavailable:
1637 case Attr::Unused:
1638 case Attr::Used:
1639 break;
1640
1641 case Attr::Visibility:
1642 // FIXME: stable encoding
1643 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1644 break;
1645
1646 case Attr::WarnUnusedResult:
1647 case Attr::Weak:
1648 case Attr::WeakImport:
1649 break;
1650 }
1651 }
1652
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001653 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001654}
1655
1656void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1657 Record.push_back(Str.size());
1658 Record.insert(Record.end(), Str.begin(), Str.end());
1659}
1660
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001661PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1662 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001663
Chris Lattner850eabd2009-04-10 18:08:30 +00001664void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001665 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001666 Stream.Emit((unsigned)'C', 8);
1667 Stream.Emit((unsigned)'P', 8);
1668 Stream.Emit((unsigned)'C', 8);
1669 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001670
1671 // The translation unit is the first declaration we'll emit.
1672 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1673 DeclsToEmit.push(Context.getTranslationUnitDecl());
1674
1675 // Write the remaining PCH contents.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001676 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001677 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001678 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001679 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001680 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001681 WriteTypesBlock(Context);
1682 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001683 WriteIdentifierTable();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001684 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1685 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001686 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001687 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
1688 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001689}
1690
1691void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1692 Record.push_back(Loc.getRawEncoding());
1693}
1694
1695void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1696 Record.push_back(Value.getBitWidth());
1697 unsigned N = Value.getNumWords();
1698 const uint64_t* Words = Value.getRawData();
1699 for (unsigned I = 0; I != N; ++I)
1700 Record.push_back(Words[I]);
1701}
1702
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001703void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1704 Record.push_back(Value.isUnsigned());
1705 AddAPInt(Value, Record);
1706}
1707
Douglas Gregore2f37202009-04-14 21:55:33 +00001708void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1709 AddAPInt(Value.bitcastToAPInt(), Record);
1710}
1711
Douglas Gregorc34897d2009-04-09 22:27:44 +00001712void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001713 if (II == 0) {
1714 Record.push_back(0);
1715 return;
1716 }
1717
1718 pch::IdentID &ID = IdentifierIDs[II];
1719 if (ID == 0)
1720 ID = IdentifierIDs.size();
1721
1722 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001723}
1724
1725void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1726 if (T.isNull()) {
1727 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1728 return;
1729 }
1730
1731 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001732 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001733 switch (BT->getKind()) {
1734 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1735 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1736 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1737 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1738 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1739 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1740 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1741 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1742 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1743 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1744 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1745 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1746 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1747 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1748 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1749 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1750 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1751 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1752 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1753 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1754 }
1755
1756 Record.push_back((ID << 3) | T.getCVRQualifiers());
1757 return;
1758 }
1759
Douglas Gregorac8f2802009-04-10 17:25:41 +00001760 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001761 if (ID == 0) // we haven't seen this type before
1762 ID = NextTypeID++;
1763
1764 // Encode the type qualifiers in the type reference.
1765 Record.push_back((ID << 3) | T.getCVRQualifiers());
1766}
1767
1768void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1769 if (D == 0) {
1770 Record.push_back(0);
1771 return;
1772 }
1773
Douglas Gregorac8f2802009-04-10 17:25:41 +00001774 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001775 if (ID == 0) {
1776 // We haven't seen this declaration before. Give it a new ID and
1777 // enqueue it in the list of declarations to emit.
1778 ID = DeclIDs.size();
1779 DeclsToEmit.push(const_cast<Decl *>(D));
1780 }
1781
1782 Record.push_back(ID);
1783}
1784
1785void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1786 Record.push_back(Name.getNameKind());
1787 switch (Name.getNameKind()) {
1788 case DeclarationName::Identifier:
1789 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1790 break;
1791
1792 case DeclarationName::ObjCZeroArgSelector:
1793 case DeclarationName::ObjCOneArgSelector:
1794 case DeclarationName::ObjCMultiArgSelector:
1795 assert(false && "Serialization of Objective-C selectors unavailable");
1796 break;
1797
1798 case DeclarationName::CXXConstructorName:
1799 case DeclarationName::CXXDestructorName:
1800 case DeclarationName::CXXConversionFunctionName:
1801 AddTypeRef(Name.getCXXNameType(), Record);
1802 break;
1803
1804 case DeclarationName::CXXOperatorName:
1805 Record.push_back(Name.getCXXOverloadedOperator());
1806 break;
1807
1808 case DeclarationName::CXXUsingDirective:
1809 // No extra data to emit
1810 break;
1811 }
1812}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001813
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001814/// \brief Write the given substatement or subexpression to the
1815/// bitstream.
1816void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00001817 RecordData Record;
1818 PCHStmtWriter Writer(*this, Record);
1819
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001820 if (!S) {
1821 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001822 return;
1823 }
1824
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001825 Writer.Code = pch::STMT_NULL_PTR;
1826 Writer.Visit(S);
1827 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00001828 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001829 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001830}
1831
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001832/// \brief Flush all of the statements that have been added to the
1833/// queue via AddStmt().
1834void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001835 RecordData Record;
1836 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001837
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001838 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1839 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00001840
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001841 if (!S) {
1842 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001843 continue;
1844 }
1845
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001846 Writer.Code = pch::STMT_NULL_PTR;
1847 Writer.Visit(S);
1848 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001849 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001850 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001851
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001852 assert(N == StmtsToEmit.size() &&
1853 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00001854
1855 // Note that we are at the end of a full expression. Any
1856 // expression records that follow this one are part of a different
1857 // expression.
1858 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001859 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001860 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001861
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001862 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00001863 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001864}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00001865
1866unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1867 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1868 "SwitchCase recorded twice");
1869 unsigned NextID = SwitchCaseIDs.size();
1870 SwitchCaseIDs[S] = NextID;
1871 return NextID;
1872}
1873
1874unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1875 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1876 "SwitchCase hasn't been seen yet");
1877 return SwitchCaseIDs[S];
1878}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00001879
1880/// \brief Retrieve the ID for the given label statement, which may
1881/// or may not have been emitted yet.
1882unsigned PCHWriter::GetLabelID(LabelStmt *S) {
1883 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
1884 if (Pos != LabelIDs.end())
1885 return Pos->second;
1886
1887 unsigned NextID = LabelIDs.size();
1888 LabelIDs[S] = NextID;
1889 return NextID;
1890}