blob: 0321e4c40baeb56fa17f8e5d0606eab35ad02d96 [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 Gregorc10f86f2009-04-14 21:18:50 +0000129 Writer.AddExpr(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 Gregorc10f86f2009-04-14 21:18:50 +0000169 Writer.AddExpr(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())
334 Writer.AddExpr(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);
341 // FIXME: function body
342 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
343 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
344 Record.push_back(D->isInline());
345 Record.push_back(D->isVirtual());
346 Record.push_back(D->isPure());
347 Record.push_back(D->inheritedPrototype());
348 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
349 Record.push_back(D->isDeleted());
350 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
351 Record.push_back(D->param_size());
352 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
353 P != PEnd; ++P)
354 Writer.AddDeclRef(*P, Record);
355 Code = pch::DECL_FUNCTION;
356}
357
Douglas Gregor982365e2009-04-13 21:20:57 +0000358void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
359 VisitValueDecl(D);
360 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000361 Record.push_back(D->getBitWidth()? 1 : 0);
362 if (D->getBitWidth())
363 Writer.AddExpr(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000364 Code = pch::DECL_FIELD;
365}
366
Douglas Gregorc34897d2009-04-09 22:27:44 +0000367void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
368 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000369 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000370 Record.push_back(D->isThreadSpecified());
371 Record.push_back(D->hasCXXDirectInitializer());
372 Record.push_back(D->isDeclaredInCondition());
373 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
374 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000375 Record.push_back(D->getInit()? 1 : 0);
376 if (D->getInit())
377 Writer.AddExpr(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000378 Code = pch::DECL_VAR;
379}
380
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000381void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
382 VisitVarDecl(D);
383 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000384 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000385 // FIXME: why isn't the "default argument" just stored as the initializer
386 // in VarDecl?
387 Code = pch::DECL_PARM_VAR;
388}
389
390void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
391 VisitParmVarDecl(D);
392 Writer.AddTypeRef(D->getOriginalType(), Record);
393 Code = pch::DECL_ORIGINAL_PARM_VAR;
394}
395
Douglas Gregor2a491792009-04-13 22:49:25 +0000396void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
397 VisitDecl(D);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000398 Writer.AddExpr(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000399 Code = pch::DECL_FILE_SCOPE_ASM;
400}
401
402void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
403 VisitDecl(D);
404 // FIXME: emit block body
405 Record.push_back(D->param_size());
406 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
407 P != PEnd; ++P)
408 Writer.AddDeclRef(*P, Record);
409 Code = pch::DECL_BLOCK;
410}
411
Douglas Gregorc34897d2009-04-09 22:27:44 +0000412/// \brief Emit the DeclContext part of a declaration context decl.
413///
414/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
415/// block for this declaration context is stored. May be 0 to indicate
416/// that there are no declarations stored within this context.
417///
418/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
419/// block for this declaration context is stored. May be 0 to indicate
420/// that there are no declarations visible from this context. Note
421/// that this value will not be emitted for non-primary declaration
422/// contexts.
423void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
424 uint64_t VisibleOffset) {
425 Record.push_back(LexicalOffset);
426 if (DC->getPrimaryContext() == DC)
427 Record.push_back(VisibleOffset);
428}
429
430//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000431// Statement/expression serialization
432//===----------------------------------------------------------------------===//
433namespace {
434 class VISIBILITY_HIDDEN PCHStmtWriter
435 : public StmtVisitor<PCHStmtWriter, void> {
436
437 PCHWriter &Writer;
438 PCHWriter::RecordData &Record;
439
440 public:
441 pch::StmtCode Code;
442
443 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
444 : Writer(Writer), Record(Record) { }
445
446 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000447 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000448 void VisitDeclRefExpr(DeclRefExpr *E);
449 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000450 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000451 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000452 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000453 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000454 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000455 void VisitUnaryOperator(UnaryOperator *E);
456 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000457 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000458 void VisitCallExpr(CallExpr *E);
459 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000460 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000461 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000462 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
463 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000464 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000465 void VisitExplicitCastExpr(ExplicitCastExpr *E);
466 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000467 };
468}
469
470void PCHStmtWriter::VisitExpr(Expr *E) {
471 Writer.AddTypeRef(E->getType(), Record);
472 Record.push_back(E->isTypeDependent());
473 Record.push_back(E->isValueDependent());
474}
475
Douglas Gregore2f37202009-04-14 21:55:33 +0000476void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
477 VisitExpr(E);
478 Writer.AddSourceLocation(E->getLocation(), Record);
479 Record.push_back(E->getIdentType()); // FIXME: stable encoding
480 Code = pch::EXPR_PREDEFINED;
481}
482
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000483void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
484 VisitExpr(E);
485 Writer.AddDeclRef(E->getDecl(), Record);
486 Writer.AddSourceLocation(E->getLocation(), Record);
487 Code = pch::EXPR_DECL_REF;
488}
489
490void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
491 VisitExpr(E);
492 Writer.AddSourceLocation(E->getLocation(), Record);
493 Writer.AddAPInt(E->getValue(), Record);
494 Code = pch::EXPR_INTEGER_LITERAL;
495}
496
Douglas Gregore2f37202009-04-14 21:55:33 +0000497void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
498 VisitExpr(E);
499 Writer.AddAPFloat(E->getValue(), Record);
500 Record.push_back(E->isExact());
501 Writer.AddSourceLocation(E->getLocation(), Record);
502 Code = pch::EXPR_FLOATING_LITERAL;
503}
504
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000505void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
506 VisitExpr(E);
507 Writer.WriteSubExpr(E->getSubExpr());
508 Code = pch::EXPR_IMAGINARY_LITERAL;
509}
510
Douglas Gregor596e0932009-04-15 16:35:07 +0000511void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
512 VisitExpr(E);
513 Record.push_back(E->getByteLength());
514 Record.push_back(E->getNumConcatenated());
515 Record.push_back(E->isWide());
516 // FIXME: String data should be stored as a blob at the end of the
517 // StringLiteral. However, we can't do so now because we have no
518 // provision for coping with abbreviations when we're jumping around
519 // the PCH file during deserialization.
520 Record.insert(Record.end(),
521 E->getStrData(), E->getStrData() + E->getByteLength());
522 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
523 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
524 Code = pch::EXPR_STRING_LITERAL;
525}
526
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000527void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
528 VisitExpr(E);
529 Record.push_back(E->getValue());
530 Writer.AddSourceLocation(E->getLoc(), Record);
531 Record.push_back(E->isWide());
532 Code = pch::EXPR_CHARACTER_LITERAL;
533}
534
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000535void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
536 VisitExpr(E);
537 Writer.AddSourceLocation(E->getLParen(), Record);
538 Writer.AddSourceLocation(E->getRParen(), Record);
539 Writer.WriteSubExpr(E->getSubExpr());
540 Code = pch::EXPR_PAREN;
541}
542
Douglas Gregor12d74052009-04-15 15:58:59 +0000543void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
544 VisitExpr(E);
545 Writer.WriteSubExpr(E->getSubExpr());
546 Record.push_back(E->getOpcode()); // FIXME: stable encoding
547 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
548 Code = pch::EXPR_UNARY_OPERATOR;
549}
550
551void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
552 VisitExpr(E);
553 Record.push_back(E->isSizeOf());
554 if (E->isArgumentType())
555 Writer.AddTypeRef(E->getArgumentType(), Record);
556 else {
557 Record.push_back(0);
558 Writer.WriteSubExpr(E->getArgumentExpr());
559 }
560 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
561 Writer.AddSourceLocation(E->getRParenLoc(), Record);
562 Code = pch::EXPR_SIZEOF_ALIGN_OF;
563}
564
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000565void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
566 VisitExpr(E);
567 Writer.WriteSubExpr(E->getLHS());
568 Writer.WriteSubExpr(E->getRHS());
569 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
570 Code = pch::EXPR_ARRAY_SUBSCRIPT;
571}
572
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000573void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
574 VisitExpr(E);
575 Record.push_back(E->getNumArgs());
576 Writer.AddSourceLocation(E->getRParenLoc(), Record);
577 Writer.WriteSubExpr(E->getCallee());
578 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
579 Arg != ArgEnd; ++Arg)
580 Writer.WriteSubExpr(*Arg);
581 Code = pch::EXPR_CALL;
582}
583
584void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
585 VisitExpr(E);
586 Writer.WriteSubExpr(E->getBase());
587 Writer.AddDeclRef(E->getMemberDecl(), Record);
588 Writer.AddSourceLocation(E->getMemberLoc(), Record);
589 Record.push_back(E->isArrow());
590 Code = pch::EXPR_MEMBER;
591}
592
Douglas Gregora151ba42009-04-14 23:32:43 +0000593void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
594 VisitExpr(E);
595 Writer.WriteSubExpr(E->getSubExpr());
596}
597
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000598void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
599 VisitExpr(E);
600 Writer.WriteSubExpr(E->getLHS());
601 Writer.WriteSubExpr(E->getRHS());
602 Record.push_back(E->getOpcode()); // FIXME: stable encoding
603 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
604 Code = pch::EXPR_BINARY_OPERATOR;
605}
606
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000607void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
608 VisitBinaryOperator(E);
609 Writer.AddTypeRef(E->getComputationLHSType(), Record);
610 Writer.AddTypeRef(E->getComputationResultType(), Record);
611 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
612}
613
614void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
615 VisitExpr(E);
616 Writer.WriteSubExpr(E->getCond());
617 Writer.WriteSubExpr(E->getLHS());
618 Writer.WriteSubExpr(E->getRHS());
619 Code = pch::EXPR_CONDITIONAL_OPERATOR;
620}
621
Douglas Gregora151ba42009-04-14 23:32:43 +0000622void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
623 VisitCastExpr(E);
624 Record.push_back(E->isLvalueCast());
625 Code = pch::EXPR_IMPLICIT_CAST;
626}
627
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000628void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
629 VisitCastExpr(E);
630 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
631}
632
633void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
634 VisitExplicitCastExpr(E);
635 Writer.AddSourceLocation(E->getLParenLoc(), Record);
636 Writer.AddSourceLocation(E->getRParenLoc(), Record);
637 Code = pch::EXPR_CSTYLE_CAST;
638}
639
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000640//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000641// PCHWriter Implementation
642//===----------------------------------------------------------------------===//
643
Douglas Gregorb5887f32009-04-10 21:16:55 +0000644/// \brief Write the target triple (e.g., i686-apple-darwin9).
645void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
646 using namespace llvm;
647 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
648 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
649 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
650 unsigned TripleAbbrev = S.EmitAbbrev(Abbrev);
651
652 RecordData Record;
653 Record.push_back(pch::TARGET_TRIPLE);
654 const char *Triple = Target.getTargetTriple();
655 S.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
656}
657
658/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000659void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
660 RecordData Record;
661 Record.push_back(LangOpts.Trigraphs);
662 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
663 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
664 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
665 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
666 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
667 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
668 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
669 Record.push_back(LangOpts.C99); // C99 Support
670 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
671 Record.push_back(LangOpts.CPlusPlus); // C++ Support
672 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
673 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
674 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
675
676 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
677 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
678 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
679
680 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
681 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
682 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
683 Record.push_back(LangOpts.LaxVectorConversions);
684 Record.push_back(LangOpts.Exceptions); // Support exception handling.
685
686 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
687 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
688 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
689
690 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
691 // by locks.
692 Record.push_back(LangOpts.Blocks); // block extension to C
693 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
694 // they are unused.
695 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
696 // (modulo the platform support).
697
698 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
699 // signed integer arithmetic overflows.
700
701 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
702 // may be ripped out at any time.
703
704 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
705 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
706 // defined.
707 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
708 // opposed to __DYNAMIC__).
709 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
710
711 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
712 // used (instead of C99 semantics).
713 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
714 Record.push_back(LangOpts.getGCMode());
715 Record.push_back(LangOpts.getVisibilityMode());
716 Record.push_back(LangOpts.InstantiationDepth);
717 S.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
718}
719
Douglas Gregorab1cef72009-04-10 03:52:48 +0000720//===----------------------------------------------------------------------===//
721// Source Manager Serialization
722//===----------------------------------------------------------------------===//
723
724/// \brief Create an abbreviation for the SLocEntry that refers to a
725/// file.
726static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) {
727 using namespace llvm;
728 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
729 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
731 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +0000734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
735 return S.EmitAbbrev(Abbrev);
736}
737
738/// \brief Create an abbreviation for the SLocEntry that refers to a
739/// buffer.
740static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &S) {
741 using namespace llvm;
742 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
743 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
749 return S.EmitAbbrev(Abbrev);
750}
751
752/// \brief Create an abbreviation for the SLocEntry that refers to a
753/// buffer's blob.
754static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &S) {
755 using namespace llvm;
756 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
757 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
759 return S.EmitAbbrev(Abbrev);
760}
761
762/// \brief Create an abbreviation for the SLocEntry that refers to an
763/// buffer.
764static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &S) {
765 using namespace llvm;
766 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
767 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +0000772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorab1cef72009-04-10 03:52:48 +0000773 return S.EmitAbbrev(Abbrev);
774}
775
776/// \brief Writes the block containing the serialized form of the
777/// source manager.
778///
779/// TODO: We should probably use an on-disk hash table (stored in a
780/// blob), indexed based on the file name, so that we only create
781/// entries for files that we actually need. In the common case (no
782/// errors), we probably won't have to create file entries for any of
783/// the files in the AST.
784void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +0000785 // Enter the source manager block.
Douglas Gregorab1cef72009-04-10 03:52:48 +0000786 S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
787
788 // Abbreviations for the various kinds of source-location entries.
789 int SLocFileAbbrv = -1;
790 int SLocBufferAbbrv = -1;
791 int SLocBufferBlobAbbrv = -1;
792 int SLocInstantiationAbbrv = -1;
793
794 // Write out the source location entry table. We skip the first
795 // entry, which is always the same dummy entry.
796 RecordData Record;
797 for (SourceManager::sloc_entry_iterator
798 SLoc = SourceMgr.sloc_entry_begin() + 1,
799 SLocEnd = SourceMgr.sloc_entry_end();
800 SLoc != SLocEnd; ++SLoc) {
801 // Figure out which record code to use.
802 unsigned Code;
803 if (SLoc->isFile()) {
804 if (SLoc->getFile().getContentCache()->Entry)
805 Code = pch::SM_SLOC_FILE_ENTRY;
806 else
807 Code = pch::SM_SLOC_BUFFER_ENTRY;
808 } else
809 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
810 Record.push_back(Code);
811
812 Record.push_back(SLoc->getOffset());
813 if (SLoc->isFile()) {
814 const SrcMgr::FileInfo &File = SLoc->getFile();
815 Record.push_back(File.getIncludeLoc().getRawEncoding());
816 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +0000817 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +0000818
819 const SrcMgr::ContentCache *Content = File.getContentCache();
820 if (Content->Entry) {
821 // The source location entry is a file. The blob associated
822 // with this entry is the file name.
823 if (SLocFileAbbrv == -1)
824 SLocFileAbbrv = CreateSLocFileAbbrev(S);
825 S.EmitRecordWithBlob(SLocFileAbbrv, Record,
826 Content->Entry->getName(),
827 strlen(Content->Entry->getName()));
828 } else {
829 // The source location entry is a buffer. The blob associated
830 // with this entry contains the contents of the buffer.
831 if (SLocBufferAbbrv == -1) {
832 SLocBufferAbbrv = CreateSLocBufferAbbrev(S);
833 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S);
834 }
835
836 // We add one to the size so that we capture the trailing NULL
837 // that is required by llvm::MemoryBuffer::getMemBuffer (on
838 // the reader side).
839 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
840 const char *Name = Buffer->getBufferIdentifier();
841 S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
842 Record.clear();
843 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
844 S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
845 Buffer->getBufferStart(),
846 Buffer->getBufferSize() + 1);
847 }
848 } else {
849 // The source location entry is an instantiation.
850 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
851 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
852 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
853 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
854
Douglas Gregor364e5802009-04-15 18:05:10 +0000855 // Compute the token length for this macro expansion.
856 unsigned NextOffset = SourceMgr.getNextOffset();
857 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
858 if (++NextSLoc != SLocEnd)
859 NextOffset = NextSLoc->getOffset();
860 Record.push_back(NextOffset - SLoc->getOffset() - 1);
861
Douglas Gregorab1cef72009-04-10 03:52:48 +0000862 if (SLocInstantiationAbbrv == -1)
863 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S);
864 S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
865 }
866
867 Record.clear();
868 }
869
Douglas Gregor635f97f2009-04-13 16:31:14 +0000870 // Write the line table.
871 if (SourceMgr.hasLineTable()) {
872 LineTableInfo &LineTable = SourceMgr.getLineTable();
873
874 // Emit the file names
875 Record.push_back(LineTable.getNumFilenames());
876 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
877 // Emit the file name
878 const char *Filename = LineTable.getFilename(I);
879 unsigned FilenameLen = Filename? strlen(Filename) : 0;
880 Record.push_back(FilenameLen);
881 if (FilenameLen)
882 Record.insert(Record.end(), Filename, Filename + FilenameLen);
883 }
884
885 // Emit the line entries
886 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
887 L != LEnd; ++L) {
888 // Emit the file ID
889 Record.push_back(L->first);
890
891 // Emit the line entries
892 Record.push_back(L->second.size());
893 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
894 LEEnd = L->second.end();
895 LE != LEEnd; ++LE) {
896 Record.push_back(LE->FileOffset);
897 Record.push_back(LE->LineNo);
898 Record.push_back(LE->FilenameID);
899 Record.push_back((unsigned)LE->FileKind);
900 Record.push_back(LE->IncludeOffset);
901 }
902 S.EmitRecord(pch::SM_LINE_TABLE, Record);
903 }
904 }
905
Douglas Gregorab1cef72009-04-10 03:52:48 +0000906 S.ExitBlock();
907}
908
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000909/// \brief Writes the block containing the serialized form of the
910/// preprocessor.
911///
Chris Lattner850eabd2009-04-10 18:08:30 +0000912void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +0000913 // Enter the preprocessor block.
914 S.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
915
Chris Lattner1b094952009-04-10 18:00:12 +0000916 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
917 // FIXME: use diagnostics subsystem for localization etc.
918 if (PP.SawDateOrTime())
919 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +0000920
Chris Lattner1b094952009-04-10 18:00:12 +0000921 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +0000922
Chris Lattner4b21c202009-04-13 01:29:17 +0000923 // If the preprocessor __COUNTER__ value has been bumped, remember it.
924 if (PP.getCounterValue() != 0) {
925 Record.push_back(PP.getCounterValue());
926 S.EmitRecord(pch::PP_COUNTER_VALUE, Record);
927 Record.clear();
928 }
929
Chris Lattner1b094952009-04-10 18:00:12 +0000930 // Loop over all the macro definitions that are live at the end of the file,
931 // emitting each to the PP section.
932 // FIXME: Eventually we want to emit an index so that we can lazily load
933 // macros.
934 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
935 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000936 // FIXME: This emits macros in hash table order, we should do it in a stable
937 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +0000938 MacroInfo *MI = I->second;
939
940 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
941 // been redefined by the header (in which case they are not isBuiltinMacro).
942 if (MI->isBuiltinMacro())
943 continue;
944
Chris Lattner29241862009-04-11 21:15:38 +0000945 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +0000946 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
947 Record.push_back(MI->isUsed());
948
949 unsigned Code;
950 if (MI->isObjectLike()) {
951 Code = pch::PP_MACRO_OBJECT_LIKE;
952 } else {
953 Code = pch::PP_MACRO_FUNCTION_LIKE;
954
955 Record.push_back(MI->isC99Varargs());
956 Record.push_back(MI->isGNUVarargs());
957 Record.push_back(MI->getNumArgs());
958 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
959 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +0000960 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +0000961 }
962 S.EmitRecord(Code, Record);
963 Record.clear();
964
Chris Lattner850eabd2009-04-10 18:08:30 +0000965 // Emit the tokens array.
966 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
967 // Note that we know that the preprocessor does not have any annotation
968 // tokens in it because they are created by the parser, and thus can't be
969 // in a macro definition.
970 const Token &Tok = MI->getReplacementToken(TokNo);
971
972 Record.push_back(Tok.getLocation().getRawEncoding());
973 Record.push_back(Tok.getLength());
974
Chris Lattner850eabd2009-04-10 18:08:30 +0000975 // FIXME: When reading literal tokens, reconstruct the literal pointer if
976 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +0000977 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +0000978
979 // FIXME: Should translate token kind to a stable encoding.
980 Record.push_back(Tok.getKind());
981 // FIXME: Should translate token flags to a stable encoding.
982 Record.push_back(Tok.getFlags());
983
984 S.EmitRecord(pch::PP_TOKEN, Record);
985 Record.clear();
986 }
Chris Lattner1b094952009-04-10 18:00:12 +0000987
988 }
989
Chris Lattner84b04f12009-04-10 17:16:57 +0000990 S.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000991}
992
993
Douglas Gregorc34897d2009-04-09 22:27:44 +0000994/// \brief Write the representation of a type to the PCH stream.
995void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000996 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +0000997 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +0000998 ID = NextTypeID++;
999
1000 // Record the offset for this type.
1001 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
1002 TypeOffsets.push_back(S.GetCurrentBitNo());
1003 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1004 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
1005 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo();
1006 }
1007
1008 RecordData Record;
1009
1010 // Emit the type's representation.
1011 PCHTypeWriter W(*this, Record);
1012 switch (T->getTypeClass()) {
1013 // For all of the concrete, non-dependent types, call the
1014 // appropriate visitor function.
1015#define TYPE(Class, Base) \
1016 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1017#define ABSTRACT_TYPE(Class, Base)
1018#define DEPENDENT_TYPE(Class, Base)
1019#include "clang/AST/TypeNodes.def"
1020
1021 // For all of the dependent type nodes (which only occur in C++
1022 // templates), produce an error.
1023#define TYPE(Class, Base)
1024#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1025#include "clang/AST/TypeNodes.def"
1026 assert(false && "Cannot serialize dependent type nodes");
1027 break;
1028 }
1029
1030 // Emit the serialized record.
1031 S.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001032
1033 // Flush any expressions that were written as part of this type.
1034 FlushExprs();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001035}
1036
1037/// \brief Write a block containing all of the types.
1038void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001039 // Enter the types block.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001040 S.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
1041
1042 // Emit all of the types in the ASTContext
1043 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1044 TEnd = Context.getTypes().end();
1045 T != TEnd; ++T) {
1046 // Builtin types are never serialized.
1047 if (isa<BuiltinType>(*T))
1048 continue;
1049
1050 WriteType(*T);
1051 }
1052
1053 // Exit the types block
1054 S.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001055}
1056
1057/// \brief Write the block containing all of the declaration IDs
1058/// lexically declared within the given DeclContext.
1059///
1060/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1061/// bistream, or 0 if no block was written.
1062uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1063 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001064 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001065 return 0;
1066
1067 uint64_t Offset = S.GetCurrentBitNo();
1068 RecordData Record;
1069 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1070 DEnd = DC->decls_end(Context);
1071 D != DEnd; ++D)
1072 AddDeclRef(*D, Record);
1073
1074 S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
1075 return Offset;
1076}
1077
1078/// \brief Write the block containing all of the declaration IDs
1079/// visible from the given DeclContext.
1080///
1081/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1082/// bistream, or 0 if no block was written.
1083uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1084 DeclContext *DC) {
1085 if (DC->getPrimaryContext() != DC)
1086 return 0;
1087
1088 // Force the DeclContext to build a its name-lookup table.
1089 DC->lookup(Context, DeclarationName());
1090
1091 // Serialize the contents of the mapping used for lookup. Note that,
1092 // although we have two very different code paths, the serialized
1093 // representation is the same for both cases: a declaration name,
1094 // followed by a size, followed by references to the visible
1095 // declarations that have that name.
1096 uint64_t Offset = S.GetCurrentBitNo();
1097 RecordData Record;
1098 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001099 if (!Map)
1100 return 0;
1101
Douglas Gregorc34897d2009-04-09 22:27:44 +00001102 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1103 D != DEnd; ++D) {
1104 AddDeclarationName(D->first, Record);
1105 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1106 Record.push_back(Result.second - Result.first);
1107 for(; Result.first != Result.second; ++Result.first)
1108 AddDeclRef(*Result.first, Record);
1109 }
1110
1111 if (Record.size() == 0)
1112 return 0;
1113
1114 S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
1115 return Offset;
1116}
1117
1118/// \brief Write a block containing all of the declarations.
1119void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001120 // Enter the declarations block.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001121 S.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
1122
1123 // Emit all of the declarations.
1124 RecordData Record;
1125 PCHDeclWriter W(*this, Record);
1126 while (!DeclsToEmit.empty()) {
1127 // Pull the next declaration off the queue
1128 Decl *D = DeclsToEmit.front();
1129 DeclsToEmit.pop();
1130
1131 // If this declaration is also a DeclContext, write blocks for the
1132 // declarations that lexically stored inside its context and those
1133 // declarations that are visible from its context. These blocks
1134 // are written before the declaration itself so that we can put
1135 // their offsets into the record for the declaration.
1136 uint64_t LexicalOffset = 0;
1137 uint64_t VisibleOffset = 0;
1138 DeclContext *DC = dyn_cast<DeclContext>(D);
1139 if (DC) {
1140 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1141 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1142 }
1143
1144 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001145 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001146 if (ID == 0)
1147 ID = DeclIDs.size();
1148
1149 unsigned Index = ID - 1;
1150
1151 // Record the offset for this declaration
1152 if (DeclOffsets.size() == Index)
1153 DeclOffsets.push_back(S.GetCurrentBitNo());
1154 else if (DeclOffsets.size() < Index) {
1155 DeclOffsets.resize(Index+1);
1156 DeclOffsets[Index] = S.GetCurrentBitNo();
1157 }
1158
1159 // Build and emit a record for this declaration
1160 Record.clear();
1161 W.Code = (pch::DeclCode)0;
1162 W.Visit(D);
1163 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001164 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001165 S.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001166
Douglas Gregor1c507882009-04-15 21:30:51 +00001167 // If the declaration had any attributes, write them now.
1168 if (D->hasAttrs())
1169 WriteAttributeRecord(D->getAttrs());
1170
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001171 // Flush any expressions that were written as part of this declaration.
1172 FlushExprs();
1173
Douglas Gregor631f6c62009-04-14 00:24:19 +00001174 // Note external declarations so that we can add them to a record
1175 // in the PCH file later.
1176 if (isa<FileScopeAsmDecl>(D))
1177 ExternalDefinitions.push_back(ID);
1178 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1179 if (// Non-static file-scope variables with initializers or that
1180 // are tentative definitions.
1181 (Var->isFileVarDecl() &&
1182 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1183 // Out-of-line definitions of static data members (C++).
1184 (Var->getDeclContext()->isRecord() &&
1185 !Var->getLexicalDeclContext()->isRecord() &&
1186 Var->getStorageClass() == VarDecl::Static))
1187 ExternalDefinitions.push_back(ID);
1188 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1189 if (Func->isThisDeclarationADefinition() &&
1190 Func->getStorageClass() != FunctionDecl::Static &&
1191 !Func->isInline())
1192 ExternalDefinitions.push_back(ID);
1193 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001194 }
1195
1196 // Exit the declarations block
1197 S.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001198}
1199
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001200/// \brief Write the identifier table into the PCH file.
1201///
1202/// The identifier table consists of a blob containing string data
1203/// (the actual identifiers themselves) and a separate "offsets" index
1204/// that maps identifier IDs to locations within the blob.
1205void PCHWriter::WriteIdentifierTable() {
1206 using namespace llvm;
1207
1208 // Create and write out the blob that contains the identifier
1209 // strings.
1210 RecordData IdentOffsets;
1211 IdentOffsets.resize(IdentifierIDs.size());
1212 {
1213 // Create the identifier string data.
1214 std::vector<char> Data;
1215 Data.push_back(0); // Data must not be empty.
1216 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1217 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1218 ID != IDEnd; ++ID) {
1219 assert(ID->first && "NULL identifier in identifier table");
1220
1221 // Make sure we're starting on an odd byte. The PCH reader
1222 // expects the low bit to be set on all of the offsets.
1223 if ((Data.size() & 0x01) == 0)
1224 Data.push_back((char)0);
1225
1226 IdentOffsets[ID->second - 1] = Data.size();
1227 Data.insert(Data.end(),
1228 ID->first->getName(),
1229 ID->first->getName() + ID->first->getLength());
1230 Data.push_back((char)0);
1231 }
1232
1233 // Create a blob abbreviation
1234 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1235 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
1237 unsigned IDTableAbbrev = S.EmitAbbrev(Abbrev);
1238
1239 // Write the identifier table
1240 RecordData Record;
1241 Record.push_back(pch::IDENTIFIER_TABLE);
1242 S.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
1243 }
1244
1245 // Write the offsets table for identifier IDs.
1246 S.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
1247}
1248
Douglas Gregor1c507882009-04-15 21:30:51 +00001249/// \brief Write a record containing the given attributes.
1250void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1251 RecordData Record;
1252 for (; Attr; Attr = Attr->getNext()) {
1253 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1254 Record.push_back(Attr->isInherited());
1255 switch (Attr->getKind()) {
1256 case Attr::Alias:
1257 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1258 break;
1259
1260 case Attr::Aligned:
1261 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1262 break;
1263
1264 case Attr::AlwaysInline:
1265 break;
1266
1267 case Attr::AnalyzerNoReturn:
1268 break;
1269
1270 case Attr::Annotate:
1271 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1272 break;
1273
1274 case Attr::AsmLabel:
1275 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1276 break;
1277
1278 case Attr::Blocks:
1279 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1280 break;
1281
1282 case Attr::Cleanup:
1283 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1284 break;
1285
1286 case Attr::Const:
1287 break;
1288
1289 case Attr::Constructor:
1290 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1291 break;
1292
1293 case Attr::DLLExport:
1294 case Attr::DLLImport:
1295 case Attr::Deprecated:
1296 break;
1297
1298 case Attr::Destructor:
1299 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1300 break;
1301
1302 case Attr::FastCall:
1303 break;
1304
1305 case Attr::Format: {
1306 const FormatAttr *Format = cast<FormatAttr>(Attr);
1307 AddString(Format->getType(), Record);
1308 Record.push_back(Format->getFormatIdx());
1309 Record.push_back(Format->getFirstArg());
1310 break;
1311 }
1312
1313 case Attr::GNUCInline:
1314 case Attr::IBOutletKind:
1315 case Attr::NoReturn:
1316 case Attr::NoThrow:
1317 case Attr::Nodebug:
1318 case Attr::Noinline:
1319 break;
1320
1321 case Attr::NonNull: {
1322 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1323 Record.push_back(NonNull->size());
1324 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1325 break;
1326 }
1327
1328 case Attr::ObjCException:
1329 case Attr::ObjCNSObject:
1330 case Attr::Overloadable:
1331 break;
1332
1333 case Attr::Packed:
1334 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1335 break;
1336
1337 case Attr::Pure:
1338 break;
1339
1340 case Attr::Regparm:
1341 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1342 break;
1343
1344 case Attr::Section:
1345 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1346 break;
1347
1348 case Attr::StdCall:
1349 case Attr::TransparentUnion:
1350 case Attr::Unavailable:
1351 case Attr::Unused:
1352 case Attr::Used:
1353 break;
1354
1355 case Attr::Visibility:
1356 // FIXME: stable encoding
1357 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1358 break;
1359
1360 case Attr::WarnUnusedResult:
1361 case Attr::Weak:
1362 case Attr::WeakImport:
1363 break;
1364 }
1365 }
1366
1367 assert((int)pch::DECL_ATTR == (int)pch::TYPE_ATTR &&
1368 "DECL_ATTR/TYPE_ATTR mismatch");
1369 S.EmitRecord(pch::DECL_ATTR, Record);
1370}
1371
1372void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1373 Record.push_back(Str.size());
1374 Record.insert(Record.end(), Str.begin(), Str.end());
1375}
1376
Douglas Gregorc34897d2009-04-09 22:27:44 +00001377PCHWriter::PCHWriter(llvm::BitstreamWriter &S)
1378 : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
1379
Chris Lattner850eabd2009-04-10 18:08:30 +00001380void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001381 // Emit the file header.
1382 S.Emit((unsigned)'C', 8);
1383 S.Emit((unsigned)'P', 8);
1384 S.Emit((unsigned)'C', 8);
1385 S.Emit((unsigned)'H', 8);
1386
1387 // The translation unit is the first declaration we'll emit.
1388 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1389 DeclsToEmit.push(Context.getTranslationUnitDecl());
1390
1391 // Write the remaining PCH contents.
Douglas Gregorb5887f32009-04-10 21:16:55 +00001392 S.EnterSubblock(pch::PCH_BLOCK_ID, 3);
1393 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001394 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001395 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001396 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001397 WriteTypesBlock(Context);
1398 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001399 WriteIdentifierTable();
Douglas Gregor179cfb12009-04-10 20:39:37 +00001400 S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1401 S.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001402 if (!ExternalDefinitions.empty())
1403 S.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001404 S.ExitBlock();
1405}
1406
1407void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1408 Record.push_back(Loc.getRawEncoding());
1409}
1410
1411void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1412 Record.push_back(Value.getBitWidth());
1413 unsigned N = Value.getNumWords();
1414 const uint64_t* Words = Value.getRawData();
1415 for (unsigned I = 0; I != N; ++I)
1416 Record.push_back(Words[I]);
1417}
1418
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001419void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1420 Record.push_back(Value.isUnsigned());
1421 AddAPInt(Value, Record);
1422}
1423
Douglas Gregore2f37202009-04-14 21:55:33 +00001424void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1425 AddAPInt(Value.bitcastToAPInt(), Record);
1426}
1427
Douglas Gregorc34897d2009-04-09 22:27:44 +00001428void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001429 if (II == 0) {
1430 Record.push_back(0);
1431 return;
1432 }
1433
1434 pch::IdentID &ID = IdentifierIDs[II];
1435 if (ID == 0)
1436 ID = IdentifierIDs.size();
1437
1438 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001439}
1440
1441void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1442 if (T.isNull()) {
1443 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1444 return;
1445 }
1446
1447 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001448 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001449 switch (BT->getKind()) {
1450 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1451 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1452 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1453 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1454 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1455 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1456 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1457 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1458 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1459 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1460 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1461 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1462 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1463 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1464 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1465 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1466 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1467 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1468 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1469 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1470 }
1471
1472 Record.push_back((ID << 3) | T.getCVRQualifiers());
1473 return;
1474 }
1475
Douglas Gregorac8f2802009-04-10 17:25:41 +00001476 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001477 if (ID == 0) // we haven't seen this type before
1478 ID = NextTypeID++;
1479
1480 // Encode the type qualifiers in the type reference.
1481 Record.push_back((ID << 3) | T.getCVRQualifiers());
1482}
1483
1484void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1485 if (D == 0) {
1486 Record.push_back(0);
1487 return;
1488 }
1489
Douglas Gregorac8f2802009-04-10 17:25:41 +00001490 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001491 if (ID == 0) {
1492 // We haven't seen this declaration before. Give it a new ID and
1493 // enqueue it in the list of declarations to emit.
1494 ID = DeclIDs.size();
1495 DeclsToEmit.push(const_cast<Decl *>(D));
1496 }
1497
1498 Record.push_back(ID);
1499}
1500
1501void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1502 Record.push_back(Name.getNameKind());
1503 switch (Name.getNameKind()) {
1504 case DeclarationName::Identifier:
1505 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1506 break;
1507
1508 case DeclarationName::ObjCZeroArgSelector:
1509 case DeclarationName::ObjCOneArgSelector:
1510 case DeclarationName::ObjCMultiArgSelector:
1511 assert(false && "Serialization of Objective-C selectors unavailable");
1512 break;
1513
1514 case DeclarationName::CXXConstructorName:
1515 case DeclarationName::CXXDestructorName:
1516 case DeclarationName::CXXConversionFunctionName:
1517 AddTypeRef(Name.getCXXNameType(), Record);
1518 break;
1519
1520 case DeclarationName::CXXOperatorName:
1521 Record.push_back(Name.getCXXOverloadedOperator());
1522 break;
1523
1524 case DeclarationName::CXXUsingDirective:
1525 // No extra data to emit
1526 break;
1527 }
1528}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001529
Douglas Gregora151ba42009-04-14 23:32:43 +00001530/// \brief Write the given subexpression to the bitstream.
1531void PCHWriter::WriteSubExpr(Expr *E) {
1532 RecordData Record;
1533 PCHStmtWriter Writer(*this, Record);
1534
1535 if (!E) {
1536 S.EmitRecord(pch::EXPR_NULL, Record);
1537 return;
1538 }
1539
1540 Writer.Code = pch::EXPR_NULL;
1541 Writer.Visit(E);
1542 assert(Writer.Code != pch::EXPR_NULL &&
1543 "Unhandled expression writing PCH file");
1544 S.EmitRecord(Writer.Code, Record);
1545}
1546
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001547/// \brief Flush all of the expressions that have been added to the
1548/// queue via AddExpr().
1549void PCHWriter::FlushExprs() {
1550 RecordData Record;
1551 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001552
Douglas Gregora151ba42009-04-14 23:32:43 +00001553 for (unsigned I = 0, N = ExprsToEmit.size(); I != N; ++I) {
1554 Expr *E = ExprsToEmit[I];
1555
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001556 if (!E) {
1557 S.EmitRecord(pch::EXPR_NULL, Record);
1558 continue;
1559 }
1560
1561 Writer.Code = pch::EXPR_NULL;
1562 Writer.Visit(E);
1563 assert(Writer.Code != pch::EXPR_NULL &&
1564 "Unhandled expression writing PCH file");
1565 S.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001566
1567 assert(N == ExprsToEmit.size() &&
1568 "Subexpression writen via AddExpr rather than WriteSubExpr!");
1569
1570 // Note that we are at the end of a full expression. Any
1571 // expression records that follow this one are part of a different
1572 // expression.
1573 Record.clear();
1574 S.EmitRecord(pch::EXPR_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001575 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001576
1577 ExprsToEmit.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001578}