blob: f96288864bea4904b3ba9df0299a6a12132328ab [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);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000468 void VisitInitListExpr(InitListExpr *E);
469 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
470 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000471 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000472 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
473 void VisitChooseExpr(ChooseExpr *E);
474 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000475 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
476 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000477 };
478}
479
480void PCHStmtWriter::VisitExpr(Expr *E) {
481 Writer.AddTypeRef(E->getType(), Record);
482 Record.push_back(E->isTypeDependent());
483 Record.push_back(E->isValueDependent());
484}
485
Douglas Gregore2f37202009-04-14 21:55:33 +0000486void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
487 VisitExpr(E);
488 Writer.AddSourceLocation(E->getLocation(), Record);
489 Record.push_back(E->getIdentType()); // FIXME: stable encoding
490 Code = pch::EXPR_PREDEFINED;
491}
492
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000493void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
494 VisitExpr(E);
495 Writer.AddDeclRef(E->getDecl(), Record);
496 Writer.AddSourceLocation(E->getLocation(), Record);
497 Code = pch::EXPR_DECL_REF;
498}
499
500void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
501 VisitExpr(E);
502 Writer.AddSourceLocation(E->getLocation(), Record);
503 Writer.AddAPInt(E->getValue(), Record);
504 Code = pch::EXPR_INTEGER_LITERAL;
505}
506
Douglas Gregore2f37202009-04-14 21:55:33 +0000507void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
508 VisitExpr(E);
509 Writer.AddAPFloat(E->getValue(), Record);
510 Record.push_back(E->isExact());
511 Writer.AddSourceLocation(E->getLocation(), Record);
512 Code = pch::EXPR_FLOATING_LITERAL;
513}
514
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000515void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
516 VisitExpr(E);
517 Writer.WriteSubExpr(E->getSubExpr());
518 Code = pch::EXPR_IMAGINARY_LITERAL;
519}
520
Douglas Gregor596e0932009-04-15 16:35:07 +0000521void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
522 VisitExpr(E);
523 Record.push_back(E->getByteLength());
524 Record.push_back(E->getNumConcatenated());
525 Record.push_back(E->isWide());
526 // FIXME: String data should be stored as a blob at the end of the
527 // StringLiteral. However, we can't do so now because we have no
528 // provision for coping with abbreviations when we're jumping around
529 // the PCH file during deserialization.
530 Record.insert(Record.end(),
531 E->getStrData(), E->getStrData() + E->getByteLength());
532 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
533 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
534 Code = pch::EXPR_STRING_LITERAL;
535}
536
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000537void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
538 VisitExpr(E);
539 Record.push_back(E->getValue());
540 Writer.AddSourceLocation(E->getLoc(), Record);
541 Record.push_back(E->isWide());
542 Code = pch::EXPR_CHARACTER_LITERAL;
543}
544
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000545void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
546 VisitExpr(E);
547 Writer.AddSourceLocation(E->getLParen(), Record);
548 Writer.AddSourceLocation(E->getRParen(), Record);
549 Writer.WriteSubExpr(E->getSubExpr());
550 Code = pch::EXPR_PAREN;
551}
552
Douglas Gregor12d74052009-04-15 15:58:59 +0000553void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
554 VisitExpr(E);
555 Writer.WriteSubExpr(E->getSubExpr());
556 Record.push_back(E->getOpcode()); // FIXME: stable encoding
557 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
558 Code = pch::EXPR_UNARY_OPERATOR;
559}
560
561void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
562 VisitExpr(E);
563 Record.push_back(E->isSizeOf());
564 if (E->isArgumentType())
565 Writer.AddTypeRef(E->getArgumentType(), Record);
566 else {
567 Record.push_back(0);
568 Writer.WriteSubExpr(E->getArgumentExpr());
569 }
570 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
571 Writer.AddSourceLocation(E->getRParenLoc(), Record);
572 Code = pch::EXPR_SIZEOF_ALIGN_OF;
573}
574
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000575void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
576 VisitExpr(E);
577 Writer.WriteSubExpr(E->getLHS());
578 Writer.WriteSubExpr(E->getRHS());
579 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
580 Code = pch::EXPR_ARRAY_SUBSCRIPT;
581}
582
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000583void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
584 VisitExpr(E);
585 Record.push_back(E->getNumArgs());
586 Writer.AddSourceLocation(E->getRParenLoc(), Record);
587 Writer.WriteSubExpr(E->getCallee());
588 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
589 Arg != ArgEnd; ++Arg)
590 Writer.WriteSubExpr(*Arg);
591 Code = pch::EXPR_CALL;
592}
593
594void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
595 VisitExpr(E);
596 Writer.WriteSubExpr(E->getBase());
597 Writer.AddDeclRef(E->getMemberDecl(), Record);
598 Writer.AddSourceLocation(E->getMemberLoc(), Record);
599 Record.push_back(E->isArrow());
600 Code = pch::EXPR_MEMBER;
601}
602
Douglas Gregora151ba42009-04-14 23:32:43 +0000603void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
604 VisitExpr(E);
605 Writer.WriteSubExpr(E->getSubExpr());
606}
607
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000608void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
609 VisitExpr(E);
610 Writer.WriteSubExpr(E->getLHS());
611 Writer.WriteSubExpr(E->getRHS());
612 Record.push_back(E->getOpcode()); // FIXME: stable encoding
613 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
614 Code = pch::EXPR_BINARY_OPERATOR;
615}
616
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000617void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
618 VisitBinaryOperator(E);
619 Writer.AddTypeRef(E->getComputationLHSType(), Record);
620 Writer.AddTypeRef(E->getComputationResultType(), Record);
621 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
622}
623
624void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
625 VisitExpr(E);
626 Writer.WriteSubExpr(E->getCond());
627 Writer.WriteSubExpr(E->getLHS());
628 Writer.WriteSubExpr(E->getRHS());
629 Code = pch::EXPR_CONDITIONAL_OPERATOR;
630}
631
Douglas Gregora151ba42009-04-14 23:32:43 +0000632void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
633 VisitCastExpr(E);
634 Record.push_back(E->isLvalueCast());
635 Code = pch::EXPR_IMPLICIT_CAST;
636}
637
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000638void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
639 VisitCastExpr(E);
640 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
641}
642
643void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
644 VisitExplicitCastExpr(E);
645 Writer.AddSourceLocation(E->getLParenLoc(), Record);
646 Writer.AddSourceLocation(E->getRParenLoc(), Record);
647 Code = pch::EXPR_CSTYLE_CAST;
648}
649
Douglas Gregorec0b8292009-04-15 23:02:49 +0000650void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
651 VisitExpr(E);
652 Writer.WriteSubExpr(E->getBase());
653 Writer.AddIdentifierRef(&E->getAccessor(), Record);
654 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
655 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
656}
657
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000658void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
659 VisitExpr(E);
660 Record.push_back(E->getNumInits());
661 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
662 Writer.WriteSubExpr(E->getInit(I));
663 Writer.WriteSubExpr(E->getSyntacticForm());
664 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
665 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
666 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
667 Record.push_back(E->hadArrayRangeDesignator());
668 Code = pch::EXPR_INIT_LIST;
669}
670
671void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
672 VisitExpr(E);
673 Record.push_back(E->getNumSubExprs());
674 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
675 Writer.WriteSubExpr(E->getSubExpr(I));
676 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
677 Record.push_back(E->usesGNUSyntax());
678 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
679 DEnd = E->designators_end();
680 D != DEnd; ++D) {
681 if (D->isFieldDesignator()) {
682 if (FieldDecl *Field = D->getField()) {
683 Record.push_back(pch::DESIG_FIELD_DECL);
684 Writer.AddDeclRef(Field, Record);
685 } else {
686 Record.push_back(pch::DESIG_FIELD_NAME);
687 Writer.AddIdentifierRef(D->getFieldName(), Record);
688 }
689 Writer.AddSourceLocation(D->getDotLoc(), Record);
690 Writer.AddSourceLocation(D->getFieldLoc(), Record);
691 } else if (D->isArrayDesignator()) {
692 Record.push_back(pch::DESIG_ARRAY);
693 Record.push_back(D->getFirstExprIndex());
694 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
695 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
696 } else {
697 assert(D->isArrayRangeDesignator() && "Unknown designator");
698 Record.push_back(pch::DESIG_ARRAY_RANGE);
699 Record.push_back(D->getFirstExprIndex());
700 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
701 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
702 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
703 }
704 }
705 Code = pch::EXPR_DESIGNATED_INIT;
706}
707
708void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
709 VisitExpr(E);
710 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
711}
712
Douglas Gregorec0b8292009-04-15 23:02:49 +0000713void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
714 VisitExpr(E);
715 Writer.WriteSubExpr(E->getSubExpr());
716 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
717 Writer.AddSourceLocation(E->getRParenLoc(), Record);
718 Code = pch::EXPR_VA_ARG;
719}
720
Douglas Gregor209d4622009-04-15 23:33:31 +0000721void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
722 VisitExpr(E);
723 Writer.AddTypeRef(E->getArgType1(), Record);
724 Writer.AddTypeRef(E->getArgType2(), Record);
725 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
726 Writer.AddSourceLocation(E->getRParenLoc(), Record);
727 Code = pch::EXPR_TYPES_COMPATIBLE;
728}
729
730void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
731 VisitExpr(E);
732 Writer.WriteSubExpr(E->getCond());
733 Writer.WriteSubExpr(E->getLHS());
734 Writer.WriteSubExpr(E->getRHS());
735 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
736 Writer.AddSourceLocation(E->getRParenLoc(), Record);
737 Code = pch::EXPR_CHOOSE;
738}
739
740void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
741 VisitExpr(E);
742 Writer.AddSourceLocation(E->getTokenLocation(), Record);
743 Code = pch::EXPR_GNU_NULL;
744}
745
Douglas Gregor725e94b2009-04-16 00:01:45 +0000746void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
747 VisitExpr(E);
748 Record.push_back(E->getNumSubExprs());
749 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
750 Writer.WriteSubExpr(E->getExpr(I));
751 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
752 Writer.AddSourceLocation(E->getRParenLoc(), Record);
753 Code = pch::EXPR_SHUFFLE_VECTOR;
754}
755
756void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
757 VisitExpr(E);
758 Writer.AddDeclRef(E->getDecl(), Record);
759 Writer.AddSourceLocation(E->getLocation(), Record);
760 Record.push_back(E->isByRef());
761 Code = pch::EXPR_BLOCK_DECL_REF;
762}
763
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000764//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000765// PCHWriter Implementation
766//===----------------------------------------------------------------------===//
767
Douglas Gregorb5887f32009-04-10 21:16:55 +0000768/// \brief Write the target triple (e.g., i686-apple-darwin9).
769void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
770 using namespace llvm;
771 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
772 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
774 unsigned TripleAbbrev = S.EmitAbbrev(Abbrev);
775
776 RecordData Record;
777 Record.push_back(pch::TARGET_TRIPLE);
778 const char *Triple = Target.getTargetTriple();
779 S.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
780}
781
782/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000783void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
784 RecordData Record;
785 Record.push_back(LangOpts.Trigraphs);
786 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
787 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
788 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
789 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
790 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
791 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
792 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
793 Record.push_back(LangOpts.C99); // C99 Support
794 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
795 Record.push_back(LangOpts.CPlusPlus); // C++ Support
796 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
797 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
798 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
799
800 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
801 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
802 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
803
804 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
805 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
806 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
807 Record.push_back(LangOpts.LaxVectorConversions);
808 Record.push_back(LangOpts.Exceptions); // Support exception handling.
809
810 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
811 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
812 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
813
814 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
815 // by locks.
816 Record.push_back(LangOpts.Blocks); // block extension to C
817 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
818 // they are unused.
819 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
820 // (modulo the platform support).
821
822 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
823 // signed integer arithmetic overflows.
824
825 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
826 // may be ripped out at any time.
827
828 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
829 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
830 // defined.
831 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
832 // opposed to __DYNAMIC__).
833 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
834
835 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
836 // used (instead of C99 semantics).
837 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
838 Record.push_back(LangOpts.getGCMode());
839 Record.push_back(LangOpts.getVisibilityMode());
840 Record.push_back(LangOpts.InstantiationDepth);
841 S.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
842}
843
Douglas Gregorab1cef72009-04-10 03:52:48 +0000844//===----------------------------------------------------------------------===//
845// Source Manager Serialization
846//===----------------------------------------------------------------------===//
847
848/// \brief Create an abbreviation for the SLocEntry that refers to a
849/// file.
850static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) {
851 using namespace llvm;
852 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
853 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
854 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
855 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
856 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
857 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +0000858 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
859 return S.EmitAbbrev(Abbrev);
860}
861
862/// \brief Create an abbreviation for the SLocEntry that refers to a
863/// buffer.
864static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &S) {
865 using namespace llvm;
866 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
867 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
873 return S.EmitAbbrev(Abbrev);
874}
875
876/// \brief Create an abbreviation for the SLocEntry that refers to a
877/// buffer's blob.
878static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &S) {
879 using namespace llvm;
880 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
881 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
883 return S.EmitAbbrev(Abbrev);
884}
885
886/// \brief Create an abbreviation for the SLocEntry that refers to an
887/// buffer.
888static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &S) {
889 using namespace llvm;
890 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
891 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
892 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
893 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +0000896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorab1cef72009-04-10 03:52:48 +0000897 return S.EmitAbbrev(Abbrev);
898}
899
900/// \brief Writes the block containing the serialized form of the
901/// source manager.
902///
903/// TODO: We should probably use an on-disk hash table (stored in a
904/// blob), indexed based on the file name, so that we only create
905/// entries for files that we actually need. In the common case (no
906/// errors), we probably won't have to create file entries for any of
907/// the files in the AST.
908void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +0000909 // Enter the source manager block.
Douglas Gregorab1cef72009-04-10 03:52:48 +0000910 S.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
911
912 // Abbreviations for the various kinds of source-location entries.
913 int SLocFileAbbrv = -1;
914 int SLocBufferAbbrv = -1;
915 int SLocBufferBlobAbbrv = -1;
916 int SLocInstantiationAbbrv = -1;
917
918 // Write out the source location entry table. We skip the first
919 // entry, which is always the same dummy entry.
920 RecordData Record;
921 for (SourceManager::sloc_entry_iterator
922 SLoc = SourceMgr.sloc_entry_begin() + 1,
923 SLocEnd = SourceMgr.sloc_entry_end();
924 SLoc != SLocEnd; ++SLoc) {
925 // Figure out which record code to use.
926 unsigned Code;
927 if (SLoc->isFile()) {
928 if (SLoc->getFile().getContentCache()->Entry)
929 Code = pch::SM_SLOC_FILE_ENTRY;
930 else
931 Code = pch::SM_SLOC_BUFFER_ENTRY;
932 } else
933 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
934 Record.push_back(Code);
935
936 Record.push_back(SLoc->getOffset());
937 if (SLoc->isFile()) {
938 const SrcMgr::FileInfo &File = SLoc->getFile();
939 Record.push_back(File.getIncludeLoc().getRawEncoding());
940 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +0000941 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +0000942
943 const SrcMgr::ContentCache *Content = File.getContentCache();
944 if (Content->Entry) {
945 // The source location entry is a file. The blob associated
946 // with this entry is the file name.
947 if (SLocFileAbbrv == -1)
948 SLocFileAbbrv = CreateSLocFileAbbrev(S);
949 S.EmitRecordWithBlob(SLocFileAbbrv, Record,
950 Content->Entry->getName(),
951 strlen(Content->Entry->getName()));
952 } else {
953 // The source location entry is a buffer. The blob associated
954 // with this entry contains the contents of the buffer.
955 if (SLocBufferAbbrv == -1) {
956 SLocBufferAbbrv = CreateSLocBufferAbbrev(S);
957 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(S);
958 }
959
960 // We add one to the size so that we capture the trailing NULL
961 // that is required by llvm::MemoryBuffer::getMemBuffer (on
962 // the reader side).
963 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
964 const char *Name = Buffer->getBufferIdentifier();
965 S.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
966 Record.clear();
967 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
968 S.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
969 Buffer->getBufferStart(),
970 Buffer->getBufferSize() + 1);
971 }
972 } else {
973 // The source location entry is an instantiation.
974 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
975 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
976 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
977 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
978
Douglas Gregor364e5802009-04-15 18:05:10 +0000979 // Compute the token length for this macro expansion.
980 unsigned NextOffset = SourceMgr.getNextOffset();
981 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
982 if (++NextSLoc != SLocEnd)
983 NextOffset = NextSLoc->getOffset();
984 Record.push_back(NextOffset - SLoc->getOffset() - 1);
985
Douglas Gregorab1cef72009-04-10 03:52:48 +0000986 if (SLocInstantiationAbbrv == -1)
987 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(S);
988 S.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
989 }
990
991 Record.clear();
992 }
993
Douglas Gregor635f97f2009-04-13 16:31:14 +0000994 // Write the line table.
995 if (SourceMgr.hasLineTable()) {
996 LineTableInfo &LineTable = SourceMgr.getLineTable();
997
998 // Emit the file names
999 Record.push_back(LineTable.getNumFilenames());
1000 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1001 // Emit the file name
1002 const char *Filename = LineTable.getFilename(I);
1003 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1004 Record.push_back(FilenameLen);
1005 if (FilenameLen)
1006 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1007 }
1008
1009 // Emit the line entries
1010 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1011 L != LEnd; ++L) {
1012 // Emit the file ID
1013 Record.push_back(L->first);
1014
1015 // Emit the line entries
1016 Record.push_back(L->second.size());
1017 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1018 LEEnd = L->second.end();
1019 LE != LEEnd; ++LE) {
1020 Record.push_back(LE->FileOffset);
1021 Record.push_back(LE->LineNo);
1022 Record.push_back(LE->FilenameID);
1023 Record.push_back((unsigned)LE->FileKind);
1024 Record.push_back(LE->IncludeOffset);
1025 }
1026 S.EmitRecord(pch::SM_LINE_TABLE, Record);
1027 }
1028 }
1029
Douglas Gregorab1cef72009-04-10 03:52:48 +00001030 S.ExitBlock();
1031}
1032
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001033/// \brief Writes the block containing the serialized form of the
1034/// preprocessor.
1035///
Chris Lattner850eabd2009-04-10 18:08:30 +00001036void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001037 // Enter the preprocessor block.
1038 S.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
1039
Chris Lattner1b094952009-04-10 18:00:12 +00001040 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1041 // FIXME: use diagnostics subsystem for localization etc.
1042 if (PP.SawDateOrTime())
1043 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001044
Chris Lattner1b094952009-04-10 18:00:12 +00001045 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001046
Chris Lattner4b21c202009-04-13 01:29:17 +00001047 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1048 if (PP.getCounterValue() != 0) {
1049 Record.push_back(PP.getCounterValue());
1050 S.EmitRecord(pch::PP_COUNTER_VALUE, Record);
1051 Record.clear();
1052 }
1053
Chris Lattner1b094952009-04-10 18:00:12 +00001054 // Loop over all the macro definitions that are live at the end of the file,
1055 // emitting each to the PP section.
1056 // FIXME: Eventually we want to emit an index so that we can lazily load
1057 // macros.
1058 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1059 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001060 // FIXME: This emits macros in hash table order, we should do it in a stable
1061 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001062 MacroInfo *MI = I->second;
1063
1064 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1065 // been redefined by the header (in which case they are not isBuiltinMacro).
1066 if (MI->isBuiltinMacro())
1067 continue;
1068
Chris Lattner29241862009-04-11 21:15:38 +00001069 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001070 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1071 Record.push_back(MI->isUsed());
1072
1073 unsigned Code;
1074 if (MI->isObjectLike()) {
1075 Code = pch::PP_MACRO_OBJECT_LIKE;
1076 } else {
1077 Code = pch::PP_MACRO_FUNCTION_LIKE;
1078
1079 Record.push_back(MI->isC99Varargs());
1080 Record.push_back(MI->isGNUVarargs());
1081 Record.push_back(MI->getNumArgs());
1082 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1083 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001084 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001085 }
1086 S.EmitRecord(Code, Record);
1087 Record.clear();
1088
Chris Lattner850eabd2009-04-10 18:08:30 +00001089 // Emit the tokens array.
1090 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1091 // Note that we know that the preprocessor does not have any annotation
1092 // tokens in it because they are created by the parser, and thus can't be
1093 // in a macro definition.
1094 const Token &Tok = MI->getReplacementToken(TokNo);
1095
1096 Record.push_back(Tok.getLocation().getRawEncoding());
1097 Record.push_back(Tok.getLength());
1098
Chris Lattner850eabd2009-04-10 18:08:30 +00001099 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1100 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001101 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001102
1103 // FIXME: Should translate token kind to a stable encoding.
1104 Record.push_back(Tok.getKind());
1105 // FIXME: Should translate token flags to a stable encoding.
1106 Record.push_back(Tok.getFlags());
1107
1108 S.EmitRecord(pch::PP_TOKEN, Record);
1109 Record.clear();
1110 }
Chris Lattner1b094952009-04-10 18:00:12 +00001111
1112 }
1113
Chris Lattner84b04f12009-04-10 17:16:57 +00001114 S.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001115}
1116
1117
Douglas Gregorc34897d2009-04-09 22:27:44 +00001118/// \brief Write the representation of a type to the PCH stream.
1119void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001120 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001121 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001122 ID = NextTypeID++;
1123
1124 // Record the offset for this type.
1125 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
1126 TypeOffsets.push_back(S.GetCurrentBitNo());
1127 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1128 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
1129 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = S.GetCurrentBitNo();
1130 }
1131
1132 RecordData Record;
1133
1134 // Emit the type's representation.
1135 PCHTypeWriter W(*this, Record);
1136 switch (T->getTypeClass()) {
1137 // For all of the concrete, non-dependent types, call the
1138 // appropriate visitor function.
1139#define TYPE(Class, Base) \
1140 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1141#define ABSTRACT_TYPE(Class, Base)
1142#define DEPENDENT_TYPE(Class, Base)
1143#include "clang/AST/TypeNodes.def"
1144
1145 // For all of the dependent type nodes (which only occur in C++
1146 // templates), produce an error.
1147#define TYPE(Class, Base)
1148#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1149#include "clang/AST/TypeNodes.def"
1150 assert(false && "Cannot serialize dependent type nodes");
1151 break;
1152 }
1153
1154 // Emit the serialized record.
1155 S.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001156
1157 // Flush any expressions that were written as part of this type.
1158 FlushExprs();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001159}
1160
1161/// \brief Write a block containing all of the types.
1162void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001163 // Enter the types block.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001164 S.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
1165
1166 // Emit all of the types in the ASTContext
1167 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1168 TEnd = Context.getTypes().end();
1169 T != TEnd; ++T) {
1170 // Builtin types are never serialized.
1171 if (isa<BuiltinType>(*T))
1172 continue;
1173
1174 WriteType(*T);
1175 }
1176
1177 // Exit the types block
1178 S.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001179}
1180
1181/// \brief Write the block containing all of the declaration IDs
1182/// lexically declared within the given DeclContext.
1183///
1184/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1185/// bistream, or 0 if no block was written.
1186uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1187 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001188 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001189 return 0;
1190
1191 uint64_t Offset = S.GetCurrentBitNo();
1192 RecordData Record;
1193 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1194 DEnd = DC->decls_end(Context);
1195 D != DEnd; ++D)
1196 AddDeclRef(*D, Record);
1197
1198 S.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
1199 return Offset;
1200}
1201
1202/// \brief Write the block containing all of the declaration IDs
1203/// visible from the given DeclContext.
1204///
1205/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1206/// bistream, or 0 if no block was written.
1207uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1208 DeclContext *DC) {
1209 if (DC->getPrimaryContext() != DC)
1210 return 0;
1211
1212 // Force the DeclContext to build a its name-lookup table.
1213 DC->lookup(Context, DeclarationName());
1214
1215 // Serialize the contents of the mapping used for lookup. Note that,
1216 // although we have two very different code paths, the serialized
1217 // representation is the same for both cases: a declaration name,
1218 // followed by a size, followed by references to the visible
1219 // declarations that have that name.
1220 uint64_t Offset = S.GetCurrentBitNo();
1221 RecordData Record;
1222 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001223 if (!Map)
1224 return 0;
1225
Douglas Gregorc34897d2009-04-09 22:27:44 +00001226 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1227 D != DEnd; ++D) {
1228 AddDeclarationName(D->first, Record);
1229 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1230 Record.push_back(Result.second - Result.first);
1231 for(; Result.first != Result.second; ++Result.first)
1232 AddDeclRef(*Result.first, Record);
1233 }
1234
1235 if (Record.size() == 0)
1236 return 0;
1237
1238 S.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
1239 return Offset;
1240}
1241
1242/// \brief Write a block containing all of the declarations.
1243void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001244 // Enter the declarations block.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001245 S.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
1246
1247 // Emit all of the declarations.
1248 RecordData Record;
1249 PCHDeclWriter W(*this, Record);
1250 while (!DeclsToEmit.empty()) {
1251 // Pull the next declaration off the queue
1252 Decl *D = DeclsToEmit.front();
1253 DeclsToEmit.pop();
1254
1255 // If this declaration is also a DeclContext, write blocks for the
1256 // declarations that lexically stored inside its context and those
1257 // declarations that are visible from its context. These blocks
1258 // are written before the declaration itself so that we can put
1259 // their offsets into the record for the declaration.
1260 uint64_t LexicalOffset = 0;
1261 uint64_t VisibleOffset = 0;
1262 DeclContext *DC = dyn_cast<DeclContext>(D);
1263 if (DC) {
1264 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1265 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1266 }
1267
1268 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001269 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001270 if (ID == 0)
1271 ID = DeclIDs.size();
1272
1273 unsigned Index = ID - 1;
1274
1275 // Record the offset for this declaration
1276 if (DeclOffsets.size() == Index)
1277 DeclOffsets.push_back(S.GetCurrentBitNo());
1278 else if (DeclOffsets.size() < Index) {
1279 DeclOffsets.resize(Index+1);
1280 DeclOffsets[Index] = S.GetCurrentBitNo();
1281 }
1282
1283 // Build and emit a record for this declaration
1284 Record.clear();
1285 W.Code = (pch::DeclCode)0;
1286 W.Visit(D);
1287 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001288 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001289 S.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001290
Douglas Gregor1c507882009-04-15 21:30:51 +00001291 // If the declaration had any attributes, write them now.
1292 if (D->hasAttrs())
1293 WriteAttributeRecord(D->getAttrs());
1294
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001295 // Flush any expressions that were written as part of this declaration.
1296 FlushExprs();
1297
Douglas Gregor631f6c62009-04-14 00:24:19 +00001298 // Note external declarations so that we can add them to a record
1299 // in the PCH file later.
1300 if (isa<FileScopeAsmDecl>(D))
1301 ExternalDefinitions.push_back(ID);
1302 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1303 if (// Non-static file-scope variables with initializers or that
1304 // are tentative definitions.
1305 (Var->isFileVarDecl() &&
1306 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1307 // Out-of-line definitions of static data members (C++).
1308 (Var->getDeclContext()->isRecord() &&
1309 !Var->getLexicalDeclContext()->isRecord() &&
1310 Var->getStorageClass() == VarDecl::Static))
1311 ExternalDefinitions.push_back(ID);
1312 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
1313 if (Func->isThisDeclarationADefinition() &&
1314 Func->getStorageClass() != FunctionDecl::Static &&
1315 !Func->isInline())
1316 ExternalDefinitions.push_back(ID);
1317 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001318 }
1319
1320 // Exit the declarations block
1321 S.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001322}
1323
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001324/// \brief Write the identifier table into the PCH file.
1325///
1326/// The identifier table consists of a blob containing string data
1327/// (the actual identifiers themselves) and a separate "offsets" index
1328/// that maps identifier IDs to locations within the blob.
1329void PCHWriter::WriteIdentifierTable() {
1330 using namespace llvm;
1331
1332 // Create and write out the blob that contains the identifier
1333 // strings.
1334 RecordData IdentOffsets;
1335 IdentOffsets.resize(IdentifierIDs.size());
1336 {
1337 // Create the identifier string data.
1338 std::vector<char> Data;
1339 Data.push_back(0); // Data must not be empty.
1340 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1341 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1342 ID != IDEnd; ++ID) {
1343 assert(ID->first && "NULL identifier in identifier table");
1344
1345 // Make sure we're starting on an odd byte. The PCH reader
1346 // expects the low bit to be set on all of the offsets.
1347 if ((Data.size() & 0x01) == 0)
1348 Data.push_back((char)0);
1349
1350 IdentOffsets[ID->second - 1] = Data.size();
1351 Data.insert(Data.end(),
1352 ID->first->getName(),
1353 ID->first->getName() + ID->first->getLength());
1354 Data.push_back((char)0);
1355 }
1356
1357 // Create a blob abbreviation
1358 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1359 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
1361 unsigned IDTableAbbrev = S.EmitAbbrev(Abbrev);
1362
1363 // Write the identifier table
1364 RecordData Record;
1365 Record.push_back(pch::IDENTIFIER_TABLE);
1366 S.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
1367 }
1368
1369 // Write the offsets table for identifier IDs.
1370 S.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
1371}
1372
Douglas Gregor1c507882009-04-15 21:30:51 +00001373/// \brief Write a record containing the given attributes.
1374void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1375 RecordData Record;
1376 for (; Attr; Attr = Attr->getNext()) {
1377 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1378 Record.push_back(Attr->isInherited());
1379 switch (Attr->getKind()) {
1380 case Attr::Alias:
1381 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1382 break;
1383
1384 case Attr::Aligned:
1385 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1386 break;
1387
1388 case Attr::AlwaysInline:
1389 break;
1390
1391 case Attr::AnalyzerNoReturn:
1392 break;
1393
1394 case Attr::Annotate:
1395 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1396 break;
1397
1398 case Attr::AsmLabel:
1399 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1400 break;
1401
1402 case Attr::Blocks:
1403 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1404 break;
1405
1406 case Attr::Cleanup:
1407 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1408 break;
1409
1410 case Attr::Const:
1411 break;
1412
1413 case Attr::Constructor:
1414 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1415 break;
1416
1417 case Attr::DLLExport:
1418 case Attr::DLLImport:
1419 case Attr::Deprecated:
1420 break;
1421
1422 case Attr::Destructor:
1423 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1424 break;
1425
1426 case Attr::FastCall:
1427 break;
1428
1429 case Attr::Format: {
1430 const FormatAttr *Format = cast<FormatAttr>(Attr);
1431 AddString(Format->getType(), Record);
1432 Record.push_back(Format->getFormatIdx());
1433 Record.push_back(Format->getFirstArg());
1434 break;
1435 }
1436
1437 case Attr::GNUCInline:
1438 case Attr::IBOutletKind:
1439 case Attr::NoReturn:
1440 case Attr::NoThrow:
1441 case Attr::Nodebug:
1442 case Attr::Noinline:
1443 break;
1444
1445 case Attr::NonNull: {
1446 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1447 Record.push_back(NonNull->size());
1448 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1449 break;
1450 }
1451
1452 case Attr::ObjCException:
1453 case Attr::ObjCNSObject:
1454 case Attr::Overloadable:
1455 break;
1456
1457 case Attr::Packed:
1458 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1459 break;
1460
1461 case Attr::Pure:
1462 break;
1463
1464 case Attr::Regparm:
1465 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1466 break;
1467
1468 case Attr::Section:
1469 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1470 break;
1471
1472 case Attr::StdCall:
1473 case Attr::TransparentUnion:
1474 case Attr::Unavailable:
1475 case Attr::Unused:
1476 case Attr::Used:
1477 break;
1478
1479 case Attr::Visibility:
1480 // FIXME: stable encoding
1481 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1482 break;
1483
1484 case Attr::WarnUnusedResult:
1485 case Attr::Weak:
1486 case Attr::WeakImport:
1487 break;
1488 }
1489 }
1490
1491 assert((int)pch::DECL_ATTR == (int)pch::TYPE_ATTR &&
1492 "DECL_ATTR/TYPE_ATTR mismatch");
1493 S.EmitRecord(pch::DECL_ATTR, Record);
1494}
1495
1496void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1497 Record.push_back(Str.size());
1498 Record.insert(Record.end(), Str.begin(), Str.end());
1499}
1500
Douglas Gregorc34897d2009-04-09 22:27:44 +00001501PCHWriter::PCHWriter(llvm::BitstreamWriter &S)
1502 : S(S), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
1503
Chris Lattner850eabd2009-04-10 18:08:30 +00001504void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001505 // Emit the file header.
1506 S.Emit((unsigned)'C', 8);
1507 S.Emit((unsigned)'P', 8);
1508 S.Emit((unsigned)'C', 8);
1509 S.Emit((unsigned)'H', 8);
1510
1511 // The translation unit is the first declaration we'll emit.
1512 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1513 DeclsToEmit.push(Context.getTranslationUnitDecl());
1514
1515 // Write the remaining PCH contents.
Douglas Gregorb5887f32009-04-10 21:16:55 +00001516 S.EnterSubblock(pch::PCH_BLOCK_ID, 3);
1517 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001518 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001519 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001520 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001521 WriteTypesBlock(Context);
1522 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001523 WriteIdentifierTable();
Douglas Gregor179cfb12009-04-10 20:39:37 +00001524 S.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1525 S.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001526 if (!ExternalDefinitions.empty())
1527 S.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001528 S.ExitBlock();
1529}
1530
1531void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1532 Record.push_back(Loc.getRawEncoding());
1533}
1534
1535void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1536 Record.push_back(Value.getBitWidth());
1537 unsigned N = Value.getNumWords();
1538 const uint64_t* Words = Value.getRawData();
1539 for (unsigned I = 0; I != N; ++I)
1540 Record.push_back(Words[I]);
1541}
1542
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001543void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1544 Record.push_back(Value.isUnsigned());
1545 AddAPInt(Value, Record);
1546}
1547
Douglas Gregore2f37202009-04-14 21:55:33 +00001548void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1549 AddAPInt(Value.bitcastToAPInt(), Record);
1550}
1551
Douglas Gregorc34897d2009-04-09 22:27:44 +00001552void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001553 if (II == 0) {
1554 Record.push_back(0);
1555 return;
1556 }
1557
1558 pch::IdentID &ID = IdentifierIDs[II];
1559 if (ID == 0)
1560 ID = IdentifierIDs.size();
1561
1562 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001563}
1564
1565void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1566 if (T.isNull()) {
1567 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1568 return;
1569 }
1570
1571 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001572 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001573 switch (BT->getKind()) {
1574 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1575 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1576 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1577 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1578 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1579 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1580 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1581 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1582 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1583 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1584 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1585 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1586 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1587 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1588 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1589 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1590 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1591 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1592 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1593 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1594 }
1595
1596 Record.push_back((ID << 3) | T.getCVRQualifiers());
1597 return;
1598 }
1599
Douglas Gregorac8f2802009-04-10 17:25:41 +00001600 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001601 if (ID == 0) // we haven't seen this type before
1602 ID = NextTypeID++;
1603
1604 // Encode the type qualifiers in the type reference.
1605 Record.push_back((ID << 3) | T.getCVRQualifiers());
1606}
1607
1608void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1609 if (D == 0) {
1610 Record.push_back(0);
1611 return;
1612 }
1613
Douglas Gregorac8f2802009-04-10 17:25:41 +00001614 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001615 if (ID == 0) {
1616 // We haven't seen this declaration before. Give it a new ID and
1617 // enqueue it in the list of declarations to emit.
1618 ID = DeclIDs.size();
1619 DeclsToEmit.push(const_cast<Decl *>(D));
1620 }
1621
1622 Record.push_back(ID);
1623}
1624
1625void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1626 Record.push_back(Name.getNameKind());
1627 switch (Name.getNameKind()) {
1628 case DeclarationName::Identifier:
1629 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1630 break;
1631
1632 case DeclarationName::ObjCZeroArgSelector:
1633 case DeclarationName::ObjCOneArgSelector:
1634 case DeclarationName::ObjCMultiArgSelector:
1635 assert(false && "Serialization of Objective-C selectors unavailable");
1636 break;
1637
1638 case DeclarationName::CXXConstructorName:
1639 case DeclarationName::CXXDestructorName:
1640 case DeclarationName::CXXConversionFunctionName:
1641 AddTypeRef(Name.getCXXNameType(), Record);
1642 break;
1643
1644 case DeclarationName::CXXOperatorName:
1645 Record.push_back(Name.getCXXOverloadedOperator());
1646 break;
1647
1648 case DeclarationName::CXXUsingDirective:
1649 // No extra data to emit
1650 break;
1651 }
1652}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001653
Douglas Gregora151ba42009-04-14 23:32:43 +00001654/// \brief Write the given subexpression to the bitstream.
1655void PCHWriter::WriteSubExpr(Expr *E) {
1656 RecordData Record;
1657 PCHStmtWriter Writer(*this, Record);
1658
1659 if (!E) {
1660 S.EmitRecord(pch::EXPR_NULL, Record);
1661 return;
1662 }
1663
1664 Writer.Code = pch::EXPR_NULL;
1665 Writer.Visit(E);
1666 assert(Writer.Code != pch::EXPR_NULL &&
1667 "Unhandled expression writing PCH file");
1668 S.EmitRecord(Writer.Code, Record);
1669}
1670
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001671/// \brief Flush all of the expressions that have been added to the
1672/// queue via AddExpr().
1673void PCHWriter::FlushExprs() {
1674 RecordData Record;
1675 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001676
Douglas Gregora151ba42009-04-14 23:32:43 +00001677 for (unsigned I = 0, N = ExprsToEmit.size(); I != N; ++I) {
1678 Expr *E = ExprsToEmit[I];
1679
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001680 if (!E) {
1681 S.EmitRecord(pch::EXPR_NULL, Record);
1682 continue;
1683 }
1684
1685 Writer.Code = pch::EXPR_NULL;
1686 Writer.Visit(E);
1687 assert(Writer.Code != pch::EXPR_NULL &&
1688 "Unhandled expression writing PCH file");
1689 S.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001690
1691 assert(N == ExprsToEmit.size() &&
1692 "Subexpression writen via AddExpr rather than WriteSubExpr!");
1693
1694 // Note that we are at the end of a full expression. Any
1695 // expression records that follow this one are part of a different
1696 // expression.
1697 Record.clear();
1698 S.EmitRecord(pch::EXPR_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001699 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001700
1701 ExprsToEmit.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001702}