blob: 612eb23b5292d3823e164131172f394d1f8a9fdf [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 Gregorec0b8292009-04-15 23:02:49 +0000467 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
468 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000469 };
470}
471
472void PCHStmtWriter::VisitExpr(Expr *E) {
473 Writer.AddTypeRef(E->getType(), Record);
474 Record.push_back(E->isTypeDependent());
475 Record.push_back(E->isValueDependent());
476}
477
Douglas Gregore2f37202009-04-14 21:55:33 +0000478void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
479 VisitExpr(E);
480 Writer.AddSourceLocation(E->getLocation(), Record);
481 Record.push_back(E->getIdentType()); // FIXME: stable encoding
482 Code = pch::EXPR_PREDEFINED;
483}
484
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000485void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
486 VisitExpr(E);
487 Writer.AddDeclRef(E->getDecl(), Record);
488 Writer.AddSourceLocation(E->getLocation(), Record);
489 Code = pch::EXPR_DECL_REF;
490}
491
492void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
493 VisitExpr(E);
494 Writer.AddSourceLocation(E->getLocation(), Record);
495 Writer.AddAPInt(E->getValue(), Record);
496 Code = pch::EXPR_INTEGER_LITERAL;
497}
498
Douglas Gregore2f37202009-04-14 21:55:33 +0000499void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
500 VisitExpr(E);
501 Writer.AddAPFloat(E->getValue(), Record);
502 Record.push_back(E->isExact());
503 Writer.AddSourceLocation(E->getLocation(), Record);
504 Code = pch::EXPR_FLOATING_LITERAL;
505}
506
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000507void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
508 VisitExpr(E);
509 Writer.WriteSubExpr(E->getSubExpr());
510 Code = pch::EXPR_IMAGINARY_LITERAL;
511}
512
Douglas Gregor596e0932009-04-15 16:35:07 +0000513void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
514 VisitExpr(E);
515 Record.push_back(E->getByteLength());
516 Record.push_back(E->getNumConcatenated());
517 Record.push_back(E->isWide());
518 // FIXME: String data should be stored as a blob at the end of the
519 // StringLiteral. However, we can't do so now because we have no
520 // provision for coping with abbreviations when we're jumping around
521 // the PCH file during deserialization.
522 Record.insert(Record.end(),
523 E->getStrData(), E->getStrData() + E->getByteLength());
524 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
525 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
526 Code = pch::EXPR_STRING_LITERAL;
527}
528
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000529void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
530 VisitExpr(E);
531 Record.push_back(E->getValue());
532 Writer.AddSourceLocation(E->getLoc(), Record);
533 Record.push_back(E->isWide());
534 Code = pch::EXPR_CHARACTER_LITERAL;
535}
536
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000537void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
538 VisitExpr(E);
539 Writer.AddSourceLocation(E->getLParen(), Record);
540 Writer.AddSourceLocation(E->getRParen(), Record);
541 Writer.WriteSubExpr(E->getSubExpr());
542 Code = pch::EXPR_PAREN;
543}
544
Douglas Gregor12d74052009-04-15 15:58:59 +0000545void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
546 VisitExpr(E);
547 Writer.WriteSubExpr(E->getSubExpr());
548 Record.push_back(E->getOpcode()); // FIXME: stable encoding
549 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
550 Code = pch::EXPR_UNARY_OPERATOR;
551}
552
553void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
554 VisitExpr(E);
555 Record.push_back(E->isSizeOf());
556 if (E->isArgumentType())
557 Writer.AddTypeRef(E->getArgumentType(), Record);
558 else {
559 Record.push_back(0);
560 Writer.WriteSubExpr(E->getArgumentExpr());
561 }
562 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
563 Writer.AddSourceLocation(E->getRParenLoc(), Record);
564 Code = pch::EXPR_SIZEOF_ALIGN_OF;
565}
566
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000567void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
568 VisitExpr(E);
569 Writer.WriteSubExpr(E->getLHS());
570 Writer.WriteSubExpr(E->getRHS());
571 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
572 Code = pch::EXPR_ARRAY_SUBSCRIPT;
573}
574
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000575void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
576 VisitExpr(E);
577 Record.push_back(E->getNumArgs());
578 Writer.AddSourceLocation(E->getRParenLoc(), Record);
579 Writer.WriteSubExpr(E->getCallee());
580 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
581 Arg != ArgEnd; ++Arg)
582 Writer.WriteSubExpr(*Arg);
583 Code = pch::EXPR_CALL;
584}
585
586void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
587 VisitExpr(E);
588 Writer.WriteSubExpr(E->getBase());
589 Writer.AddDeclRef(E->getMemberDecl(), Record);
590 Writer.AddSourceLocation(E->getMemberLoc(), Record);
591 Record.push_back(E->isArrow());
592 Code = pch::EXPR_MEMBER;
593}
594
Douglas Gregora151ba42009-04-14 23:32:43 +0000595void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
596 VisitExpr(E);
597 Writer.WriteSubExpr(E->getSubExpr());
598}
599
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000600void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
601 VisitExpr(E);
602 Writer.WriteSubExpr(E->getLHS());
603 Writer.WriteSubExpr(E->getRHS());
604 Record.push_back(E->getOpcode()); // FIXME: stable encoding
605 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
606 Code = pch::EXPR_BINARY_OPERATOR;
607}
608
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000609void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
610 VisitBinaryOperator(E);
611 Writer.AddTypeRef(E->getComputationLHSType(), Record);
612 Writer.AddTypeRef(E->getComputationResultType(), Record);
613 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
614}
615
616void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
617 VisitExpr(E);
618 Writer.WriteSubExpr(E->getCond());
619 Writer.WriteSubExpr(E->getLHS());
620 Writer.WriteSubExpr(E->getRHS());
621 Code = pch::EXPR_CONDITIONAL_OPERATOR;
622}
623
Douglas Gregora151ba42009-04-14 23:32:43 +0000624void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
625 VisitCastExpr(E);
626 Record.push_back(E->isLvalueCast());
627 Code = pch::EXPR_IMPLICIT_CAST;
628}
629
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000630void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
631 VisitCastExpr(E);
632 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
633}
634
635void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
636 VisitExplicitCastExpr(E);
637 Writer.AddSourceLocation(E->getLParenLoc(), Record);
638 Writer.AddSourceLocation(E->getRParenLoc(), Record);
639 Code = pch::EXPR_CSTYLE_CAST;
640}
641
Douglas Gregorec0b8292009-04-15 23:02:49 +0000642void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
643 VisitExpr(E);
644 Writer.WriteSubExpr(E->getBase());
645 Writer.AddIdentifierRef(&E->getAccessor(), Record);
646 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
647 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
648}
649
650void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
651 VisitExpr(E);
652 Writer.WriteSubExpr(E->getSubExpr());
653 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
654 Writer.AddSourceLocation(E->getRParenLoc(), Record);
655 Code = pch::EXPR_VA_ARG;
656}
657
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000658//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000659// PCHWriter Implementation
660//===----------------------------------------------------------------------===//
661
Douglas Gregorb5887f32009-04-10 21:16:55 +0000662/// \brief Write the target triple (e.g., i686-apple-darwin9).
663void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
664 using namespace llvm;
665 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
666 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
667 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
668 unsigned TripleAbbrev = S.EmitAbbrev(Abbrev);
669
670 RecordData Record;
671 Record.push_back(pch::TARGET_TRIPLE);
672 const char *Triple = Target.getTargetTriple();
673 S.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
674}
675
676/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000677void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
678 RecordData Record;
679 Record.push_back(LangOpts.Trigraphs);
680 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
681 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
682 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
683 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
684 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
685 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
686 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
687 Record.push_back(LangOpts.C99); // C99 Support
688 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
689 Record.push_back(LangOpts.CPlusPlus); // C++ Support
690 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
691 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
692 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
693
694 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
695 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
696 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
697
698 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
699 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
700 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
701 Record.push_back(LangOpts.LaxVectorConversions);
702 Record.push_back(LangOpts.Exceptions); // Support exception handling.
703
704 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
705 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
706 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
707
708 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
709 // by locks.
710 Record.push_back(LangOpts.Blocks); // block extension to C
711 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
712 // they are unused.
713 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
714 // (modulo the platform support).
715
716 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
717 // signed integer arithmetic overflows.
718
719 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
720 // may be ripped out at any time.
721
722 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
723 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
724 // defined.
725 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
726 // opposed to __DYNAMIC__).
727 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
728
729 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
730 // used (instead of C99 semantics).
731 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
732 Record.push_back(LangOpts.getGCMode());
733 Record.push_back(LangOpts.getVisibilityMode());
734 Record.push_back(LangOpts.InstantiationDepth);
735 S.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
736}
737
Douglas Gregorab1cef72009-04-10 03:52:48 +0000738//===----------------------------------------------------------------------===//
739// Source Manager Serialization
740//===----------------------------------------------------------------------===//
741
742/// \brief Create an abbreviation for the SLocEntry that refers to a
743/// file.
744static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) {
745 using namespace llvm;
746 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
747 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +0000752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
753 return S.EmitAbbrev(Abbrev);
754}
755
756/// \brief Create an abbreviation for the SLocEntry that refers to a
757/// buffer.
758static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &S) {
759 using namespace llvm;
760 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
761 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
764 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
765 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
766 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
767 return S.EmitAbbrev(Abbrev);
768}
769
770/// \brief Create an abbreviation for the SLocEntry that refers to a
771/// buffer's blob.
772static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &S) {
773 using namespace llvm;
774 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
775 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
777 return S.EmitAbbrev(Abbrev);
778}
779
780/// \brief Create an abbreviation for the SLocEntry that refers to an
781/// buffer.
782static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &S) {
783 using namespace llvm;
784 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
785 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
786 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +0000790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorab1cef72009-04-10 03:52:48 +0000791 return S.EmitAbbrev(Abbrev);
792}
793
794/// \brief Writes the block containing the serialized form of the
795/// source manager.
796///
797/// TODO: We should probably use an on-disk hash table (stored in a
798/// blob), indexed based on the file name, so that we only create
799/// entries for files that we actually need. In the common case (no
800/// errors), we probably won't have to create file entries for any of
801/// the files in the AST.
802void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +0000803 // Enter the source manager block.
Douglas Gregorab1cef72009-04-10 03:52:48 +0000804 S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
805
806 // Abbreviations for the various kinds of source-location entries.
807 int SLocFileAbbrv = -1;
808 int SLocBufferAbbrv = -1;
809 int SLocBufferBlobAbbrv = -1;
810 int SLocInstantiationAbbrv = -1;
811
812 // Write out the source location entry table. We skip the first
813 // entry, which is always the same dummy entry.
814 RecordData Record;
815 for (SourceManager::sloc_entry_iterator
816 SLoc = SourceMgr.sloc_entry_begin() + 1,
817 SLocEnd = SourceMgr.sloc_entry_end();
818 SLoc != SLocEnd; ++SLoc) {
819 // Figure out which record code to use.
820 unsigned Code;
821 if (SLoc->isFile()) {
822 if (SLoc->getFile().getContentCache()->Entry)
823 Code = pch::SM_SLOC_FILE_ENTRY;
824 else
825 Code = pch::SM_SLOC_BUFFER_ENTRY;
826 } else
827 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
828 Record.push_back(Code);
829
830 Record.push_back(SLoc->getOffset());
831 if (SLoc->isFile()) {
832 const SrcMgr::FileInfo &File = SLoc->getFile();
833 Record.push_back(File.getIncludeLoc().getRawEncoding());
834 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +0000835 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +0000836
837 const SrcMgr::ContentCache *Content = File.getContentCache();
838 if (Content->Entry) {
839 // The source location entry is a file. The blob associated
840 // with this entry is the file name.
841 if (SLocFileAbbrv == -1)
842 SLocFileAbbrv = CreateSLocFileAbbrev(S);
843 S.EmitRecordWithBlob(SLocFileAbbrv, Record,
844 Content->Entry->getName(),
845 strlen(Content->Entry->getName()));
846 } else {
847 // The source location entry is a buffer. The blob associated
848 // with this entry contains the contents of the buffer.
849 if (SLocBufferAbbrv == -1) {
850 SLocBufferAbbrv = CreateSLocBufferAbbrev(S);
851 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S);
852 }
853
854 // We add one to the size so that we capture the trailing NULL
855 // that is required by llvm::MemoryBuffer::getMemBuffer (on
856 // the reader side).
857 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
858 const char *Name = Buffer->getBufferIdentifier();
859 S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
860 Record.clear();
861 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
862 S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
863 Buffer->getBufferStart(),
864 Buffer->getBufferSize() + 1);
865 }
866 } else {
867 // The source location entry is an instantiation.
868 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
869 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
870 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
871 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
872
Douglas Gregor364e5802009-04-15 18:05:10 +0000873 // Compute the token length for this macro expansion.
874 unsigned NextOffset = SourceMgr.getNextOffset();
875 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
876 if (++NextSLoc != SLocEnd)
877 NextOffset = NextSLoc->getOffset();
878 Record.push_back(NextOffset - SLoc->getOffset() - 1);
879
Douglas Gregorab1cef72009-04-10 03:52:48 +0000880 if (SLocInstantiationAbbrv == -1)
881 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S);
882 S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
883 }
884
885 Record.clear();
886 }
887
Douglas Gregor635f97f2009-04-13 16:31:14 +0000888 // Write the line table.
889 if (SourceMgr.hasLineTable()) {
890 LineTableInfo &LineTable = SourceMgr.getLineTable();
891
892 // Emit the file names
893 Record.push_back(LineTable.getNumFilenames());
894 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
895 // Emit the file name
896 const char *Filename = LineTable.getFilename(I);
897 unsigned FilenameLen = Filename? strlen(Filename) : 0;
898 Record.push_back(FilenameLen);
899 if (FilenameLen)
900 Record.insert(Record.end(), Filename, Filename + FilenameLen);
901 }
902
903 // Emit the line entries
904 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
905 L != LEnd; ++L) {
906 // Emit the file ID
907 Record.push_back(L->first);
908
909 // Emit the line entries
910 Record.push_back(L->second.size());
911 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
912 LEEnd = L->second.end();
913 LE != LEEnd; ++LE) {
914 Record.push_back(LE->FileOffset);
915 Record.push_back(LE->LineNo);
916 Record.push_back(LE->FilenameID);
917 Record.push_back((unsigned)LE->FileKind);
918 Record.push_back(LE->IncludeOffset);
919 }
920 S.EmitRecord(pch::SM_LINE_TABLE, Record);
921 }
922 }
923
Douglas Gregorab1cef72009-04-10 03:52:48 +0000924 S.ExitBlock();
925}
926
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000927/// \brief Writes the block containing the serialized form of the
928/// preprocessor.
929///
Chris Lattner850eabd2009-04-10 18:08:30 +0000930void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +0000931 // Enter the preprocessor block.
932 S.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
933
Chris Lattner1b094952009-04-10 18:00:12 +0000934 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
935 // FIXME: use diagnostics subsystem for localization etc.
936 if (PP.SawDateOrTime())
937 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +0000938
Chris Lattner1b094952009-04-10 18:00:12 +0000939 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +0000940
Chris Lattner4b21c202009-04-13 01:29:17 +0000941 // If the preprocessor __COUNTER__ value has been bumped, remember it.
942 if (PP.getCounterValue() != 0) {
943 Record.push_back(PP.getCounterValue());
944 S.EmitRecord(pch::PP_COUNTER_VALUE, Record);
945 Record.clear();
946 }
947
Chris Lattner1b094952009-04-10 18:00:12 +0000948 // Loop over all the macro definitions that are live at the end of the file,
949 // emitting each to the PP section.
950 // FIXME: Eventually we want to emit an index so that we can lazily load
951 // macros.
952 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
953 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000954 // FIXME: This emits macros in hash table order, we should do it in a stable
955 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +0000956 MacroInfo *MI = I->second;
957
958 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
959 // been redefined by the header (in which case they are not isBuiltinMacro).
960 if (MI->isBuiltinMacro())
961 continue;
962
Chris Lattner29241862009-04-11 21:15:38 +0000963 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +0000964 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
965 Record.push_back(MI->isUsed());
966
967 unsigned Code;
968 if (MI->isObjectLike()) {
969 Code = pch::PP_MACRO_OBJECT_LIKE;
970 } else {
971 Code = pch::PP_MACRO_FUNCTION_LIKE;
972
973 Record.push_back(MI->isC99Varargs());
974 Record.push_back(MI->isGNUVarargs());
975 Record.push_back(MI->getNumArgs());
976 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
977 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +0000978 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +0000979 }
980 S.EmitRecord(Code, Record);
981 Record.clear();
982
Chris Lattner850eabd2009-04-10 18:08:30 +0000983 // Emit the tokens array.
984 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
985 // Note that we know that the preprocessor does not have any annotation
986 // tokens in it because they are created by the parser, and thus can't be
987 // in a macro definition.
988 const Token &Tok = MI->getReplacementToken(TokNo);
989
990 Record.push_back(Tok.getLocation().getRawEncoding());
991 Record.push_back(Tok.getLength());
992
Chris Lattner850eabd2009-04-10 18:08:30 +0000993 // FIXME: When reading literal tokens, reconstruct the literal pointer if
994 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +0000995 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +0000996
997 // FIXME: Should translate token kind to a stable encoding.
998 Record.push_back(Tok.getKind());
999 // FIXME: Should translate token flags to a stable encoding.
1000 Record.push_back(Tok.getFlags());
1001
1002 S.EmitRecord(pch::PP_TOKEN, Record);
1003 Record.clear();
1004 }
Chris Lattner1b094952009-04-10 18:00:12 +00001005
1006 }
1007
Chris Lattner84b04f12009-04-10 17:16:57 +00001008 S.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001009}
1010
1011
Douglas Gregorc34897d2009-04-09 22:27:44 +00001012/// \brief Write the representation of a type to the PCH stream.
1013void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001014 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001015 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001016 ID = NextTypeID++;
1017
1018 // Record the offset for this type.
1019 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
1020 TypeOffsets.push_back(S.GetCurrentBitNo());
1021 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1022 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
1023 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo();
1024 }
1025
1026 RecordData Record;
1027
1028 // Emit the type's representation.
1029 PCHTypeWriter W(*this, Record);
1030 switch (T->getTypeClass()) {
1031 // For all of the concrete, non-dependent types, call the
1032 // appropriate visitor function.
1033#define TYPE(Class, Base) \
1034 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1035#define ABSTRACT_TYPE(Class, Base)
1036#define DEPENDENT_TYPE(Class, Base)
1037#include "clang/AST/TypeNodes.def"
1038
1039 // For all of the dependent type nodes (which only occur in C++
1040 // templates), produce an error.
1041#define TYPE(Class, Base)
1042#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1043#include "clang/AST/TypeNodes.def"
1044 assert(false && "Cannot serialize dependent type nodes");
1045 break;
1046 }
1047
1048 // Emit the serialized record.
1049 S.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001050
1051 // Flush any expressions that were written as part of this type.
1052 FlushExprs();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001053}
1054
1055/// \brief Write a block containing all of the types.
1056void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001057 // Enter the types block.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001058 S.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
1059
1060 // Emit all of the types in the ASTContext
1061 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1062 TEnd = Context.getTypes().end();
1063 T != TEnd; ++T) {
1064 // Builtin types are never serialized.
1065 if (isa<BuiltinType>(*T))
1066 continue;
1067
1068 WriteType(*T);
1069 }
1070
1071 // Exit the types block
1072 S.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001073}
1074
1075/// \brief Write the block containing all of the declaration IDs
1076/// lexically declared within the given DeclContext.
1077///
1078/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1079/// bistream, or 0 if no block was written.
1080uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1081 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001082 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001083 return 0;
1084
1085 uint64_t Offset = S.GetCurrentBitNo();
1086 RecordData Record;
1087 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1088 DEnd = DC->decls_end(Context);
1089 D != DEnd; ++D)
1090 AddDeclRef(*D, Record);
1091
1092 S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
1093 return Offset;
1094}
1095
1096/// \brief Write the block containing all of the declaration IDs
1097/// visible from the given DeclContext.
1098///
1099/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1100/// bistream, or 0 if no block was written.
1101uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1102 DeclContext *DC) {
1103 if (DC->getPrimaryContext() != DC)
1104 return 0;
1105
1106 // Force the DeclContext to build a its name-lookup table.
1107 DC->lookup(Context, DeclarationName());
1108
1109 // Serialize the contents of the mapping used for lookup. Note that,
1110 // although we have two very different code paths, the serialized
1111 // representation is the same for both cases: a declaration name,
1112 // followed by a size, followed by references to the visible
1113 // declarations that have that name.
1114 uint64_t Offset = S.GetCurrentBitNo();
1115 RecordData Record;
1116 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001117 if (!Map)
1118 return 0;
1119
Douglas Gregorc34897d2009-04-09 22:27:44 +00001120 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1121 D != DEnd; ++D) {
1122 AddDeclarationName(D->first, Record);
1123 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1124 Record.push_back(Result.second - Result.first);
1125 for(; Result.first != Result.second; ++Result.first)
1126 AddDeclRef(*Result.first, Record);
1127 }
1128
1129 if (Record.size() == 0)
1130 return 0;
1131
1132 S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
1133 return Offset;
1134}
1135
1136/// \brief Write a block containing all of the declarations.
1137void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001138 // Enter the declarations block.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001139 S.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
1140
1141 // Emit all of the declarations.
1142 RecordData Record;
1143 PCHDeclWriter W(*this, Record);
1144 while (!DeclsToEmit.empty()) {
1145 // Pull the next declaration off the queue
1146 Decl *D = DeclsToEmit.front();
1147 DeclsToEmit.pop();
1148
1149 // If this declaration is also a DeclContext, write blocks for the
1150 // declarations that lexically stored inside its context and those
1151 // declarations that are visible from its context. These blocks
1152 // are written before the declaration itself so that we can put
1153 // their offsets into the record for the declaration.
1154 uint64_t LexicalOffset = 0;
1155 uint64_t VisibleOffset = 0;
1156 DeclContext *DC = dyn_cast<DeclContext>(D);
1157 if (DC) {
1158 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1159 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1160 }
1161
1162 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001163 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001164 if (ID == 0)
1165 ID = DeclIDs.size();
1166
1167 unsigned Index = ID - 1;
1168
1169 // Record the offset for this declaration
1170 if (DeclOffsets.size() == Index)
1171 DeclOffsets.push_back(S.GetCurrentBitNo());
1172 else if (DeclOffsets.size() < Index) {
1173 DeclOffsets.resize(Index+1);
1174 DeclOffsets[Index] = S.GetCurrentBitNo();
1175 }
1176
1177 // Build and emit a record for this declaration
1178 Record.clear();
1179 W.Code = (pch::DeclCode)0;
1180 W.Visit(D);
1181 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001182 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001183 S.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001184
Douglas Gregor1c507882009-04-15 21:30:51 +00001185 // If the declaration had any attributes, write them now.
1186 if (D->hasAttrs())
1187 WriteAttributeRecord(D->getAttrs());
1188
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001189 // Flush any expressions that were written as part of this declaration.
1190 FlushExprs();
1191
Douglas Gregor631f6c62009-04-14 00:24:19 +00001192 // Note external declarations so that we can add them to a record
1193 // in the PCH file later.
1194 if (isa<FileScopeAsmDecl>(D))
1195 ExternalDefinitions.push_back(ID);
1196 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1197 if (// Non-static file-scope variables with initializers or that
1198 // are tentative definitions.
1199 (Var->isFileVarDecl() &&
1200 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1201 // Out-of-line definitions of static data members (C++).
1202 (Var->getDeclContext()->isRecord() &&
1203 !Var->getLexicalDeclContext()->isRecord() &&
1204 Var->getStorageClass() == VarDecl::Static))
1205 ExternalDefinitions.push_back(ID);
1206 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1207 if (Func->isThisDeclarationADefinition() &&
1208 Func->getStorageClass() != FunctionDecl::Static &&
1209 !Func->isInline())
1210 ExternalDefinitions.push_back(ID);
1211 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001212 }
1213
1214 // Exit the declarations block
1215 S.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001216}
1217
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001218/// \brief Write the identifier table into the PCH file.
1219///
1220/// The identifier table consists of a blob containing string data
1221/// (the actual identifiers themselves) and a separate "offsets" index
1222/// that maps identifier IDs to locations within the blob.
1223void PCHWriter::WriteIdentifierTable() {
1224 using namespace llvm;
1225
1226 // Create and write out the blob that contains the identifier
1227 // strings.
1228 RecordData IdentOffsets;
1229 IdentOffsets.resize(IdentifierIDs.size());
1230 {
1231 // Create the identifier string data.
1232 std::vector<char> Data;
1233 Data.push_back(0); // Data must not be empty.
1234 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1235 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1236 ID != IDEnd; ++ID) {
1237 assert(ID->first && "NULL identifier in identifier table");
1238
1239 // Make sure we're starting on an odd byte. The PCH reader
1240 // expects the low bit to be set on all of the offsets.
1241 if ((Data.size() & 0x01) == 0)
1242 Data.push_back((char)0);
1243
1244 IdentOffsets[ID->second - 1] = Data.size();
1245 Data.insert(Data.end(),
1246 ID->first->getName(),
1247 ID->first->getName() + ID->first->getLength());
1248 Data.push_back((char)0);
1249 }
1250
1251 // Create a blob abbreviation
1252 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1253 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
1255 unsigned IDTableAbbrev = S.EmitAbbrev(Abbrev);
1256
1257 // Write the identifier table
1258 RecordData Record;
1259 Record.push_back(pch::IDENTIFIER_TABLE);
1260 S.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
1261 }
1262
1263 // Write the offsets table for identifier IDs.
1264 S.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
1265}
1266
Douglas Gregor1c507882009-04-15 21:30:51 +00001267/// \brief Write a record containing the given attributes.
1268void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1269 RecordData Record;
1270 for (; Attr; Attr = Attr->getNext()) {
1271 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1272 Record.push_back(Attr->isInherited());
1273 switch (Attr->getKind()) {
1274 case Attr::Alias:
1275 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1276 break;
1277
1278 case Attr::Aligned:
1279 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1280 break;
1281
1282 case Attr::AlwaysInline:
1283 break;
1284
1285 case Attr::AnalyzerNoReturn:
1286 break;
1287
1288 case Attr::Annotate:
1289 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1290 break;
1291
1292 case Attr::AsmLabel:
1293 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1294 break;
1295
1296 case Attr::Blocks:
1297 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1298 break;
1299
1300 case Attr::Cleanup:
1301 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1302 break;
1303
1304 case Attr::Const:
1305 break;
1306
1307 case Attr::Constructor:
1308 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1309 break;
1310
1311 case Attr::DLLExport:
1312 case Attr::DLLImport:
1313 case Attr::Deprecated:
1314 break;
1315
1316 case Attr::Destructor:
1317 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1318 break;
1319
1320 case Attr::FastCall:
1321 break;
1322
1323 case Attr::Format: {
1324 const FormatAttr *Format = cast<FormatAttr>(Attr);
1325 AddString(Format->getType(), Record);
1326 Record.push_back(Format->getFormatIdx());
1327 Record.push_back(Format->getFirstArg());
1328 break;
1329 }
1330
1331 case Attr::GNUCInline:
1332 case Attr::IBOutletKind:
1333 case Attr::NoReturn:
1334 case Attr::NoThrow:
1335 case Attr::Nodebug:
1336 case Attr::Noinline:
1337 break;
1338
1339 case Attr::NonNull: {
1340 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1341 Record.push_back(NonNull->size());
1342 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1343 break;
1344 }
1345
1346 case Attr::ObjCException:
1347 case Attr::ObjCNSObject:
1348 case Attr::Overloadable:
1349 break;
1350
1351 case Attr::Packed:
1352 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1353 break;
1354
1355 case Attr::Pure:
1356 break;
1357
1358 case Attr::Regparm:
1359 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1360 break;
1361
1362 case Attr::Section:
1363 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1364 break;
1365
1366 case Attr::StdCall:
1367 case Attr::TransparentUnion:
1368 case Attr::Unavailable:
1369 case Attr::Unused:
1370 case Attr::Used:
1371 break;
1372
1373 case Attr::Visibility:
1374 // FIXME: stable encoding
1375 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1376 break;
1377
1378 case Attr::WarnUnusedResult:
1379 case Attr::Weak:
1380 case Attr::WeakImport:
1381 break;
1382 }
1383 }
1384
1385 assert((int)pch::DECL_ATTR == (int)pch::TYPE_ATTR &&
1386 "DECL_ATTR/TYPE_ATTR mismatch");
1387 S.EmitRecord(pch::DECL_ATTR, Record);
1388}
1389
1390void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1391 Record.push_back(Str.size());
1392 Record.insert(Record.end(), Str.begin(), Str.end());
1393}
1394
Douglas Gregorc34897d2009-04-09 22:27:44 +00001395PCHWriter::PCHWriter(llvm::BitstreamWriter &S)
1396 : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
1397
Chris Lattner850eabd2009-04-10 18:08:30 +00001398void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001399 // Emit the file header.
1400 S.Emit((unsigned)'C', 8);
1401 S.Emit((unsigned)'P', 8);
1402 S.Emit((unsigned)'C', 8);
1403 S.Emit((unsigned)'H', 8);
1404
1405 // The translation unit is the first declaration we'll emit.
1406 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1407 DeclsToEmit.push(Context.getTranslationUnitDecl());
1408
1409 // Write the remaining PCH contents.
Douglas Gregorb5887f32009-04-10 21:16:55 +00001410 S.EnterSubblock(pch::PCH_BLOCK_ID, 3);
1411 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001412 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001413 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001414 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001415 WriteTypesBlock(Context);
1416 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001417 WriteIdentifierTable();
Douglas Gregor179cfb12009-04-10 20:39:37 +00001418 S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1419 S.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001420 if (!ExternalDefinitions.empty())
1421 S.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001422 S.ExitBlock();
1423}
1424
1425void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1426 Record.push_back(Loc.getRawEncoding());
1427}
1428
1429void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1430 Record.push_back(Value.getBitWidth());
1431 unsigned N = Value.getNumWords();
1432 const uint64_t* Words = Value.getRawData();
1433 for (unsigned I = 0; I != N; ++I)
1434 Record.push_back(Words[I]);
1435}
1436
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001437void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1438 Record.push_back(Value.isUnsigned());
1439 AddAPInt(Value, Record);
1440}
1441
Douglas Gregore2f37202009-04-14 21:55:33 +00001442void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1443 AddAPInt(Value.bitcastToAPInt(), Record);
1444}
1445
Douglas Gregorc34897d2009-04-09 22:27:44 +00001446void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001447 if (II == 0) {
1448 Record.push_back(0);
1449 return;
1450 }
1451
1452 pch::IdentID &ID = IdentifierIDs[II];
1453 if (ID == 0)
1454 ID = IdentifierIDs.size();
1455
1456 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001457}
1458
1459void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1460 if (T.isNull()) {
1461 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1462 return;
1463 }
1464
1465 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001466 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001467 switch (BT->getKind()) {
1468 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1469 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1470 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1471 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1472 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1473 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1474 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1475 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1476 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1477 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1478 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1479 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1480 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1481 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1482 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1483 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1484 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1485 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1486 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1487 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1488 }
1489
1490 Record.push_back((ID << 3) | T.getCVRQualifiers());
1491 return;
1492 }
1493
Douglas Gregorac8f2802009-04-10 17:25:41 +00001494 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001495 if (ID == 0) // we haven't seen this type before
1496 ID = NextTypeID++;
1497
1498 // Encode the type qualifiers in the type reference.
1499 Record.push_back((ID << 3) | T.getCVRQualifiers());
1500}
1501
1502void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1503 if (D == 0) {
1504 Record.push_back(0);
1505 return;
1506 }
1507
Douglas Gregorac8f2802009-04-10 17:25:41 +00001508 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001509 if (ID == 0) {
1510 // We haven't seen this declaration before. Give it a new ID and
1511 // enqueue it in the list of declarations to emit.
1512 ID = DeclIDs.size();
1513 DeclsToEmit.push(const_cast<Decl *>(D));
1514 }
1515
1516 Record.push_back(ID);
1517}
1518
1519void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1520 Record.push_back(Name.getNameKind());
1521 switch (Name.getNameKind()) {
1522 case DeclarationName::Identifier:
1523 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1524 break;
1525
1526 case DeclarationName::ObjCZeroArgSelector:
1527 case DeclarationName::ObjCOneArgSelector:
1528 case DeclarationName::ObjCMultiArgSelector:
1529 assert(false && "Serialization of Objective-C selectors unavailable");
1530 break;
1531
1532 case DeclarationName::CXXConstructorName:
1533 case DeclarationName::CXXDestructorName:
1534 case DeclarationName::CXXConversionFunctionName:
1535 AddTypeRef(Name.getCXXNameType(), Record);
1536 break;
1537
1538 case DeclarationName::CXXOperatorName:
1539 Record.push_back(Name.getCXXOverloadedOperator());
1540 break;
1541
1542 case DeclarationName::CXXUsingDirective:
1543 // No extra data to emit
1544 break;
1545 }
1546}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001547
Douglas Gregora151ba42009-04-14 23:32:43 +00001548/// \brief Write the given subexpression to the bitstream.
1549void PCHWriter::WriteSubExpr(Expr *E) {
1550 RecordData Record;
1551 PCHStmtWriter Writer(*this, Record);
1552
1553 if (!E) {
1554 S.EmitRecord(pch::EXPR_NULL, Record);
1555 return;
1556 }
1557
1558 Writer.Code = pch::EXPR_NULL;
1559 Writer.Visit(E);
1560 assert(Writer.Code != pch::EXPR_NULL &&
1561 "Unhandled expression writing PCH file");
1562 S.EmitRecord(Writer.Code, Record);
1563}
1564
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001565/// \brief Flush all of the expressions that have been added to the
1566/// queue via AddExpr().
1567void PCHWriter::FlushExprs() {
1568 RecordData Record;
1569 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001570
Douglas Gregora151ba42009-04-14 23:32:43 +00001571 for (unsigned I = 0, N = ExprsToEmit.size(); I != N; ++I) {
1572 Expr *E = ExprsToEmit[I];
1573
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001574 if (!E) {
1575 S.EmitRecord(pch::EXPR_NULL, Record);
1576 continue;
1577 }
1578
1579 Writer.Code = pch::EXPR_NULL;
1580 Writer.Visit(E);
1581 assert(Writer.Code != pch::EXPR_NULL &&
1582 "Unhandled expression writing PCH file");
1583 S.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001584
1585 assert(N == ExprsToEmit.size() &&
1586 "Subexpression writen via AddExpr rather than WriteSubExpr!");
1587
1588 // Note that we are at the end of a full expression. Any
1589 // expression records that follow this one are part of a different
1590 // expression.
1591 Record.clear();
1592 S.EmitRecord(pch::EXPR_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001593 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001594
1595 ExprsToEmit.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001596}