blob: e71a60051ab7e37d2436b9ec02e5052987304393 [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);
Douglas Gregore246b742009-04-17 19:21:43 +0000406 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000407 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);
Douglas Gregore246b742009-04-17 19:21:43 +0000499 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000500 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000501 };
502}
503
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000504void PCHStmtWriter::VisitStmt(Stmt *S) {
505}
506
507void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
508 VisitStmt(S);
509 Writer.AddSourceLocation(S->getSemiLoc(), Record);
510 Code = pch::STMT_NULL;
511}
512
513void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
514 VisitStmt(S);
515 Record.push_back(S->size());
516 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
517 CS != CSEnd; ++CS)
518 Writer.WriteSubStmt(*CS);
519 Writer.AddSourceLocation(S->getLBracLoc(), Record);
520 Writer.AddSourceLocation(S->getRBracLoc(), Record);
521 Code = pch::STMT_COMPOUND;
522}
523
524void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
525 VisitStmt(S);
526 Record.push_back(Writer.RecordSwitchCaseID(S));
527}
528
529void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
530 VisitSwitchCase(S);
531 Writer.WriteSubStmt(S->getLHS());
532 Writer.WriteSubStmt(S->getRHS());
533 Writer.WriteSubStmt(S->getSubStmt());
534 Writer.AddSourceLocation(S->getCaseLoc(), Record);
535 Code = pch::STMT_CASE;
536}
537
538void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
539 VisitSwitchCase(S);
540 Writer.WriteSubStmt(S->getSubStmt());
541 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
542 Code = pch::STMT_DEFAULT;
543}
544
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000545void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
546 VisitStmt(S);
547 Writer.AddIdentifierRef(S->getID(), Record);
548 Writer.WriteSubStmt(S->getSubStmt());
549 Writer.AddSourceLocation(S->getIdentLoc(), Record);
550 Record.push_back(Writer.GetLabelID(S));
551 Code = pch::STMT_LABEL;
552}
553
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000554void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
555 VisitStmt(S);
556 Writer.WriteSubStmt(S->getCond());
557 Writer.WriteSubStmt(S->getThen());
558 Writer.WriteSubStmt(S->getElse());
559 Writer.AddSourceLocation(S->getIfLoc(), Record);
560 Code = pch::STMT_IF;
561}
562
563void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
564 VisitStmt(S);
565 Writer.WriteSubStmt(S->getCond());
566 Writer.WriteSubStmt(S->getBody());
567 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
568 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
569 SC = SC->getNextSwitchCase())
570 Record.push_back(Writer.getSwitchCaseID(SC));
571 Code = pch::STMT_SWITCH;
572}
573
Douglas Gregora6b503f2009-04-17 00:16:09 +0000574void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
575 VisitStmt(S);
576 Writer.WriteSubStmt(S->getCond());
577 Writer.WriteSubStmt(S->getBody());
578 Writer.AddSourceLocation(S->getWhileLoc(), Record);
579 Code = pch::STMT_WHILE;
580}
581
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000582void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
583 VisitStmt(S);
584 Writer.WriteSubStmt(S->getCond());
585 Writer.WriteSubStmt(S->getBody());
586 Writer.AddSourceLocation(S->getDoLoc(), Record);
587 Code = pch::STMT_DO;
588}
589
590void PCHStmtWriter::VisitForStmt(ForStmt *S) {
591 VisitStmt(S);
592 Writer.WriteSubStmt(S->getInit());
593 Writer.WriteSubStmt(S->getCond());
594 Writer.WriteSubStmt(S->getInc());
595 Writer.WriteSubStmt(S->getBody());
596 Writer.AddSourceLocation(S->getForLoc(), Record);
597 Code = pch::STMT_FOR;
598}
599
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000600void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
601 VisitStmt(S);
602 Record.push_back(Writer.GetLabelID(S->getLabel()));
603 Writer.AddSourceLocation(S->getGotoLoc(), Record);
604 Writer.AddSourceLocation(S->getLabelLoc(), Record);
605 Code = pch::STMT_GOTO;
606}
607
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000608void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
609 VisitStmt(S);
610 Writer.WriteSubStmt(S->getTarget());
611 Code = pch::STMT_INDIRECT_GOTO;
612}
613
Douglas Gregora6b503f2009-04-17 00:16:09 +0000614void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
615 VisitStmt(S);
616 Writer.AddSourceLocation(S->getContinueLoc(), Record);
617 Code = pch::STMT_CONTINUE;
618}
619
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000620void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
621 VisitStmt(S);
622 Writer.AddSourceLocation(S->getBreakLoc(), Record);
623 Code = pch::STMT_BREAK;
624}
625
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000626void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
627 VisitStmt(S);
628 Writer.WriteSubStmt(S->getRetValue());
629 Writer.AddSourceLocation(S->getReturnLoc(), Record);
630 Code = pch::STMT_RETURN;
631}
632
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000633void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
634 VisitStmt(S);
635 Writer.AddSourceLocation(S->getStartLoc(), Record);
636 Writer.AddSourceLocation(S->getEndLoc(), Record);
637 DeclGroupRef DG = S->getDeclGroup();
638 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
639 Writer.AddDeclRef(*D, Record);
640 Code = pch::STMT_DECL;
641}
642
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000643void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000644 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000645 Writer.AddTypeRef(E->getType(), Record);
646 Record.push_back(E->isTypeDependent());
647 Record.push_back(E->isValueDependent());
648}
649
Douglas Gregore2f37202009-04-14 21:55:33 +0000650void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
651 VisitExpr(E);
652 Writer.AddSourceLocation(E->getLocation(), Record);
653 Record.push_back(E->getIdentType()); // FIXME: stable encoding
654 Code = pch::EXPR_PREDEFINED;
655}
656
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000657void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
658 VisitExpr(E);
659 Writer.AddDeclRef(E->getDecl(), Record);
660 Writer.AddSourceLocation(E->getLocation(), Record);
661 Code = pch::EXPR_DECL_REF;
662}
663
664void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
665 VisitExpr(E);
666 Writer.AddSourceLocation(E->getLocation(), Record);
667 Writer.AddAPInt(E->getValue(), Record);
668 Code = pch::EXPR_INTEGER_LITERAL;
669}
670
Douglas Gregore2f37202009-04-14 21:55:33 +0000671void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
672 VisitExpr(E);
673 Writer.AddAPFloat(E->getValue(), Record);
674 Record.push_back(E->isExact());
675 Writer.AddSourceLocation(E->getLocation(), Record);
676 Code = pch::EXPR_FLOATING_LITERAL;
677}
678
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000679void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
680 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000681 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000682 Code = pch::EXPR_IMAGINARY_LITERAL;
683}
684
Douglas Gregor596e0932009-04-15 16:35:07 +0000685void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
686 VisitExpr(E);
687 Record.push_back(E->getByteLength());
688 Record.push_back(E->getNumConcatenated());
689 Record.push_back(E->isWide());
690 // FIXME: String data should be stored as a blob at the end of the
691 // StringLiteral. However, we can't do so now because we have no
692 // provision for coping with abbreviations when we're jumping around
693 // the PCH file during deserialization.
694 Record.insert(Record.end(),
695 E->getStrData(), E->getStrData() + E->getByteLength());
696 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
697 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
698 Code = pch::EXPR_STRING_LITERAL;
699}
700
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000701void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
702 VisitExpr(E);
703 Record.push_back(E->getValue());
704 Writer.AddSourceLocation(E->getLoc(), Record);
705 Record.push_back(E->isWide());
706 Code = pch::EXPR_CHARACTER_LITERAL;
707}
708
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000709void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
710 VisitExpr(E);
711 Writer.AddSourceLocation(E->getLParen(), Record);
712 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000713 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000714 Code = pch::EXPR_PAREN;
715}
716
Douglas Gregor12d74052009-04-15 15:58:59 +0000717void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
718 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000719 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000720 Record.push_back(E->getOpcode()); // FIXME: stable encoding
721 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
722 Code = pch::EXPR_UNARY_OPERATOR;
723}
724
725void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
726 VisitExpr(E);
727 Record.push_back(E->isSizeOf());
728 if (E->isArgumentType())
729 Writer.AddTypeRef(E->getArgumentType(), Record);
730 else {
731 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000732 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000733 }
734 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
735 Writer.AddSourceLocation(E->getRParenLoc(), Record);
736 Code = pch::EXPR_SIZEOF_ALIGN_OF;
737}
738
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000739void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
740 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000741 Writer.WriteSubStmt(E->getLHS());
742 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000743 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
744 Code = pch::EXPR_ARRAY_SUBSCRIPT;
745}
746
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000747void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
748 VisitExpr(E);
749 Record.push_back(E->getNumArgs());
750 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000751 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000752 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
753 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000754 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000755 Code = pch::EXPR_CALL;
756}
757
758void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
759 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000760 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000761 Writer.AddDeclRef(E->getMemberDecl(), Record);
762 Writer.AddSourceLocation(E->getMemberLoc(), Record);
763 Record.push_back(E->isArrow());
764 Code = pch::EXPR_MEMBER;
765}
766
Douglas Gregora151ba42009-04-14 23:32:43 +0000767void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
768 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000769 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000770}
771
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000772void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
773 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000774 Writer.WriteSubStmt(E->getLHS());
775 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000776 Record.push_back(E->getOpcode()); // FIXME: stable encoding
777 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
778 Code = pch::EXPR_BINARY_OPERATOR;
779}
780
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000781void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
782 VisitBinaryOperator(E);
783 Writer.AddTypeRef(E->getComputationLHSType(), Record);
784 Writer.AddTypeRef(E->getComputationResultType(), Record);
785 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
786}
787
788void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
789 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000790 Writer.WriteSubStmt(E->getCond());
791 Writer.WriteSubStmt(E->getLHS());
792 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000793 Code = pch::EXPR_CONDITIONAL_OPERATOR;
794}
795
Douglas Gregora151ba42009-04-14 23:32:43 +0000796void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
797 VisitCastExpr(E);
798 Record.push_back(E->isLvalueCast());
799 Code = pch::EXPR_IMPLICIT_CAST;
800}
801
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000802void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
803 VisitCastExpr(E);
804 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
805}
806
807void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
808 VisitExplicitCastExpr(E);
809 Writer.AddSourceLocation(E->getLParenLoc(), Record);
810 Writer.AddSourceLocation(E->getRParenLoc(), Record);
811 Code = pch::EXPR_CSTYLE_CAST;
812}
813
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000814void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
815 VisitExpr(E);
816 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000817 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000818 Record.push_back(E->isFileScope());
819 Code = pch::EXPR_COMPOUND_LITERAL;
820}
821
Douglas Gregorec0b8292009-04-15 23:02:49 +0000822void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
823 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000824 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000825 Writer.AddIdentifierRef(&E->getAccessor(), Record);
826 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
827 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
828}
829
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000830void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
831 VisitExpr(E);
832 Record.push_back(E->getNumInits());
833 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000834 Writer.WriteSubStmt(E->getInit(I));
835 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000836 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
837 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
838 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
839 Record.push_back(E->hadArrayRangeDesignator());
840 Code = pch::EXPR_INIT_LIST;
841}
842
843void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
844 VisitExpr(E);
845 Record.push_back(E->getNumSubExprs());
846 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000847 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000848 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
849 Record.push_back(E->usesGNUSyntax());
850 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
851 DEnd = E->designators_end();
852 D != DEnd; ++D) {
853 if (D->isFieldDesignator()) {
854 if (FieldDecl *Field = D->getField()) {
855 Record.push_back(pch::DESIG_FIELD_DECL);
856 Writer.AddDeclRef(Field, Record);
857 } else {
858 Record.push_back(pch::DESIG_FIELD_NAME);
859 Writer.AddIdentifierRef(D->getFieldName(), Record);
860 }
861 Writer.AddSourceLocation(D->getDotLoc(), Record);
862 Writer.AddSourceLocation(D->getFieldLoc(), Record);
863 } else if (D->isArrayDesignator()) {
864 Record.push_back(pch::DESIG_ARRAY);
865 Record.push_back(D->getFirstExprIndex());
866 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
867 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
868 } else {
869 assert(D->isArrayRangeDesignator() && "Unknown designator");
870 Record.push_back(pch::DESIG_ARRAY_RANGE);
871 Record.push_back(D->getFirstExprIndex());
872 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
873 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
874 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
875 }
876 }
877 Code = pch::EXPR_DESIGNATED_INIT;
878}
879
880void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
881 VisitExpr(E);
882 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
883}
884
Douglas Gregorec0b8292009-04-15 23:02:49 +0000885void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
886 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000887 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000888 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
889 Writer.AddSourceLocation(E->getRParenLoc(), Record);
890 Code = pch::EXPR_VA_ARG;
891}
892
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000893void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
894 VisitExpr(E);
895 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
896 Writer.AddSourceLocation(E->getLabelLoc(), Record);
897 Record.push_back(Writer.GetLabelID(E->getLabel()));
898 Code = pch::EXPR_ADDR_LABEL;
899}
900
Douglas Gregoreca12f62009-04-17 19:05:30 +0000901void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
902 VisitExpr(E);
903 Writer.WriteSubStmt(E->getSubStmt());
904 Writer.AddSourceLocation(E->getLParenLoc(), Record);
905 Writer.AddSourceLocation(E->getRParenLoc(), Record);
906 Code = pch::EXPR_STMT;
907}
908
Douglas Gregor209d4622009-04-15 23:33:31 +0000909void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
910 VisitExpr(E);
911 Writer.AddTypeRef(E->getArgType1(), Record);
912 Writer.AddTypeRef(E->getArgType2(), Record);
913 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
914 Writer.AddSourceLocation(E->getRParenLoc(), Record);
915 Code = pch::EXPR_TYPES_COMPATIBLE;
916}
917
918void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
919 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000920 Writer.WriteSubStmt(E->getCond());
921 Writer.WriteSubStmt(E->getLHS());
922 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +0000923 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
924 Writer.AddSourceLocation(E->getRParenLoc(), Record);
925 Code = pch::EXPR_CHOOSE;
926}
927
928void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
929 VisitExpr(E);
930 Writer.AddSourceLocation(E->getTokenLocation(), Record);
931 Code = pch::EXPR_GNU_NULL;
932}
933
Douglas Gregor725e94b2009-04-16 00:01:45 +0000934void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
935 VisitExpr(E);
936 Record.push_back(E->getNumSubExprs());
937 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000938 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +0000939 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
940 Writer.AddSourceLocation(E->getRParenLoc(), Record);
941 Code = pch::EXPR_SHUFFLE_VECTOR;
942}
943
Douglas Gregore246b742009-04-17 19:21:43 +0000944void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
945 VisitExpr(E);
946 Writer.AddDeclRef(E->getBlockDecl(), Record);
947 Record.push_back(E->hasBlockDeclRefExprs());
948 Code = pch::EXPR_BLOCK;
949}
950
Douglas Gregor725e94b2009-04-16 00:01:45 +0000951void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
952 VisitExpr(E);
953 Writer.AddDeclRef(E->getDecl(), Record);
954 Writer.AddSourceLocation(E->getLocation(), Record);
955 Record.push_back(E->isByRef());
956 Code = pch::EXPR_BLOCK_DECL_REF;
957}
958
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000959//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000960// PCHWriter Implementation
961//===----------------------------------------------------------------------===//
962
Douglas Gregorb5887f32009-04-10 21:16:55 +0000963/// \brief Write the target triple (e.g., i686-apple-darwin9).
964void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
965 using namespace llvm;
966 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
967 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000969 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +0000970
971 RecordData Record;
972 Record.push_back(pch::TARGET_TRIPLE);
973 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000974 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +0000975}
976
977/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000978void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
979 RecordData Record;
980 Record.push_back(LangOpts.Trigraphs);
981 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
982 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
983 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
984 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
985 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
986 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
987 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
988 Record.push_back(LangOpts.C99); // C99 Support
989 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
990 Record.push_back(LangOpts.CPlusPlus); // C++ Support
991 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
992 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
993 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
994
995 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
996 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
997 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
998
999 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1000 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1001 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1002 Record.push_back(LangOpts.LaxVectorConversions);
1003 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1004
1005 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1006 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1007 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1008
1009 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1010 // by locks.
1011 Record.push_back(LangOpts.Blocks); // block extension to C
1012 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1013 // they are unused.
1014 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1015 // (modulo the platform support).
1016
1017 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1018 // signed integer arithmetic overflows.
1019
1020 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1021 // may be ripped out at any time.
1022
1023 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1024 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1025 // defined.
1026 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1027 // opposed to __DYNAMIC__).
1028 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1029
1030 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1031 // used (instead of C99 semantics).
1032 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1033 Record.push_back(LangOpts.getGCMode());
1034 Record.push_back(LangOpts.getVisibilityMode());
1035 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001036 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001037}
1038
Douglas Gregorab1cef72009-04-10 03:52:48 +00001039//===----------------------------------------------------------------------===//
1040// Source Manager Serialization
1041//===----------------------------------------------------------------------===//
1042
1043/// \brief Create an abbreviation for the SLocEntry that refers to a
1044/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001045static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001046 using namespace llvm;
1047 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1048 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1052 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001054 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001055}
1056
1057/// \brief Create an abbreviation for the SLocEntry that refers to a
1058/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001059static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001060 using namespace llvm;
1061 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1062 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001068 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001069}
1070
1071/// \brief Create an abbreviation for the SLocEntry that refers to a
1072/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001073static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001074 using namespace llvm;
1075 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1076 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1077 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001078 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001079}
1080
1081/// \brief Create an abbreviation for the SLocEntry that refers to an
1082/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001083static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001084 using namespace llvm;
1085 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1086 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001091 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001092 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001093}
1094
1095/// \brief Writes the block containing the serialized form of the
1096/// source manager.
1097///
1098/// TODO: We should probably use an on-disk hash table (stored in a
1099/// blob), indexed based on the file name, so that we only create
1100/// entries for files that we actually need. In the common case (no
1101/// errors), we probably won't have to create file entries for any of
1102/// the files in the AST.
1103void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001104 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001105 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001106
1107 // Abbreviations for the various kinds of source-location entries.
1108 int SLocFileAbbrv = -1;
1109 int SLocBufferAbbrv = -1;
1110 int SLocBufferBlobAbbrv = -1;
1111 int SLocInstantiationAbbrv = -1;
1112
1113 // Write out the source location entry table. We skip the first
1114 // entry, which is always the same dummy entry.
1115 RecordData Record;
1116 for (SourceManager::sloc_entry_iterator
1117 SLoc = SourceMgr.sloc_entry_begin() + 1,
1118 SLocEnd = SourceMgr.sloc_entry_end();
1119 SLoc != SLocEnd; ++SLoc) {
1120 // Figure out which record code to use.
1121 unsigned Code;
1122 if (SLoc->isFile()) {
1123 if (SLoc->getFile().getContentCache()->Entry)
1124 Code = pch::SM_SLOC_FILE_ENTRY;
1125 else
1126 Code = pch::SM_SLOC_BUFFER_ENTRY;
1127 } else
1128 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1129 Record.push_back(Code);
1130
1131 Record.push_back(SLoc->getOffset());
1132 if (SLoc->isFile()) {
1133 const SrcMgr::FileInfo &File = SLoc->getFile();
1134 Record.push_back(File.getIncludeLoc().getRawEncoding());
1135 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001136 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001137
1138 const SrcMgr::ContentCache *Content = File.getContentCache();
1139 if (Content->Entry) {
1140 // The source location entry is a file. The blob associated
1141 // with this entry is the file name.
1142 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001143 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1144 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001145 Content->Entry->getName(),
1146 strlen(Content->Entry->getName()));
1147 } else {
1148 // The source location entry is a buffer. The blob associated
1149 // with this entry contains the contents of the buffer.
1150 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001151 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1152 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001153 }
1154
1155 // We add one to the size so that we capture the trailing NULL
1156 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1157 // the reader side).
1158 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1159 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001160 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001161 Record.clear();
1162 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001163 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001164 Buffer->getBufferStart(),
1165 Buffer->getBufferSize() + 1);
1166 }
1167 } else {
1168 // The source location entry is an instantiation.
1169 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1170 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1171 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1172 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1173
Douglas Gregor364e5802009-04-15 18:05:10 +00001174 // Compute the token length for this macro expansion.
1175 unsigned NextOffset = SourceMgr.getNextOffset();
1176 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1177 if (++NextSLoc != SLocEnd)
1178 NextOffset = NextSLoc->getOffset();
1179 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1180
Douglas Gregorab1cef72009-04-10 03:52:48 +00001181 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001182 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1183 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001184 }
1185
1186 Record.clear();
1187 }
1188
Douglas Gregor635f97f2009-04-13 16:31:14 +00001189 // Write the line table.
1190 if (SourceMgr.hasLineTable()) {
1191 LineTableInfo &LineTable = SourceMgr.getLineTable();
1192
1193 // Emit the file names
1194 Record.push_back(LineTable.getNumFilenames());
1195 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1196 // Emit the file name
1197 const char *Filename = LineTable.getFilename(I);
1198 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1199 Record.push_back(FilenameLen);
1200 if (FilenameLen)
1201 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1202 }
1203
1204 // Emit the line entries
1205 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1206 L != LEnd; ++L) {
1207 // Emit the file ID
1208 Record.push_back(L->first);
1209
1210 // Emit the line entries
1211 Record.push_back(L->second.size());
1212 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1213 LEEnd = L->second.end();
1214 LE != LEEnd; ++LE) {
1215 Record.push_back(LE->FileOffset);
1216 Record.push_back(LE->LineNo);
1217 Record.push_back(LE->FilenameID);
1218 Record.push_back((unsigned)LE->FileKind);
1219 Record.push_back(LE->IncludeOffset);
1220 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001221 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001222 }
1223 }
1224
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001225 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001226}
1227
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001228/// \brief Writes the block containing the serialized form of the
1229/// preprocessor.
1230///
Chris Lattner850eabd2009-04-10 18:08:30 +00001231void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001232 // Enter the preprocessor block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001233 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattner84b04f12009-04-10 17:16:57 +00001234
Chris Lattner1b094952009-04-10 18:00:12 +00001235 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1236 // FIXME: use diagnostics subsystem for localization etc.
1237 if (PP.SawDateOrTime())
1238 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001239
Chris Lattner1b094952009-04-10 18:00:12 +00001240 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001241
Chris Lattner4b21c202009-04-13 01:29:17 +00001242 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1243 if (PP.getCounterValue() != 0) {
1244 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001245 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001246 Record.clear();
1247 }
1248
Chris Lattner1b094952009-04-10 18:00:12 +00001249 // Loop over all the macro definitions that are live at the end of the file,
1250 // emitting each to the PP section.
1251 // FIXME: Eventually we want to emit an index so that we can lazily load
1252 // macros.
1253 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1254 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001255 // FIXME: This emits macros in hash table order, we should do it in a stable
1256 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001257 MacroInfo *MI = I->second;
1258
1259 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1260 // been redefined by the header (in which case they are not isBuiltinMacro).
1261 if (MI->isBuiltinMacro())
1262 continue;
1263
Chris Lattner29241862009-04-11 21:15:38 +00001264 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001265 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1266 Record.push_back(MI->isUsed());
1267
1268 unsigned Code;
1269 if (MI->isObjectLike()) {
1270 Code = pch::PP_MACRO_OBJECT_LIKE;
1271 } else {
1272 Code = pch::PP_MACRO_FUNCTION_LIKE;
1273
1274 Record.push_back(MI->isC99Varargs());
1275 Record.push_back(MI->isGNUVarargs());
1276 Record.push_back(MI->getNumArgs());
1277 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1278 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001279 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001280 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001281 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001282 Record.clear();
1283
Chris Lattner850eabd2009-04-10 18:08:30 +00001284 // Emit the tokens array.
1285 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1286 // Note that we know that the preprocessor does not have any annotation
1287 // tokens in it because they are created by the parser, and thus can't be
1288 // in a macro definition.
1289 const Token &Tok = MI->getReplacementToken(TokNo);
1290
1291 Record.push_back(Tok.getLocation().getRawEncoding());
1292 Record.push_back(Tok.getLength());
1293
Chris Lattner850eabd2009-04-10 18:08:30 +00001294 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1295 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001296 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001297
1298 // FIXME: Should translate token kind to a stable encoding.
1299 Record.push_back(Tok.getKind());
1300 // FIXME: Should translate token flags to a stable encoding.
1301 Record.push_back(Tok.getFlags());
1302
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001303 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001304 Record.clear();
1305 }
Chris Lattner1b094952009-04-10 18:00:12 +00001306
1307 }
1308
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001309 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001310}
1311
1312
Douglas Gregorc34897d2009-04-09 22:27:44 +00001313/// \brief Write the representation of a type to the PCH stream.
1314void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001315 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001316 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001317 ID = NextTypeID++;
1318
1319 // Record the offset for this type.
1320 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001321 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001322 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1323 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001324 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001325 }
1326
1327 RecordData Record;
1328
1329 // Emit the type's representation.
1330 PCHTypeWriter W(*this, Record);
1331 switch (T->getTypeClass()) {
1332 // For all of the concrete, non-dependent types, call the
1333 // appropriate visitor function.
1334#define TYPE(Class, Base) \
1335 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1336#define ABSTRACT_TYPE(Class, Base)
1337#define DEPENDENT_TYPE(Class, Base)
1338#include "clang/AST/TypeNodes.def"
1339
1340 // For all of the dependent type nodes (which only occur in C++
1341 // templates), produce an error.
1342#define TYPE(Class, Base)
1343#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1344#include "clang/AST/TypeNodes.def"
1345 assert(false && "Cannot serialize dependent type nodes");
1346 break;
1347 }
1348
1349 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001350 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001351
1352 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001353 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001354}
1355
1356/// \brief Write a block containing all of the types.
1357void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001358 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001359 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001360
1361 // Emit all of the types in the ASTContext
1362 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1363 TEnd = Context.getTypes().end();
1364 T != TEnd; ++T) {
1365 // Builtin types are never serialized.
1366 if (isa<BuiltinType>(*T))
1367 continue;
1368
1369 WriteType(*T);
1370 }
1371
1372 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001373 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001374}
1375
1376/// \brief Write the block containing all of the declaration IDs
1377/// lexically declared within the given DeclContext.
1378///
1379/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1380/// bistream, or 0 if no block was written.
1381uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1382 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001383 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001384 return 0;
1385
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001386 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001387 RecordData Record;
1388 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1389 DEnd = DC->decls_end(Context);
1390 D != DEnd; ++D)
1391 AddDeclRef(*D, Record);
1392
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001393 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001394 return Offset;
1395}
1396
1397/// \brief Write the block containing all of the declaration IDs
1398/// visible from the given DeclContext.
1399///
1400/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1401/// bistream, or 0 if no block was written.
1402uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1403 DeclContext *DC) {
1404 if (DC->getPrimaryContext() != DC)
1405 return 0;
1406
1407 // Force the DeclContext to build a its name-lookup table.
1408 DC->lookup(Context, DeclarationName());
1409
1410 // Serialize the contents of the mapping used for lookup. Note that,
1411 // although we have two very different code paths, the serialized
1412 // representation is the same for both cases: a declaration name,
1413 // followed by a size, followed by references to the visible
1414 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001415 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001416 RecordData Record;
1417 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001418 if (!Map)
1419 return 0;
1420
Douglas Gregorc34897d2009-04-09 22:27:44 +00001421 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1422 D != DEnd; ++D) {
1423 AddDeclarationName(D->first, Record);
1424 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1425 Record.push_back(Result.second - Result.first);
1426 for(; Result.first != Result.second; ++Result.first)
1427 AddDeclRef(*Result.first, Record);
1428 }
1429
1430 if (Record.size() == 0)
1431 return 0;
1432
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001433 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001434 return Offset;
1435}
1436
1437/// \brief Write a block containing all of the declarations.
1438void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001439 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001440 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001441
1442 // Emit all of the declarations.
1443 RecordData Record;
1444 PCHDeclWriter W(*this, Record);
1445 while (!DeclsToEmit.empty()) {
1446 // Pull the next declaration off the queue
1447 Decl *D = DeclsToEmit.front();
1448 DeclsToEmit.pop();
1449
1450 // If this declaration is also a DeclContext, write blocks for the
1451 // declarations that lexically stored inside its context and those
1452 // declarations that are visible from its context. These blocks
1453 // are written before the declaration itself so that we can put
1454 // their offsets into the record for the declaration.
1455 uint64_t LexicalOffset = 0;
1456 uint64_t VisibleOffset = 0;
1457 DeclContext *DC = dyn_cast<DeclContext>(D);
1458 if (DC) {
1459 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1460 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1461 }
1462
1463 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001464 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001465 if (ID == 0)
1466 ID = DeclIDs.size();
1467
1468 unsigned Index = ID - 1;
1469
1470 // Record the offset for this declaration
1471 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001472 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001473 else if (DeclOffsets.size() < Index) {
1474 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001475 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001476 }
1477
1478 // Build and emit a record for this declaration
1479 Record.clear();
1480 W.Code = (pch::DeclCode)0;
1481 W.Visit(D);
1482 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001483 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001484 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001485
Douglas Gregor1c507882009-04-15 21:30:51 +00001486 // If the declaration had any attributes, write them now.
1487 if (D->hasAttrs())
1488 WriteAttributeRecord(D->getAttrs());
1489
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001490 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001491 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001492
Douglas Gregor631f6c62009-04-14 00:24:19 +00001493 // Note external declarations so that we can add them to a record
1494 // in the PCH file later.
1495 if (isa<FileScopeAsmDecl>(D))
1496 ExternalDefinitions.push_back(ID);
1497 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1498 if (// Non-static file-scope variables with initializers or that
1499 // are tentative definitions.
1500 (Var->isFileVarDecl() &&
1501 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1502 // Out-of-line definitions of static data members (C++).
1503 (Var->getDeclContext()->isRecord() &&
1504 !Var->getLexicalDeclContext()->isRecord() &&
1505 Var->getStorageClass() == VarDecl::Static))
1506 ExternalDefinitions.push_back(ID);
1507 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1508 if (Func->isThisDeclarationADefinition() &&
1509 Func->getStorageClass() != FunctionDecl::Static &&
1510 !Func->isInline())
1511 ExternalDefinitions.push_back(ID);
1512 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001513 }
1514
1515 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001516 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001517}
1518
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001519/// \brief Write the identifier table into the PCH file.
1520///
1521/// The identifier table consists of a blob containing string data
1522/// (the actual identifiers themselves) and a separate "offsets" index
1523/// that maps identifier IDs to locations within the blob.
1524void PCHWriter::WriteIdentifierTable() {
1525 using namespace llvm;
1526
1527 // Create and write out the blob that contains the identifier
1528 // strings.
1529 RecordData IdentOffsets;
1530 IdentOffsets.resize(IdentifierIDs.size());
1531 {
1532 // Create the identifier string data.
1533 std::vector<char> Data;
1534 Data.push_back(0); // Data must not be empty.
1535 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1536 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1537 ID != IDEnd; ++ID) {
1538 assert(ID->first && "NULL identifier in identifier table");
1539
1540 // Make sure we're starting on an odd byte. The PCH reader
1541 // expects the low bit to be set on all of the offsets.
1542 if ((Data.size() & 0x01) == 0)
1543 Data.push_back((char)0);
1544
1545 IdentOffsets[ID->second - 1] = Data.size();
1546 Data.insert(Data.end(),
1547 ID->first->getName(),
1548 ID->first->getName() + ID->first->getLength());
1549 Data.push_back((char)0);
1550 }
1551
1552 // Create a blob abbreviation
1553 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1554 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001556 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001557
1558 // Write the identifier table
1559 RecordData Record;
1560 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001561 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001562 }
1563
1564 // Write the offsets table for identifier IDs.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001565 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001566}
1567
Douglas Gregor1c507882009-04-15 21:30:51 +00001568/// \brief Write a record containing the given attributes.
1569void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1570 RecordData Record;
1571 for (; Attr; Attr = Attr->getNext()) {
1572 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1573 Record.push_back(Attr->isInherited());
1574 switch (Attr->getKind()) {
1575 case Attr::Alias:
1576 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1577 break;
1578
1579 case Attr::Aligned:
1580 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1581 break;
1582
1583 case Attr::AlwaysInline:
1584 break;
1585
1586 case Attr::AnalyzerNoReturn:
1587 break;
1588
1589 case Attr::Annotate:
1590 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1591 break;
1592
1593 case Attr::AsmLabel:
1594 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1595 break;
1596
1597 case Attr::Blocks:
1598 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1599 break;
1600
1601 case Attr::Cleanup:
1602 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1603 break;
1604
1605 case Attr::Const:
1606 break;
1607
1608 case Attr::Constructor:
1609 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1610 break;
1611
1612 case Attr::DLLExport:
1613 case Attr::DLLImport:
1614 case Attr::Deprecated:
1615 break;
1616
1617 case Attr::Destructor:
1618 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1619 break;
1620
1621 case Attr::FastCall:
1622 break;
1623
1624 case Attr::Format: {
1625 const FormatAttr *Format = cast<FormatAttr>(Attr);
1626 AddString(Format->getType(), Record);
1627 Record.push_back(Format->getFormatIdx());
1628 Record.push_back(Format->getFirstArg());
1629 break;
1630 }
1631
1632 case Attr::GNUCInline:
1633 case Attr::IBOutletKind:
1634 case Attr::NoReturn:
1635 case Attr::NoThrow:
1636 case Attr::Nodebug:
1637 case Attr::Noinline:
1638 break;
1639
1640 case Attr::NonNull: {
1641 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1642 Record.push_back(NonNull->size());
1643 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1644 break;
1645 }
1646
1647 case Attr::ObjCException:
1648 case Attr::ObjCNSObject:
1649 case Attr::Overloadable:
1650 break;
1651
1652 case Attr::Packed:
1653 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1654 break;
1655
1656 case Attr::Pure:
1657 break;
1658
1659 case Attr::Regparm:
1660 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1661 break;
1662
1663 case Attr::Section:
1664 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1665 break;
1666
1667 case Attr::StdCall:
1668 case Attr::TransparentUnion:
1669 case Attr::Unavailable:
1670 case Attr::Unused:
1671 case Attr::Used:
1672 break;
1673
1674 case Attr::Visibility:
1675 // FIXME: stable encoding
1676 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1677 break;
1678
1679 case Attr::WarnUnusedResult:
1680 case Attr::Weak:
1681 case Attr::WeakImport:
1682 break;
1683 }
1684 }
1685
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001686 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001687}
1688
1689void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1690 Record.push_back(Str.size());
1691 Record.insert(Record.end(), Str.begin(), Str.end());
1692}
1693
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001694PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1695 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001696
Chris Lattner850eabd2009-04-10 18:08:30 +00001697void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001698 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001699 Stream.Emit((unsigned)'C', 8);
1700 Stream.Emit((unsigned)'P', 8);
1701 Stream.Emit((unsigned)'C', 8);
1702 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001703
1704 // The translation unit is the first declaration we'll emit.
1705 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1706 DeclsToEmit.push(Context.getTranslationUnitDecl());
1707
1708 // Write the remaining PCH contents.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001709 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001710 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001711 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001712 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001713 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001714 WriteTypesBlock(Context);
1715 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001716 WriteIdentifierTable();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001717 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1718 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001719 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001720 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
1721 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001722}
1723
1724void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1725 Record.push_back(Loc.getRawEncoding());
1726}
1727
1728void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1729 Record.push_back(Value.getBitWidth());
1730 unsigned N = Value.getNumWords();
1731 const uint64_t* Words = Value.getRawData();
1732 for (unsigned I = 0; I != N; ++I)
1733 Record.push_back(Words[I]);
1734}
1735
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001736void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1737 Record.push_back(Value.isUnsigned());
1738 AddAPInt(Value, Record);
1739}
1740
Douglas Gregore2f37202009-04-14 21:55:33 +00001741void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1742 AddAPInt(Value.bitcastToAPInt(), Record);
1743}
1744
Douglas Gregorc34897d2009-04-09 22:27:44 +00001745void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001746 if (II == 0) {
1747 Record.push_back(0);
1748 return;
1749 }
1750
1751 pch::IdentID &ID = IdentifierIDs[II];
1752 if (ID == 0)
1753 ID = IdentifierIDs.size();
1754
1755 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001756}
1757
1758void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1759 if (T.isNull()) {
1760 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1761 return;
1762 }
1763
1764 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001765 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001766 switch (BT->getKind()) {
1767 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1768 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1769 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1770 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1771 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1772 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1773 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1774 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1775 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1776 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1777 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1778 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1779 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1780 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1781 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1782 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1783 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1784 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1785 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1786 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1787 }
1788
1789 Record.push_back((ID << 3) | T.getCVRQualifiers());
1790 return;
1791 }
1792
Douglas Gregorac8f2802009-04-10 17:25:41 +00001793 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001794 if (ID == 0) // we haven't seen this type before
1795 ID = NextTypeID++;
1796
1797 // Encode the type qualifiers in the type reference.
1798 Record.push_back((ID << 3) | T.getCVRQualifiers());
1799}
1800
1801void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1802 if (D == 0) {
1803 Record.push_back(0);
1804 return;
1805 }
1806
Douglas Gregorac8f2802009-04-10 17:25:41 +00001807 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001808 if (ID == 0) {
1809 // We haven't seen this declaration before. Give it a new ID and
1810 // enqueue it in the list of declarations to emit.
1811 ID = DeclIDs.size();
1812 DeclsToEmit.push(const_cast<Decl *>(D));
1813 }
1814
1815 Record.push_back(ID);
1816}
1817
1818void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1819 Record.push_back(Name.getNameKind());
1820 switch (Name.getNameKind()) {
1821 case DeclarationName::Identifier:
1822 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1823 break;
1824
1825 case DeclarationName::ObjCZeroArgSelector:
1826 case DeclarationName::ObjCOneArgSelector:
1827 case DeclarationName::ObjCMultiArgSelector:
1828 assert(false && "Serialization of Objective-C selectors unavailable");
1829 break;
1830
1831 case DeclarationName::CXXConstructorName:
1832 case DeclarationName::CXXDestructorName:
1833 case DeclarationName::CXXConversionFunctionName:
1834 AddTypeRef(Name.getCXXNameType(), Record);
1835 break;
1836
1837 case DeclarationName::CXXOperatorName:
1838 Record.push_back(Name.getCXXOverloadedOperator());
1839 break;
1840
1841 case DeclarationName::CXXUsingDirective:
1842 // No extra data to emit
1843 break;
1844 }
1845}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001846
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001847/// \brief Write the given substatement or subexpression to the
1848/// bitstream.
1849void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00001850 RecordData Record;
1851 PCHStmtWriter Writer(*this, Record);
1852
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001853 if (!S) {
1854 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001855 return;
1856 }
1857
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001858 Writer.Code = pch::STMT_NULL_PTR;
1859 Writer.Visit(S);
1860 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00001861 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001862 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001863}
1864
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001865/// \brief Flush all of the statements that have been added to the
1866/// queue via AddStmt().
1867void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001868 RecordData Record;
1869 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001870
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001871 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1872 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00001873
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001874 if (!S) {
1875 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001876 continue;
1877 }
1878
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001879 Writer.Code = pch::STMT_NULL_PTR;
1880 Writer.Visit(S);
1881 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001882 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001883 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001884
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001885 assert(N == StmtsToEmit.size() &&
1886 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00001887
1888 // Note that we are at the end of a full expression. Any
1889 // expression records that follow this one are part of a different
1890 // expression.
1891 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001892 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001893 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001894
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001895 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00001896 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001897}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00001898
1899unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1900 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1901 "SwitchCase recorded twice");
1902 unsigned NextID = SwitchCaseIDs.size();
1903 SwitchCaseIDs[S] = NextID;
1904 return NextID;
1905}
1906
1907unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1908 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1909 "SwitchCase hasn't been seen yet");
1910 return SwitchCaseIDs[S];
1911}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00001912
1913/// \brief Retrieve the ID for the given label statement, which may
1914/// or may not have been emitted yet.
1915unsigned PCHWriter::GetLabelID(LabelStmt *S) {
1916 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
1917 if (Pos != LabelIDs.end())
1918 return Pos->second;
1919
1920 unsigned NextID = LabelIDs.size();
1921 LabelIDs[S] = NextID;
1922 return NextID;
1923}