blob: 8f09030ac8007d9822c14d28f25301264f3579e8 [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);
454 void VisitIfStmt(IfStmt *S);
455 void VisitSwitchStmt(SwitchStmt *S);
456 void VisitBreakStmt(BreakStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000457 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000458 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000459 void VisitDeclRefExpr(DeclRefExpr *E);
460 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000461 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000462 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000463 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000464 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000465 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000466 void VisitUnaryOperator(UnaryOperator *E);
467 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000468 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000469 void VisitCallExpr(CallExpr *E);
470 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000471 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000472 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000473 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
474 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000475 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000476 void VisitExplicitCastExpr(ExplicitCastExpr *E);
477 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000478 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000479 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000480 void VisitInitListExpr(InitListExpr *E);
481 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
482 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000483 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000484 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
485 void VisitChooseExpr(ChooseExpr *E);
486 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000487 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
488 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000489 };
490}
491
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000492void PCHStmtWriter::VisitStmt(Stmt *S) {
493}
494
495void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
496 VisitStmt(S);
497 Writer.AddSourceLocation(S->getSemiLoc(), Record);
498 Code = pch::STMT_NULL;
499}
500
501void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
502 VisitStmt(S);
503 Record.push_back(S->size());
504 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
505 CS != CSEnd; ++CS)
506 Writer.WriteSubStmt(*CS);
507 Writer.AddSourceLocation(S->getLBracLoc(), Record);
508 Writer.AddSourceLocation(S->getRBracLoc(), Record);
509 Code = pch::STMT_COMPOUND;
510}
511
512void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
513 VisitStmt(S);
514 Record.push_back(Writer.RecordSwitchCaseID(S));
515}
516
517void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
518 VisitSwitchCase(S);
519 Writer.WriteSubStmt(S->getLHS());
520 Writer.WriteSubStmt(S->getRHS());
521 Writer.WriteSubStmt(S->getSubStmt());
522 Writer.AddSourceLocation(S->getCaseLoc(), Record);
523 Code = pch::STMT_CASE;
524}
525
526void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
527 VisitSwitchCase(S);
528 Writer.WriteSubStmt(S->getSubStmt());
529 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
530 Code = pch::STMT_DEFAULT;
531}
532
533void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
534 VisitStmt(S);
535 Writer.WriteSubStmt(S->getCond());
536 Writer.WriteSubStmt(S->getThen());
537 Writer.WriteSubStmt(S->getElse());
538 Writer.AddSourceLocation(S->getIfLoc(), Record);
539 Code = pch::STMT_IF;
540}
541
542void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
543 VisitStmt(S);
544 Writer.WriteSubStmt(S->getCond());
545 Writer.WriteSubStmt(S->getBody());
546 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
547 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
548 SC = SC->getNextSwitchCase())
549 Record.push_back(Writer.getSwitchCaseID(SC));
550 Code = pch::STMT_SWITCH;
551}
552
553void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
554 VisitStmt(S);
555 Writer.AddSourceLocation(S->getBreakLoc(), Record);
556 Code = pch::STMT_BREAK;
557}
558
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000559void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000560 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000561 Writer.AddTypeRef(E->getType(), Record);
562 Record.push_back(E->isTypeDependent());
563 Record.push_back(E->isValueDependent());
564}
565
Douglas Gregore2f37202009-04-14 21:55:33 +0000566void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
567 VisitExpr(E);
568 Writer.AddSourceLocation(E->getLocation(), Record);
569 Record.push_back(E->getIdentType()); // FIXME: stable encoding
570 Code = pch::EXPR_PREDEFINED;
571}
572
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000573void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
574 VisitExpr(E);
575 Writer.AddDeclRef(E->getDecl(), Record);
576 Writer.AddSourceLocation(E->getLocation(), Record);
577 Code = pch::EXPR_DECL_REF;
578}
579
580void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
581 VisitExpr(E);
582 Writer.AddSourceLocation(E->getLocation(), Record);
583 Writer.AddAPInt(E->getValue(), Record);
584 Code = pch::EXPR_INTEGER_LITERAL;
585}
586
Douglas Gregore2f37202009-04-14 21:55:33 +0000587void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
588 VisitExpr(E);
589 Writer.AddAPFloat(E->getValue(), Record);
590 Record.push_back(E->isExact());
591 Writer.AddSourceLocation(E->getLocation(), Record);
592 Code = pch::EXPR_FLOATING_LITERAL;
593}
594
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000595void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
596 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000597 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000598 Code = pch::EXPR_IMAGINARY_LITERAL;
599}
600
Douglas Gregor596e0932009-04-15 16:35:07 +0000601void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
602 VisitExpr(E);
603 Record.push_back(E->getByteLength());
604 Record.push_back(E->getNumConcatenated());
605 Record.push_back(E->isWide());
606 // FIXME: String data should be stored as a blob at the end of the
607 // StringLiteral. However, we can't do so now because we have no
608 // provision for coping with abbreviations when we're jumping around
609 // the PCH file during deserialization.
610 Record.insert(Record.end(),
611 E->getStrData(), E->getStrData() + E->getByteLength());
612 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
613 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
614 Code = pch::EXPR_STRING_LITERAL;
615}
616
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000617void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
618 VisitExpr(E);
619 Record.push_back(E->getValue());
620 Writer.AddSourceLocation(E->getLoc(), Record);
621 Record.push_back(E->isWide());
622 Code = pch::EXPR_CHARACTER_LITERAL;
623}
624
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000625void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
626 VisitExpr(E);
627 Writer.AddSourceLocation(E->getLParen(), Record);
628 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000629 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000630 Code = pch::EXPR_PAREN;
631}
632
Douglas Gregor12d74052009-04-15 15:58:59 +0000633void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
634 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000635 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000636 Record.push_back(E->getOpcode()); // FIXME: stable encoding
637 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
638 Code = pch::EXPR_UNARY_OPERATOR;
639}
640
641void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
642 VisitExpr(E);
643 Record.push_back(E->isSizeOf());
644 if (E->isArgumentType())
645 Writer.AddTypeRef(E->getArgumentType(), Record);
646 else {
647 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000648 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000649 }
650 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
651 Writer.AddSourceLocation(E->getRParenLoc(), Record);
652 Code = pch::EXPR_SIZEOF_ALIGN_OF;
653}
654
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000655void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
656 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000657 Writer.WriteSubStmt(E->getLHS());
658 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000659 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
660 Code = pch::EXPR_ARRAY_SUBSCRIPT;
661}
662
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000663void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
664 VisitExpr(E);
665 Record.push_back(E->getNumArgs());
666 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000667 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000668 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
669 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000670 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000671 Code = pch::EXPR_CALL;
672}
673
674void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
675 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000676 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000677 Writer.AddDeclRef(E->getMemberDecl(), Record);
678 Writer.AddSourceLocation(E->getMemberLoc(), Record);
679 Record.push_back(E->isArrow());
680 Code = pch::EXPR_MEMBER;
681}
682
Douglas Gregora151ba42009-04-14 23:32:43 +0000683void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
684 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000685 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000686}
687
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000688void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
689 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000690 Writer.WriteSubStmt(E->getLHS());
691 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000692 Record.push_back(E->getOpcode()); // FIXME: stable encoding
693 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
694 Code = pch::EXPR_BINARY_OPERATOR;
695}
696
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000697void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
698 VisitBinaryOperator(E);
699 Writer.AddTypeRef(E->getComputationLHSType(), Record);
700 Writer.AddTypeRef(E->getComputationResultType(), Record);
701 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
702}
703
704void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
705 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000706 Writer.WriteSubStmt(E->getCond());
707 Writer.WriteSubStmt(E->getLHS());
708 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000709 Code = pch::EXPR_CONDITIONAL_OPERATOR;
710}
711
Douglas Gregora151ba42009-04-14 23:32:43 +0000712void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
713 VisitCastExpr(E);
714 Record.push_back(E->isLvalueCast());
715 Code = pch::EXPR_IMPLICIT_CAST;
716}
717
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000718void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
719 VisitCastExpr(E);
720 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
721}
722
723void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
724 VisitExplicitCastExpr(E);
725 Writer.AddSourceLocation(E->getLParenLoc(), Record);
726 Writer.AddSourceLocation(E->getRParenLoc(), Record);
727 Code = pch::EXPR_CSTYLE_CAST;
728}
729
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000730void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
731 VisitExpr(E);
732 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000733 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000734 Record.push_back(E->isFileScope());
735 Code = pch::EXPR_COMPOUND_LITERAL;
736}
737
Douglas Gregorec0b8292009-04-15 23:02:49 +0000738void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
739 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000740 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000741 Writer.AddIdentifierRef(&E->getAccessor(), Record);
742 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
743 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
744}
745
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000746void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
747 VisitExpr(E);
748 Record.push_back(E->getNumInits());
749 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000750 Writer.WriteSubStmt(E->getInit(I));
751 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000752 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
753 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
754 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
755 Record.push_back(E->hadArrayRangeDesignator());
756 Code = pch::EXPR_INIT_LIST;
757}
758
759void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
760 VisitExpr(E);
761 Record.push_back(E->getNumSubExprs());
762 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000763 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000764 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
765 Record.push_back(E->usesGNUSyntax());
766 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
767 DEnd = E->designators_end();
768 D != DEnd; ++D) {
769 if (D->isFieldDesignator()) {
770 if (FieldDecl *Field = D->getField()) {
771 Record.push_back(pch::DESIG_FIELD_DECL);
772 Writer.AddDeclRef(Field, Record);
773 } else {
774 Record.push_back(pch::DESIG_FIELD_NAME);
775 Writer.AddIdentifierRef(D->getFieldName(), Record);
776 }
777 Writer.AddSourceLocation(D->getDotLoc(), Record);
778 Writer.AddSourceLocation(D->getFieldLoc(), Record);
779 } else if (D->isArrayDesignator()) {
780 Record.push_back(pch::DESIG_ARRAY);
781 Record.push_back(D->getFirstExprIndex());
782 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
783 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
784 } else {
785 assert(D->isArrayRangeDesignator() && "Unknown designator");
786 Record.push_back(pch::DESIG_ARRAY_RANGE);
787 Record.push_back(D->getFirstExprIndex());
788 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
789 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
790 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
791 }
792 }
793 Code = pch::EXPR_DESIGNATED_INIT;
794}
795
796void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
797 VisitExpr(E);
798 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
799}
800
Douglas Gregorec0b8292009-04-15 23:02:49 +0000801void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
802 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000803 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000804 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
805 Writer.AddSourceLocation(E->getRParenLoc(), Record);
806 Code = pch::EXPR_VA_ARG;
807}
808
Douglas Gregor209d4622009-04-15 23:33:31 +0000809void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
810 VisitExpr(E);
811 Writer.AddTypeRef(E->getArgType1(), Record);
812 Writer.AddTypeRef(E->getArgType2(), Record);
813 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
814 Writer.AddSourceLocation(E->getRParenLoc(), Record);
815 Code = pch::EXPR_TYPES_COMPATIBLE;
816}
817
818void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
819 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000820 Writer.WriteSubStmt(E->getCond());
821 Writer.WriteSubStmt(E->getLHS());
822 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +0000823 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
824 Writer.AddSourceLocation(E->getRParenLoc(), Record);
825 Code = pch::EXPR_CHOOSE;
826}
827
828void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
829 VisitExpr(E);
830 Writer.AddSourceLocation(E->getTokenLocation(), Record);
831 Code = pch::EXPR_GNU_NULL;
832}
833
Douglas Gregor725e94b2009-04-16 00:01:45 +0000834void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
835 VisitExpr(E);
836 Record.push_back(E->getNumSubExprs());
837 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000838 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +0000839 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
840 Writer.AddSourceLocation(E->getRParenLoc(), Record);
841 Code = pch::EXPR_SHUFFLE_VECTOR;
842}
843
844void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
845 VisitExpr(E);
846 Writer.AddDeclRef(E->getDecl(), Record);
847 Writer.AddSourceLocation(E->getLocation(), Record);
848 Record.push_back(E->isByRef());
849 Code = pch::EXPR_BLOCK_DECL_REF;
850}
851
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000852//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000853// PCHWriter Implementation
854//===----------------------------------------------------------------------===//
855
Douglas Gregorb5887f32009-04-10 21:16:55 +0000856/// \brief Write the target triple (e.g., i686-apple-darwin9).
857void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
858 using namespace llvm;
859 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
860 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
861 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000862 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +0000863
864 RecordData Record;
865 Record.push_back(pch::TARGET_TRIPLE);
866 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000867 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +0000868}
869
870/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000871void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
872 RecordData Record;
873 Record.push_back(LangOpts.Trigraphs);
874 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
875 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
876 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
877 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
878 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
879 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
880 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
881 Record.push_back(LangOpts.C99); // C99 Support
882 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
883 Record.push_back(LangOpts.CPlusPlus); // C++ Support
884 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
885 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
886 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
887
888 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
889 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
890 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
891
892 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
893 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
894 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
895 Record.push_back(LangOpts.LaxVectorConversions);
896 Record.push_back(LangOpts.Exceptions); // Support exception handling.
897
898 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
899 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
900 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
901
902 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
903 // by locks.
904 Record.push_back(LangOpts.Blocks); // block extension to C
905 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
906 // they are unused.
907 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
908 // (modulo the platform support).
909
910 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
911 // signed integer arithmetic overflows.
912
913 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
914 // may be ripped out at any time.
915
916 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
917 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
918 // defined.
919 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
920 // opposed to __DYNAMIC__).
921 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
922
923 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
924 // used (instead of C99 semantics).
925 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
926 Record.push_back(LangOpts.getGCMode());
927 Record.push_back(LangOpts.getVisibilityMode());
928 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000929 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000930}
931
Douglas Gregorab1cef72009-04-10 03:52:48 +0000932//===----------------------------------------------------------------------===//
933// Source Manager Serialization
934//===----------------------------------------------------------------------===//
935
936/// \brief Create an abbreviation for the SLocEntry that refers to a
937/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000938static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000939 using namespace llvm;
940 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
941 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +0000946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000947 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000948}
949
950/// \brief Create an abbreviation for the SLocEntry that refers to a
951/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000952static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000953 using namespace llvm;
954 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
955 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000961 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000962}
963
964/// \brief Create an abbreviation for the SLocEntry that refers to a
965/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000966static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000967 using namespace llvm;
968 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
969 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000971 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000972}
973
974/// \brief Create an abbreviation for the SLocEntry that refers to an
975/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000976static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000977 using namespace llvm;
978 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
979 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
981 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +0000984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000985 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000986}
987
988/// \brief Writes the block containing the serialized form of the
989/// source manager.
990///
991/// TODO: We should probably use an on-disk hash table (stored in a
992/// blob), indexed based on the file name, so that we only create
993/// entries for files that we actually need. In the common case (no
994/// errors), we probably won't have to create file entries for any of
995/// the files in the AST.
996void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +0000997 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000998 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000999
1000 // Abbreviations for the various kinds of source-location entries.
1001 int SLocFileAbbrv = -1;
1002 int SLocBufferAbbrv = -1;
1003 int SLocBufferBlobAbbrv = -1;
1004 int SLocInstantiationAbbrv = -1;
1005
1006 // Write out the source location entry table. We skip the first
1007 // entry, which is always the same dummy entry.
1008 RecordData Record;
1009 for (SourceManager::sloc_entry_iterator
1010 SLoc = SourceMgr.sloc_entry_begin() + 1,
1011 SLocEnd = SourceMgr.sloc_entry_end();
1012 SLoc != SLocEnd; ++SLoc) {
1013 // Figure out which record code to use.
1014 unsigned Code;
1015 if (SLoc->isFile()) {
1016 if (SLoc->getFile().getContentCache()->Entry)
1017 Code = pch::SM_SLOC_FILE_ENTRY;
1018 else
1019 Code = pch::SM_SLOC_BUFFER_ENTRY;
1020 } else
1021 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1022 Record.push_back(Code);
1023
1024 Record.push_back(SLoc->getOffset());
1025 if (SLoc->isFile()) {
1026 const SrcMgr::FileInfo &File = SLoc->getFile();
1027 Record.push_back(File.getIncludeLoc().getRawEncoding());
1028 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001029 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001030
1031 const SrcMgr::ContentCache *Content = File.getContentCache();
1032 if (Content->Entry) {
1033 // The source location entry is a file. The blob associated
1034 // with this entry is the file name.
1035 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001036 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1037 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001038 Content->Entry->getName(),
1039 strlen(Content->Entry->getName()));
1040 } else {
1041 // The source location entry is a buffer. The blob associated
1042 // with this entry contains the contents of the buffer.
1043 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001044 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1045 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001046 }
1047
1048 // We add one to the size so that we capture the trailing NULL
1049 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1050 // the reader side).
1051 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1052 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001053 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001054 Record.clear();
1055 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001056 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001057 Buffer->getBufferStart(),
1058 Buffer->getBufferSize() + 1);
1059 }
1060 } else {
1061 // The source location entry is an instantiation.
1062 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1063 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1064 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1065 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1066
Douglas Gregor364e5802009-04-15 18:05:10 +00001067 // Compute the token length for this macro expansion.
1068 unsigned NextOffset = SourceMgr.getNextOffset();
1069 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1070 if (++NextSLoc != SLocEnd)
1071 NextOffset = NextSLoc->getOffset();
1072 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1073
Douglas Gregorab1cef72009-04-10 03:52:48 +00001074 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001075 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1076 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001077 }
1078
1079 Record.clear();
1080 }
1081
Douglas Gregor635f97f2009-04-13 16:31:14 +00001082 // Write the line table.
1083 if (SourceMgr.hasLineTable()) {
1084 LineTableInfo &LineTable = SourceMgr.getLineTable();
1085
1086 // Emit the file names
1087 Record.push_back(LineTable.getNumFilenames());
1088 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1089 // Emit the file name
1090 const char *Filename = LineTable.getFilename(I);
1091 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1092 Record.push_back(FilenameLen);
1093 if (FilenameLen)
1094 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1095 }
1096
1097 // Emit the line entries
1098 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1099 L != LEnd; ++L) {
1100 // Emit the file ID
1101 Record.push_back(L->first);
1102
1103 // Emit the line entries
1104 Record.push_back(L->second.size());
1105 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1106 LEEnd = L->second.end();
1107 LE != LEEnd; ++LE) {
1108 Record.push_back(LE->FileOffset);
1109 Record.push_back(LE->LineNo);
1110 Record.push_back(LE->FilenameID);
1111 Record.push_back((unsigned)LE->FileKind);
1112 Record.push_back(LE->IncludeOffset);
1113 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001114 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001115 }
1116 }
1117
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001118 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001119}
1120
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001121/// \brief Writes the block containing the serialized form of the
1122/// preprocessor.
1123///
Chris Lattner850eabd2009-04-10 18:08:30 +00001124void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001125 // Enter the preprocessor block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001126 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattner84b04f12009-04-10 17:16:57 +00001127
Chris Lattner1b094952009-04-10 18:00:12 +00001128 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1129 // FIXME: use diagnostics subsystem for localization etc.
1130 if (PP.SawDateOrTime())
1131 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001132
Chris Lattner1b094952009-04-10 18:00:12 +00001133 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001134
Chris Lattner4b21c202009-04-13 01:29:17 +00001135 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1136 if (PP.getCounterValue() != 0) {
1137 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001138 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001139 Record.clear();
1140 }
1141
Chris Lattner1b094952009-04-10 18:00:12 +00001142 // Loop over all the macro definitions that are live at the end of the file,
1143 // emitting each to the PP section.
1144 // FIXME: Eventually we want to emit an index so that we can lazily load
1145 // macros.
1146 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1147 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001148 // FIXME: This emits macros in hash table order, we should do it in a stable
1149 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001150 MacroInfo *MI = I->second;
1151
1152 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1153 // been redefined by the header (in which case they are not isBuiltinMacro).
1154 if (MI->isBuiltinMacro())
1155 continue;
1156
Chris Lattner29241862009-04-11 21:15:38 +00001157 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001158 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1159 Record.push_back(MI->isUsed());
1160
1161 unsigned Code;
1162 if (MI->isObjectLike()) {
1163 Code = pch::PP_MACRO_OBJECT_LIKE;
1164 } else {
1165 Code = pch::PP_MACRO_FUNCTION_LIKE;
1166
1167 Record.push_back(MI->isC99Varargs());
1168 Record.push_back(MI->isGNUVarargs());
1169 Record.push_back(MI->getNumArgs());
1170 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1171 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001172 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001173 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001174 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001175 Record.clear();
1176
Chris Lattner850eabd2009-04-10 18:08:30 +00001177 // Emit the tokens array.
1178 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1179 // Note that we know that the preprocessor does not have any annotation
1180 // tokens in it because they are created by the parser, and thus can't be
1181 // in a macro definition.
1182 const Token &Tok = MI->getReplacementToken(TokNo);
1183
1184 Record.push_back(Tok.getLocation().getRawEncoding());
1185 Record.push_back(Tok.getLength());
1186
Chris Lattner850eabd2009-04-10 18:08:30 +00001187 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1188 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001189 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001190
1191 // FIXME: Should translate token kind to a stable encoding.
1192 Record.push_back(Tok.getKind());
1193 // FIXME: Should translate token flags to a stable encoding.
1194 Record.push_back(Tok.getFlags());
1195
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001196 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001197 Record.clear();
1198 }
Chris Lattner1b094952009-04-10 18:00:12 +00001199
1200 }
1201
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001202 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001203}
1204
1205
Douglas Gregorc34897d2009-04-09 22:27:44 +00001206/// \brief Write the representation of a type to the PCH stream.
1207void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001208 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001209 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001210 ID = NextTypeID++;
1211
1212 // Record the offset for this type.
1213 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001214 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001215 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1216 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001217 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001218 }
1219
1220 RecordData Record;
1221
1222 // Emit the type's representation.
1223 PCHTypeWriter W(*this, Record);
1224 switch (T->getTypeClass()) {
1225 // For all of the concrete, non-dependent types, call the
1226 // appropriate visitor function.
1227#define TYPE(Class, Base) \
1228 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1229#define ABSTRACT_TYPE(Class, Base)
1230#define DEPENDENT_TYPE(Class, Base)
1231#include "clang/AST/TypeNodes.def"
1232
1233 // For all of the dependent type nodes (which only occur in C++
1234 // templates), produce an error.
1235#define TYPE(Class, Base)
1236#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1237#include "clang/AST/TypeNodes.def"
1238 assert(false && "Cannot serialize dependent type nodes");
1239 break;
1240 }
1241
1242 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001243 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001244
1245 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001246 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001247}
1248
1249/// \brief Write a block containing all of the types.
1250void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001251 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001252 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001253
1254 // Emit all of the types in the ASTContext
1255 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1256 TEnd = Context.getTypes().end();
1257 T != TEnd; ++T) {
1258 // Builtin types are never serialized.
1259 if (isa<BuiltinType>(*T))
1260 continue;
1261
1262 WriteType(*T);
1263 }
1264
1265 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001266 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001267}
1268
1269/// \brief Write the block containing all of the declaration IDs
1270/// lexically declared within the given DeclContext.
1271///
1272/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1273/// bistream, or 0 if no block was written.
1274uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1275 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001276 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001277 return 0;
1278
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001279 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001280 RecordData Record;
1281 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1282 DEnd = DC->decls_end(Context);
1283 D != DEnd; ++D)
1284 AddDeclRef(*D, Record);
1285
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001286 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001287 return Offset;
1288}
1289
1290/// \brief Write the block containing all of the declaration IDs
1291/// visible from the given DeclContext.
1292///
1293/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1294/// bistream, or 0 if no block was written.
1295uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1296 DeclContext *DC) {
1297 if (DC->getPrimaryContext() != DC)
1298 return 0;
1299
1300 // Force the DeclContext to build a its name-lookup table.
1301 DC->lookup(Context, DeclarationName());
1302
1303 // Serialize the contents of the mapping used for lookup. Note that,
1304 // although we have two very different code paths, the serialized
1305 // representation is the same for both cases: a declaration name,
1306 // followed by a size, followed by references to the visible
1307 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001308 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001309 RecordData Record;
1310 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001311 if (!Map)
1312 return 0;
1313
Douglas Gregorc34897d2009-04-09 22:27:44 +00001314 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1315 D != DEnd; ++D) {
1316 AddDeclarationName(D->first, Record);
1317 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1318 Record.push_back(Result.second - Result.first);
1319 for(; Result.first != Result.second; ++Result.first)
1320 AddDeclRef(*Result.first, Record);
1321 }
1322
1323 if (Record.size() == 0)
1324 return 0;
1325
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001326 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001327 return Offset;
1328}
1329
1330/// \brief Write a block containing all of the declarations.
1331void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001332 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001333 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001334
1335 // Emit all of the declarations.
1336 RecordData Record;
1337 PCHDeclWriter W(*this, Record);
1338 while (!DeclsToEmit.empty()) {
1339 // Pull the next declaration off the queue
1340 Decl *D = DeclsToEmit.front();
1341 DeclsToEmit.pop();
1342
1343 // If this declaration is also a DeclContext, write blocks for the
1344 // declarations that lexically stored inside its context and those
1345 // declarations that are visible from its context. These blocks
1346 // are written before the declaration itself so that we can put
1347 // their offsets into the record for the declaration.
1348 uint64_t LexicalOffset = 0;
1349 uint64_t VisibleOffset = 0;
1350 DeclContext *DC = dyn_cast<DeclContext>(D);
1351 if (DC) {
1352 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1353 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1354 }
1355
1356 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001357 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001358 if (ID == 0)
1359 ID = DeclIDs.size();
1360
1361 unsigned Index = ID - 1;
1362
1363 // Record the offset for this declaration
1364 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001365 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001366 else if (DeclOffsets.size() < Index) {
1367 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001368 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001369 }
1370
1371 // Build and emit a record for this declaration
1372 Record.clear();
1373 W.Code = (pch::DeclCode)0;
1374 W.Visit(D);
1375 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001376 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001377 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001378
Douglas Gregor1c507882009-04-15 21:30:51 +00001379 // If the declaration had any attributes, write them now.
1380 if (D->hasAttrs())
1381 WriteAttributeRecord(D->getAttrs());
1382
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001383 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001384 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001385
Douglas Gregor631f6c62009-04-14 00:24:19 +00001386 // Note external declarations so that we can add them to a record
1387 // in the PCH file later.
1388 if (isa<FileScopeAsmDecl>(D))
1389 ExternalDefinitions.push_back(ID);
1390 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1391 if (// Non-static file-scope variables with initializers or that
1392 // are tentative definitions.
1393 (Var->isFileVarDecl() &&
1394 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1395 // Out-of-line definitions of static data members (C++).
1396 (Var->getDeclContext()->isRecord() &&
1397 !Var->getLexicalDeclContext()->isRecord() &&
1398 Var->getStorageClass() == VarDecl::Static))
1399 ExternalDefinitions.push_back(ID);
1400 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1401 if (Func->isThisDeclarationADefinition() &&
1402 Func->getStorageClass() != FunctionDecl::Static &&
1403 !Func->isInline())
1404 ExternalDefinitions.push_back(ID);
1405 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001406 }
1407
1408 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001409 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001410}
1411
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001412/// \brief Write the identifier table into the PCH file.
1413///
1414/// The identifier table consists of a blob containing string data
1415/// (the actual identifiers themselves) and a separate "offsets" index
1416/// that maps identifier IDs to locations within the blob.
1417void PCHWriter::WriteIdentifierTable() {
1418 using namespace llvm;
1419
1420 // Create and write out the blob that contains the identifier
1421 // strings.
1422 RecordData IdentOffsets;
1423 IdentOffsets.resize(IdentifierIDs.size());
1424 {
1425 // Create the identifier string data.
1426 std::vector<char> Data;
1427 Data.push_back(0); // Data must not be empty.
1428 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1429 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1430 ID != IDEnd; ++ID) {
1431 assert(ID->first && "NULL identifier in identifier table");
1432
1433 // Make sure we're starting on an odd byte. The PCH reader
1434 // expects the low bit to be set on all of the offsets.
1435 if ((Data.size() & 0x01) == 0)
1436 Data.push_back((char)0);
1437
1438 IdentOffsets[ID->second - 1] = Data.size();
1439 Data.insert(Data.end(),
1440 ID->first->getName(),
1441 ID->first->getName() + ID->first->getLength());
1442 Data.push_back((char)0);
1443 }
1444
1445 // Create a blob abbreviation
1446 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1447 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1448 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001449 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001450
1451 // Write the identifier table
1452 RecordData Record;
1453 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001454 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001455 }
1456
1457 // Write the offsets table for identifier IDs.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001458 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001459}
1460
Douglas Gregor1c507882009-04-15 21:30:51 +00001461/// \brief Write a record containing the given attributes.
1462void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1463 RecordData Record;
1464 for (; Attr; Attr = Attr->getNext()) {
1465 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1466 Record.push_back(Attr->isInherited());
1467 switch (Attr->getKind()) {
1468 case Attr::Alias:
1469 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1470 break;
1471
1472 case Attr::Aligned:
1473 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1474 break;
1475
1476 case Attr::AlwaysInline:
1477 break;
1478
1479 case Attr::AnalyzerNoReturn:
1480 break;
1481
1482 case Attr::Annotate:
1483 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1484 break;
1485
1486 case Attr::AsmLabel:
1487 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1488 break;
1489
1490 case Attr::Blocks:
1491 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1492 break;
1493
1494 case Attr::Cleanup:
1495 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1496 break;
1497
1498 case Attr::Const:
1499 break;
1500
1501 case Attr::Constructor:
1502 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1503 break;
1504
1505 case Attr::DLLExport:
1506 case Attr::DLLImport:
1507 case Attr::Deprecated:
1508 break;
1509
1510 case Attr::Destructor:
1511 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1512 break;
1513
1514 case Attr::FastCall:
1515 break;
1516
1517 case Attr::Format: {
1518 const FormatAttr *Format = cast<FormatAttr>(Attr);
1519 AddString(Format->getType(), Record);
1520 Record.push_back(Format->getFormatIdx());
1521 Record.push_back(Format->getFirstArg());
1522 break;
1523 }
1524
1525 case Attr::GNUCInline:
1526 case Attr::IBOutletKind:
1527 case Attr::NoReturn:
1528 case Attr::NoThrow:
1529 case Attr::Nodebug:
1530 case Attr::Noinline:
1531 break;
1532
1533 case Attr::NonNull: {
1534 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1535 Record.push_back(NonNull->size());
1536 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1537 break;
1538 }
1539
1540 case Attr::ObjCException:
1541 case Attr::ObjCNSObject:
1542 case Attr::Overloadable:
1543 break;
1544
1545 case Attr::Packed:
1546 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1547 break;
1548
1549 case Attr::Pure:
1550 break;
1551
1552 case Attr::Regparm:
1553 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1554 break;
1555
1556 case Attr::Section:
1557 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1558 break;
1559
1560 case Attr::StdCall:
1561 case Attr::TransparentUnion:
1562 case Attr::Unavailable:
1563 case Attr::Unused:
1564 case Attr::Used:
1565 break;
1566
1567 case Attr::Visibility:
1568 // FIXME: stable encoding
1569 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1570 break;
1571
1572 case Attr::WarnUnusedResult:
1573 case Attr::Weak:
1574 case Attr::WeakImport:
1575 break;
1576 }
1577 }
1578
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001579 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001580}
1581
1582void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1583 Record.push_back(Str.size());
1584 Record.insert(Record.end(), Str.begin(), Str.end());
1585}
1586
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001587PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1588 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001589
Chris Lattner850eabd2009-04-10 18:08:30 +00001590void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001591 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001592 Stream.Emit((unsigned)'C', 8);
1593 Stream.Emit((unsigned)'P', 8);
1594 Stream.Emit((unsigned)'C', 8);
1595 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001596
1597 // The translation unit is the first declaration we'll emit.
1598 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1599 DeclsToEmit.push(Context.getTranslationUnitDecl());
1600
1601 // Write the remaining PCH contents.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001602 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001603 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001604 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001605 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001606 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001607 WriteTypesBlock(Context);
1608 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001609 WriteIdentifierTable();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001610 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1611 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001612 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001613 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
1614 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001615}
1616
1617void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1618 Record.push_back(Loc.getRawEncoding());
1619}
1620
1621void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1622 Record.push_back(Value.getBitWidth());
1623 unsigned N = Value.getNumWords();
1624 const uint64_t* Words = Value.getRawData();
1625 for (unsigned I = 0; I != N; ++I)
1626 Record.push_back(Words[I]);
1627}
1628
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001629void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1630 Record.push_back(Value.isUnsigned());
1631 AddAPInt(Value, Record);
1632}
1633
Douglas Gregore2f37202009-04-14 21:55:33 +00001634void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1635 AddAPInt(Value.bitcastToAPInt(), Record);
1636}
1637
Douglas Gregorc34897d2009-04-09 22:27:44 +00001638void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001639 if (II == 0) {
1640 Record.push_back(0);
1641 return;
1642 }
1643
1644 pch::IdentID &ID = IdentifierIDs[II];
1645 if (ID == 0)
1646 ID = IdentifierIDs.size();
1647
1648 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001649}
1650
1651void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1652 if (T.isNull()) {
1653 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1654 return;
1655 }
1656
1657 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001658 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001659 switch (BT->getKind()) {
1660 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1661 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1662 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1663 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1664 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1665 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1666 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1667 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1668 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1669 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1670 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1671 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1672 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1673 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1674 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1675 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1676 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1677 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1678 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1679 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1680 }
1681
1682 Record.push_back((ID << 3) | T.getCVRQualifiers());
1683 return;
1684 }
1685
Douglas Gregorac8f2802009-04-10 17:25:41 +00001686 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001687 if (ID == 0) // we haven't seen this type before
1688 ID = NextTypeID++;
1689
1690 // Encode the type qualifiers in the type reference.
1691 Record.push_back((ID << 3) | T.getCVRQualifiers());
1692}
1693
1694void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1695 if (D == 0) {
1696 Record.push_back(0);
1697 return;
1698 }
1699
Douglas Gregorac8f2802009-04-10 17:25:41 +00001700 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001701 if (ID == 0) {
1702 // We haven't seen this declaration before. Give it a new ID and
1703 // enqueue it in the list of declarations to emit.
1704 ID = DeclIDs.size();
1705 DeclsToEmit.push(const_cast<Decl *>(D));
1706 }
1707
1708 Record.push_back(ID);
1709}
1710
1711void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1712 Record.push_back(Name.getNameKind());
1713 switch (Name.getNameKind()) {
1714 case DeclarationName::Identifier:
1715 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1716 break;
1717
1718 case DeclarationName::ObjCZeroArgSelector:
1719 case DeclarationName::ObjCOneArgSelector:
1720 case DeclarationName::ObjCMultiArgSelector:
1721 assert(false && "Serialization of Objective-C selectors unavailable");
1722 break;
1723
1724 case DeclarationName::CXXConstructorName:
1725 case DeclarationName::CXXDestructorName:
1726 case DeclarationName::CXXConversionFunctionName:
1727 AddTypeRef(Name.getCXXNameType(), Record);
1728 break;
1729
1730 case DeclarationName::CXXOperatorName:
1731 Record.push_back(Name.getCXXOverloadedOperator());
1732 break;
1733
1734 case DeclarationName::CXXUsingDirective:
1735 // No extra data to emit
1736 break;
1737 }
1738}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001739
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001740/// \brief Write the given substatement or subexpression to the
1741/// bitstream.
1742void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00001743 RecordData Record;
1744 PCHStmtWriter Writer(*this, Record);
1745
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001746 if (!S) {
1747 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001748 return;
1749 }
1750
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001751 Writer.Code = pch::STMT_NULL_PTR;
1752 Writer.Visit(S);
1753 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00001754 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001755 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001756}
1757
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001758/// \brief Flush all of the statements that have been added to the
1759/// queue via AddStmt().
1760void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001761 RecordData Record;
1762 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001763
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001764 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1765 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00001766
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001767 if (!S) {
1768 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001769 continue;
1770 }
1771
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001772 Writer.Code = pch::STMT_NULL_PTR;
1773 Writer.Visit(S);
1774 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001775 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001776 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001777
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001778 assert(N == StmtsToEmit.size() &&
1779 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00001780
1781 // Note that we are at the end of a full expression. Any
1782 // expression records that follow this one are part of a different
1783 // expression.
1784 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001785 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001786 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001787
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001788 StmtsToEmit.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001789}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00001790
1791unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1792 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1793 "SwitchCase recorded twice");
1794 unsigned NextID = SwitchCaseIDs.size();
1795 SwitchCaseIDs[S] = NextID;
1796 return NextID;
1797}
1798
1799unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1800 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1801 "SwitchCase hasn't been seen yet");
1802 return SwitchCaseIDs[S];
1803}