blob: 4bbaaa0c760596b8240c473ef57cde917c895694 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclContextInternals.h"
18#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000022#include "clang/Lex/MacroInfo.h"
23#include "clang/Lex/Preprocessor.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000028#include "llvm/ADT/APFloat.h"
29#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000030#include "llvm/Bitcode/BitstreamWriter.h"
31#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000032#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000033#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Type serialization
38//===----------------------------------------------------------------------===//
39namespace {
40 class VISIBILITY_HIDDEN PCHTypeWriter {
41 PCHWriter &Writer;
42 PCHWriter::RecordData &Record;
43
44 public:
45 /// \brief Type code that corresponds to the record generated.
46 pch::TypeCode Code;
47
48 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
49 : Writer(Writer), Record(Record) { }
50
51 void VisitArrayType(const ArrayType *T);
52 void VisitFunctionType(const FunctionType *T);
53 void VisitTagType(const TagType *T);
54
55#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
56#define ABSTRACT_TYPE(Class, Base)
57#define DEPENDENT_TYPE(Class, Base)
58#include "clang/AST/TypeNodes.def"
59 };
60}
61
62void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
63 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
64 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
65 Record.push_back(T->getAddressSpace());
66 Code = pch::TYPE_EXT_QUAL;
67}
68
69void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
73void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
74 Record.push_back(T->getWidth());
75 Record.push_back(T->isSigned());
76 Code = pch::TYPE_FIXED_WIDTH_INT;
77}
78
79void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
80 Writer.AddTypeRef(T->getElementType(), Record);
81 Code = pch::TYPE_COMPLEX;
82}
83
84void PCHTypeWriter::VisitPointerType(const PointerType *T) {
85 Writer.AddTypeRef(T->getPointeeType(), Record);
86 Code = pch::TYPE_POINTER;
87}
88
89void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
90 Writer.AddTypeRef(T->getPointeeType(), Record);
91 Code = pch::TYPE_BLOCK_POINTER;
92}
93
94void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
95 Writer.AddTypeRef(T->getPointeeType(), Record);
96 Code = pch::TYPE_LVALUE_REFERENCE;
97}
98
99void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
100 Writer.AddTypeRef(T->getPointeeType(), Record);
101 Code = pch::TYPE_RVALUE_REFERENCE;
102}
103
104void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
105 Writer.AddTypeRef(T->getPointeeType(), Record);
106 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
107 Code = pch::TYPE_MEMBER_POINTER;
108}
109
110void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
111 Writer.AddTypeRef(T->getElementType(), Record);
112 Record.push_back(T->getSizeModifier()); // FIXME: stable values
113 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
114}
115
116void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
117 VisitArrayType(T);
118 Writer.AddAPInt(T->getSize(), Record);
119 Code = pch::TYPE_CONSTANT_ARRAY;
120}
121
122void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
123 VisitArrayType(T);
124 Code = pch::TYPE_INCOMPLETE_ARRAY;
125}
126
127void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
128 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000129 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000130 Code = pch::TYPE_VARIABLE_ARRAY;
131}
132
133void PCHTypeWriter::VisitVectorType(const VectorType *T) {
134 Writer.AddTypeRef(T->getElementType(), Record);
135 Record.push_back(T->getNumElements());
136 Code = pch::TYPE_VECTOR;
137}
138
139void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
140 VisitVectorType(T);
141 Code = pch::TYPE_EXT_VECTOR;
142}
143
144void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
145 Writer.AddTypeRef(T->getResultType(), Record);
146}
147
148void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
149 VisitFunctionType(T);
150 Code = pch::TYPE_FUNCTION_NO_PROTO;
151}
152
153void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
154 VisitFunctionType(T);
155 Record.push_back(T->getNumArgs());
156 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
157 Writer.AddTypeRef(T->getArgType(I), Record);
158 Record.push_back(T->isVariadic());
159 Record.push_back(T->getTypeQuals());
160 Code = pch::TYPE_FUNCTION_PROTO;
161}
162
163void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
164 Writer.AddDeclRef(T->getDecl(), Record);
165 Code = pch::TYPE_TYPEDEF;
166}
167
168void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000169 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000170 Code = pch::TYPE_TYPEOF_EXPR;
171}
172
173void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
174 Writer.AddTypeRef(T->getUnderlyingType(), Record);
175 Code = pch::TYPE_TYPEOF;
176}
177
178void PCHTypeWriter::VisitTagType(const TagType *T) {
179 Writer.AddDeclRef(T->getDecl(), Record);
180 assert(!T->isBeingDefined() &&
181 "Cannot serialize in the middle of a type definition");
182}
183
184void PCHTypeWriter::VisitRecordType(const RecordType *T) {
185 VisitTagType(T);
186 Code = pch::TYPE_RECORD;
187}
188
189void PCHTypeWriter::VisitEnumType(const EnumType *T) {
190 VisitTagType(T);
191 Code = pch::TYPE_ENUM;
192}
193
194void
195PCHTypeWriter::VisitTemplateSpecializationType(
196 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000197 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000198 assert(false && "Cannot serialize template specialization types");
199}
200
201void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000202 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000203 assert(false && "Cannot serialize qualified name types");
204}
205
206void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
207 Writer.AddDeclRef(T->getDecl(), Record);
208 Code = pch::TYPE_OBJC_INTERFACE;
209}
210
211void
212PCHTypeWriter::VisitObjCQualifiedInterfaceType(
213 const ObjCQualifiedInterfaceType *T) {
214 VisitObjCInterfaceType(T);
215 Record.push_back(T->getNumProtocols());
216 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
217 Writer.AddDeclRef(T->getProtocol(I), Record);
218 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
219}
220
221void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
222 Record.push_back(T->getNumProtocols());
223 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
224 Writer.AddDeclRef(T->getProtocols(I), Record);
225 Code = pch::TYPE_OBJC_QUALIFIED_ID;
226}
227
228void
229PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) {
230 Record.push_back(T->getNumProtocols());
231 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
232 Writer.AddDeclRef(T->getProtocols(I), Record);
233 Code = pch::TYPE_OBJC_QUALIFIED_CLASS;
234}
235
236//===----------------------------------------------------------------------===//
237// Declaration serialization
238//===----------------------------------------------------------------------===//
239namespace {
240 class VISIBILITY_HIDDEN PCHDeclWriter
241 : public DeclVisitor<PCHDeclWriter, void> {
242
243 PCHWriter &Writer;
244 PCHWriter::RecordData &Record;
245
246 public:
247 pch::DeclCode Code;
248
249 PCHDeclWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
250 : Writer(Writer), Record(Record) { }
251
252 void VisitDecl(Decl *D);
253 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
254 void VisitNamedDecl(NamedDecl *D);
255 void VisitTypeDecl(TypeDecl *D);
256 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000257 void VisitTagDecl(TagDecl *D);
258 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000259 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000260 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000261 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000262 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000263 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000264 void VisitVarDecl(VarDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000265 void VisitParmVarDecl(ParmVarDecl *D);
266 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000267 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
268 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000269 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
270 uint64_t VisibleOffset);
271 };
272}
273
274void PCHDeclWriter::VisitDecl(Decl *D) {
275 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
276 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
277 Writer.AddSourceLocation(D->getLocation(), Record);
278 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000279 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000280 Record.push_back(D->isImplicit());
281 Record.push_back(D->getAccess());
282}
283
284void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
285 VisitDecl(D);
286 Code = pch::DECL_TRANSLATION_UNIT;
287}
288
289void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
290 VisitDecl(D);
291 Writer.AddDeclarationName(D->getDeclName(), Record);
292}
293
294void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
295 VisitNamedDecl(D);
296 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
297}
298
299void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
300 VisitTypeDecl(D);
301 Writer.AddTypeRef(D->getUnderlyingType(), Record);
302 Code = pch::DECL_TYPEDEF;
303}
304
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000305void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
306 VisitTypeDecl(D);
307 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
308 Record.push_back(D->isDefinition());
309 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
310}
311
312void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
313 VisitTagDecl(D);
314 Writer.AddTypeRef(D->getIntegerType(), Record);
315 Code = pch::DECL_ENUM;
316}
317
Douglas Gregor982365e2009-04-13 21:20:57 +0000318void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
319 VisitTagDecl(D);
320 Record.push_back(D->hasFlexibleArrayMember());
321 Record.push_back(D->isAnonymousStructOrUnion());
322 Code = pch::DECL_RECORD;
323}
324
Douglas Gregorc34897d2009-04-09 22:27:44 +0000325void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
326 VisitNamedDecl(D);
327 Writer.AddTypeRef(D->getType(), Record);
328}
329
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000330void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
331 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000332 Record.push_back(D->getInitExpr()? 1 : 0);
333 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000334 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000335 Writer.AddAPSInt(D->getInitVal(), Record);
336 Code = pch::DECL_ENUM_CONSTANT;
337}
338
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000339void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
340 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000341 Record.push_back(D->isThisDeclarationADefinition());
342 if (D->isThisDeclarationADefinition())
343 Writer.AddStmt(D->getBody());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000344 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
345 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
346 Record.push_back(D->isInline());
347 Record.push_back(D->isVirtual());
348 Record.push_back(D->isPure());
349 Record.push_back(D->inheritedPrototype());
350 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
351 Record.push_back(D->isDeleted());
352 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
353 Record.push_back(D->param_size());
354 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
355 P != PEnd; ++P)
356 Writer.AddDeclRef(*P, Record);
357 Code = pch::DECL_FUNCTION;
358}
359
Douglas Gregor982365e2009-04-13 21:20:57 +0000360void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
361 VisitValueDecl(D);
362 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000363 Record.push_back(D->getBitWidth()? 1 : 0);
364 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000365 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000366 Code = pch::DECL_FIELD;
367}
368
Douglas Gregorc34897d2009-04-09 22:27:44 +0000369void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
370 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000371 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000372 Record.push_back(D->isThreadSpecified());
373 Record.push_back(D->hasCXXDirectInitializer());
374 Record.push_back(D->isDeclaredInCondition());
375 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
376 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000377 Record.push_back(D->getInit()? 1 : 0);
378 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000379 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000380 Code = pch::DECL_VAR;
381}
382
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000383void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
384 VisitVarDecl(D);
385 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000386 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000387 // FIXME: why isn't the "default argument" just stored as the initializer
388 // in VarDecl?
389 Code = pch::DECL_PARM_VAR;
390}
391
392void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
393 VisitParmVarDecl(D);
394 Writer.AddTypeRef(D->getOriginalType(), Record);
395 Code = pch::DECL_ORIGINAL_PARM_VAR;
396}
397
Douglas Gregor2a491792009-04-13 22:49:25 +0000398void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
399 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000400 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000401 Code = pch::DECL_FILE_SCOPE_ASM;
402}
403
404void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
405 VisitDecl(D);
406 // FIXME: emit block body
407 Record.push_back(D->param_size());
408 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
409 P != PEnd; ++P)
410 Writer.AddDeclRef(*P, Record);
411 Code = pch::DECL_BLOCK;
412}
413
Douglas Gregorc34897d2009-04-09 22:27:44 +0000414/// \brief Emit the DeclContext part of a declaration context decl.
415///
416/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
417/// block for this declaration context is stored. May be 0 to indicate
418/// that there are no declarations stored within this context.
419///
420/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
421/// block for this declaration context is stored. May be 0 to indicate
422/// that there are no declarations visible from this context. Note
423/// that this value will not be emitted for non-primary declaration
424/// contexts.
425void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
426 uint64_t VisibleOffset) {
427 Record.push_back(LexicalOffset);
428 if (DC->getPrimaryContext() == DC)
429 Record.push_back(VisibleOffset);
430}
431
432//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000433// Statement/expression serialization
434//===----------------------------------------------------------------------===//
435namespace {
436 class VISIBILITY_HIDDEN PCHStmtWriter
437 : public StmtVisitor<PCHStmtWriter, void> {
438
439 PCHWriter &Writer;
440 PCHWriter::RecordData &Record;
441
442 public:
443 pch::StmtCode Code;
444
445 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
446 : Writer(Writer), Record(Record) { }
447
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000448 void VisitStmt(Stmt *S);
449 void VisitNullStmt(NullStmt *S);
450 void VisitCompoundStmt(CompoundStmt *S);
451 void VisitSwitchCase(SwitchCase *S);
452 void VisitCaseStmt(CaseStmt *S);
453 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000454 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000455 void VisitIfStmt(IfStmt *S);
456 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000457 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000458 void VisitDoStmt(DoStmt *S);
459 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000460 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000461 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000462 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000463 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000464 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000465 void VisitDeclStmt(DeclStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000466 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000467 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000468 void VisitDeclRefExpr(DeclRefExpr *E);
469 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000470 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000471 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000472 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000473 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000474 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000475 void VisitUnaryOperator(UnaryOperator *E);
476 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000477 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000478 void VisitCallExpr(CallExpr *E);
479 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000480 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000481 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000482 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
483 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000484 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000485 void VisitExplicitCastExpr(ExplicitCastExpr *E);
486 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000487 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000488 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000489 void VisitInitListExpr(InitListExpr *E);
490 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
491 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000492 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000493 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000494 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
495 void VisitChooseExpr(ChooseExpr *E);
496 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000497 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
498 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000499 };
500}
501
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000502void PCHStmtWriter::VisitStmt(Stmt *S) {
503}
504
505void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
506 VisitStmt(S);
507 Writer.AddSourceLocation(S->getSemiLoc(), Record);
508 Code = pch::STMT_NULL;
509}
510
511void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
512 VisitStmt(S);
513 Record.push_back(S->size());
514 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
515 CS != CSEnd; ++CS)
516 Writer.WriteSubStmt(*CS);
517 Writer.AddSourceLocation(S->getLBracLoc(), Record);
518 Writer.AddSourceLocation(S->getRBracLoc(), Record);
519 Code = pch::STMT_COMPOUND;
520}
521
522void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
523 VisitStmt(S);
524 Record.push_back(Writer.RecordSwitchCaseID(S));
525}
526
527void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
528 VisitSwitchCase(S);
529 Writer.WriteSubStmt(S->getLHS());
530 Writer.WriteSubStmt(S->getRHS());
531 Writer.WriteSubStmt(S->getSubStmt());
532 Writer.AddSourceLocation(S->getCaseLoc(), Record);
533 Code = pch::STMT_CASE;
534}
535
536void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
537 VisitSwitchCase(S);
538 Writer.WriteSubStmt(S->getSubStmt());
539 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
540 Code = pch::STMT_DEFAULT;
541}
542
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000543void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
544 VisitStmt(S);
545 Writer.AddIdentifierRef(S->getID(), Record);
546 Writer.WriteSubStmt(S->getSubStmt());
547 Writer.AddSourceLocation(S->getIdentLoc(), Record);
548 Record.push_back(Writer.GetLabelID(S));
549 Code = pch::STMT_LABEL;
550}
551
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000552void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
553 VisitStmt(S);
554 Writer.WriteSubStmt(S->getCond());
555 Writer.WriteSubStmt(S->getThen());
556 Writer.WriteSubStmt(S->getElse());
557 Writer.AddSourceLocation(S->getIfLoc(), Record);
558 Code = pch::STMT_IF;
559}
560
561void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
562 VisitStmt(S);
563 Writer.WriteSubStmt(S->getCond());
564 Writer.WriteSubStmt(S->getBody());
565 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
566 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
567 SC = SC->getNextSwitchCase())
568 Record.push_back(Writer.getSwitchCaseID(SC));
569 Code = pch::STMT_SWITCH;
570}
571
Douglas Gregora6b503f2009-04-17 00:16:09 +0000572void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
573 VisitStmt(S);
574 Writer.WriteSubStmt(S->getCond());
575 Writer.WriteSubStmt(S->getBody());
576 Writer.AddSourceLocation(S->getWhileLoc(), Record);
577 Code = pch::STMT_WHILE;
578}
579
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000580void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
581 VisitStmt(S);
582 Writer.WriteSubStmt(S->getCond());
583 Writer.WriteSubStmt(S->getBody());
584 Writer.AddSourceLocation(S->getDoLoc(), Record);
585 Code = pch::STMT_DO;
586}
587
588void PCHStmtWriter::VisitForStmt(ForStmt *S) {
589 VisitStmt(S);
590 Writer.WriteSubStmt(S->getInit());
591 Writer.WriteSubStmt(S->getCond());
592 Writer.WriteSubStmt(S->getInc());
593 Writer.WriteSubStmt(S->getBody());
594 Writer.AddSourceLocation(S->getForLoc(), Record);
595 Code = pch::STMT_FOR;
596}
597
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000598void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
599 VisitStmt(S);
600 Record.push_back(Writer.GetLabelID(S->getLabel()));
601 Writer.AddSourceLocation(S->getGotoLoc(), Record);
602 Writer.AddSourceLocation(S->getLabelLoc(), Record);
603 Code = pch::STMT_GOTO;
604}
605
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000606void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
607 VisitStmt(S);
608 Writer.WriteSubStmt(S->getTarget());
609 Code = pch::STMT_INDIRECT_GOTO;
610}
611
Douglas Gregora6b503f2009-04-17 00:16:09 +0000612void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
613 VisitStmt(S);
614 Writer.AddSourceLocation(S->getContinueLoc(), Record);
615 Code = pch::STMT_CONTINUE;
616}
617
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000618void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
619 VisitStmt(S);
620 Writer.AddSourceLocation(S->getBreakLoc(), Record);
621 Code = pch::STMT_BREAK;
622}
623
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000624void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
625 VisitStmt(S);
626 Writer.WriteSubStmt(S->getRetValue());
627 Writer.AddSourceLocation(S->getReturnLoc(), Record);
628 Code = pch::STMT_RETURN;
629}
630
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000631void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
632 VisitStmt(S);
633 Writer.AddSourceLocation(S->getStartLoc(), Record);
634 Writer.AddSourceLocation(S->getEndLoc(), Record);
635 DeclGroupRef DG = S->getDeclGroup();
636 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
637 Writer.AddDeclRef(*D, Record);
638 Code = pch::STMT_DECL;
639}
640
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000641void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000642 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000643 Writer.AddTypeRef(E->getType(), Record);
644 Record.push_back(E->isTypeDependent());
645 Record.push_back(E->isValueDependent());
646}
647
Douglas Gregore2f37202009-04-14 21:55:33 +0000648void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
649 VisitExpr(E);
650 Writer.AddSourceLocation(E->getLocation(), Record);
651 Record.push_back(E->getIdentType()); // FIXME: stable encoding
652 Code = pch::EXPR_PREDEFINED;
653}
654
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000655void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
656 VisitExpr(E);
657 Writer.AddDeclRef(E->getDecl(), Record);
658 Writer.AddSourceLocation(E->getLocation(), Record);
659 Code = pch::EXPR_DECL_REF;
660}
661
662void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
663 VisitExpr(E);
664 Writer.AddSourceLocation(E->getLocation(), Record);
665 Writer.AddAPInt(E->getValue(), Record);
666 Code = pch::EXPR_INTEGER_LITERAL;
667}
668
Douglas Gregore2f37202009-04-14 21:55:33 +0000669void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
670 VisitExpr(E);
671 Writer.AddAPFloat(E->getValue(), Record);
672 Record.push_back(E->isExact());
673 Writer.AddSourceLocation(E->getLocation(), Record);
674 Code = pch::EXPR_FLOATING_LITERAL;
675}
676
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000677void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
678 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000679 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000680 Code = pch::EXPR_IMAGINARY_LITERAL;
681}
682
Douglas Gregor596e0932009-04-15 16:35:07 +0000683void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
684 VisitExpr(E);
685 Record.push_back(E->getByteLength());
686 Record.push_back(E->getNumConcatenated());
687 Record.push_back(E->isWide());
688 // FIXME: String data should be stored as a blob at the end of the
689 // StringLiteral. However, we can't do so now because we have no
690 // provision for coping with abbreviations when we're jumping around
691 // the PCH file during deserialization.
692 Record.insert(Record.end(),
693 E->getStrData(), E->getStrData() + E->getByteLength());
694 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
695 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
696 Code = pch::EXPR_STRING_LITERAL;
697}
698
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000699void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
700 VisitExpr(E);
701 Record.push_back(E->getValue());
702 Writer.AddSourceLocation(E->getLoc(), Record);
703 Record.push_back(E->isWide());
704 Code = pch::EXPR_CHARACTER_LITERAL;
705}
706
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000707void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
708 VisitExpr(E);
709 Writer.AddSourceLocation(E->getLParen(), Record);
710 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000711 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000712 Code = pch::EXPR_PAREN;
713}
714
Douglas Gregor12d74052009-04-15 15:58:59 +0000715void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
716 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000717 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000718 Record.push_back(E->getOpcode()); // FIXME: stable encoding
719 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
720 Code = pch::EXPR_UNARY_OPERATOR;
721}
722
723void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
724 VisitExpr(E);
725 Record.push_back(E->isSizeOf());
726 if (E->isArgumentType())
727 Writer.AddTypeRef(E->getArgumentType(), Record);
728 else {
729 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000730 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000731 }
732 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
733 Writer.AddSourceLocation(E->getRParenLoc(), Record);
734 Code = pch::EXPR_SIZEOF_ALIGN_OF;
735}
736
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000737void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
738 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000739 Writer.WriteSubStmt(E->getLHS());
740 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000741 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
742 Code = pch::EXPR_ARRAY_SUBSCRIPT;
743}
744
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000745void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
746 VisitExpr(E);
747 Record.push_back(E->getNumArgs());
748 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000749 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000750 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
751 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000752 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000753 Code = pch::EXPR_CALL;
754}
755
756void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
757 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000758 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000759 Writer.AddDeclRef(E->getMemberDecl(), Record);
760 Writer.AddSourceLocation(E->getMemberLoc(), Record);
761 Record.push_back(E->isArrow());
762 Code = pch::EXPR_MEMBER;
763}
764
Douglas Gregora151ba42009-04-14 23:32:43 +0000765void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
766 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000767 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000768}
769
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000770void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
771 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000772 Writer.WriteSubStmt(E->getLHS());
773 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000774 Record.push_back(E->getOpcode()); // FIXME: stable encoding
775 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
776 Code = pch::EXPR_BINARY_OPERATOR;
777}
778
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000779void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
780 VisitBinaryOperator(E);
781 Writer.AddTypeRef(E->getComputationLHSType(), Record);
782 Writer.AddTypeRef(E->getComputationResultType(), Record);
783 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
784}
785
786void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
787 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000788 Writer.WriteSubStmt(E->getCond());
789 Writer.WriteSubStmt(E->getLHS());
790 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000791 Code = pch::EXPR_CONDITIONAL_OPERATOR;
792}
793
Douglas Gregora151ba42009-04-14 23:32:43 +0000794void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
795 VisitCastExpr(E);
796 Record.push_back(E->isLvalueCast());
797 Code = pch::EXPR_IMPLICIT_CAST;
798}
799
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000800void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
801 VisitCastExpr(E);
802 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
803}
804
805void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
806 VisitExplicitCastExpr(E);
807 Writer.AddSourceLocation(E->getLParenLoc(), Record);
808 Writer.AddSourceLocation(E->getRParenLoc(), Record);
809 Code = pch::EXPR_CSTYLE_CAST;
810}
811
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000812void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
813 VisitExpr(E);
814 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000815 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000816 Record.push_back(E->isFileScope());
817 Code = pch::EXPR_COMPOUND_LITERAL;
818}
819
Douglas Gregorec0b8292009-04-15 23:02:49 +0000820void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
821 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000822 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000823 Writer.AddIdentifierRef(&E->getAccessor(), Record);
824 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
825 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
826}
827
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000828void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
829 VisitExpr(E);
830 Record.push_back(E->getNumInits());
831 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000832 Writer.WriteSubStmt(E->getInit(I));
833 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000834 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
835 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
836 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
837 Record.push_back(E->hadArrayRangeDesignator());
838 Code = pch::EXPR_INIT_LIST;
839}
840
841void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
842 VisitExpr(E);
843 Record.push_back(E->getNumSubExprs());
844 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000845 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000846 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
847 Record.push_back(E->usesGNUSyntax());
848 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
849 DEnd = E->designators_end();
850 D != DEnd; ++D) {
851 if (D->isFieldDesignator()) {
852 if (FieldDecl *Field = D->getField()) {
853 Record.push_back(pch::DESIG_FIELD_DECL);
854 Writer.AddDeclRef(Field, Record);
855 } else {
856 Record.push_back(pch::DESIG_FIELD_NAME);
857 Writer.AddIdentifierRef(D->getFieldName(), Record);
858 }
859 Writer.AddSourceLocation(D->getDotLoc(), Record);
860 Writer.AddSourceLocation(D->getFieldLoc(), Record);
861 } else if (D->isArrayDesignator()) {
862 Record.push_back(pch::DESIG_ARRAY);
863 Record.push_back(D->getFirstExprIndex());
864 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
865 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
866 } else {
867 assert(D->isArrayRangeDesignator() && "Unknown designator");
868 Record.push_back(pch::DESIG_ARRAY_RANGE);
869 Record.push_back(D->getFirstExprIndex());
870 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
871 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
872 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
873 }
874 }
875 Code = pch::EXPR_DESIGNATED_INIT;
876}
877
878void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
879 VisitExpr(E);
880 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
881}
882
Douglas Gregorec0b8292009-04-15 23:02:49 +0000883void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
884 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000885 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000886 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
887 Writer.AddSourceLocation(E->getRParenLoc(), Record);
888 Code = pch::EXPR_VA_ARG;
889}
890
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000891void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
892 VisitExpr(E);
893 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
894 Writer.AddSourceLocation(E->getLabelLoc(), Record);
895 Record.push_back(Writer.GetLabelID(E->getLabel()));
896 Code = pch::EXPR_ADDR_LABEL;
897}
898
Douglas Gregor209d4622009-04-15 23:33:31 +0000899void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
900 VisitExpr(E);
901 Writer.AddTypeRef(E->getArgType1(), Record);
902 Writer.AddTypeRef(E->getArgType2(), Record);
903 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
904 Writer.AddSourceLocation(E->getRParenLoc(), Record);
905 Code = pch::EXPR_TYPES_COMPATIBLE;
906}
907
908void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
909 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000910 Writer.WriteSubStmt(E->getCond());
911 Writer.WriteSubStmt(E->getLHS());
912 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +0000913 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
914 Writer.AddSourceLocation(E->getRParenLoc(), Record);
915 Code = pch::EXPR_CHOOSE;
916}
917
918void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
919 VisitExpr(E);
920 Writer.AddSourceLocation(E->getTokenLocation(), Record);
921 Code = pch::EXPR_GNU_NULL;
922}
923
Douglas Gregor725e94b2009-04-16 00:01:45 +0000924void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
925 VisitExpr(E);
926 Record.push_back(E->getNumSubExprs());
927 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000928 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +0000929 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
930 Writer.AddSourceLocation(E->getRParenLoc(), Record);
931 Code = pch::EXPR_SHUFFLE_VECTOR;
932}
933
934void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
935 VisitExpr(E);
936 Writer.AddDeclRef(E->getDecl(), Record);
937 Writer.AddSourceLocation(E->getLocation(), Record);
938 Record.push_back(E->isByRef());
939 Code = pch::EXPR_BLOCK_DECL_REF;
940}
941
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000942//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000943// PCHWriter Implementation
944//===----------------------------------------------------------------------===//
945
Douglas Gregorb5887f32009-04-10 21:16:55 +0000946/// \brief Write the target triple (e.g., i686-apple-darwin9).
947void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
948 using namespace llvm;
949 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
950 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000952 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +0000953
954 RecordData Record;
955 Record.push_back(pch::TARGET_TRIPLE);
956 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000957 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +0000958}
959
960/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000961void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
962 RecordData Record;
963 Record.push_back(LangOpts.Trigraphs);
964 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
965 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
966 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
967 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
968 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
969 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
970 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
971 Record.push_back(LangOpts.C99); // C99 Support
972 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
973 Record.push_back(LangOpts.CPlusPlus); // C++ Support
974 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
975 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
976 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
977
978 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
979 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
980 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
981
982 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
983 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
984 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
985 Record.push_back(LangOpts.LaxVectorConversions);
986 Record.push_back(LangOpts.Exceptions); // Support exception handling.
987
988 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
989 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
990 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
991
992 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
993 // by locks.
994 Record.push_back(LangOpts.Blocks); // block extension to C
995 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
996 // they are unused.
997 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
998 // (modulo the platform support).
999
1000 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1001 // signed integer arithmetic overflows.
1002
1003 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1004 // may be ripped out at any time.
1005
1006 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1007 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1008 // defined.
1009 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1010 // opposed to __DYNAMIC__).
1011 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1012
1013 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1014 // used (instead of C99 semantics).
1015 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1016 Record.push_back(LangOpts.getGCMode());
1017 Record.push_back(LangOpts.getVisibilityMode());
1018 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001019 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001020}
1021
Douglas Gregorab1cef72009-04-10 03:52:48 +00001022//===----------------------------------------------------------------------===//
1023// Source Manager Serialization
1024//===----------------------------------------------------------------------===//
1025
1026/// \brief Create an abbreviation for the SLocEntry that refers to a
1027/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001028static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001029 using namespace llvm;
1030 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1031 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001037 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001038}
1039
1040/// \brief Create an abbreviation for the SLocEntry that refers to a
1041/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001042static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001043 using namespace llvm;
1044 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1045 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001051 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001052}
1053
1054/// \brief Create an abbreviation for the SLocEntry that refers to a
1055/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001056static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001057 using namespace llvm;
1058 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1059 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001061 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001062}
1063
1064/// \brief Create an abbreviation for the SLocEntry that refers to an
1065/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001066static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001067 using namespace llvm;
1068 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1069 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1072 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001075 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001076}
1077
1078/// \brief Writes the block containing the serialized form of the
1079/// source manager.
1080///
1081/// TODO: We should probably use an on-disk hash table (stored in a
1082/// blob), indexed based on the file name, so that we only create
1083/// entries for files that we actually need. In the common case (no
1084/// errors), we probably won't have to create file entries for any of
1085/// the files in the AST.
1086void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001087 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001088 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001089
1090 // Abbreviations for the various kinds of source-location entries.
1091 int SLocFileAbbrv = -1;
1092 int SLocBufferAbbrv = -1;
1093 int SLocBufferBlobAbbrv = -1;
1094 int SLocInstantiationAbbrv = -1;
1095
1096 // Write out the source location entry table. We skip the first
1097 // entry, which is always the same dummy entry.
1098 RecordData Record;
1099 for (SourceManager::sloc_entry_iterator
1100 SLoc = SourceMgr.sloc_entry_begin() + 1,
1101 SLocEnd = SourceMgr.sloc_entry_end();
1102 SLoc != SLocEnd; ++SLoc) {
1103 // Figure out which record code to use.
1104 unsigned Code;
1105 if (SLoc->isFile()) {
1106 if (SLoc->getFile().getContentCache()->Entry)
1107 Code = pch::SM_SLOC_FILE_ENTRY;
1108 else
1109 Code = pch::SM_SLOC_BUFFER_ENTRY;
1110 } else
1111 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1112 Record.push_back(Code);
1113
1114 Record.push_back(SLoc->getOffset());
1115 if (SLoc->isFile()) {
1116 const SrcMgr::FileInfo &File = SLoc->getFile();
1117 Record.push_back(File.getIncludeLoc().getRawEncoding());
1118 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001119 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001120
1121 const SrcMgr::ContentCache *Content = File.getContentCache();
1122 if (Content->Entry) {
1123 // The source location entry is a file. The blob associated
1124 // with this entry is the file name.
1125 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001126 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1127 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001128 Content->Entry->getName(),
1129 strlen(Content->Entry->getName()));
1130 } else {
1131 // The source location entry is a buffer. The blob associated
1132 // with this entry contains the contents of the buffer.
1133 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001134 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1135 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001136 }
1137
1138 // We add one to the size so that we capture the trailing NULL
1139 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1140 // the reader side).
1141 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1142 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001143 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001144 Record.clear();
1145 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001146 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001147 Buffer->getBufferStart(),
1148 Buffer->getBufferSize() + 1);
1149 }
1150 } else {
1151 // The source location entry is an instantiation.
1152 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1153 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1154 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1155 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1156
Douglas Gregor364e5802009-04-15 18:05:10 +00001157 // Compute the token length for this macro expansion.
1158 unsigned NextOffset = SourceMgr.getNextOffset();
1159 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1160 if (++NextSLoc != SLocEnd)
1161 NextOffset = NextSLoc->getOffset();
1162 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1163
Douglas Gregorab1cef72009-04-10 03:52:48 +00001164 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001165 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1166 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001167 }
1168
1169 Record.clear();
1170 }
1171
Douglas Gregor635f97f2009-04-13 16:31:14 +00001172 // Write the line table.
1173 if (SourceMgr.hasLineTable()) {
1174 LineTableInfo &LineTable = SourceMgr.getLineTable();
1175
1176 // Emit the file names
1177 Record.push_back(LineTable.getNumFilenames());
1178 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1179 // Emit the file name
1180 const char *Filename = LineTable.getFilename(I);
1181 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1182 Record.push_back(FilenameLen);
1183 if (FilenameLen)
1184 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1185 }
1186
1187 // Emit the line entries
1188 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1189 L != LEnd; ++L) {
1190 // Emit the file ID
1191 Record.push_back(L->first);
1192
1193 // Emit the line entries
1194 Record.push_back(L->second.size());
1195 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1196 LEEnd = L->second.end();
1197 LE != LEEnd; ++LE) {
1198 Record.push_back(LE->FileOffset);
1199 Record.push_back(LE->LineNo);
1200 Record.push_back(LE->FilenameID);
1201 Record.push_back((unsigned)LE->FileKind);
1202 Record.push_back(LE->IncludeOffset);
1203 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001204 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001205 }
1206 }
1207
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001208 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001209}
1210
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001211/// \brief Writes the block containing the serialized form of the
1212/// preprocessor.
1213///
Chris Lattner850eabd2009-04-10 18:08:30 +00001214void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001215 // Enter the preprocessor block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001216 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattner84b04f12009-04-10 17:16:57 +00001217
Chris Lattner1b094952009-04-10 18:00:12 +00001218 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1219 // FIXME: use diagnostics subsystem for localization etc.
1220 if (PP.SawDateOrTime())
1221 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001222
Chris Lattner1b094952009-04-10 18:00:12 +00001223 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001224
Chris Lattner4b21c202009-04-13 01:29:17 +00001225 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1226 if (PP.getCounterValue() != 0) {
1227 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001228 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001229 Record.clear();
1230 }
1231
Chris Lattner1b094952009-04-10 18:00:12 +00001232 // Loop over all the macro definitions that are live at the end of the file,
1233 // emitting each to the PP section.
1234 // FIXME: Eventually we want to emit an index so that we can lazily load
1235 // macros.
1236 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1237 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001238 // FIXME: This emits macros in hash table order, we should do it in a stable
1239 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001240 MacroInfo *MI = I->second;
1241
1242 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1243 // been redefined by the header (in which case they are not isBuiltinMacro).
1244 if (MI->isBuiltinMacro())
1245 continue;
1246
Chris Lattner29241862009-04-11 21:15:38 +00001247 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001248 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1249 Record.push_back(MI->isUsed());
1250
1251 unsigned Code;
1252 if (MI->isObjectLike()) {
1253 Code = pch::PP_MACRO_OBJECT_LIKE;
1254 } else {
1255 Code = pch::PP_MACRO_FUNCTION_LIKE;
1256
1257 Record.push_back(MI->isC99Varargs());
1258 Record.push_back(MI->isGNUVarargs());
1259 Record.push_back(MI->getNumArgs());
1260 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1261 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001262 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001263 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001264 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001265 Record.clear();
1266
Chris Lattner850eabd2009-04-10 18:08:30 +00001267 // Emit the tokens array.
1268 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1269 // Note that we know that the preprocessor does not have any annotation
1270 // tokens in it because they are created by the parser, and thus can't be
1271 // in a macro definition.
1272 const Token &Tok = MI->getReplacementToken(TokNo);
1273
1274 Record.push_back(Tok.getLocation().getRawEncoding());
1275 Record.push_back(Tok.getLength());
1276
Chris Lattner850eabd2009-04-10 18:08:30 +00001277 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1278 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001279 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001280
1281 // FIXME: Should translate token kind to a stable encoding.
1282 Record.push_back(Tok.getKind());
1283 // FIXME: Should translate token flags to a stable encoding.
1284 Record.push_back(Tok.getFlags());
1285
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001286 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001287 Record.clear();
1288 }
Chris Lattner1b094952009-04-10 18:00:12 +00001289
1290 }
1291
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001292 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001293}
1294
1295
Douglas Gregorc34897d2009-04-09 22:27:44 +00001296/// \brief Write the representation of a type to the PCH stream.
1297void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001298 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001299 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001300 ID = NextTypeID++;
1301
1302 // Record the offset for this type.
1303 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001304 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001305 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1306 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001307 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001308 }
1309
1310 RecordData Record;
1311
1312 // Emit the type's representation.
1313 PCHTypeWriter W(*this, Record);
1314 switch (T->getTypeClass()) {
1315 // For all of the concrete, non-dependent types, call the
1316 // appropriate visitor function.
1317#define TYPE(Class, Base) \
1318 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1319#define ABSTRACT_TYPE(Class, Base)
1320#define DEPENDENT_TYPE(Class, Base)
1321#include "clang/AST/TypeNodes.def"
1322
1323 // For all of the dependent type nodes (which only occur in C++
1324 // templates), produce an error.
1325#define TYPE(Class, Base)
1326#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1327#include "clang/AST/TypeNodes.def"
1328 assert(false && "Cannot serialize dependent type nodes");
1329 break;
1330 }
1331
1332 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001333 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001334
1335 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001336 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001337}
1338
1339/// \brief Write a block containing all of the types.
1340void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001341 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001342 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001343
1344 // Emit all of the types in the ASTContext
1345 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1346 TEnd = Context.getTypes().end();
1347 T != TEnd; ++T) {
1348 // Builtin types are never serialized.
1349 if (isa<BuiltinType>(*T))
1350 continue;
1351
1352 WriteType(*T);
1353 }
1354
1355 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001356 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001357}
1358
1359/// \brief Write the block containing all of the declaration IDs
1360/// lexically declared within the given DeclContext.
1361///
1362/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1363/// bistream, or 0 if no block was written.
1364uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1365 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001366 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001367 return 0;
1368
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001369 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001370 RecordData Record;
1371 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1372 DEnd = DC->decls_end(Context);
1373 D != DEnd; ++D)
1374 AddDeclRef(*D, Record);
1375
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001376 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001377 return Offset;
1378}
1379
1380/// \brief Write the block containing all of the declaration IDs
1381/// visible from the given DeclContext.
1382///
1383/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1384/// bistream, or 0 if no block was written.
1385uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1386 DeclContext *DC) {
1387 if (DC->getPrimaryContext() != DC)
1388 return 0;
1389
1390 // Force the DeclContext to build a its name-lookup table.
1391 DC->lookup(Context, DeclarationName());
1392
1393 // Serialize the contents of the mapping used for lookup. Note that,
1394 // although we have two very different code paths, the serialized
1395 // representation is the same for both cases: a declaration name,
1396 // followed by a size, followed by references to the visible
1397 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001398 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001399 RecordData Record;
1400 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001401 if (!Map)
1402 return 0;
1403
Douglas Gregorc34897d2009-04-09 22:27:44 +00001404 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1405 D != DEnd; ++D) {
1406 AddDeclarationName(D->first, Record);
1407 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1408 Record.push_back(Result.second - Result.first);
1409 for(; Result.first != Result.second; ++Result.first)
1410 AddDeclRef(*Result.first, Record);
1411 }
1412
1413 if (Record.size() == 0)
1414 return 0;
1415
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001416 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001417 return Offset;
1418}
1419
1420/// \brief Write a block containing all of the declarations.
1421void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001422 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001423 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001424
1425 // Emit all of the declarations.
1426 RecordData Record;
1427 PCHDeclWriter W(*this, Record);
1428 while (!DeclsToEmit.empty()) {
1429 // Pull the next declaration off the queue
1430 Decl *D = DeclsToEmit.front();
1431 DeclsToEmit.pop();
1432
1433 // If this declaration is also a DeclContext, write blocks for the
1434 // declarations that lexically stored inside its context and those
1435 // declarations that are visible from its context. These blocks
1436 // are written before the declaration itself so that we can put
1437 // their offsets into the record for the declaration.
1438 uint64_t LexicalOffset = 0;
1439 uint64_t VisibleOffset = 0;
1440 DeclContext *DC = dyn_cast<DeclContext>(D);
1441 if (DC) {
1442 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1443 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1444 }
1445
1446 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001447 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001448 if (ID == 0)
1449 ID = DeclIDs.size();
1450
1451 unsigned Index = ID - 1;
1452
1453 // Record the offset for this declaration
1454 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001455 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001456 else if (DeclOffsets.size() < Index) {
1457 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001458 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001459 }
1460
1461 // Build and emit a record for this declaration
1462 Record.clear();
1463 W.Code = (pch::DeclCode)0;
1464 W.Visit(D);
1465 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001466 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001467 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001468
Douglas Gregor1c507882009-04-15 21:30:51 +00001469 // If the declaration had any attributes, write them now.
1470 if (D->hasAttrs())
1471 WriteAttributeRecord(D->getAttrs());
1472
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001473 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001474 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001475
Douglas Gregor631f6c62009-04-14 00:24:19 +00001476 // Note external declarations so that we can add them to a record
1477 // in the PCH file later.
1478 if (isa<FileScopeAsmDecl>(D))
1479 ExternalDefinitions.push_back(ID);
1480 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1481 if (// Non-static file-scope variables with initializers or that
1482 // are tentative definitions.
1483 (Var->isFileVarDecl() &&
1484 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1485 // Out-of-line definitions of static data members (C++).
1486 (Var->getDeclContext()->isRecord() &&
1487 !Var->getLexicalDeclContext()->isRecord() &&
1488 Var->getStorageClass() == VarDecl::Static))
1489 ExternalDefinitions.push_back(ID);
1490 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1491 if (Func->isThisDeclarationADefinition() &&
1492 Func->getStorageClass() != FunctionDecl::Static &&
1493 !Func->isInline())
1494 ExternalDefinitions.push_back(ID);
1495 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001496 }
1497
1498 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001499 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001500}
1501
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001502/// \brief Write the identifier table into the PCH file.
1503///
1504/// The identifier table consists of a blob containing string data
1505/// (the actual identifiers themselves) and a separate "offsets" index
1506/// that maps identifier IDs to locations within the blob.
1507void PCHWriter::WriteIdentifierTable() {
1508 using namespace llvm;
1509
1510 // Create and write out the blob that contains the identifier
1511 // strings.
1512 RecordData IdentOffsets;
1513 IdentOffsets.resize(IdentifierIDs.size());
1514 {
1515 // Create the identifier string data.
1516 std::vector<char> Data;
1517 Data.push_back(0); // Data must not be empty.
1518 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1519 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1520 ID != IDEnd; ++ID) {
1521 assert(ID->first && "NULL identifier in identifier table");
1522
1523 // Make sure we're starting on an odd byte. The PCH reader
1524 // expects the low bit to be set on all of the offsets.
1525 if ((Data.size() & 0x01) == 0)
1526 Data.push_back((char)0);
1527
1528 IdentOffsets[ID->second - 1] = Data.size();
1529 Data.insert(Data.end(),
1530 ID->first->getName(),
1531 ID->first->getName() + ID->first->getLength());
1532 Data.push_back((char)0);
1533 }
1534
1535 // Create a blob abbreviation
1536 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1537 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001539 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001540
1541 // Write the identifier table
1542 RecordData Record;
1543 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001544 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001545 }
1546
1547 // Write the offsets table for identifier IDs.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001548 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001549}
1550
Douglas Gregor1c507882009-04-15 21:30:51 +00001551/// \brief Write a record containing the given attributes.
1552void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1553 RecordData Record;
1554 for (; Attr; Attr = Attr->getNext()) {
1555 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1556 Record.push_back(Attr->isInherited());
1557 switch (Attr->getKind()) {
1558 case Attr::Alias:
1559 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1560 break;
1561
1562 case Attr::Aligned:
1563 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1564 break;
1565
1566 case Attr::AlwaysInline:
1567 break;
1568
1569 case Attr::AnalyzerNoReturn:
1570 break;
1571
1572 case Attr::Annotate:
1573 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1574 break;
1575
1576 case Attr::AsmLabel:
1577 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1578 break;
1579
1580 case Attr::Blocks:
1581 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1582 break;
1583
1584 case Attr::Cleanup:
1585 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1586 break;
1587
1588 case Attr::Const:
1589 break;
1590
1591 case Attr::Constructor:
1592 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1593 break;
1594
1595 case Attr::DLLExport:
1596 case Attr::DLLImport:
1597 case Attr::Deprecated:
1598 break;
1599
1600 case Attr::Destructor:
1601 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1602 break;
1603
1604 case Attr::FastCall:
1605 break;
1606
1607 case Attr::Format: {
1608 const FormatAttr *Format = cast<FormatAttr>(Attr);
1609 AddString(Format->getType(), Record);
1610 Record.push_back(Format->getFormatIdx());
1611 Record.push_back(Format->getFirstArg());
1612 break;
1613 }
1614
1615 case Attr::GNUCInline:
1616 case Attr::IBOutletKind:
1617 case Attr::NoReturn:
1618 case Attr::NoThrow:
1619 case Attr::Nodebug:
1620 case Attr::Noinline:
1621 break;
1622
1623 case Attr::NonNull: {
1624 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1625 Record.push_back(NonNull->size());
1626 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1627 break;
1628 }
1629
1630 case Attr::ObjCException:
1631 case Attr::ObjCNSObject:
1632 case Attr::Overloadable:
1633 break;
1634
1635 case Attr::Packed:
1636 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1637 break;
1638
1639 case Attr::Pure:
1640 break;
1641
1642 case Attr::Regparm:
1643 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1644 break;
1645
1646 case Attr::Section:
1647 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1648 break;
1649
1650 case Attr::StdCall:
1651 case Attr::TransparentUnion:
1652 case Attr::Unavailable:
1653 case Attr::Unused:
1654 case Attr::Used:
1655 break;
1656
1657 case Attr::Visibility:
1658 // FIXME: stable encoding
1659 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1660 break;
1661
1662 case Attr::WarnUnusedResult:
1663 case Attr::Weak:
1664 case Attr::WeakImport:
1665 break;
1666 }
1667 }
1668
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001669 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001670}
1671
1672void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1673 Record.push_back(Str.size());
1674 Record.insert(Record.end(), Str.begin(), Str.end());
1675}
1676
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001677PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1678 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001679
Chris Lattner850eabd2009-04-10 18:08:30 +00001680void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001681 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001682 Stream.Emit((unsigned)'C', 8);
1683 Stream.Emit((unsigned)'P', 8);
1684 Stream.Emit((unsigned)'C', 8);
1685 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001686
1687 // The translation unit is the first declaration we'll emit.
1688 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1689 DeclsToEmit.push(Context.getTranslationUnitDecl());
1690
1691 // Write the remaining PCH contents.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001692 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001693 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001694 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001695 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001696 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001697 WriteTypesBlock(Context);
1698 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001699 WriteIdentifierTable();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001700 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1701 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001702 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001703 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
1704 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001705}
1706
1707void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1708 Record.push_back(Loc.getRawEncoding());
1709}
1710
1711void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1712 Record.push_back(Value.getBitWidth());
1713 unsigned N = Value.getNumWords();
1714 const uint64_t* Words = Value.getRawData();
1715 for (unsigned I = 0; I != N; ++I)
1716 Record.push_back(Words[I]);
1717}
1718
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001719void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1720 Record.push_back(Value.isUnsigned());
1721 AddAPInt(Value, Record);
1722}
1723
Douglas Gregore2f37202009-04-14 21:55:33 +00001724void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1725 AddAPInt(Value.bitcastToAPInt(), Record);
1726}
1727
Douglas Gregorc34897d2009-04-09 22:27:44 +00001728void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001729 if (II == 0) {
1730 Record.push_back(0);
1731 return;
1732 }
1733
1734 pch::IdentID &ID = IdentifierIDs[II];
1735 if (ID == 0)
1736 ID = IdentifierIDs.size();
1737
1738 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001739}
1740
1741void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1742 if (T.isNull()) {
1743 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1744 return;
1745 }
1746
1747 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001748 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001749 switch (BT->getKind()) {
1750 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1751 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1752 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1753 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1754 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1755 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1756 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1757 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1758 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1759 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1760 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1761 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1762 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1763 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1764 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1765 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1766 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1767 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1768 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1769 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1770 }
1771
1772 Record.push_back((ID << 3) | T.getCVRQualifiers());
1773 return;
1774 }
1775
Douglas Gregorac8f2802009-04-10 17:25:41 +00001776 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001777 if (ID == 0) // we haven't seen this type before
1778 ID = NextTypeID++;
1779
1780 // Encode the type qualifiers in the type reference.
1781 Record.push_back((ID << 3) | T.getCVRQualifiers());
1782}
1783
1784void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1785 if (D == 0) {
1786 Record.push_back(0);
1787 return;
1788 }
1789
Douglas Gregorac8f2802009-04-10 17:25:41 +00001790 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001791 if (ID == 0) {
1792 // We haven't seen this declaration before. Give it a new ID and
1793 // enqueue it in the list of declarations to emit.
1794 ID = DeclIDs.size();
1795 DeclsToEmit.push(const_cast<Decl *>(D));
1796 }
1797
1798 Record.push_back(ID);
1799}
1800
1801void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1802 Record.push_back(Name.getNameKind());
1803 switch (Name.getNameKind()) {
1804 case DeclarationName::Identifier:
1805 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1806 break;
1807
1808 case DeclarationName::ObjCZeroArgSelector:
1809 case DeclarationName::ObjCOneArgSelector:
1810 case DeclarationName::ObjCMultiArgSelector:
1811 assert(false && "Serialization of Objective-C selectors unavailable");
1812 break;
1813
1814 case DeclarationName::CXXConstructorName:
1815 case DeclarationName::CXXDestructorName:
1816 case DeclarationName::CXXConversionFunctionName:
1817 AddTypeRef(Name.getCXXNameType(), Record);
1818 break;
1819
1820 case DeclarationName::CXXOperatorName:
1821 Record.push_back(Name.getCXXOverloadedOperator());
1822 break;
1823
1824 case DeclarationName::CXXUsingDirective:
1825 // No extra data to emit
1826 break;
1827 }
1828}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001829
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001830/// \brief Write the given substatement or subexpression to the
1831/// bitstream.
1832void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00001833 RecordData Record;
1834 PCHStmtWriter Writer(*this, Record);
1835
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001836 if (!S) {
1837 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001838 return;
1839 }
1840
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001841 Writer.Code = pch::STMT_NULL_PTR;
1842 Writer.Visit(S);
1843 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00001844 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001845 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001846}
1847
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001848/// \brief Flush all of the statements that have been added to the
1849/// queue via AddStmt().
1850void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001851 RecordData Record;
1852 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001853
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001854 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1855 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00001856
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001857 if (!S) {
1858 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001859 continue;
1860 }
1861
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001862 Writer.Code = pch::STMT_NULL_PTR;
1863 Writer.Visit(S);
1864 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001865 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001866 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001867
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001868 assert(N == StmtsToEmit.size() &&
1869 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00001870
1871 // Note that we are at the end of a full expression. Any
1872 // expression records that follow this one are part of a different
1873 // expression.
1874 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001875 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001876 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001877
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001878 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00001879 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001880}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00001881
1882unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1883 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1884 "SwitchCase recorded twice");
1885 unsigned NextID = SwitchCaseIDs.size();
1886 SwitchCaseIDs[S] = NextID;
1887 return NextID;
1888}
1889
1890unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1891 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1892 "SwitchCase hasn't been seen yet");
1893 return SwitchCaseIDs[S];
1894}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00001895
1896/// \brief Retrieve the ID for the given label statement, which may
1897/// or may not have been emitted yet.
1898unsigned PCHWriter::GetLabelID(LabelStmt *S) {
1899 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
1900 if (Pos != LabelIDs.end())
1901 return Pos->second;
1902
1903 unsigned NextID = LabelIDs.size();
1904 LabelIDs[S] = NextID;
1905 return NextID;
1906}