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