blob: dc5aabdd5ba7bc8b8ac5cd3c5ed2a24a0fb0446b [file] [log] [blame]
Douglas Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000022#include "clang/Lex/MacroInfo.h"
23#include "clang/Lex/Preprocessor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000028#include "llvm/ADT/APFloat.h"
29#include "llvm/ADT/APInt.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000030#include "llvm/Bitcode/BitstreamWriter.h"
31#include "llvm/Support/Compiler.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "llvm/Support/MemoryBuffer.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000033#include <cstdio>
Douglas Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +0000129 Writer.AddExpr(T->getSizeExpr());
Douglas Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +0000169 Writer.AddExpr(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-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 Gregor6a2bfb22009-04-15 18:43:11 +0000197 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198 assert(false && "Cannot serialize template specialization types");
199}
200
201void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000202 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-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 Gregor0a2b45e2009-04-13 18:14:40 +0000257 void VisitTagDecl(TagDecl *D);
258 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000259 void VisitRecordDecl(RecordDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000260 void VisitValueDecl(ValueDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000261 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000262 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000263 void VisitFieldDecl(FieldDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264 void VisitVarDecl(VarDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000265 void VisitParmVarDecl(ParmVarDecl *D);
266 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor1028bc62009-04-13 22:49:25 +0000267 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
268 void VisitBlockDecl(BlockDecl *D);
Douglas Gregor2cf26342009-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 Gregor68a2eb02009-04-15 21:30:51 +0000279 Record.push_back(D->hasAttrs());
Douglas Gregor2cf26342009-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 Gregor0a2b45e2009-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 Gregor8c700062009-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 Gregor2cf26342009-04-09 22:27:44 +0000325void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
326 VisitNamedDecl(D);
327 Writer.AddTypeRef(D->getType(), Record);
328}
329
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000330void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
331 VisitValueDecl(D);
Douglas Gregor0b748912009-04-14 21:18:50 +0000332 Record.push_back(D->getInitExpr()? 1 : 0);
333 if (D->getInitExpr())
334 Writer.AddExpr(D->getInitExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000335 Writer.AddAPSInt(D->getInitVal(), Record);
336 Code = pch::DECL_ENUM_CONSTANT;
337}
338
Douglas Gregor3a2f7e42009-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 Gregor8c700062009-04-13 21:20:57 +0000358void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
359 VisitValueDecl(D);
360 Record.push_back(D->isMutable());
Douglas Gregor0b748912009-04-14 21:18:50 +0000361 Record.push_back(D->getBitWidth()? 1 : 0);
362 if (D->getBitWidth())
363 Writer.AddExpr(D->getBitWidth());
Douglas Gregor8c700062009-04-13 21:20:57 +0000364 Code = pch::DECL_FIELD;
365}
366
Douglas Gregor2cf26342009-04-09 22:27:44 +0000367void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
368 VisitValueDecl(D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000369 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +0000375 Record.push_back(D->getInit()? 1 : 0);
376 if (D->getInit())
377 Writer.AddExpr(D->getInit());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000378 Code = pch::DECL_VAR;
379}
380
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000381void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
382 VisitVarDecl(D);
383 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000384 // FIXME: emit default argument (C++)
Douglas Gregor3a2f7e42009-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 Gregor1028bc62009-04-13 22:49:25 +0000396void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
397 VisitDecl(D);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000398 Writer.AddExpr(D->getAsmString());
Douglas Gregor1028bc62009-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 Gregor2cf26342009-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 Gregor0b748912009-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 Gregor17fc2232009-04-14 21:55:33 +0000447 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000448 void VisitDeclRefExpr(DeclRefExpr *E);
449 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000450 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000451 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000452 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000453 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000454 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000455 void VisitUnaryOperator(UnaryOperator *E);
456 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000457 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000458 void VisitCallExpr(CallExpr *E);
459 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000460 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000461 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000462 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
463 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000464 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000465 void VisitExplicitCastExpr(ExplicitCastExpr *E);
466 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000467 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
468 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000469 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
470 void VisitChooseExpr(ChooseExpr *E);
471 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000472 };
473}
474
475void PCHStmtWriter::VisitExpr(Expr *E) {
476 Writer.AddTypeRef(E->getType(), Record);
477 Record.push_back(E->isTypeDependent());
478 Record.push_back(E->isValueDependent());
479}
480
Douglas Gregor17fc2232009-04-14 21:55:33 +0000481void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
482 VisitExpr(E);
483 Writer.AddSourceLocation(E->getLocation(), Record);
484 Record.push_back(E->getIdentType()); // FIXME: stable encoding
485 Code = pch::EXPR_PREDEFINED;
486}
487
Douglas Gregor0b748912009-04-14 21:18:50 +0000488void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
489 VisitExpr(E);
490 Writer.AddDeclRef(E->getDecl(), Record);
491 Writer.AddSourceLocation(E->getLocation(), Record);
492 Code = pch::EXPR_DECL_REF;
493}
494
495void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
496 VisitExpr(E);
497 Writer.AddSourceLocation(E->getLocation(), Record);
498 Writer.AddAPInt(E->getValue(), Record);
499 Code = pch::EXPR_INTEGER_LITERAL;
500}
501
Douglas Gregor17fc2232009-04-14 21:55:33 +0000502void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
503 VisitExpr(E);
504 Writer.AddAPFloat(E->getValue(), Record);
505 Record.push_back(E->isExact());
506 Writer.AddSourceLocation(E->getLocation(), Record);
507 Code = pch::EXPR_FLOATING_LITERAL;
508}
509
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000510void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
511 VisitExpr(E);
512 Writer.WriteSubExpr(E->getSubExpr());
513 Code = pch::EXPR_IMAGINARY_LITERAL;
514}
515
Douglas Gregor673ecd62009-04-15 16:35:07 +0000516void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
517 VisitExpr(E);
518 Record.push_back(E->getByteLength());
519 Record.push_back(E->getNumConcatenated());
520 Record.push_back(E->isWide());
521 // FIXME: String data should be stored as a blob at the end of the
522 // StringLiteral. However, we can't do so now because we have no
523 // provision for coping with abbreviations when we're jumping around
524 // the PCH file during deserialization.
525 Record.insert(Record.end(),
526 E->getStrData(), E->getStrData() + E->getByteLength());
527 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
528 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
529 Code = pch::EXPR_STRING_LITERAL;
530}
531
Douglas Gregor0b748912009-04-14 21:18:50 +0000532void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
533 VisitExpr(E);
534 Record.push_back(E->getValue());
535 Writer.AddSourceLocation(E->getLoc(), Record);
536 Record.push_back(E->isWide());
537 Code = pch::EXPR_CHARACTER_LITERAL;
538}
539
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000540void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
541 VisitExpr(E);
542 Writer.AddSourceLocation(E->getLParen(), Record);
543 Writer.AddSourceLocation(E->getRParen(), Record);
544 Writer.WriteSubExpr(E->getSubExpr());
545 Code = pch::EXPR_PAREN;
546}
547
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000548void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
549 VisitExpr(E);
550 Writer.WriteSubExpr(E->getSubExpr());
551 Record.push_back(E->getOpcode()); // FIXME: stable encoding
552 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
553 Code = pch::EXPR_UNARY_OPERATOR;
554}
555
556void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
557 VisitExpr(E);
558 Record.push_back(E->isSizeOf());
559 if (E->isArgumentType())
560 Writer.AddTypeRef(E->getArgumentType(), Record);
561 else {
562 Record.push_back(0);
563 Writer.WriteSubExpr(E->getArgumentExpr());
564 }
565 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
566 Writer.AddSourceLocation(E->getRParenLoc(), Record);
567 Code = pch::EXPR_SIZEOF_ALIGN_OF;
568}
569
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000570void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
571 VisitExpr(E);
572 Writer.WriteSubExpr(E->getLHS());
573 Writer.WriteSubExpr(E->getRHS());
574 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
575 Code = pch::EXPR_ARRAY_SUBSCRIPT;
576}
577
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000578void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
579 VisitExpr(E);
580 Record.push_back(E->getNumArgs());
581 Writer.AddSourceLocation(E->getRParenLoc(), Record);
582 Writer.WriteSubExpr(E->getCallee());
583 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
584 Arg != ArgEnd; ++Arg)
585 Writer.WriteSubExpr(*Arg);
586 Code = pch::EXPR_CALL;
587}
588
589void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
590 VisitExpr(E);
591 Writer.WriteSubExpr(E->getBase());
592 Writer.AddDeclRef(E->getMemberDecl(), Record);
593 Writer.AddSourceLocation(E->getMemberLoc(), Record);
594 Record.push_back(E->isArrow());
595 Code = pch::EXPR_MEMBER;
596}
597
Douglas Gregor087fd532009-04-14 23:32:43 +0000598void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
599 VisitExpr(E);
600 Writer.WriteSubExpr(E->getSubExpr());
601}
602
Douglas Gregordb600c32009-04-15 00:25:59 +0000603void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
604 VisitExpr(E);
605 Writer.WriteSubExpr(E->getLHS());
606 Writer.WriteSubExpr(E->getRHS());
607 Record.push_back(E->getOpcode()); // FIXME: stable encoding
608 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
609 Code = pch::EXPR_BINARY_OPERATOR;
610}
611
Douglas Gregorad90e962009-04-15 22:40:36 +0000612void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
613 VisitBinaryOperator(E);
614 Writer.AddTypeRef(E->getComputationLHSType(), Record);
615 Writer.AddTypeRef(E->getComputationResultType(), Record);
616 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
617}
618
619void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
620 VisitExpr(E);
621 Writer.WriteSubExpr(E->getCond());
622 Writer.WriteSubExpr(E->getLHS());
623 Writer.WriteSubExpr(E->getRHS());
624 Code = pch::EXPR_CONDITIONAL_OPERATOR;
625}
626
Douglas Gregor087fd532009-04-14 23:32:43 +0000627void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
628 VisitCastExpr(E);
629 Record.push_back(E->isLvalueCast());
630 Code = pch::EXPR_IMPLICIT_CAST;
631}
632
Douglas Gregordb600c32009-04-15 00:25:59 +0000633void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
634 VisitCastExpr(E);
635 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
636}
637
638void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
639 VisitExplicitCastExpr(E);
640 Writer.AddSourceLocation(E->getLParenLoc(), Record);
641 Writer.AddSourceLocation(E->getRParenLoc(), Record);
642 Code = pch::EXPR_CSTYLE_CAST;
643}
644
Douglas Gregord3c98a02009-04-15 23:02:49 +0000645void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
646 VisitExpr(E);
647 Writer.WriteSubExpr(E->getBase());
648 Writer.AddIdentifierRef(&E->getAccessor(), Record);
649 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
650 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
651}
652
653void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
654 VisitExpr(E);
655 Writer.WriteSubExpr(E->getSubExpr());
656 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
657 Writer.AddSourceLocation(E->getRParenLoc(), Record);
658 Code = pch::EXPR_VA_ARG;
659}
660
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000661void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
662 VisitExpr(E);
663 Writer.AddTypeRef(E->getArgType1(), Record);
664 Writer.AddTypeRef(E->getArgType2(), Record);
665 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
666 Writer.AddSourceLocation(E->getRParenLoc(), Record);
667 Code = pch::EXPR_TYPES_COMPATIBLE;
668}
669
670void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
671 VisitExpr(E);
672 Writer.WriteSubExpr(E->getCond());
673 Writer.WriteSubExpr(E->getLHS());
674 Writer.WriteSubExpr(E->getRHS());
675 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
676 Writer.AddSourceLocation(E->getRParenLoc(), Record);
677 Code = pch::EXPR_CHOOSE;
678}
679
680void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
681 VisitExpr(E);
682 Writer.AddSourceLocation(E->getTokenLocation(), Record);
683 Code = pch::EXPR_GNU_NULL;
684}
685
Douglas Gregor0b748912009-04-14 21:18:50 +0000686//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000687// PCHWriter Implementation
688//===----------------------------------------------------------------------===//
689
Douglas Gregor2bec0412009-04-10 21:16:55 +0000690/// \brief Write the target triple (e.g., i686-apple-darwin9).
691void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
692 using namespace llvm;
693 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
694 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
695 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
696 unsigned TripleAbbrev = S.EmitAbbrev(Abbrev);
697
698 RecordData Record;
699 Record.push_back(pch::TARGET_TRIPLE);
700 const char *Triple = Target.getTargetTriple();
701 S.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
702}
703
704/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000705void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
706 RecordData Record;
707 Record.push_back(LangOpts.Trigraphs);
708 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
709 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
710 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
711 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
712 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
713 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
714 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
715 Record.push_back(LangOpts.C99); // C99 Support
716 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
717 Record.push_back(LangOpts.CPlusPlus); // C++ Support
718 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
719 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
720 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
721
722 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
723 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
724 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
725
726 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
727 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
728 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
729 Record.push_back(LangOpts.LaxVectorConversions);
730 Record.push_back(LangOpts.Exceptions); // Support exception handling.
731
732 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
733 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
734 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
735
736 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
737 // by locks.
738 Record.push_back(LangOpts.Blocks); // block extension to C
739 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
740 // they are unused.
741 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
742 // (modulo the platform support).
743
744 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
745 // signed integer arithmetic overflows.
746
747 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
748 // may be ripped out at any time.
749
750 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
751 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
752 // defined.
753 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
754 // opposed to __DYNAMIC__).
755 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
756
757 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
758 // used (instead of C99 semantics).
759 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
760 Record.push_back(LangOpts.getGCMode());
761 Record.push_back(LangOpts.getVisibilityMode());
762 Record.push_back(LangOpts.InstantiationDepth);
763 S.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
764}
765
Douglas Gregor14f79002009-04-10 03:52:48 +0000766//===----------------------------------------------------------------------===//
767// Source Manager Serialization
768//===----------------------------------------------------------------------===//
769
770/// \brief Create an abbreviation for the SLocEntry that refers to a
771/// file.
772static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) {
773 using namespace llvm;
774 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
775 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
781 return S.EmitAbbrev(Abbrev);
782}
783
784/// \brief Create an abbreviation for the SLocEntry that refers to a
785/// buffer.
786static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &S) {
787 using namespace llvm;
788 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
789 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
795 return S.EmitAbbrev(Abbrev);
796}
797
798/// \brief Create an abbreviation for the SLocEntry that refers to a
799/// buffer's blob.
800static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &S) {
801 using namespace llvm;
802 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
803 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
805 return S.EmitAbbrev(Abbrev);
806}
807
808/// \brief Create an abbreviation for the SLocEntry that refers to an
809/// buffer.
810static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &S) {
811 using namespace llvm;
812 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
813 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
817 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000818 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor14f79002009-04-10 03:52:48 +0000819 return S.EmitAbbrev(Abbrev);
820}
821
822/// \brief Writes the block containing the serialized form of the
823/// source manager.
824///
825/// TODO: We should probably use an on-disk hash table (stored in a
826/// blob), indexed based on the file name, so that we only create
827/// entries for files that we actually need. In the common case (no
828/// errors), we probably won't have to create file entries for any of
829/// the files in the AST.
830void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +0000831 // Enter the source manager block.
Douglas Gregor14f79002009-04-10 03:52:48 +0000832 S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
833
834 // Abbreviations for the various kinds of source-location entries.
835 int SLocFileAbbrv = -1;
836 int SLocBufferAbbrv = -1;
837 int SLocBufferBlobAbbrv = -1;
838 int SLocInstantiationAbbrv = -1;
839
840 // Write out the source location entry table. We skip the first
841 // entry, which is always the same dummy entry.
842 RecordData Record;
843 for (SourceManager::sloc_entry_iterator
844 SLoc = SourceMgr.sloc_entry_begin() + 1,
845 SLocEnd = SourceMgr.sloc_entry_end();
846 SLoc != SLocEnd; ++SLoc) {
847 // Figure out which record code to use.
848 unsigned Code;
849 if (SLoc->isFile()) {
850 if (SLoc->getFile().getContentCache()->Entry)
851 Code = pch::SM_SLOC_FILE_ENTRY;
852 else
853 Code = pch::SM_SLOC_BUFFER_ENTRY;
854 } else
855 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
856 Record.push_back(Code);
857
858 Record.push_back(SLoc->getOffset());
859 if (SLoc->isFile()) {
860 const SrcMgr::FileInfo &File = SLoc->getFile();
861 Record.push_back(File.getIncludeLoc().getRawEncoding());
862 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +0000863 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +0000864
865 const SrcMgr::ContentCache *Content = File.getContentCache();
866 if (Content->Entry) {
867 // The source location entry is a file. The blob associated
868 // with this entry is the file name.
869 if (SLocFileAbbrv == -1)
870 SLocFileAbbrv = CreateSLocFileAbbrev(S);
871 S.EmitRecordWithBlob(SLocFileAbbrv, Record,
872 Content->Entry->getName(),
873 strlen(Content->Entry->getName()));
874 } else {
875 // The source location entry is a buffer. The blob associated
876 // with this entry contains the contents of the buffer.
877 if (SLocBufferAbbrv == -1) {
878 SLocBufferAbbrv = CreateSLocBufferAbbrev(S);
879 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S);
880 }
881
882 // We add one to the size so that we capture the trailing NULL
883 // that is required by llvm::MemoryBuffer::getMemBuffer (on
884 // the reader side).
885 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
886 const char *Name = Buffer->getBufferIdentifier();
887 S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
888 Record.clear();
889 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
890 S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
891 Buffer->getBufferStart(),
892 Buffer->getBufferSize() + 1);
893 }
894 } else {
895 // The source location entry is an instantiation.
896 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
897 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
898 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
899 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
900
Douglas Gregorf60e9912009-04-15 18:05:10 +0000901 // Compute the token length for this macro expansion.
902 unsigned NextOffset = SourceMgr.getNextOffset();
903 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
904 if (++NextSLoc != SLocEnd)
905 NextOffset = NextSLoc->getOffset();
906 Record.push_back(NextOffset - SLoc->getOffset() - 1);
907
Douglas Gregor14f79002009-04-10 03:52:48 +0000908 if (SLocInstantiationAbbrv == -1)
909 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S);
910 S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
911 }
912
913 Record.clear();
914 }
915
Douglas Gregorbd945002009-04-13 16:31:14 +0000916 // Write the line table.
917 if (SourceMgr.hasLineTable()) {
918 LineTableInfo &LineTable = SourceMgr.getLineTable();
919
920 // Emit the file names
921 Record.push_back(LineTable.getNumFilenames());
922 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
923 // Emit the file name
924 const char *Filename = LineTable.getFilename(I);
925 unsigned FilenameLen = Filename? strlen(Filename) : 0;
926 Record.push_back(FilenameLen);
927 if (FilenameLen)
928 Record.insert(Record.end(), Filename, Filename + FilenameLen);
929 }
930
931 // Emit the line entries
932 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
933 L != LEnd; ++L) {
934 // Emit the file ID
935 Record.push_back(L->first);
936
937 // Emit the line entries
938 Record.push_back(L->second.size());
939 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
940 LEEnd = L->second.end();
941 LE != LEEnd; ++LE) {
942 Record.push_back(LE->FileOffset);
943 Record.push_back(LE->LineNo);
944 Record.push_back(LE->FilenameID);
945 Record.push_back((unsigned)LE->FileKind);
946 Record.push_back(LE->IncludeOffset);
947 }
948 S.EmitRecord(pch::SM_LINE_TABLE, Record);
949 }
950 }
951
Douglas Gregor14f79002009-04-10 03:52:48 +0000952 S.ExitBlock();
953}
954
Chris Lattner0b1fb982009-04-10 17:15:23 +0000955/// \brief Writes the block containing the serialized form of the
956/// preprocessor.
957///
Chris Lattnerdf961c22009-04-10 18:08:30 +0000958void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +0000959 // Enter the preprocessor block.
960 S.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
961
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000962 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
963 // FIXME: use diagnostics subsystem for localization etc.
964 if (PP.SawDateOrTime())
965 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +0000966
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000967 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +0000968
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000969 // If the preprocessor __COUNTER__ value has been bumped, remember it.
970 if (PP.getCounterValue() != 0) {
971 Record.push_back(PP.getCounterValue());
972 S.EmitRecord(pch::PP_COUNTER_VALUE, Record);
973 Record.clear();
974 }
975
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000976 // Loop over all the macro definitions that are live at the end of the file,
977 // emitting each to the PP section.
978 // FIXME: Eventually we want to emit an index so that we can lazily load
979 // macros.
980 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
981 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +0000982 // FIXME: This emits macros in hash table order, we should do it in a stable
983 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000984 MacroInfo *MI = I->second;
985
986 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
987 // been redefined by the header (in which case they are not isBuiltinMacro).
988 if (MI->isBuiltinMacro())
989 continue;
990
Chris Lattner7356a312009-04-11 21:15:38 +0000991 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000992 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
993 Record.push_back(MI->isUsed());
994
995 unsigned Code;
996 if (MI->isObjectLike()) {
997 Code = pch::PP_MACRO_OBJECT_LIKE;
998 } else {
999 Code = pch::PP_MACRO_FUNCTION_LIKE;
1000
1001 Record.push_back(MI->isC99Varargs());
1002 Record.push_back(MI->isGNUVarargs());
1003 Record.push_back(MI->getNumArgs());
1004 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1005 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001006 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001007 }
1008 S.EmitRecord(Code, Record);
1009 Record.clear();
1010
Chris Lattnerdf961c22009-04-10 18:08:30 +00001011 // Emit the tokens array.
1012 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1013 // Note that we know that the preprocessor does not have any annotation
1014 // tokens in it because they are created by the parser, and thus can't be
1015 // in a macro definition.
1016 const Token &Tok = MI->getReplacementToken(TokNo);
1017
1018 Record.push_back(Tok.getLocation().getRawEncoding());
1019 Record.push_back(Tok.getLength());
1020
Chris Lattnerdf961c22009-04-10 18:08:30 +00001021 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1022 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001023 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001024
1025 // FIXME: Should translate token kind to a stable encoding.
1026 Record.push_back(Tok.getKind());
1027 // FIXME: Should translate token flags to a stable encoding.
1028 Record.push_back(Tok.getFlags());
1029
1030 S.EmitRecord(pch::PP_TOKEN, Record);
1031 Record.clear();
1032 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001033
1034 }
1035
Chris Lattnerf04ad692009-04-10 17:16:57 +00001036 S.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001037}
1038
1039
Douglas Gregor2cf26342009-04-09 22:27:44 +00001040/// \brief Write the representation of a type to the PCH stream.
1041void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001042 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001043 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001044 ID = NextTypeID++;
1045
1046 // Record the offset for this type.
1047 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
1048 TypeOffsets.push_back(S.GetCurrentBitNo());
1049 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1050 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
1051 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo();
1052 }
1053
1054 RecordData Record;
1055
1056 // Emit the type's representation.
1057 PCHTypeWriter W(*this, Record);
1058 switch (T->getTypeClass()) {
1059 // For all of the concrete, non-dependent types, call the
1060 // appropriate visitor function.
1061#define TYPE(Class, Base) \
1062 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1063#define ABSTRACT_TYPE(Class, Base)
1064#define DEPENDENT_TYPE(Class, Base)
1065#include "clang/AST/TypeNodes.def"
1066
1067 // For all of the dependent type nodes (which only occur in C++
1068 // templates), produce an error.
1069#define TYPE(Class, Base)
1070#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1071#include "clang/AST/TypeNodes.def"
1072 assert(false && "Cannot serialize dependent type nodes");
1073 break;
1074 }
1075
1076 // Emit the serialized record.
1077 S.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001078
1079 // Flush any expressions that were written as part of this type.
1080 FlushExprs();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001081}
1082
1083/// \brief Write a block containing all of the types.
1084void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001085 // Enter the types block.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001086 S.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
1087
1088 // Emit all of the types in the ASTContext
1089 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1090 TEnd = Context.getTypes().end();
1091 T != TEnd; ++T) {
1092 // Builtin types are never serialized.
1093 if (isa<BuiltinType>(*T))
1094 continue;
1095
1096 WriteType(*T);
1097 }
1098
1099 // Exit the types block
1100 S.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001101}
1102
1103/// \brief Write the block containing all of the declaration IDs
1104/// lexically declared within the given DeclContext.
1105///
1106/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1107/// bistream, or 0 if no block was written.
1108uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1109 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001110 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001111 return 0;
1112
1113 uint64_t Offset = S.GetCurrentBitNo();
1114 RecordData Record;
1115 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1116 DEnd = DC->decls_end(Context);
1117 D != DEnd; ++D)
1118 AddDeclRef(*D, Record);
1119
1120 S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
1121 return Offset;
1122}
1123
1124/// \brief Write the block containing all of the declaration IDs
1125/// visible from the given DeclContext.
1126///
1127/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1128/// bistream, or 0 if no block was written.
1129uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1130 DeclContext *DC) {
1131 if (DC->getPrimaryContext() != DC)
1132 return 0;
1133
1134 // Force the DeclContext to build a its name-lookup table.
1135 DC->lookup(Context, DeclarationName());
1136
1137 // Serialize the contents of the mapping used for lookup. Note that,
1138 // although we have two very different code paths, the serialized
1139 // representation is the same for both cases: a declaration name,
1140 // followed by a size, followed by references to the visible
1141 // declarations that have that name.
1142 uint64_t Offset = S.GetCurrentBitNo();
1143 RecordData Record;
1144 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001145 if (!Map)
1146 return 0;
1147
Douglas Gregor2cf26342009-04-09 22:27:44 +00001148 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1149 D != DEnd; ++D) {
1150 AddDeclarationName(D->first, Record);
1151 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1152 Record.push_back(Result.second - Result.first);
1153 for(; Result.first != Result.second; ++Result.first)
1154 AddDeclRef(*Result.first, Record);
1155 }
1156
1157 if (Record.size() == 0)
1158 return 0;
1159
1160 S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
1161 return Offset;
1162}
1163
1164/// \brief Write a block containing all of the declarations.
1165void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001166 // Enter the declarations block.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001167 S.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
1168
1169 // Emit all of the declarations.
1170 RecordData Record;
1171 PCHDeclWriter W(*this, Record);
1172 while (!DeclsToEmit.empty()) {
1173 // Pull the next declaration off the queue
1174 Decl *D = DeclsToEmit.front();
1175 DeclsToEmit.pop();
1176
1177 // If this declaration is also a DeclContext, write blocks for the
1178 // declarations that lexically stored inside its context and those
1179 // declarations that are visible from its context. These blocks
1180 // are written before the declaration itself so that we can put
1181 // their offsets into the record for the declaration.
1182 uint64_t LexicalOffset = 0;
1183 uint64_t VisibleOffset = 0;
1184 DeclContext *DC = dyn_cast<DeclContext>(D);
1185 if (DC) {
1186 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1187 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1188 }
1189
1190 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001191 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001192 if (ID == 0)
1193 ID = DeclIDs.size();
1194
1195 unsigned Index = ID - 1;
1196
1197 // Record the offset for this declaration
1198 if (DeclOffsets.size() == Index)
1199 DeclOffsets.push_back(S.GetCurrentBitNo());
1200 else if (DeclOffsets.size() < Index) {
1201 DeclOffsets.resize(Index+1);
1202 DeclOffsets[Index] = S.GetCurrentBitNo();
1203 }
1204
1205 // Build and emit a record for this declaration
1206 Record.clear();
1207 W.Code = (pch::DeclCode)0;
1208 W.Visit(D);
1209 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001210 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregor2cf26342009-04-09 22:27:44 +00001211 S.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001212
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001213 // If the declaration had any attributes, write them now.
1214 if (D->hasAttrs())
1215 WriteAttributeRecord(D->getAttrs());
1216
Douglas Gregor0b748912009-04-14 21:18:50 +00001217 // Flush any expressions that were written as part of this declaration.
1218 FlushExprs();
1219
Douglas Gregorfdd01722009-04-14 00:24:19 +00001220 // Note external declarations so that we can add them to a record
1221 // in the PCH file later.
1222 if (isa<FileScopeAsmDecl>(D))
1223 ExternalDefinitions.push_back(ID);
1224 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1225 if (// Non-static file-scope variables with initializers or that
1226 // are tentative definitions.
1227 (Var->isFileVarDecl() &&
1228 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1229 // Out-of-line definitions of static data members (C++).
1230 (Var->getDeclContext()->isRecord() &&
1231 !Var->getLexicalDeclContext()->isRecord() &&
1232 Var->getStorageClass() == VarDecl::Static))
1233 ExternalDefinitions.push_back(ID);
1234 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1235 if (Func->isThisDeclarationADefinition() &&
1236 Func->getStorageClass() != FunctionDecl::Static &&
1237 !Func->isInline())
1238 ExternalDefinitions.push_back(ID);
1239 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001240 }
1241
1242 // Exit the declarations block
1243 S.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001244}
1245
Douglas Gregorafaf3082009-04-11 00:14:32 +00001246/// \brief Write the identifier table into the PCH file.
1247///
1248/// The identifier table consists of a blob containing string data
1249/// (the actual identifiers themselves) and a separate "offsets" index
1250/// that maps identifier IDs to locations within the blob.
1251void PCHWriter::WriteIdentifierTable() {
1252 using namespace llvm;
1253
1254 // Create and write out the blob that contains the identifier
1255 // strings.
1256 RecordData IdentOffsets;
1257 IdentOffsets.resize(IdentifierIDs.size());
1258 {
1259 // Create the identifier string data.
1260 std::vector<char> Data;
1261 Data.push_back(0); // Data must not be empty.
1262 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1263 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1264 ID != IDEnd; ++ID) {
1265 assert(ID->first && "NULL identifier in identifier table");
1266
1267 // Make sure we're starting on an odd byte. The PCH reader
1268 // expects the low bit to be set on all of the offsets.
1269 if ((Data.size() & 0x01) == 0)
1270 Data.push_back((char)0);
1271
1272 IdentOffsets[ID->second - 1] = Data.size();
1273 Data.insert(Data.end(),
1274 ID->first->getName(),
1275 ID->first->getName() + ID->first->getLength());
1276 Data.push_back((char)0);
1277 }
1278
1279 // Create a blob abbreviation
1280 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1281 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
1283 unsigned IDTableAbbrev = S.EmitAbbrev(Abbrev);
1284
1285 // Write the identifier table
1286 RecordData Record;
1287 Record.push_back(pch::IDENTIFIER_TABLE);
1288 S.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
1289 }
1290
1291 // Write the offsets table for identifier IDs.
1292 S.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
1293}
1294
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001295/// \brief Write a record containing the given attributes.
1296void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1297 RecordData Record;
1298 for (; Attr; Attr = Attr->getNext()) {
1299 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1300 Record.push_back(Attr->isInherited());
1301 switch (Attr->getKind()) {
1302 case Attr::Alias:
1303 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1304 break;
1305
1306 case Attr::Aligned:
1307 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1308 break;
1309
1310 case Attr::AlwaysInline:
1311 break;
1312
1313 case Attr::AnalyzerNoReturn:
1314 break;
1315
1316 case Attr::Annotate:
1317 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1318 break;
1319
1320 case Attr::AsmLabel:
1321 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1322 break;
1323
1324 case Attr::Blocks:
1325 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1326 break;
1327
1328 case Attr::Cleanup:
1329 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1330 break;
1331
1332 case Attr::Const:
1333 break;
1334
1335 case Attr::Constructor:
1336 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1337 break;
1338
1339 case Attr::DLLExport:
1340 case Attr::DLLImport:
1341 case Attr::Deprecated:
1342 break;
1343
1344 case Attr::Destructor:
1345 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1346 break;
1347
1348 case Attr::FastCall:
1349 break;
1350
1351 case Attr::Format: {
1352 const FormatAttr *Format = cast<FormatAttr>(Attr);
1353 AddString(Format->getType(), Record);
1354 Record.push_back(Format->getFormatIdx());
1355 Record.push_back(Format->getFirstArg());
1356 break;
1357 }
1358
1359 case Attr::GNUCInline:
1360 case Attr::IBOutletKind:
1361 case Attr::NoReturn:
1362 case Attr::NoThrow:
1363 case Attr::Nodebug:
1364 case Attr::Noinline:
1365 break;
1366
1367 case Attr::NonNull: {
1368 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1369 Record.push_back(NonNull->size());
1370 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1371 break;
1372 }
1373
1374 case Attr::ObjCException:
1375 case Attr::ObjCNSObject:
1376 case Attr::Overloadable:
1377 break;
1378
1379 case Attr::Packed:
1380 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1381 break;
1382
1383 case Attr::Pure:
1384 break;
1385
1386 case Attr::Regparm:
1387 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1388 break;
1389
1390 case Attr::Section:
1391 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1392 break;
1393
1394 case Attr::StdCall:
1395 case Attr::TransparentUnion:
1396 case Attr::Unavailable:
1397 case Attr::Unused:
1398 case Attr::Used:
1399 break;
1400
1401 case Attr::Visibility:
1402 // FIXME: stable encoding
1403 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1404 break;
1405
1406 case Attr::WarnUnusedResult:
1407 case Attr::Weak:
1408 case Attr::WeakImport:
1409 break;
1410 }
1411 }
1412
1413 assert((int)pch::DECL_ATTR == (int)pch::TYPE_ATTR &&
1414 "DECL_ATTR/TYPE_ATTR mismatch");
1415 S.EmitRecord(pch::DECL_ATTR, Record);
1416}
1417
1418void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1419 Record.push_back(Str.size());
1420 Record.insert(Record.end(), Str.begin(), Str.end());
1421}
1422
Douglas Gregor2cf26342009-04-09 22:27:44 +00001423PCHWriter::PCHWriter(llvm::BitstreamWriter &S)
1424 : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
1425
Chris Lattnerdf961c22009-04-10 18:08:30 +00001426void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001427 // Emit the file header.
1428 S.Emit((unsigned)'C', 8);
1429 S.Emit((unsigned)'P', 8);
1430 S.Emit((unsigned)'C', 8);
1431 S.Emit((unsigned)'H', 8);
1432
1433 // The translation unit is the first declaration we'll emit.
1434 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1435 DeclsToEmit.push(Context.getTranslationUnitDecl());
1436
1437 // Write the remaining PCH contents.
Douglas Gregor2bec0412009-04-10 21:16:55 +00001438 S.EnterSubblock(pch::PCH_BLOCK_ID, 3);
1439 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001440 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001441 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001442 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001443 WriteTypesBlock(Context);
1444 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001445 WriteIdentifierTable();
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001446 S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1447 S.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001448 if (!ExternalDefinitions.empty())
1449 S.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001450 S.ExitBlock();
1451}
1452
1453void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1454 Record.push_back(Loc.getRawEncoding());
1455}
1456
1457void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1458 Record.push_back(Value.getBitWidth());
1459 unsigned N = Value.getNumWords();
1460 const uint64_t* Words = Value.getRawData();
1461 for (unsigned I = 0; I != N; ++I)
1462 Record.push_back(Words[I]);
1463}
1464
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001465void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1466 Record.push_back(Value.isUnsigned());
1467 AddAPInt(Value, Record);
1468}
1469
Douglas Gregor17fc2232009-04-14 21:55:33 +00001470void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1471 AddAPInt(Value.bitcastToAPInt(), Record);
1472}
1473
Douglas Gregor2cf26342009-04-09 22:27:44 +00001474void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001475 if (II == 0) {
1476 Record.push_back(0);
1477 return;
1478 }
1479
1480 pch::IdentID &ID = IdentifierIDs[II];
1481 if (ID == 0)
1482 ID = IdentifierIDs.size();
1483
1484 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001485}
1486
1487void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1488 if (T.isNull()) {
1489 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1490 return;
1491 }
1492
1493 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001494 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001495 switch (BT->getKind()) {
1496 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1497 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1498 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1499 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1500 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1501 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1502 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1503 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1504 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1505 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1506 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1507 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1508 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1509 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1510 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1511 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1512 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1513 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1514 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1515 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1516 }
1517
1518 Record.push_back((ID << 3) | T.getCVRQualifiers());
1519 return;
1520 }
1521
Douglas Gregor8038d512009-04-10 17:25:41 +00001522 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001523 if (ID == 0) // we haven't seen this type before
1524 ID = NextTypeID++;
1525
1526 // Encode the type qualifiers in the type reference.
1527 Record.push_back((ID << 3) | T.getCVRQualifiers());
1528}
1529
1530void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1531 if (D == 0) {
1532 Record.push_back(0);
1533 return;
1534 }
1535
Douglas Gregor8038d512009-04-10 17:25:41 +00001536 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001537 if (ID == 0) {
1538 // We haven't seen this declaration before. Give it a new ID and
1539 // enqueue it in the list of declarations to emit.
1540 ID = DeclIDs.size();
1541 DeclsToEmit.push(const_cast<Decl *>(D));
1542 }
1543
1544 Record.push_back(ID);
1545}
1546
1547void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1548 Record.push_back(Name.getNameKind());
1549 switch (Name.getNameKind()) {
1550 case DeclarationName::Identifier:
1551 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1552 break;
1553
1554 case DeclarationName::ObjCZeroArgSelector:
1555 case DeclarationName::ObjCOneArgSelector:
1556 case DeclarationName::ObjCMultiArgSelector:
1557 assert(false && "Serialization of Objective-C selectors unavailable");
1558 break;
1559
1560 case DeclarationName::CXXConstructorName:
1561 case DeclarationName::CXXDestructorName:
1562 case DeclarationName::CXXConversionFunctionName:
1563 AddTypeRef(Name.getCXXNameType(), Record);
1564 break;
1565
1566 case DeclarationName::CXXOperatorName:
1567 Record.push_back(Name.getCXXOverloadedOperator());
1568 break;
1569
1570 case DeclarationName::CXXUsingDirective:
1571 // No extra data to emit
1572 break;
1573 }
1574}
Douglas Gregor0b748912009-04-14 21:18:50 +00001575
Douglas Gregor087fd532009-04-14 23:32:43 +00001576/// \brief Write the given subexpression to the bitstream.
1577void PCHWriter::WriteSubExpr(Expr *E) {
1578 RecordData Record;
1579 PCHStmtWriter Writer(*this, Record);
1580
1581 if (!E) {
1582 S.EmitRecord(pch::EXPR_NULL, Record);
1583 return;
1584 }
1585
1586 Writer.Code = pch::EXPR_NULL;
1587 Writer.Visit(E);
1588 assert(Writer.Code != pch::EXPR_NULL &&
1589 "Unhandled expression writing PCH file");
1590 S.EmitRecord(Writer.Code, Record);
1591}
1592
Douglas Gregor0b748912009-04-14 21:18:50 +00001593/// \brief Flush all of the expressions that have been added to the
1594/// queue via AddExpr().
1595void PCHWriter::FlushExprs() {
1596 RecordData Record;
1597 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001598
Douglas Gregor087fd532009-04-14 23:32:43 +00001599 for (unsigned I = 0, N = ExprsToEmit.size(); I != N; ++I) {
1600 Expr *E = ExprsToEmit[I];
1601
Douglas Gregor0b748912009-04-14 21:18:50 +00001602 if (!E) {
1603 S.EmitRecord(pch::EXPR_NULL, Record);
1604 continue;
1605 }
1606
1607 Writer.Code = pch::EXPR_NULL;
1608 Writer.Visit(E);
1609 assert(Writer.Code != pch::EXPR_NULL &&
1610 "Unhandled expression writing PCH file");
1611 S.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00001612
1613 assert(N == ExprsToEmit.size() &&
1614 "Subexpression writen via AddExpr rather than WriteSubExpr!");
1615
1616 // Note that we are at the end of a full expression. Any
1617 // expression records that follow this one are part of a different
1618 // expression.
1619 Record.clear();
1620 S.EmitRecord(pch::EXPR_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001621 }
Douglas Gregor087fd532009-04-14 23:32:43 +00001622
1623 ExprsToEmit.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00001624}