blob: 6aea7a53727ec130cd718e8f1dec0618fe71b9dd [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 Gregorc72f6c82009-04-16 22:23:12 +0000129 Writer.AddStmt(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 Gregorc72f6c82009-04-16 22:23:12 +0000169 Writer.AddStmt(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;
Douglas Gregore3241e92009-04-18 00:02:19 +0000244 ASTContext &Context;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000245 PCHWriter::RecordData &Record;
246
247 public:
248 pch::DeclCode Code;
249
Douglas Gregore3241e92009-04-18 00:02:19 +0000250 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
251 PCHWriter::RecordData &Record)
252 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000253
254 void VisitDecl(Decl *D);
255 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
256 void VisitNamedDecl(NamedDecl *D);
257 void VisitTypeDecl(TypeDecl *D);
258 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000259 void VisitTagDecl(TagDecl *D);
260 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000261 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000262 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000263 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000264 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000265 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000266 void VisitVarDecl(VarDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000267 void VisitParmVarDecl(ParmVarDecl *D);
268 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000269 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
270 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000271 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
272 uint64_t VisibleOffset);
Steve Naroff79ea0e02009-04-20 15:06:07 +0000273 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000274 };
275}
276
277void PCHDeclWriter::VisitDecl(Decl *D) {
278 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
279 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
280 Writer.AddSourceLocation(D->getLocation(), Record);
281 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000282 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000283 Record.push_back(D->isImplicit());
284 Record.push_back(D->getAccess());
285}
286
287void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
288 VisitDecl(D);
289 Code = pch::DECL_TRANSLATION_UNIT;
290}
291
292void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
293 VisitDecl(D);
294 Writer.AddDeclarationName(D->getDeclName(), Record);
295}
296
297void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
298 VisitNamedDecl(D);
299 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
300}
301
302void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
303 VisitTypeDecl(D);
304 Writer.AddTypeRef(D->getUnderlyingType(), Record);
305 Code = pch::DECL_TYPEDEF;
306}
307
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000308void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
309 VisitTypeDecl(D);
310 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
311 Record.push_back(D->isDefinition());
312 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
313}
314
315void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
316 VisitTagDecl(D);
317 Writer.AddTypeRef(D->getIntegerType(), Record);
318 Code = pch::DECL_ENUM;
319}
320
Douglas Gregor982365e2009-04-13 21:20:57 +0000321void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
322 VisitTagDecl(D);
323 Record.push_back(D->hasFlexibleArrayMember());
324 Record.push_back(D->isAnonymousStructOrUnion());
325 Code = pch::DECL_RECORD;
326}
327
Douglas Gregorc34897d2009-04-09 22:27:44 +0000328void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
329 VisitNamedDecl(D);
330 Writer.AddTypeRef(D->getType(), Record);
331}
332
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000333void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
334 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000335 Record.push_back(D->getInitExpr()? 1 : 0);
336 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000337 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000338 Writer.AddAPSInt(D->getInitVal(), Record);
339 Code = pch::DECL_ENUM_CONSTANT;
340}
341
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000342void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
343 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000344 Record.push_back(D->isThisDeclarationADefinition());
345 if (D->isThisDeclarationADefinition())
Douglas Gregore3241e92009-04-18 00:02:19 +0000346 Writer.AddStmt(D->getBody(Context));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000347 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
348 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
349 Record.push_back(D->isInline());
350 Record.push_back(D->isVirtual());
351 Record.push_back(D->isPure());
352 Record.push_back(D->inheritedPrototype());
353 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
354 Record.push_back(D->isDeleted());
355 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
356 Record.push_back(D->param_size());
357 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
358 P != PEnd; ++P)
359 Writer.AddDeclRef(*P, Record);
360 Code = pch::DECL_FUNCTION;
361}
362
Steve Naroff79ea0e02009-04-20 15:06:07 +0000363void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
364 VisitNamedDecl(D);
365 // FIXME: convert to LazyStmtPtr?
366 // Unlike C/C++, method bodies will never be in header files.
367 Record.push_back(D->getBody() != 0);
368 if (D->getBody() != 0) {
369 Writer.AddStmt(D->getBody(Context));
370 Writer.AddDeclRef(D->getSelfDecl(), Record);
371 Writer.AddDeclRef(D->getCmdDecl(), Record);
372 }
373 Record.push_back(D->isInstanceMethod());
374 Record.push_back(D->isVariadic());
375 Record.push_back(D->isSynthesized());
376 // FIXME: stable encoding for @required/@optional
377 Record.push_back(D->getImplementationControl());
378 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
379 Record.push_back(D->getObjCDeclQualifier());
380 Writer.AddTypeRef(D->getResultType(), Record);
381 Writer.AddSourceLocation(D->getLocEnd(), Record);
382 Record.push_back(D->param_size());
383 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
384 PEnd = D->param_end(); P != PEnd; ++P)
385 Writer.AddDeclRef(*P, Record);
386 Code = pch::DECL_OBJC_METHOD;
387}
388
Douglas Gregor982365e2009-04-13 21:20:57 +0000389void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
390 VisitValueDecl(D);
391 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000392 Record.push_back(D->getBitWidth()? 1 : 0);
393 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000394 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000395 Code = pch::DECL_FIELD;
396}
397
Douglas Gregorc34897d2009-04-09 22:27:44 +0000398void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
399 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000400 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000401 Record.push_back(D->isThreadSpecified());
402 Record.push_back(D->hasCXXDirectInitializer());
403 Record.push_back(D->isDeclaredInCondition());
404 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
405 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000406 Record.push_back(D->getInit()? 1 : 0);
407 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000408 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000409 Code = pch::DECL_VAR;
410}
411
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000412void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
413 VisitVarDecl(D);
414 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000415 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000416 // FIXME: why isn't the "default argument" just stored as the initializer
417 // in VarDecl?
418 Code = pch::DECL_PARM_VAR;
419}
420
421void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
422 VisitParmVarDecl(D);
423 Writer.AddTypeRef(D->getOriginalType(), Record);
424 Code = pch::DECL_ORIGINAL_PARM_VAR;
425}
426
Douglas Gregor2a491792009-04-13 22:49:25 +0000427void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
428 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000429 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000430 Code = pch::DECL_FILE_SCOPE_ASM;
431}
432
433void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
434 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000435 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000436 Record.push_back(D->param_size());
437 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
438 P != PEnd; ++P)
439 Writer.AddDeclRef(*P, Record);
440 Code = pch::DECL_BLOCK;
441}
442
Douglas Gregorc34897d2009-04-09 22:27:44 +0000443/// \brief Emit the DeclContext part of a declaration context decl.
444///
445/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
446/// block for this declaration context is stored. May be 0 to indicate
447/// that there are no declarations stored within this context.
448///
449/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
450/// block for this declaration context is stored. May be 0 to indicate
451/// that there are no declarations visible from this context. Note
452/// that this value will not be emitted for non-primary declaration
453/// contexts.
454void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
455 uint64_t VisibleOffset) {
456 Record.push_back(LexicalOffset);
457 if (DC->getPrimaryContext() == DC)
458 Record.push_back(VisibleOffset);
459}
460
461//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000462// Statement/expression serialization
463//===----------------------------------------------------------------------===//
464namespace {
465 class VISIBILITY_HIDDEN PCHStmtWriter
466 : public StmtVisitor<PCHStmtWriter, void> {
467
468 PCHWriter &Writer;
469 PCHWriter::RecordData &Record;
470
471 public:
472 pch::StmtCode Code;
473
474 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
475 : Writer(Writer), Record(Record) { }
476
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000477 void VisitStmt(Stmt *S);
478 void VisitNullStmt(NullStmt *S);
479 void VisitCompoundStmt(CompoundStmt *S);
480 void VisitSwitchCase(SwitchCase *S);
481 void VisitCaseStmt(CaseStmt *S);
482 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000483 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000484 void VisitIfStmt(IfStmt *S);
485 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000486 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000487 void VisitDoStmt(DoStmt *S);
488 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000489 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000490 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000491 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000492 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000493 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000494 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000495 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000496 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000497 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000498 void VisitDeclRefExpr(DeclRefExpr *E);
499 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000500 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000501 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000502 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000503 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000504 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000505 void VisitUnaryOperator(UnaryOperator *E);
506 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000507 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000508 void VisitCallExpr(CallExpr *E);
509 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000510 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000511 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000512 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
513 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000514 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000515 void VisitExplicitCastExpr(ExplicitCastExpr *E);
516 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000517 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000518 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000519 void VisitInitListExpr(InitListExpr *E);
520 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
521 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000522 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000523 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000524 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000525 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
526 void VisitChooseExpr(ChooseExpr *E);
527 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000528 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000529 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000530 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000531 };
532}
533
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000534void PCHStmtWriter::VisitStmt(Stmt *S) {
535}
536
537void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
538 VisitStmt(S);
539 Writer.AddSourceLocation(S->getSemiLoc(), Record);
540 Code = pch::STMT_NULL;
541}
542
543void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
544 VisitStmt(S);
545 Record.push_back(S->size());
546 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
547 CS != CSEnd; ++CS)
548 Writer.WriteSubStmt(*CS);
549 Writer.AddSourceLocation(S->getLBracLoc(), Record);
550 Writer.AddSourceLocation(S->getRBracLoc(), Record);
551 Code = pch::STMT_COMPOUND;
552}
553
554void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
555 VisitStmt(S);
556 Record.push_back(Writer.RecordSwitchCaseID(S));
557}
558
559void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
560 VisitSwitchCase(S);
561 Writer.WriteSubStmt(S->getLHS());
562 Writer.WriteSubStmt(S->getRHS());
563 Writer.WriteSubStmt(S->getSubStmt());
564 Writer.AddSourceLocation(S->getCaseLoc(), Record);
565 Code = pch::STMT_CASE;
566}
567
568void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
569 VisitSwitchCase(S);
570 Writer.WriteSubStmt(S->getSubStmt());
571 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
572 Code = pch::STMT_DEFAULT;
573}
574
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000575void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
576 VisitStmt(S);
577 Writer.AddIdentifierRef(S->getID(), Record);
578 Writer.WriteSubStmt(S->getSubStmt());
579 Writer.AddSourceLocation(S->getIdentLoc(), Record);
580 Record.push_back(Writer.GetLabelID(S));
581 Code = pch::STMT_LABEL;
582}
583
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000584void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
585 VisitStmt(S);
586 Writer.WriteSubStmt(S->getCond());
587 Writer.WriteSubStmt(S->getThen());
588 Writer.WriteSubStmt(S->getElse());
589 Writer.AddSourceLocation(S->getIfLoc(), Record);
590 Code = pch::STMT_IF;
591}
592
593void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
594 VisitStmt(S);
595 Writer.WriteSubStmt(S->getCond());
596 Writer.WriteSubStmt(S->getBody());
597 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
598 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
599 SC = SC->getNextSwitchCase())
600 Record.push_back(Writer.getSwitchCaseID(SC));
601 Code = pch::STMT_SWITCH;
602}
603
Douglas Gregora6b503f2009-04-17 00:16:09 +0000604void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
605 VisitStmt(S);
606 Writer.WriteSubStmt(S->getCond());
607 Writer.WriteSubStmt(S->getBody());
608 Writer.AddSourceLocation(S->getWhileLoc(), Record);
609 Code = pch::STMT_WHILE;
610}
611
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000612void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
613 VisitStmt(S);
614 Writer.WriteSubStmt(S->getCond());
615 Writer.WriteSubStmt(S->getBody());
616 Writer.AddSourceLocation(S->getDoLoc(), Record);
617 Code = pch::STMT_DO;
618}
619
620void PCHStmtWriter::VisitForStmt(ForStmt *S) {
621 VisitStmt(S);
622 Writer.WriteSubStmt(S->getInit());
623 Writer.WriteSubStmt(S->getCond());
624 Writer.WriteSubStmt(S->getInc());
625 Writer.WriteSubStmt(S->getBody());
626 Writer.AddSourceLocation(S->getForLoc(), Record);
627 Code = pch::STMT_FOR;
628}
629
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000630void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
631 VisitStmt(S);
632 Record.push_back(Writer.GetLabelID(S->getLabel()));
633 Writer.AddSourceLocation(S->getGotoLoc(), Record);
634 Writer.AddSourceLocation(S->getLabelLoc(), Record);
635 Code = pch::STMT_GOTO;
636}
637
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000638void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
639 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000640 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000641 Writer.WriteSubStmt(S->getTarget());
642 Code = pch::STMT_INDIRECT_GOTO;
643}
644
Douglas Gregora6b503f2009-04-17 00:16:09 +0000645void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
646 VisitStmt(S);
647 Writer.AddSourceLocation(S->getContinueLoc(), Record);
648 Code = pch::STMT_CONTINUE;
649}
650
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000651void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
652 VisitStmt(S);
653 Writer.AddSourceLocation(S->getBreakLoc(), Record);
654 Code = pch::STMT_BREAK;
655}
656
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000657void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
658 VisitStmt(S);
659 Writer.WriteSubStmt(S->getRetValue());
660 Writer.AddSourceLocation(S->getReturnLoc(), Record);
661 Code = pch::STMT_RETURN;
662}
663
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000664void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
665 VisitStmt(S);
666 Writer.AddSourceLocation(S->getStartLoc(), Record);
667 Writer.AddSourceLocation(S->getEndLoc(), Record);
668 DeclGroupRef DG = S->getDeclGroup();
669 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
670 Writer.AddDeclRef(*D, Record);
671 Code = pch::STMT_DECL;
672}
673
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000674void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
675 VisitStmt(S);
676 Record.push_back(S->getNumOutputs());
677 Record.push_back(S->getNumInputs());
678 Record.push_back(S->getNumClobbers());
679 Writer.AddSourceLocation(S->getAsmLoc(), Record);
680 Writer.AddSourceLocation(S->getRParenLoc(), Record);
681 Record.push_back(S->isVolatile());
682 Record.push_back(S->isSimple());
683 Writer.WriteSubStmt(S->getAsmString());
684
685 // Outputs
686 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
687 Writer.AddString(S->getOutputName(I), Record);
688 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
689 Writer.WriteSubStmt(S->getOutputExpr(I));
690 }
691
692 // Inputs
693 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
694 Writer.AddString(S->getInputName(I), Record);
695 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
696 Writer.WriteSubStmt(S->getInputExpr(I));
697 }
698
699 // Clobbers
700 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
701 Writer.WriteSubStmt(S->getClobber(I));
702
703 Code = pch::STMT_ASM;
704}
705
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000706void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000707 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000708 Writer.AddTypeRef(E->getType(), Record);
709 Record.push_back(E->isTypeDependent());
710 Record.push_back(E->isValueDependent());
711}
712
Douglas Gregore2f37202009-04-14 21:55:33 +0000713void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
714 VisitExpr(E);
715 Writer.AddSourceLocation(E->getLocation(), Record);
716 Record.push_back(E->getIdentType()); // FIXME: stable encoding
717 Code = pch::EXPR_PREDEFINED;
718}
719
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000720void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
721 VisitExpr(E);
722 Writer.AddDeclRef(E->getDecl(), Record);
723 Writer.AddSourceLocation(E->getLocation(), Record);
724 Code = pch::EXPR_DECL_REF;
725}
726
727void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
728 VisitExpr(E);
729 Writer.AddSourceLocation(E->getLocation(), Record);
730 Writer.AddAPInt(E->getValue(), Record);
731 Code = pch::EXPR_INTEGER_LITERAL;
732}
733
Douglas Gregore2f37202009-04-14 21:55:33 +0000734void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
735 VisitExpr(E);
736 Writer.AddAPFloat(E->getValue(), Record);
737 Record.push_back(E->isExact());
738 Writer.AddSourceLocation(E->getLocation(), Record);
739 Code = pch::EXPR_FLOATING_LITERAL;
740}
741
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000742void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
743 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000744 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000745 Code = pch::EXPR_IMAGINARY_LITERAL;
746}
747
Douglas Gregor596e0932009-04-15 16:35:07 +0000748void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
749 VisitExpr(E);
750 Record.push_back(E->getByteLength());
751 Record.push_back(E->getNumConcatenated());
752 Record.push_back(E->isWide());
753 // FIXME: String data should be stored as a blob at the end of the
754 // StringLiteral. However, we can't do so now because we have no
755 // provision for coping with abbreviations when we're jumping around
756 // the PCH file during deserialization.
757 Record.insert(Record.end(),
758 E->getStrData(), E->getStrData() + E->getByteLength());
759 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
760 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
761 Code = pch::EXPR_STRING_LITERAL;
762}
763
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000764void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
765 VisitExpr(E);
766 Record.push_back(E->getValue());
767 Writer.AddSourceLocation(E->getLoc(), Record);
768 Record.push_back(E->isWide());
769 Code = pch::EXPR_CHARACTER_LITERAL;
770}
771
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000772void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
773 VisitExpr(E);
774 Writer.AddSourceLocation(E->getLParen(), Record);
775 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000776 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000777 Code = pch::EXPR_PAREN;
778}
779
Douglas Gregor12d74052009-04-15 15:58:59 +0000780void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
781 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000782 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000783 Record.push_back(E->getOpcode()); // FIXME: stable encoding
784 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
785 Code = pch::EXPR_UNARY_OPERATOR;
786}
787
788void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
789 VisitExpr(E);
790 Record.push_back(E->isSizeOf());
791 if (E->isArgumentType())
792 Writer.AddTypeRef(E->getArgumentType(), Record);
793 else {
794 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000795 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000796 }
797 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
798 Writer.AddSourceLocation(E->getRParenLoc(), Record);
799 Code = pch::EXPR_SIZEOF_ALIGN_OF;
800}
801
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000802void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
803 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000804 Writer.WriteSubStmt(E->getLHS());
805 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000806 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
807 Code = pch::EXPR_ARRAY_SUBSCRIPT;
808}
809
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000810void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
811 VisitExpr(E);
812 Record.push_back(E->getNumArgs());
813 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000814 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000815 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
816 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000817 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000818 Code = pch::EXPR_CALL;
819}
820
821void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
822 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000823 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000824 Writer.AddDeclRef(E->getMemberDecl(), Record);
825 Writer.AddSourceLocation(E->getMemberLoc(), Record);
826 Record.push_back(E->isArrow());
827 Code = pch::EXPR_MEMBER;
828}
829
Douglas Gregora151ba42009-04-14 23:32:43 +0000830void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
831 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000832 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000833}
834
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000835void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
836 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000837 Writer.WriteSubStmt(E->getLHS());
838 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000839 Record.push_back(E->getOpcode()); // FIXME: stable encoding
840 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
841 Code = pch::EXPR_BINARY_OPERATOR;
842}
843
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000844void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
845 VisitBinaryOperator(E);
846 Writer.AddTypeRef(E->getComputationLHSType(), Record);
847 Writer.AddTypeRef(E->getComputationResultType(), Record);
848 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
849}
850
851void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
852 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000853 Writer.WriteSubStmt(E->getCond());
854 Writer.WriteSubStmt(E->getLHS());
855 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000856 Code = pch::EXPR_CONDITIONAL_OPERATOR;
857}
858
Douglas Gregora151ba42009-04-14 23:32:43 +0000859void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
860 VisitCastExpr(E);
861 Record.push_back(E->isLvalueCast());
862 Code = pch::EXPR_IMPLICIT_CAST;
863}
864
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000865void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
866 VisitCastExpr(E);
867 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
868}
869
870void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
871 VisitExplicitCastExpr(E);
872 Writer.AddSourceLocation(E->getLParenLoc(), Record);
873 Writer.AddSourceLocation(E->getRParenLoc(), Record);
874 Code = pch::EXPR_CSTYLE_CAST;
875}
876
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000877void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
878 VisitExpr(E);
879 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000880 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000881 Record.push_back(E->isFileScope());
882 Code = pch::EXPR_COMPOUND_LITERAL;
883}
884
Douglas Gregorec0b8292009-04-15 23:02:49 +0000885void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
886 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000887 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000888 Writer.AddIdentifierRef(&E->getAccessor(), Record);
889 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
890 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
891}
892
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000893void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
894 VisitExpr(E);
895 Record.push_back(E->getNumInits());
896 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000897 Writer.WriteSubStmt(E->getInit(I));
898 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000899 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
900 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
901 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
902 Record.push_back(E->hadArrayRangeDesignator());
903 Code = pch::EXPR_INIT_LIST;
904}
905
906void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
907 VisitExpr(E);
908 Record.push_back(E->getNumSubExprs());
909 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000910 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000911 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
912 Record.push_back(E->usesGNUSyntax());
913 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
914 DEnd = E->designators_end();
915 D != DEnd; ++D) {
916 if (D->isFieldDesignator()) {
917 if (FieldDecl *Field = D->getField()) {
918 Record.push_back(pch::DESIG_FIELD_DECL);
919 Writer.AddDeclRef(Field, Record);
920 } else {
921 Record.push_back(pch::DESIG_FIELD_NAME);
922 Writer.AddIdentifierRef(D->getFieldName(), Record);
923 }
924 Writer.AddSourceLocation(D->getDotLoc(), Record);
925 Writer.AddSourceLocation(D->getFieldLoc(), Record);
926 } else if (D->isArrayDesignator()) {
927 Record.push_back(pch::DESIG_ARRAY);
928 Record.push_back(D->getFirstExprIndex());
929 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
930 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
931 } else {
932 assert(D->isArrayRangeDesignator() && "Unknown designator");
933 Record.push_back(pch::DESIG_ARRAY_RANGE);
934 Record.push_back(D->getFirstExprIndex());
935 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
936 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
937 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
938 }
939 }
940 Code = pch::EXPR_DESIGNATED_INIT;
941}
942
943void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
944 VisitExpr(E);
945 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
946}
947
Douglas Gregorec0b8292009-04-15 23:02:49 +0000948void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
949 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000950 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000951 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
952 Writer.AddSourceLocation(E->getRParenLoc(), Record);
953 Code = pch::EXPR_VA_ARG;
954}
955
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000956void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
957 VisitExpr(E);
958 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
959 Writer.AddSourceLocation(E->getLabelLoc(), Record);
960 Record.push_back(Writer.GetLabelID(E->getLabel()));
961 Code = pch::EXPR_ADDR_LABEL;
962}
963
Douglas Gregoreca12f62009-04-17 19:05:30 +0000964void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
965 VisitExpr(E);
966 Writer.WriteSubStmt(E->getSubStmt());
967 Writer.AddSourceLocation(E->getLParenLoc(), Record);
968 Writer.AddSourceLocation(E->getRParenLoc(), Record);
969 Code = pch::EXPR_STMT;
970}
971
Douglas Gregor209d4622009-04-15 23:33:31 +0000972void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
973 VisitExpr(E);
974 Writer.AddTypeRef(E->getArgType1(), Record);
975 Writer.AddTypeRef(E->getArgType2(), Record);
976 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
977 Writer.AddSourceLocation(E->getRParenLoc(), Record);
978 Code = pch::EXPR_TYPES_COMPATIBLE;
979}
980
981void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
982 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000983 Writer.WriteSubStmt(E->getCond());
984 Writer.WriteSubStmt(E->getLHS());
985 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +0000986 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
987 Writer.AddSourceLocation(E->getRParenLoc(), Record);
988 Code = pch::EXPR_CHOOSE;
989}
990
991void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
992 VisitExpr(E);
993 Writer.AddSourceLocation(E->getTokenLocation(), Record);
994 Code = pch::EXPR_GNU_NULL;
995}
996
Douglas Gregor725e94b2009-04-16 00:01:45 +0000997void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
998 VisitExpr(E);
999 Record.push_back(E->getNumSubExprs());
1000 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001001 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001002 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1003 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1004 Code = pch::EXPR_SHUFFLE_VECTOR;
1005}
1006
Douglas Gregore246b742009-04-17 19:21:43 +00001007void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1008 VisitExpr(E);
1009 Writer.AddDeclRef(E->getBlockDecl(), Record);
1010 Record.push_back(E->hasBlockDeclRefExprs());
1011 Code = pch::EXPR_BLOCK;
1012}
1013
Douglas Gregor725e94b2009-04-16 00:01:45 +00001014void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1015 VisitExpr(E);
1016 Writer.AddDeclRef(E->getDecl(), Record);
1017 Writer.AddSourceLocation(E->getLocation(), Record);
1018 Record.push_back(E->isByRef());
1019 Code = pch::EXPR_BLOCK_DECL_REF;
1020}
1021
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001022//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001023// PCHWriter Implementation
1024//===----------------------------------------------------------------------===//
1025
Douglas Gregorb5887f32009-04-10 21:16:55 +00001026/// \brief Write the target triple (e.g., i686-apple-darwin9).
1027void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1028 using namespace llvm;
1029 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1030 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001032 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001033
1034 RecordData Record;
1035 Record.push_back(pch::TARGET_TRIPLE);
1036 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001037 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001038}
1039
1040/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001041void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1042 RecordData Record;
1043 Record.push_back(LangOpts.Trigraphs);
1044 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1045 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1046 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1047 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1048 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1049 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1050 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1051 Record.push_back(LangOpts.C99); // C99 Support
1052 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1053 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1054 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1055 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1056 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1057
1058 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1059 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1060 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1061
1062 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1063 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1064 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1065 Record.push_back(LangOpts.LaxVectorConversions);
1066 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1067
1068 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1069 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1070 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1071
1072 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1073 // by locks.
1074 Record.push_back(LangOpts.Blocks); // block extension to C
1075 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1076 // they are unused.
1077 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1078 // (modulo the platform support).
1079
1080 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1081 // signed integer arithmetic overflows.
1082
1083 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1084 // may be ripped out at any time.
1085
1086 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1087 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1088 // defined.
1089 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1090 // opposed to __DYNAMIC__).
1091 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1092
1093 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1094 // used (instead of C99 semantics).
1095 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1096 Record.push_back(LangOpts.getGCMode());
1097 Record.push_back(LangOpts.getVisibilityMode());
1098 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001099 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001100}
1101
Douglas Gregorab1cef72009-04-10 03:52:48 +00001102//===----------------------------------------------------------------------===//
1103// Source Manager Serialization
1104//===----------------------------------------------------------------------===//
1105
1106/// \brief Create an abbreviation for the SLocEntry that refers to a
1107/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001108static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001109 using namespace llvm;
1110 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1111 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1115 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001117 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001118}
1119
1120/// \brief Create an abbreviation for the SLocEntry that refers to a
1121/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001122static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001123 using namespace llvm;
1124 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1125 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1128 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1129 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1130 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001131 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001132}
1133
1134/// \brief Create an abbreviation for the SLocEntry that refers to a
1135/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001136static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001137 using namespace llvm;
1138 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1139 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1140 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001141 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001142}
1143
1144/// \brief Create an abbreviation for the SLocEntry that refers to an
1145/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001146static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001147 using namespace llvm;
1148 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1149 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001155 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001156}
1157
1158/// \brief Writes the block containing the serialized form of the
1159/// source manager.
1160///
1161/// TODO: We should probably use an on-disk hash table (stored in a
1162/// blob), indexed based on the file name, so that we only create
1163/// entries for files that we actually need. In the common case (no
1164/// errors), we probably won't have to create file entries for any of
1165/// the files in the AST.
1166void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001167 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001168 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001169
1170 // Abbreviations for the various kinds of source-location entries.
1171 int SLocFileAbbrv = -1;
1172 int SLocBufferAbbrv = -1;
1173 int SLocBufferBlobAbbrv = -1;
1174 int SLocInstantiationAbbrv = -1;
1175
1176 // Write out the source location entry table. We skip the first
1177 // entry, which is always the same dummy entry.
1178 RecordData Record;
1179 for (SourceManager::sloc_entry_iterator
1180 SLoc = SourceMgr.sloc_entry_begin() + 1,
1181 SLocEnd = SourceMgr.sloc_entry_end();
1182 SLoc != SLocEnd; ++SLoc) {
1183 // Figure out which record code to use.
1184 unsigned Code;
1185 if (SLoc->isFile()) {
1186 if (SLoc->getFile().getContentCache()->Entry)
1187 Code = pch::SM_SLOC_FILE_ENTRY;
1188 else
1189 Code = pch::SM_SLOC_BUFFER_ENTRY;
1190 } else
1191 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1192 Record.push_back(Code);
1193
1194 Record.push_back(SLoc->getOffset());
1195 if (SLoc->isFile()) {
1196 const SrcMgr::FileInfo &File = SLoc->getFile();
1197 Record.push_back(File.getIncludeLoc().getRawEncoding());
1198 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001199 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001200
1201 const SrcMgr::ContentCache *Content = File.getContentCache();
1202 if (Content->Entry) {
1203 // The source location entry is a file. The blob associated
1204 // with this entry is the file name.
1205 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001206 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1207 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001208 Content->Entry->getName(),
1209 strlen(Content->Entry->getName()));
1210 } else {
1211 // The source location entry is a buffer. The blob associated
1212 // with this entry contains the contents of the buffer.
1213 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001214 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1215 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001216 }
1217
1218 // We add one to the size so that we capture the trailing NULL
1219 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1220 // the reader side).
1221 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1222 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001223 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001224 Record.clear();
1225 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001226 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001227 Buffer->getBufferStart(),
1228 Buffer->getBufferSize() + 1);
1229 }
1230 } else {
1231 // The source location entry is an instantiation.
1232 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1233 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1234 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1235 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1236
Douglas Gregor364e5802009-04-15 18:05:10 +00001237 // Compute the token length for this macro expansion.
1238 unsigned NextOffset = SourceMgr.getNextOffset();
1239 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1240 if (++NextSLoc != SLocEnd)
1241 NextOffset = NextSLoc->getOffset();
1242 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1243
Douglas Gregorab1cef72009-04-10 03:52:48 +00001244 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001245 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1246 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001247 }
1248
1249 Record.clear();
1250 }
1251
Douglas Gregor635f97f2009-04-13 16:31:14 +00001252 // Write the line table.
1253 if (SourceMgr.hasLineTable()) {
1254 LineTableInfo &LineTable = SourceMgr.getLineTable();
1255
1256 // Emit the file names
1257 Record.push_back(LineTable.getNumFilenames());
1258 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1259 // Emit the file name
1260 const char *Filename = LineTable.getFilename(I);
1261 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1262 Record.push_back(FilenameLen);
1263 if (FilenameLen)
1264 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1265 }
1266
1267 // Emit the line entries
1268 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1269 L != LEnd; ++L) {
1270 // Emit the file ID
1271 Record.push_back(L->first);
1272
1273 // Emit the line entries
1274 Record.push_back(L->second.size());
1275 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1276 LEEnd = L->second.end();
1277 LE != LEEnd; ++LE) {
1278 Record.push_back(LE->FileOffset);
1279 Record.push_back(LE->LineNo);
1280 Record.push_back(LE->FilenameID);
1281 Record.push_back((unsigned)LE->FileKind);
1282 Record.push_back(LE->IncludeOffset);
1283 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001284 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001285 }
1286 }
1287
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001288 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001289}
1290
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001291/// \brief Writes the block containing the serialized form of the
1292/// preprocessor.
1293///
Chris Lattner850eabd2009-04-10 18:08:30 +00001294void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001295 // Enter the preprocessor block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001296 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattner84b04f12009-04-10 17:16:57 +00001297
Chris Lattner1b094952009-04-10 18:00:12 +00001298 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1299 // FIXME: use diagnostics subsystem for localization etc.
1300 if (PP.SawDateOrTime())
1301 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001302
Chris Lattner1b094952009-04-10 18:00:12 +00001303 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001304
Chris Lattner4b21c202009-04-13 01:29:17 +00001305 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1306 if (PP.getCounterValue() != 0) {
1307 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001308 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001309 Record.clear();
1310 }
1311
Chris Lattner1b094952009-04-10 18:00:12 +00001312 // Loop over all the macro definitions that are live at the end of the file,
1313 // emitting each to the PP section.
1314 // FIXME: Eventually we want to emit an index so that we can lazily load
1315 // macros.
1316 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1317 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001318 // FIXME: This emits macros in hash table order, we should do it in a stable
1319 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001320 MacroInfo *MI = I->second;
1321
1322 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1323 // been redefined by the header (in which case they are not isBuiltinMacro).
1324 if (MI->isBuiltinMacro())
1325 continue;
1326
Chris Lattner29241862009-04-11 21:15:38 +00001327 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001328 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1329 Record.push_back(MI->isUsed());
1330
1331 unsigned Code;
1332 if (MI->isObjectLike()) {
1333 Code = pch::PP_MACRO_OBJECT_LIKE;
1334 } else {
1335 Code = pch::PP_MACRO_FUNCTION_LIKE;
1336
1337 Record.push_back(MI->isC99Varargs());
1338 Record.push_back(MI->isGNUVarargs());
1339 Record.push_back(MI->getNumArgs());
1340 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1341 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001342 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001343 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001344 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001345 Record.clear();
1346
Chris Lattner850eabd2009-04-10 18:08:30 +00001347 // Emit the tokens array.
1348 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1349 // Note that we know that the preprocessor does not have any annotation
1350 // tokens in it because they are created by the parser, and thus can't be
1351 // in a macro definition.
1352 const Token &Tok = MI->getReplacementToken(TokNo);
1353
1354 Record.push_back(Tok.getLocation().getRawEncoding());
1355 Record.push_back(Tok.getLength());
1356
Chris Lattner850eabd2009-04-10 18:08:30 +00001357 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1358 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001359 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001360
1361 // FIXME: Should translate token kind to a stable encoding.
1362 Record.push_back(Tok.getKind());
1363 // FIXME: Should translate token flags to a stable encoding.
1364 Record.push_back(Tok.getFlags());
1365
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001366 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001367 Record.clear();
1368 }
Chris Lattner1b094952009-04-10 18:00:12 +00001369
1370 }
1371
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001372 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001373}
1374
1375
Douglas Gregorc34897d2009-04-09 22:27:44 +00001376/// \brief Write the representation of a type to the PCH stream.
1377void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001378 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001379 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001380 ID = NextTypeID++;
1381
1382 // Record the offset for this type.
1383 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001384 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001385 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1386 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001387 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001388 }
1389
1390 RecordData Record;
1391
1392 // Emit the type's representation.
1393 PCHTypeWriter W(*this, Record);
1394 switch (T->getTypeClass()) {
1395 // For all of the concrete, non-dependent types, call the
1396 // appropriate visitor function.
1397#define TYPE(Class, Base) \
1398 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1399#define ABSTRACT_TYPE(Class, Base)
1400#define DEPENDENT_TYPE(Class, Base)
1401#include "clang/AST/TypeNodes.def"
1402
1403 // For all of the dependent type nodes (which only occur in C++
1404 // templates), produce an error.
1405#define TYPE(Class, Base)
1406#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1407#include "clang/AST/TypeNodes.def"
1408 assert(false && "Cannot serialize dependent type nodes");
1409 break;
1410 }
1411
1412 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001413 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001414
1415 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001416 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001417}
1418
1419/// \brief Write a block containing all of the types.
1420void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001421 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001422 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001423
1424 // Emit all of the types in the ASTContext
1425 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1426 TEnd = Context.getTypes().end();
1427 T != TEnd; ++T) {
1428 // Builtin types are never serialized.
1429 if (isa<BuiltinType>(*T))
1430 continue;
1431
1432 WriteType(*T);
1433 }
1434
1435 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001436 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001437}
1438
1439/// \brief Write the block containing all of the declaration IDs
1440/// lexically declared within the given DeclContext.
1441///
1442/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1443/// bistream, or 0 if no block was written.
1444uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1445 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001446 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001447 return 0;
1448
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001449 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001450 RecordData Record;
1451 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1452 DEnd = DC->decls_end(Context);
1453 D != DEnd; ++D)
1454 AddDeclRef(*D, Record);
1455
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001456 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001457 return Offset;
1458}
1459
1460/// \brief Write the block containing all of the declaration IDs
1461/// visible from the given DeclContext.
1462///
1463/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1464/// bistream, or 0 if no block was written.
1465uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1466 DeclContext *DC) {
1467 if (DC->getPrimaryContext() != DC)
1468 return 0;
1469
Douglas Gregor5afd9802009-04-18 15:49:20 +00001470 // Since there is no name lookup into functions or methods, don't
1471 // bother to build a visible-declarations table.
1472 if (DC->isFunctionOrMethod())
1473 return 0;
1474
Douglas Gregorc34897d2009-04-09 22:27:44 +00001475 // Force the DeclContext to build a its name-lookup table.
1476 DC->lookup(Context, DeclarationName());
1477
1478 // Serialize the contents of the mapping used for lookup. Note that,
1479 // although we have two very different code paths, the serialized
1480 // representation is the same for both cases: a declaration name,
1481 // followed by a size, followed by references to the visible
1482 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001483 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001484 RecordData Record;
1485 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001486 if (!Map)
1487 return 0;
1488
Douglas Gregorc34897d2009-04-09 22:27:44 +00001489 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1490 D != DEnd; ++D) {
1491 AddDeclarationName(D->first, Record);
1492 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1493 Record.push_back(Result.second - Result.first);
1494 for(; Result.first != Result.second; ++Result.first)
1495 AddDeclRef(*Result.first, Record);
1496 }
1497
1498 if (Record.size() == 0)
1499 return 0;
1500
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001501 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001502 return Offset;
1503}
1504
1505/// \brief Write a block containing all of the declarations.
1506void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001507 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001508 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001509
1510 // Emit all of the declarations.
1511 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001512 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001513 while (!DeclsToEmit.empty()) {
1514 // Pull the next declaration off the queue
1515 Decl *D = DeclsToEmit.front();
1516 DeclsToEmit.pop();
1517
1518 // If this declaration is also a DeclContext, write blocks for the
1519 // declarations that lexically stored inside its context and those
1520 // declarations that are visible from its context. These blocks
1521 // are written before the declaration itself so that we can put
1522 // their offsets into the record for the declaration.
1523 uint64_t LexicalOffset = 0;
1524 uint64_t VisibleOffset = 0;
1525 DeclContext *DC = dyn_cast<DeclContext>(D);
1526 if (DC) {
1527 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1528 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1529 }
1530
1531 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001532 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001533 if (ID == 0)
1534 ID = DeclIDs.size();
1535
1536 unsigned Index = ID - 1;
1537
1538 // Record the offset for this declaration
1539 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001540 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001541 else if (DeclOffsets.size() < Index) {
1542 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001543 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001544 }
1545
1546 // Build and emit a record for this declaration
1547 Record.clear();
1548 W.Code = (pch::DeclCode)0;
1549 W.Visit(D);
1550 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001551 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001552 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001553
Douglas Gregor1c507882009-04-15 21:30:51 +00001554 // If the declaration had any attributes, write them now.
1555 if (D->hasAttrs())
1556 WriteAttributeRecord(D->getAttrs());
1557
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001558 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001559 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001560
Douglas Gregor631f6c62009-04-14 00:24:19 +00001561 // Note external declarations so that we can add them to a record
1562 // in the PCH file later.
1563 if (isa<FileScopeAsmDecl>(D))
1564 ExternalDefinitions.push_back(ID);
1565 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1566 if (// Non-static file-scope variables with initializers or that
1567 // are tentative definitions.
1568 (Var->isFileVarDecl() &&
1569 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1570 // Out-of-line definitions of static data members (C++).
1571 (Var->getDeclContext()->isRecord() &&
1572 !Var->getLexicalDeclContext()->isRecord() &&
1573 Var->getStorageClass() == VarDecl::Static))
1574 ExternalDefinitions.push_back(ID);
1575 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00001576 if (Func->isThisDeclarationADefinition())
Douglas Gregor631f6c62009-04-14 00:24:19 +00001577 ExternalDefinitions.push_back(ID);
1578 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001579 }
1580
1581 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001582 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001583}
1584
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001585/// \brief Write the identifier table into the PCH file.
1586///
1587/// The identifier table consists of a blob containing string data
1588/// (the actual identifiers themselves) and a separate "offsets" index
1589/// that maps identifier IDs to locations within the blob.
1590void PCHWriter::WriteIdentifierTable() {
1591 using namespace llvm;
1592
1593 // Create and write out the blob that contains the identifier
1594 // strings.
1595 RecordData IdentOffsets;
1596 IdentOffsets.resize(IdentifierIDs.size());
1597 {
1598 // Create the identifier string data.
1599 std::vector<char> Data;
1600 Data.push_back(0); // Data must not be empty.
1601 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1602 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1603 ID != IDEnd; ++ID) {
1604 assert(ID->first && "NULL identifier in identifier table");
1605
1606 // Make sure we're starting on an odd byte. The PCH reader
1607 // expects the low bit to be set on all of the offsets.
1608 if ((Data.size() & 0x01) == 0)
1609 Data.push_back((char)0);
1610
1611 IdentOffsets[ID->second - 1] = Data.size();
1612 Data.insert(Data.end(),
1613 ID->first->getName(),
1614 ID->first->getName() + ID->first->getLength());
1615 Data.push_back((char)0);
1616 }
1617
1618 // Create a blob abbreviation
1619 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1620 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001622 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001623
1624 // Write the identifier table
1625 RecordData Record;
1626 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001627 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001628 }
1629
1630 // Write the offsets table for identifier IDs.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001631 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001632}
1633
Douglas Gregor1c507882009-04-15 21:30:51 +00001634/// \brief Write a record containing the given attributes.
1635void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1636 RecordData Record;
1637 for (; Attr; Attr = Attr->getNext()) {
1638 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1639 Record.push_back(Attr->isInherited());
1640 switch (Attr->getKind()) {
1641 case Attr::Alias:
1642 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1643 break;
1644
1645 case Attr::Aligned:
1646 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1647 break;
1648
1649 case Attr::AlwaysInline:
1650 break;
1651
1652 case Attr::AnalyzerNoReturn:
1653 break;
1654
1655 case Attr::Annotate:
1656 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1657 break;
1658
1659 case Attr::AsmLabel:
1660 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1661 break;
1662
1663 case Attr::Blocks:
1664 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1665 break;
1666
1667 case Attr::Cleanup:
1668 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1669 break;
1670
1671 case Attr::Const:
1672 break;
1673
1674 case Attr::Constructor:
1675 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1676 break;
1677
1678 case Attr::DLLExport:
1679 case Attr::DLLImport:
1680 case Attr::Deprecated:
1681 break;
1682
1683 case Attr::Destructor:
1684 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1685 break;
1686
1687 case Attr::FastCall:
1688 break;
1689
1690 case Attr::Format: {
1691 const FormatAttr *Format = cast<FormatAttr>(Attr);
1692 AddString(Format->getType(), Record);
1693 Record.push_back(Format->getFormatIdx());
1694 Record.push_back(Format->getFirstArg());
1695 break;
1696 }
1697
1698 case Attr::GNUCInline:
1699 case Attr::IBOutletKind:
1700 case Attr::NoReturn:
1701 case Attr::NoThrow:
1702 case Attr::Nodebug:
1703 case Attr::Noinline:
1704 break;
1705
1706 case Attr::NonNull: {
1707 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1708 Record.push_back(NonNull->size());
1709 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1710 break;
1711 }
1712
1713 case Attr::ObjCException:
1714 case Attr::ObjCNSObject:
1715 case Attr::Overloadable:
1716 break;
1717
1718 case Attr::Packed:
1719 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1720 break;
1721
1722 case Attr::Pure:
1723 break;
1724
1725 case Attr::Regparm:
1726 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1727 break;
1728
1729 case Attr::Section:
1730 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1731 break;
1732
1733 case Attr::StdCall:
1734 case Attr::TransparentUnion:
1735 case Attr::Unavailable:
1736 case Attr::Unused:
1737 case Attr::Used:
1738 break;
1739
1740 case Attr::Visibility:
1741 // FIXME: stable encoding
1742 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1743 break;
1744
1745 case Attr::WarnUnusedResult:
1746 case Attr::Weak:
1747 case Attr::WeakImport:
1748 break;
1749 }
1750 }
1751
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001752 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001753}
1754
1755void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1756 Record.push_back(Str.size());
1757 Record.insert(Record.end(), Str.begin(), Str.end());
1758}
1759
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001760PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor456e0952009-04-17 22:13:46 +00001761 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), NumStatements(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001762
Chris Lattner850eabd2009-04-10 18:08:30 +00001763void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001764 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001765 Stream.Emit((unsigned)'C', 8);
1766 Stream.Emit((unsigned)'P', 8);
1767 Stream.Emit((unsigned)'C', 8);
1768 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001769
1770 // The translation unit is the first declaration we'll emit.
1771 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1772 DeclsToEmit.push(Context.getTranslationUnitDecl());
1773
1774 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00001775 RecordData Record;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001776 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001777 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001778 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001779 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001780 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001781 WriteTypesBlock(Context);
1782 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001783 WriteIdentifierTable();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001784 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1785 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregore01ad442009-04-18 05:55:16 +00001786
1787 // Write the record of special types.
1788 Record.clear();
1789 AddTypeRef(Context.getBuiltinVaListType(), Record);
1790 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1791
Douglas Gregor631f6c62009-04-14 00:24:19 +00001792 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001793 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor456e0952009-04-17 22:13:46 +00001794
1795 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00001796 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00001797 Record.push_back(NumStatements);
1798 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001799 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001800}
1801
1802void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1803 Record.push_back(Loc.getRawEncoding());
1804}
1805
1806void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1807 Record.push_back(Value.getBitWidth());
1808 unsigned N = Value.getNumWords();
1809 const uint64_t* Words = Value.getRawData();
1810 for (unsigned I = 0; I != N; ++I)
1811 Record.push_back(Words[I]);
1812}
1813
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001814void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1815 Record.push_back(Value.isUnsigned());
1816 AddAPInt(Value, Record);
1817}
1818
Douglas Gregore2f37202009-04-14 21:55:33 +00001819void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1820 AddAPInt(Value.bitcastToAPInt(), Record);
1821}
1822
Douglas Gregorc34897d2009-04-09 22:27:44 +00001823void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001824 if (II == 0) {
1825 Record.push_back(0);
1826 return;
1827 }
1828
1829 pch::IdentID &ID = IdentifierIDs[II];
1830 if (ID == 0)
1831 ID = IdentifierIDs.size();
1832
1833 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001834}
1835
1836void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1837 if (T.isNull()) {
1838 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1839 return;
1840 }
1841
1842 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001843 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001844 switch (BT->getKind()) {
1845 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1846 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1847 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1848 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1849 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1850 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1851 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1852 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1853 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1854 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1855 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1856 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1857 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1858 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1859 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1860 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1861 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1862 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1863 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1864 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1865 }
1866
1867 Record.push_back((ID << 3) | T.getCVRQualifiers());
1868 return;
1869 }
1870
Douglas Gregorac8f2802009-04-10 17:25:41 +00001871 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001872 if (ID == 0) // we haven't seen this type before
1873 ID = NextTypeID++;
1874
1875 // Encode the type qualifiers in the type reference.
1876 Record.push_back((ID << 3) | T.getCVRQualifiers());
1877}
1878
1879void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1880 if (D == 0) {
1881 Record.push_back(0);
1882 return;
1883 }
1884
Douglas Gregorac8f2802009-04-10 17:25:41 +00001885 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001886 if (ID == 0) {
1887 // We haven't seen this declaration before. Give it a new ID and
1888 // enqueue it in the list of declarations to emit.
1889 ID = DeclIDs.size();
1890 DeclsToEmit.push(const_cast<Decl *>(D));
1891 }
1892
1893 Record.push_back(ID);
1894}
1895
1896void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1897 Record.push_back(Name.getNameKind());
1898 switch (Name.getNameKind()) {
1899 case DeclarationName::Identifier:
1900 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1901 break;
1902
1903 case DeclarationName::ObjCZeroArgSelector:
1904 case DeclarationName::ObjCOneArgSelector:
1905 case DeclarationName::ObjCMultiArgSelector:
1906 assert(false && "Serialization of Objective-C selectors unavailable");
1907 break;
1908
1909 case DeclarationName::CXXConstructorName:
1910 case DeclarationName::CXXDestructorName:
1911 case DeclarationName::CXXConversionFunctionName:
1912 AddTypeRef(Name.getCXXNameType(), Record);
1913 break;
1914
1915 case DeclarationName::CXXOperatorName:
1916 Record.push_back(Name.getCXXOverloadedOperator());
1917 break;
1918
1919 case DeclarationName::CXXUsingDirective:
1920 // No extra data to emit
1921 break;
1922 }
1923}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001924
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001925/// \brief Write the given substatement or subexpression to the
1926/// bitstream.
1927void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00001928 RecordData Record;
1929 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00001930 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00001931
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001932 if (!S) {
1933 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001934 return;
1935 }
1936
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001937 Writer.Code = pch::STMT_NULL_PTR;
1938 Writer.Visit(S);
1939 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00001940 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001941 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001942}
1943
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001944/// \brief Flush all of the statements that have been added to the
1945/// queue via AddStmt().
1946void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001947 RecordData Record;
1948 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001949
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001950 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00001951 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001952 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00001953
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001954 if (!S) {
1955 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001956 continue;
1957 }
1958
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001959 Writer.Code = pch::STMT_NULL_PTR;
1960 Writer.Visit(S);
1961 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001962 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001963 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001964
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001965 assert(N == StmtsToEmit.size() &&
1966 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00001967
1968 // Note that we are at the end of a full expression. Any
1969 // expression records that follow this one are part of a different
1970 // expression.
1971 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001972 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001973 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001974
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001975 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00001976 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001977}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00001978
1979unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1980 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1981 "SwitchCase recorded twice");
1982 unsigned NextID = SwitchCaseIDs.size();
1983 SwitchCaseIDs[S] = NextID;
1984 return NextID;
1985}
1986
1987unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1988 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1989 "SwitchCase hasn't been seen yet");
1990 return SwitchCaseIDs[S];
1991}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00001992
1993/// \brief Retrieve the ID for the given label statement, which may
1994/// or may not have been emitted yet.
1995unsigned PCHWriter::GetLabelID(LabelStmt *S) {
1996 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
1997 if (Pos != LabelIDs.end())
1998 return Pos->second;
1999
2000 unsigned NextID = LabelIDs.size();
2001 LabelIDs[S] = NextID;
2002 return NextID;
2003}