blob: f34323c1604c71d1d589ab712d99ceb7ecfc121f [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"
Douglas Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorff9a6092009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
20#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000036#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000037using namespace clang;
38
39//===----------------------------------------------------------------------===//
40// Type serialization
41//===----------------------------------------------------------------------===//
42namespace {
43 class VISIBILITY_HIDDEN PCHTypeWriter {
44 PCHWriter &Writer;
45 PCHWriter::RecordData &Record;
46
47 public:
48 /// \brief Type code that corresponds to the record generated.
49 pch::TypeCode Code;
50
51 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
52 : Writer(Writer), Record(Record) { }
53
54 void VisitArrayType(const ArrayType *T);
55 void VisitFunctionType(const FunctionType *T);
56 void VisitTagType(const TagType *T);
57
58#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
59#define ABSTRACT_TYPE(Class, Base)
60#define DEPENDENT_TYPE(Class, Base)
61#include "clang/AST/TypeNodes.def"
62 };
63}
64
65void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
66 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
67 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
68 Record.push_back(T->getAddressSpace());
69 Code = pch::TYPE_EXT_QUAL;
70}
71
72void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
73 assert(false && "Built-in types are never serialized");
74}
75
76void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
77 Record.push_back(T->getWidth());
78 Record.push_back(T->isSigned());
79 Code = pch::TYPE_FIXED_WIDTH_INT;
80}
81
82void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
83 Writer.AddTypeRef(T->getElementType(), Record);
84 Code = pch::TYPE_COMPLEX;
85}
86
87void PCHTypeWriter::VisitPointerType(const PointerType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_POINTER;
90}
91
92void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_BLOCK_POINTER;
95}
96
97void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
98 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Code = pch::TYPE_LVALUE_REFERENCE;
100}
101
102void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
103 Writer.AddTypeRef(T->getPointeeType(), Record);
104 Code = pch::TYPE_RVALUE_REFERENCE;
105}
106
107void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
108 Writer.AddTypeRef(T->getPointeeType(), Record);
109 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
110 Code = pch::TYPE_MEMBER_POINTER;
111}
112
113void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
114 Writer.AddTypeRef(T->getElementType(), Record);
115 Record.push_back(T->getSizeModifier()); // FIXME: stable values
116 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
117}
118
119void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
120 VisitArrayType(T);
121 Writer.AddAPInt(T->getSize(), Record);
122 Code = pch::TYPE_CONSTANT_ARRAY;
123}
124
125void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
126 VisitArrayType(T);
127 Code = pch::TYPE_INCOMPLETE_ARRAY;
128}
129
130void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
131 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000132 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000133 Code = pch::TYPE_VARIABLE_ARRAY;
134}
135
136void PCHTypeWriter::VisitVectorType(const VectorType *T) {
137 Writer.AddTypeRef(T->getElementType(), Record);
138 Record.push_back(T->getNumElements());
139 Code = pch::TYPE_VECTOR;
140}
141
142void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
143 VisitVectorType(T);
144 Code = pch::TYPE_EXT_VECTOR;
145}
146
147void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
148 Writer.AddTypeRef(T->getResultType(), Record);
149}
150
151void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
152 VisitFunctionType(T);
153 Code = pch::TYPE_FUNCTION_NO_PROTO;
154}
155
156void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
157 VisitFunctionType(T);
158 Record.push_back(T->getNumArgs());
159 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
160 Writer.AddTypeRef(T->getArgType(I), Record);
161 Record.push_back(T->isVariadic());
162 Record.push_back(T->getTypeQuals());
163 Code = pch::TYPE_FUNCTION_PROTO;
164}
165
166void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
167 Writer.AddDeclRef(T->getDecl(), Record);
168 Code = pch::TYPE_TYPEDEF;
169}
170
171void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000172 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000173 Code = pch::TYPE_TYPEOF_EXPR;
174}
175
176void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
177 Writer.AddTypeRef(T->getUnderlyingType(), Record);
178 Code = pch::TYPE_TYPEOF;
179}
180
181void PCHTypeWriter::VisitTagType(const TagType *T) {
182 Writer.AddDeclRef(T->getDecl(), Record);
183 assert(!T->isBeingDefined() &&
184 "Cannot serialize in the middle of a type definition");
185}
186
187void PCHTypeWriter::VisitRecordType(const RecordType *T) {
188 VisitTagType(T);
189 Code = pch::TYPE_RECORD;
190}
191
192void PCHTypeWriter::VisitEnumType(const EnumType *T) {
193 VisitTagType(T);
194 Code = pch::TYPE_ENUM;
195}
196
197void
198PCHTypeWriter::VisitTemplateSpecializationType(
199 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000200 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000201 assert(false && "Cannot serialize template specialization types");
202}
203
204void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000205 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000206 assert(false && "Cannot serialize qualified name types");
207}
208
209void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
210 Writer.AddDeclRef(T->getDecl(), Record);
211 Code = pch::TYPE_OBJC_INTERFACE;
212}
213
214void
215PCHTypeWriter::VisitObjCQualifiedInterfaceType(
216 const ObjCQualifiedInterfaceType *T) {
217 VisitObjCInterfaceType(T);
218 Record.push_back(T->getNumProtocols());
219 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
220 Writer.AddDeclRef(T->getProtocol(I), Record);
221 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
222}
223
224void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
225 Record.push_back(T->getNumProtocols());
226 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
227 Writer.AddDeclRef(T->getProtocols(I), Record);
228 Code = pch::TYPE_OBJC_QUALIFIED_ID;
229}
230
Douglas Gregorc34897d2009-04-09 22:27:44 +0000231//===----------------------------------------------------------------------===//
232// Declaration serialization
233//===----------------------------------------------------------------------===//
234namespace {
235 class VISIBILITY_HIDDEN PCHDeclWriter
236 : public DeclVisitor<PCHDeclWriter, void> {
237
238 PCHWriter &Writer;
Douglas Gregore3241e92009-04-18 00:02:19 +0000239 ASTContext &Context;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000240 PCHWriter::RecordData &Record;
241
242 public:
243 pch::DeclCode Code;
244
Douglas Gregore3241e92009-04-18 00:02:19 +0000245 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
246 PCHWriter::RecordData &Record)
247 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000248
249 void VisitDecl(Decl *D);
250 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
251 void VisitNamedDecl(NamedDecl *D);
252 void VisitTypeDecl(TypeDecl *D);
253 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000254 void VisitTagDecl(TagDecl *D);
255 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000256 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000257 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000258 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000259 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000260 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000261 void VisitVarDecl(VarDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000262 void VisitParmVarDecl(ParmVarDecl *D);
263 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000264 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
265 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000266 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
267 uint64_t VisibleOffset);
Steve Naroff79ea0e02009-04-20 15:06:07 +0000268 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +0000269 void VisitObjCContainerDecl(ObjCContainerDecl *D);
270 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
271 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000272 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
273 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
274 void VisitObjCClassDecl(ObjCClassDecl *D);
275 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
276 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
277 void VisitObjCImplDecl(ObjCImplDecl *D);
278 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
279 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
280 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
281 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
282 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000283 };
284}
285
286void PCHDeclWriter::VisitDecl(Decl *D) {
287 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
288 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
289 Writer.AddSourceLocation(D->getLocation(), Record);
290 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000291 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000292 Record.push_back(D->isImplicit());
293 Record.push_back(D->getAccess());
294}
295
296void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
297 VisitDecl(D);
298 Code = pch::DECL_TRANSLATION_UNIT;
299}
300
301void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
302 VisitDecl(D);
303 Writer.AddDeclarationName(D->getDeclName(), Record);
304}
305
306void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
307 VisitNamedDecl(D);
308 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
309}
310
311void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
312 VisitTypeDecl(D);
313 Writer.AddTypeRef(D->getUnderlyingType(), Record);
314 Code = pch::DECL_TYPEDEF;
315}
316
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000317void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
318 VisitTypeDecl(D);
319 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
320 Record.push_back(D->isDefinition());
321 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
322}
323
324void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
325 VisitTagDecl(D);
326 Writer.AddTypeRef(D->getIntegerType(), Record);
327 Code = pch::DECL_ENUM;
328}
329
Douglas Gregor982365e2009-04-13 21:20:57 +0000330void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
331 VisitTagDecl(D);
332 Record.push_back(D->hasFlexibleArrayMember());
333 Record.push_back(D->isAnonymousStructOrUnion());
334 Code = pch::DECL_RECORD;
335}
336
Douglas Gregorc34897d2009-04-09 22:27:44 +0000337void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
338 VisitNamedDecl(D);
339 Writer.AddTypeRef(D->getType(), Record);
340}
341
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000342void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
343 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000344 Record.push_back(D->getInitExpr()? 1 : 0);
345 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000346 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000347 Writer.AddAPSInt(D->getInitVal(), Record);
348 Code = pch::DECL_ENUM_CONSTANT;
349}
350
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000351void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
352 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000353 Record.push_back(D->isThisDeclarationADefinition());
354 if (D->isThisDeclarationADefinition())
Douglas Gregore3241e92009-04-18 00:02:19 +0000355 Writer.AddStmt(D->getBody(Context));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000356 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
357 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
358 Record.push_back(D->isInline());
359 Record.push_back(D->isVirtual());
360 Record.push_back(D->isPure());
361 Record.push_back(D->inheritedPrototype());
362 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
363 Record.push_back(D->isDeleted());
364 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
365 Record.push_back(D->param_size());
366 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
367 P != PEnd; ++P)
368 Writer.AddDeclRef(*P, Record);
369 Code = pch::DECL_FUNCTION;
370}
371
Steve Naroff79ea0e02009-04-20 15:06:07 +0000372void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
373 VisitNamedDecl(D);
374 // FIXME: convert to LazyStmtPtr?
375 // Unlike C/C++, method bodies will never be in header files.
376 Record.push_back(D->getBody() != 0);
377 if (D->getBody() != 0) {
378 Writer.AddStmt(D->getBody(Context));
379 Writer.AddDeclRef(D->getSelfDecl(), Record);
380 Writer.AddDeclRef(D->getCmdDecl(), Record);
381 }
382 Record.push_back(D->isInstanceMethod());
383 Record.push_back(D->isVariadic());
384 Record.push_back(D->isSynthesized());
385 // FIXME: stable encoding for @required/@optional
386 Record.push_back(D->getImplementationControl());
387 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
388 Record.push_back(D->getObjCDeclQualifier());
389 Writer.AddTypeRef(D->getResultType(), Record);
390 Writer.AddSourceLocation(D->getLocEnd(), Record);
391 Record.push_back(D->param_size());
392 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
393 PEnd = D->param_end(); P != PEnd; ++P)
394 Writer.AddDeclRef(*P, Record);
395 Code = pch::DECL_OBJC_METHOD;
396}
397
Steve Naroff7333b492009-04-20 20:09:33 +0000398void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
399 VisitNamedDecl(D);
400 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
401 // Abstract class (no need to define a stable pch::DECL code).
402}
403
404void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
405 VisitObjCContainerDecl(D);
406 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
407 Writer.AddDeclRef(D->getSuperClass(), Record);
408 Record.push_back(D->ivar_size());
409 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
410 IEnd = D->ivar_end(); I != IEnd; ++I)
411 Writer.AddDeclRef(*I, Record);
412 Record.push_back(D->isForwardDecl());
413 Record.push_back(D->isImplicitInterfaceDecl());
414 Writer.AddSourceLocation(D->getClassLoc(), Record);
415 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
416 Writer.AddSourceLocation(D->getLocEnd(), Record);
417 // FIXME: add protocols, categories.
Steve Naroff97b53bd2009-04-21 15:12:33 +0000418 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff7333b492009-04-20 20:09:33 +0000419}
420
421void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
422 VisitFieldDecl(D);
423 // FIXME: stable encoding for @public/@private/@protected/@package
424 Record.push_back(D->getAccessControl());
Steve Naroff97b53bd2009-04-21 15:12:33 +0000425 Code = pch::DECL_OBJC_IVAR;
426}
427
428void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
429 VisitObjCContainerDecl(D);
430 Record.push_back(D->isForwardDecl());
431 Writer.AddSourceLocation(D->getLocEnd(), Record);
432 Record.push_back(D->protocol_size());
433 for (ObjCProtocolDecl::protocol_iterator
434 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
435 Writer.AddDeclRef(*I, Record);
436 Code = pch::DECL_OBJC_PROTOCOL;
437}
438
439void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
440 VisitFieldDecl(D);
441 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
442}
443
444void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
445 VisitDecl(D);
446 Record.push_back(D->size());
447 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
448 Writer.AddDeclRef(*I, Record);
449 Code = pch::DECL_OBJC_CLASS;
450}
451
452void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
453 VisitDecl(D);
454 Record.push_back(D->protocol_size());
455 for (ObjCProtocolDecl::protocol_iterator
456 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
457 Writer.AddDeclRef(*I, Record);
458 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
459}
460
461void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
462 VisitObjCContainerDecl(D);
463 Writer.AddDeclRef(D->getClassInterface(), Record);
464 Record.push_back(D->protocol_size());
465 for (ObjCProtocolDecl::protocol_iterator
466 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
467 Writer.AddDeclRef(*I, Record);
468 Writer.AddDeclRef(D->getNextClassCategory(), Record);
469 Writer.AddSourceLocation(D->getLocEnd(), Record);
470 Code = pch::DECL_OBJC_CATEGORY;
471}
472
473void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
474 VisitNamedDecl(D);
475 Writer.AddDeclRef(D->getClassInterface(), Record);
476 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
477}
478
479void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
480 VisitNamedDecl(D);
481 // FIXME: Implement.
482 Code = pch::DECL_OBJC_PROPERTY;
483}
484
485void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
486 VisitDecl(D);
487 // FIXME: Implement.
488 // Abstract class (no need to define a stable pch::DECL code).
489}
490
491void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
492 VisitObjCImplDecl(D);
493 // FIXME: Implement.
494 Code = pch::DECL_OBJC_CATEGORY_IMPL;
495}
496
497void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
498 VisitObjCImplDecl(D);
499 // FIXME: Implement.
500 Code = pch::DECL_OBJC_IMPLEMENTATION;
501}
502
503void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
504 VisitDecl(D);
505 // FIXME: Implement.
506 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff7333b492009-04-20 20:09:33 +0000507}
508
Douglas Gregor982365e2009-04-13 21:20:57 +0000509void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
510 VisitValueDecl(D);
511 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000512 Record.push_back(D->getBitWidth()? 1 : 0);
513 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000514 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000515 Code = pch::DECL_FIELD;
516}
517
Douglas Gregorc34897d2009-04-09 22:27:44 +0000518void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
519 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000520 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000521 Record.push_back(D->isThreadSpecified());
522 Record.push_back(D->hasCXXDirectInitializer());
523 Record.push_back(D->isDeclaredInCondition());
524 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
525 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000526 Record.push_back(D->getInit()? 1 : 0);
527 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000528 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000529 Code = pch::DECL_VAR;
530}
531
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000532void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
533 VisitVarDecl(D);
534 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000535 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000536 // FIXME: why isn't the "default argument" just stored as the initializer
537 // in VarDecl?
538 Code = pch::DECL_PARM_VAR;
539}
540
541void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
542 VisitParmVarDecl(D);
543 Writer.AddTypeRef(D->getOriginalType(), Record);
544 Code = pch::DECL_ORIGINAL_PARM_VAR;
545}
546
Douglas Gregor2a491792009-04-13 22:49:25 +0000547void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
548 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000549 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000550 Code = pch::DECL_FILE_SCOPE_ASM;
551}
552
553void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
554 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000555 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000556 Record.push_back(D->param_size());
557 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
558 P != PEnd; ++P)
559 Writer.AddDeclRef(*P, Record);
560 Code = pch::DECL_BLOCK;
561}
562
Douglas Gregorc34897d2009-04-09 22:27:44 +0000563/// \brief Emit the DeclContext part of a declaration context decl.
564///
565/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
566/// block for this declaration context is stored. May be 0 to indicate
567/// that there are no declarations stored within this context.
568///
569/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
570/// block for this declaration context is stored. May be 0 to indicate
571/// that there are no declarations visible from this context. Note
572/// that this value will not be emitted for non-primary declaration
573/// contexts.
574void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
575 uint64_t VisibleOffset) {
576 Record.push_back(LexicalOffset);
577 if (DC->getPrimaryContext() == DC)
578 Record.push_back(VisibleOffset);
579}
580
581//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000582// Statement/expression serialization
583//===----------------------------------------------------------------------===//
584namespace {
585 class VISIBILITY_HIDDEN PCHStmtWriter
586 : public StmtVisitor<PCHStmtWriter, void> {
587
588 PCHWriter &Writer;
589 PCHWriter::RecordData &Record;
590
591 public:
592 pch::StmtCode Code;
593
594 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
595 : Writer(Writer), Record(Record) { }
596
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000597 void VisitStmt(Stmt *S);
598 void VisitNullStmt(NullStmt *S);
599 void VisitCompoundStmt(CompoundStmt *S);
600 void VisitSwitchCase(SwitchCase *S);
601 void VisitCaseStmt(CaseStmt *S);
602 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000603 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000604 void VisitIfStmt(IfStmt *S);
605 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000606 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000607 void VisitDoStmt(DoStmt *S);
608 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000609 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000610 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000611 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000612 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000613 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000614 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000615 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000616 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000617 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000618 void VisitDeclRefExpr(DeclRefExpr *E);
619 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000620 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000621 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000622 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000623 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000624 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000625 void VisitUnaryOperator(UnaryOperator *E);
626 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000627 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000628 void VisitCallExpr(CallExpr *E);
629 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000630 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000631 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000632 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
633 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000634 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000635 void VisitExplicitCastExpr(ExplicitCastExpr *E);
636 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000637 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000638 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000639 void VisitInitListExpr(InitListExpr *E);
640 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
641 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000642 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000643 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000644 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000645 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
646 void VisitChooseExpr(ChooseExpr *E);
647 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000648 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000649 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000650 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000651
652 // Objective-C
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000653 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000654 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000655 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
656 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000657 };
658}
659
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000660void PCHStmtWriter::VisitStmt(Stmt *S) {
661}
662
663void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
664 VisitStmt(S);
665 Writer.AddSourceLocation(S->getSemiLoc(), Record);
666 Code = pch::STMT_NULL;
667}
668
669void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
670 VisitStmt(S);
671 Record.push_back(S->size());
672 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
673 CS != CSEnd; ++CS)
674 Writer.WriteSubStmt(*CS);
675 Writer.AddSourceLocation(S->getLBracLoc(), Record);
676 Writer.AddSourceLocation(S->getRBracLoc(), Record);
677 Code = pch::STMT_COMPOUND;
678}
679
680void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
681 VisitStmt(S);
682 Record.push_back(Writer.RecordSwitchCaseID(S));
683}
684
685void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
686 VisitSwitchCase(S);
687 Writer.WriteSubStmt(S->getLHS());
688 Writer.WriteSubStmt(S->getRHS());
689 Writer.WriteSubStmt(S->getSubStmt());
690 Writer.AddSourceLocation(S->getCaseLoc(), Record);
691 Code = pch::STMT_CASE;
692}
693
694void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
695 VisitSwitchCase(S);
696 Writer.WriteSubStmt(S->getSubStmt());
697 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
698 Code = pch::STMT_DEFAULT;
699}
700
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000701void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
702 VisitStmt(S);
703 Writer.AddIdentifierRef(S->getID(), Record);
704 Writer.WriteSubStmt(S->getSubStmt());
705 Writer.AddSourceLocation(S->getIdentLoc(), Record);
706 Record.push_back(Writer.GetLabelID(S));
707 Code = pch::STMT_LABEL;
708}
709
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000710void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
711 VisitStmt(S);
712 Writer.WriteSubStmt(S->getCond());
713 Writer.WriteSubStmt(S->getThen());
714 Writer.WriteSubStmt(S->getElse());
715 Writer.AddSourceLocation(S->getIfLoc(), Record);
716 Code = pch::STMT_IF;
717}
718
719void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
720 VisitStmt(S);
721 Writer.WriteSubStmt(S->getCond());
722 Writer.WriteSubStmt(S->getBody());
723 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
724 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
725 SC = SC->getNextSwitchCase())
726 Record.push_back(Writer.getSwitchCaseID(SC));
727 Code = pch::STMT_SWITCH;
728}
729
Douglas Gregora6b503f2009-04-17 00:16:09 +0000730void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
731 VisitStmt(S);
732 Writer.WriteSubStmt(S->getCond());
733 Writer.WriteSubStmt(S->getBody());
734 Writer.AddSourceLocation(S->getWhileLoc(), Record);
735 Code = pch::STMT_WHILE;
736}
737
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000738void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
739 VisitStmt(S);
740 Writer.WriteSubStmt(S->getCond());
741 Writer.WriteSubStmt(S->getBody());
742 Writer.AddSourceLocation(S->getDoLoc(), Record);
743 Code = pch::STMT_DO;
744}
745
746void PCHStmtWriter::VisitForStmt(ForStmt *S) {
747 VisitStmt(S);
748 Writer.WriteSubStmt(S->getInit());
749 Writer.WriteSubStmt(S->getCond());
750 Writer.WriteSubStmt(S->getInc());
751 Writer.WriteSubStmt(S->getBody());
752 Writer.AddSourceLocation(S->getForLoc(), Record);
753 Code = pch::STMT_FOR;
754}
755
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000756void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
757 VisitStmt(S);
758 Record.push_back(Writer.GetLabelID(S->getLabel()));
759 Writer.AddSourceLocation(S->getGotoLoc(), Record);
760 Writer.AddSourceLocation(S->getLabelLoc(), Record);
761 Code = pch::STMT_GOTO;
762}
763
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000764void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
765 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000766 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000767 Writer.WriteSubStmt(S->getTarget());
768 Code = pch::STMT_INDIRECT_GOTO;
769}
770
Douglas Gregora6b503f2009-04-17 00:16:09 +0000771void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
772 VisitStmt(S);
773 Writer.AddSourceLocation(S->getContinueLoc(), Record);
774 Code = pch::STMT_CONTINUE;
775}
776
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000777void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
778 VisitStmt(S);
779 Writer.AddSourceLocation(S->getBreakLoc(), Record);
780 Code = pch::STMT_BREAK;
781}
782
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000783void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
784 VisitStmt(S);
785 Writer.WriteSubStmt(S->getRetValue());
786 Writer.AddSourceLocation(S->getReturnLoc(), Record);
787 Code = pch::STMT_RETURN;
788}
789
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000790void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
791 VisitStmt(S);
792 Writer.AddSourceLocation(S->getStartLoc(), Record);
793 Writer.AddSourceLocation(S->getEndLoc(), Record);
794 DeclGroupRef DG = S->getDeclGroup();
795 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
796 Writer.AddDeclRef(*D, Record);
797 Code = pch::STMT_DECL;
798}
799
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000800void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
801 VisitStmt(S);
802 Record.push_back(S->getNumOutputs());
803 Record.push_back(S->getNumInputs());
804 Record.push_back(S->getNumClobbers());
805 Writer.AddSourceLocation(S->getAsmLoc(), Record);
806 Writer.AddSourceLocation(S->getRParenLoc(), Record);
807 Record.push_back(S->isVolatile());
808 Record.push_back(S->isSimple());
809 Writer.WriteSubStmt(S->getAsmString());
810
811 // Outputs
812 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
813 Writer.AddString(S->getOutputName(I), Record);
814 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
815 Writer.WriteSubStmt(S->getOutputExpr(I));
816 }
817
818 // Inputs
819 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
820 Writer.AddString(S->getInputName(I), Record);
821 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
822 Writer.WriteSubStmt(S->getInputExpr(I));
823 }
824
825 // Clobbers
826 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
827 Writer.WriteSubStmt(S->getClobber(I));
828
829 Code = pch::STMT_ASM;
830}
831
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000832void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000833 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000834 Writer.AddTypeRef(E->getType(), Record);
835 Record.push_back(E->isTypeDependent());
836 Record.push_back(E->isValueDependent());
837}
838
Douglas Gregore2f37202009-04-14 21:55:33 +0000839void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
840 VisitExpr(E);
841 Writer.AddSourceLocation(E->getLocation(), Record);
842 Record.push_back(E->getIdentType()); // FIXME: stable encoding
843 Code = pch::EXPR_PREDEFINED;
844}
845
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000846void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
847 VisitExpr(E);
848 Writer.AddDeclRef(E->getDecl(), Record);
849 Writer.AddSourceLocation(E->getLocation(), Record);
850 Code = pch::EXPR_DECL_REF;
851}
852
853void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
854 VisitExpr(E);
855 Writer.AddSourceLocation(E->getLocation(), Record);
856 Writer.AddAPInt(E->getValue(), Record);
857 Code = pch::EXPR_INTEGER_LITERAL;
858}
859
Douglas Gregore2f37202009-04-14 21:55:33 +0000860void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
861 VisitExpr(E);
862 Writer.AddAPFloat(E->getValue(), Record);
863 Record.push_back(E->isExact());
864 Writer.AddSourceLocation(E->getLocation(), Record);
865 Code = pch::EXPR_FLOATING_LITERAL;
866}
867
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000868void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
869 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000870 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000871 Code = pch::EXPR_IMAGINARY_LITERAL;
872}
873
Douglas Gregor596e0932009-04-15 16:35:07 +0000874void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
875 VisitExpr(E);
876 Record.push_back(E->getByteLength());
877 Record.push_back(E->getNumConcatenated());
878 Record.push_back(E->isWide());
879 // FIXME: String data should be stored as a blob at the end of the
880 // StringLiteral. However, we can't do so now because we have no
881 // provision for coping with abbreviations when we're jumping around
882 // the PCH file during deserialization.
883 Record.insert(Record.end(),
884 E->getStrData(), E->getStrData() + E->getByteLength());
885 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
886 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
887 Code = pch::EXPR_STRING_LITERAL;
888}
889
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000890void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
891 VisitExpr(E);
892 Record.push_back(E->getValue());
893 Writer.AddSourceLocation(E->getLoc(), Record);
894 Record.push_back(E->isWide());
895 Code = pch::EXPR_CHARACTER_LITERAL;
896}
897
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000898void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
899 VisitExpr(E);
900 Writer.AddSourceLocation(E->getLParen(), Record);
901 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000902 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000903 Code = pch::EXPR_PAREN;
904}
905
Douglas Gregor12d74052009-04-15 15:58:59 +0000906void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
907 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000908 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000909 Record.push_back(E->getOpcode()); // FIXME: stable encoding
910 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
911 Code = pch::EXPR_UNARY_OPERATOR;
912}
913
914void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
915 VisitExpr(E);
916 Record.push_back(E->isSizeOf());
917 if (E->isArgumentType())
918 Writer.AddTypeRef(E->getArgumentType(), Record);
919 else {
920 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000921 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000922 }
923 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
924 Writer.AddSourceLocation(E->getRParenLoc(), Record);
925 Code = pch::EXPR_SIZEOF_ALIGN_OF;
926}
927
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000928void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
929 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000930 Writer.WriteSubStmt(E->getLHS());
931 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000932 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
933 Code = pch::EXPR_ARRAY_SUBSCRIPT;
934}
935
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000936void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
937 VisitExpr(E);
938 Record.push_back(E->getNumArgs());
939 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000940 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000941 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
942 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000943 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000944 Code = pch::EXPR_CALL;
945}
946
947void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
948 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000949 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000950 Writer.AddDeclRef(E->getMemberDecl(), Record);
951 Writer.AddSourceLocation(E->getMemberLoc(), Record);
952 Record.push_back(E->isArrow());
953 Code = pch::EXPR_MEMBER;
954}
955
Douglas Gregora151ba42009-04-14 23:32:43 +0000956void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
957 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000958 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000959}
960
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000961void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
962 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000963 Writer.WriteSubStmt(E->getLHS());
964 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000965 Record.push_back(E->getOpcode()); // FIXME: stable encoding
966 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
967 Code = pch::EXPR_BINARY_OPERATOR;
968}
969
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000970void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
971 VisitBinaryOperator(E);
972 Writer.AddTypeRef(E->getComputationLHSType(), Record);
973 Writer.AddTypeRef(E->getComputationResultType(), Record);
974 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
975}
976
977void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
978 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000979 Writer.WriteSubStmt(E->getCond());
980 Writer.WriteSubStmt(E->getLHS());
981 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000982 Code = pch::EXPR_CONDITIONAL_OPERATOR;
983}
984
Douglas Gregora151ba42009-04-14 23:32:43 +0000985void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
986 VisitCastExpr(E);
987 Record.push_back(E->isLvalueCast());
988 Code = pch::EXPR_IMPLICIT_CAST;
989}
990
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000991void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
992 VisitCastExpr(E);
993 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
994}
995
996void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
997 VisitExplicitCastExpr(E);
998 Writer.AddSourceLocation(E->getLParenLoc(), Record);
999 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1000 Code = pch::EXPR_CSTYLE_CAST;
1001}
1002
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001003void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1004 VisitExpr(E);
1005 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001006 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001007 Record.push_back(E->isFileScope());
1008 Code = pch::EXPR_COMPOUND_LITERAL;
1009}
1010
Douglas Gregorec0b8292009-04-15 23:02:49 +00001011void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1012 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001013 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001014 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1015 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1016 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1017}
1018
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001019void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1020 VisitExpr(E);
1021 Record.push_back(E->getNumInits());
1022 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001023 Writer.WriteSubStmt(E->getInit(I));
1024 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001025 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1026 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1027 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1028 Record.push_back(E->hadArrayRangeDesignator());
1029 Code = pch::EXPR_INIT_LIST;
1030}
1031
1032void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1033 VisitExpr(E);
1034 Record.push_back(E->getNumSubExprs());
1035 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001036 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001037 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1038 Record.push_back(E->usesGNUSyntax());
1039 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1040 DEnd = E->designators_end();
1041 D != DEnd; ++D) {
1042 if (D->isFieldDesignator()) {
1043 if (FieldDecl *Field = D->getField()) {
1044 Record.push_back(pch::DESIG_FIELD_DECL);
1045 Writer.AddDeclRef(Field, Record);
1046 } else {
1047 Record.push_back(pch::DESIG_FIELD_NAME);
1048 Writer.AddIdentifierRef(D->getFieldName(), Record);
1049 }
1050 Writer.AddSourceLocation(D->getDotLoc(), Record);
1051 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1052 } else if (D->isArrayDesignator()) {
1053 Record.push_back(pch::DESIG_ARRAY);
1054 Record.push_back(D->getFirstExprIndex());
1055 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1056 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1057 } else {
1058 assert(D->isArrayRangeDesignator() && "Unknown designator");
1059 Record.push_back(pch::DESIG_ARRAY_RANGE);
1060 Record.push_back(D->getFirstExprIndex());
1061 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1062 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1063 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1064 }
1065 }
1066 Code = pch::EXPR_DESIGNATED_INIT;
1067}
1068
1069void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1070 VisitExpr(E);
1071 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1072}
1073
Douglas Gregorec0b8292009-04-15 23:02:49 +00001074void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1075 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001076 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001077 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1078 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1079 Code = pch::EXPR_VA_ARG;
1080}
1081
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001082void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1083 VisitExpr(E);
1084 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1085 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1086 Record.push_back(Writer.GetLabelID(E->getLabel()));
1087 Code = pch::EXPR_ADDR_LABEL;
1088}
1089
Douglas Gregoreca12f62009-04-17 19:05:30 +00001090void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1091 VisitExpr(E);
1092 Writer.WriteSubStmt(E->getSubStmt());
1093 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1094 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1095 Code = pch::EXPR_STMT;
1096}
1097
Douglas Gregor209d4622009-04-15 23:33:31 +00001098void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1099 VisitExpr(E);
1100 Writer.AddTypeRef(E->getArgType1(), Record);
1101 Writer.AddTypeRef(E->getArgType2(), Record);
1102 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1103 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1104 Code = pch::EXPR_TYPES_COMPATIBLE;
1105}
1106
1107void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1108 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001109 Writer.WriteSubStmt(E->getCond());
1110 Writer.WriteSubStmt(E->getLHS());
1111 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001112 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1113 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1114 Code = pch::EXPR_CHOOSE;
1115}
1116
1117void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1118 VisitExpr(E);
1119 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1120 Code = pch::EXPR_GNU_NULL;
1121}
1122
Douglas Gregor725e94b2009-04-16 00:01:45 +00001123void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1124 VisitExpr(E);
1125 Record.push_back(E->getNumSubExprs());
1126 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001127 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001128 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1129 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1130 Code = pch::EXPR_SHUFFLE_VECTOR;
1131}
1132
Douglas Gregore246b742009-04-17 19:21:43 +00001133void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1134 VisitExpr(E);
1135 Writer.AddDeclRef(E->getBlockDecl(), Record);
1136 Record.push_back(E->hasBlockDeclRefExprs());
1137 Code = pch::EXPR_BLOCK;
1138}
1139
Douglas Gregor725e94b2009-04-16 00:01:45 +00001140void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1141 VisitExpr(E);
1142 Writer.AddDeclRef(E->getDecl(), Record);
1143 Writer.AddSourceLocation(E->getLocation(), Record);
1144 Record.push_back(E->isByRef());
1145 Code = pch::EXPR_BLOCK_DECL_REF;
1146}
1147
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001148//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001149// Objective-C Expressions and Statements.
1150//===----------------------------------------------------------------------===//
1151
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001152void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1153 VisitExpr(E);
1154 Writer.WriteSubStmt(E->getString());
1155 Writer.AddSourceLocation(E->getAtLoc(), Record);
1156 Code = pch::EXPR_OBJC_STRING_LITERAL;
1157}
1158
Chris Lattner80f83c62009-04-22 05:57:30 +00001159void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1160 VisitExpr(E);
1161 Writer.AddTypeRef(E->getEncodedType(), Record);
1162 Writer.AddSourceLocation(E->getAtLoc(), Record);
1163 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1164 Code = pch::EXPR_OBJC_ENCODE;
1165}
1166
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001167void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1168 VisitExpr(E);
Chris Lattner9b9f2352009-04-22 06:40:03 +00001169 assert(0 && "Can't write a selector yet!");
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001170 // FIXME! Write selectors.
1171 //Writer.WriteSubStmt(E->getSelector());
1172 Writer.AddSourceLocation(E->getAtLoc(), Record);
1173 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1174 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1175}
1176
1177void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1178 VisitExpr(E);
1179 Writer.AddDeclRef(E->getProtocol(), Record);
1180 Writer.AddSourceLocation(E->getAtLoc(), Record);
1181 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1182 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1183}
1184
Chris Lattner80f83c62009-04-22 05:57:30 +00001185
1186//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001187// PCHWriter Implementation
1188//===----------------------------------------------------------------------===//
1189
Douglas Gregorb5887f32009-04-10 21:16:55 +00001190/// \brief Write the target triple (e.g., i686-apple-darwin9).
1191void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1192 using namespace llvm;
1193 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1194 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001196 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001197
1198 RecordData Record;
1199 Record.push_back(pch::TARGET_TRIPLE);
1200 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001201 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001202}
1203
1204/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001205void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1206 RecordData Record;
1207 Record.push_back(LangOpts.Trigraphs);
1208 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1209 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1210 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1211 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1212 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1213 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1214 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1215 Record.push_back(LangOpts.C99); // C99 Support
1216 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1217 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1218 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1219 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1220 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1221
1222 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1223 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1224 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1225
1226 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1227 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1228 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1229 Record.push_back(LangOpts.LaxVectorConversions);
1230 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1231
1232 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1233 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1234 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1235
1236 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1237 // by locks.
1238 Record.push_back(LangOpts.Blocks); // block extension to C
1239 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1240 // they are unused.
1241 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1242 // (modulo the platform support).
1243
1244 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1245 // signed integer arithmetic overflows.
1246
1247 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1248 // may be ripped out at any time.
1249
1250 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1251 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1252 // defined.
1253 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1254 // opposed to __DYNAMIC__).
1255 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1256
1257 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1258 // used (instead of C99 semantics).
1259 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1260 Record.push_back(LangOpts.getGCMode());
1261 Record.push_back(LangOpts.getVisibilityMode());
1262 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001263 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001264}
1265
Douglas Gregorab1cef72009-04-10 03:52:48 +00001266//===----------------------------------------------------------------------===//
1267// Source Manager Serialization
1268//===----------------------------------------------------------------------===//
1269
1270/// \brief Create an abbreviation for the SLocEntry that refers to a
1271/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001272static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001273 using namespace llvm;
1274 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1275 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001281 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001282}
1283
1284/// \brief Create an abbreviation for the SLocEntry that refers to a
1285/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001286static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001287 using namespace llvm;
1288 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1289 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001295 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001296}
1297
1298/// \brief Create an abbreviation for the SLocEntry that refers to a
1299/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001300static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001301 using namespace llvm;
1302 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1303 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1304 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001305 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001306}
1307
1308/// \brief Create an abbreviation for the SLocEntry that refers to an
1309/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001310static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001311 using namespace llvm;
1312 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1313 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1314 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001319 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001320}
1321
1322/// \brief Writes the block containing the serialized form of the
1323/// source manager.
1324///
1325/// TODO: We should probably use an on-disk hash table (stored in a
1326/// blob), indexed based on the file name, so that we only create
1327/// entries for files that we actually need. In the common case (no
1328/// errors), we probably won't have to create file entries for any of
1329/// the files in the AST.
1330void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001331 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001332 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001333
1334 // Abbreviations for the various kinds of source-location entries.
1335 int SLocFileAbbrv = -1;
1336 int SLocBufferAbbrv = -1;
1337 int SLocBufferBlobAbbrv = -1;
1338 int SLocInstantiationAbbrv = -1;
1339
1340 // Write out the source location entry table. We skip the first
1341 // entry, which is always the same dummy entry.
1342 RecordData Record;
1343 for (SourceManager::sloc_entry_iterator
1344 SLoc = SourceMgr.sloc_entry_begin() + 1,
1345 SLocEnd = SourceMgr.sloc_entry_end();
1346 SLoc != SLocEnd; ++SLoc) {
1347 // Figure out which record code to use.
1348 unsigned Code;
1349 if (SLoc->isFile()) {
1350 if (SLoc->getFile().getContentCache()->Entry)
1351 Code = pch::SM_SLOC_FILE_ENTRY;
1352 else
1353 Code = pch::SM_SLOC_BUFFER_ENTRY;
1354 } else
1355 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1356 Record.push_back(Code);
1357
1358 Record.push_back(SLoc->getOffset());
1359 if (SLoc->isFile()) {
1360 const SrcMgr::FileInfo &File = SLoc->getFile();
1361 Record.push_back(File.getIncludeLoc().getRawEncoding());
1362 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001363 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001364
1365 const SrcMgr::ContentCache *Content = File.getContentCache();
1366 if (Content->Entry) {
1367 // The source location entry is a file. The blob associated
1368 // with this entry is the file name.
1369 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001370 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1371 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001372 Content->Entry->getName(),
1373 strlen(Content->Entry->getName()));
1374 } else {
1375 // The source location entry is a buffer. The blob associated
1376 // with this entry contains the contents of the buffer.
1377 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001378 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1379 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001380 }
1381
1382 // We add one to the size so that we capture the trailing NULL
1383 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1384 // the reader side).
1385 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1386 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001387 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001388 Record.clear();
1389 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001390 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001391 Buffer->getBufferStart(),
1392 Buffer->getBufferSize() + 1);
1393 }
1394 } else {
1395 // The source location entry is an instantiation.
1396 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1397 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1398 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1399 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1400
Douglas Gregor364e5802009-04-15 18:05:10 +00001401 // Compute the token length for this macro expansion.
1402 unsigned NextOffset = SourceMgr.getNextOffset();
1403 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1404 if (++NextSLoc != SLocEnd)
1405 NextOffset = NextSLoc->getOffset();
1406 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1407
Douglas Gregorab1cef72009-04-10 03:52:48 +00001408 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001409 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1410 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001411 }
1412
1413 Record.clear();
1414 }
1415
Douglas Gregor635f97f2009-04-13 16:31:14 +00001416 // Write the line table.
1417 if (SourceMgr.hasLineTable()) {
1418 LineTableInfo &LineTable = SourceMgr.getLineTable();
1419
1420 // Emit the file names
1421 Record.push_back(LineTable.getNumFilenames());
1422 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1423 // Emit the file name
1424 const char *Filename = LineTable.getFilename(I);
1425 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1426 Record.push_back(FilenameLen);
1427 if (FilenameLen)
1428 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1429 }
1430
1431 // Emit the line entries
1432 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1433 L != LEnd; ++L) {
1434 // Emit the file ID
1435 Record.push_back(L->first);
1436
1437 // Emit the line entries
1438 Record.push_back(L->second.size());
1439 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1440 LEEnd = L->second.end();
1441 LE != LEEnd; ++LE) {
1442 Record.push_back(LE->FileOffset);
1443 Record.push_back(LE->LineNo);
1444 Record.push_back(LE->FilenameID);
1445 Record.push_back((unsigned)LE->FileKind);
1446 Record.push_back(LE->IncludeOffset);
1447 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001448 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001449 }
1450 }
1451
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001452 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001453}
1454
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001455/// \brief Writes the block containing the serialized form of the
1456/// preprocessor.
1457///
Chris Lattner850eabd2009-04-10 18:08:30 +00001458void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001459 // Enter the preprocessor block.
Douglas Gregorc713da92009-04-21 22:25:48 +00001460 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner84b04f12009-04-10 17:16:57 +00001461
Chris Lattner1b094952009-04-10 18:00:12 +00001462 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1463 // FIXME: use diagnostics subsystem for localization etc.
1464 if (PP.SawDateOrTime())
1465 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001466
Chris Lattner1b094952009-04-10 18:00:12 +00001467 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001468
Chris Lattner4b21c202009-04-13 01:29:17 +00001469 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1470 if (PP.getCounterValue() != 0) {
1471 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001472 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001473 Record.clear();
1474 }
1475
Chris Lattner1b094952009-04-10 18:00:12 +00001476 // Loop over all the macro definitions that are live at the end of the file,
1477 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001478 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1479 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001480 // FIXME: This emits macros in hash table order, we should do it in a stable
1481 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001482 MacroInfo *MI = I->second;
1483
1484 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1485 // been redefined by the header (in which case they are not isBuiltinMacro).
1486 if (MI->isBuiltinMacro())
1487 continue;
1488
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001489 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001490 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001491 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001492 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1493 Record.push_back(MI->isUsed());
1494
1495 unsigned Code;
1496 if (MI->isObjectLike()) {
1497 Code = pch::PP_MACRO_OBJECT_LIKE;
1498 } else {
1499 Code = pch::PP_MACRO_FUNCTION_LIKE;
1500
1501 Record.push_back(MI->isC99Varargs());
1502 Record.push_back(MI->isGNUVarargs());
1503 Record.push_back(MI->getNumArgs());
1504 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1505 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001506 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001507 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001508 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001509 Record.clear();
1510
Chris Lattner850eabd2009-04-10 18:08:30 +00001511 // Emit the tokens array.
1512 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1513 // Note that we know that the preprocessor does not have any annotation
1514 // tokens in it because they are created by the parser, and thus can't be
1515 // in a macro definition.
1516 const Token &Tok = MI->getReplacementToken(TokNo);
1517
1518 Record.push_back(Tok.getLocation().getRawEncoding());
1519 Record.push_back(Tok.getLength());
1520
Chris Lattner850eabd2009-04-10 18:08:30 +00001521 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1522 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001523 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001524
1525 // FIXME: Should translate token kind to a stable encoding.
1526 Record.push_back(Tok.getKind());
1527 // FIXME: Should translate token flags to a stable encoding.
1528 Record.push_back(Tok.getFlags());
1529
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001530 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001531 Record.clear();
1532 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001533 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001534 }
1535
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001536 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001537}
1538
1539
Douglas Gregorc34897d2009-04-09 22:27:44 +00001540/// \brief Write the representation of a type to the PCH stream.
1541void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001542 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001543 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001544 ID = NextTypeID++;
1545
1546 // Record the offset for this type.
1547 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001548 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001549 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1550 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001551 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001552 }
1553
1554 RecordData Record;
1555
1556 // Emit the type's representation.
1557 PCHTypeWriter W(*this, Record);
1558 switch (T->getTypeClass()) {
1559 // For all of the concrete, non-dependent types, call the
1560 // appropriate visitor function.
1561#define TYPE(Class, Base) \
1562 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1563#define ABSTRACT_TYPE(Class, Base)
1564#define DEPENDENT_TYPE(Class, Base)
1565#include "clang/AST/TypeNodes.def"
1566
1567 // For all of the dependent type nodes (which only occur in C++
1568 // templates), produce an error.
1569#define TYPE(Class, Base)
1570#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1571#include "clang/AST/TypeNodes.def"
1572 assert(false && "Cannot serialize dependent type nodes");
1573 break;
1574 }
1575
1576 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001577 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001578
1579 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001580 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001581}
1582
1583/// \brief Write a block containing all of the types.
1584void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001585 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001586 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001587
1588 // Emit all of the types in the ASTContext
1589 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1590 TEnd = Context.getTypes().end();
1591 T != TEnd; ++T) {
1592 // Builtin types are never serialized.
1593 if (isa<BuiltinType>(*T))
1594 continue;
1595
1596 WriteType(*T);
1597 }
1598
1599 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001600 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001601}
1602
1603/// \brief Write the block containing all of the declaration IDs
1604/// lexically declared within the given DeclContext.
1605///
1606/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1607/// bistream, or 0 if no block was written.
1608uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1609 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001610 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001611 return 0;
1612
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001613 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001614 RecordData Record;
1615 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1616 DEnd = DC->decls_end(Context);
1617 D != DEnd; ++D)
1618 AddDeclRef(*D, Record);
1619
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001620 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001621 return Offset;
1622}
1623
1624/// \brief Write the block containing all of the declaration IDs
1625/// visible from the given DeclContext.
1626///
1627/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1628/// bistream, or 0 if no block was written.
1629uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1630 DeclContext *DC) {
1631 if (DC->getPrimaryContext() != DC)
1632 return 0;
1633
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001634 // Since there is no name lookup into functions or methods, and we
1635 // perform name lookup for the translation unit via the
1636 // IdentifierInfo chains, don't bother to build a
1637 // visible-declarations table for these entities.
1638 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001639 return 0;
1640
Douglas Gregorc34897d2009-04-09 22:27:44 +00001641 // Force the DeclContext to build a its name-lookup table.
1642 DC->lookup(Context, DeclarationName());
1643
1644 // Serialize the contents of the mapping used for lookup. Note that,
1645 // although we have two very different code paths, the serialized
1646 // representation is the same for both cases: a declaration name,
1647 // followed by a size, followed by references to the visible
1648 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001649 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001650 RecordData Record;
1651 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001652 if (!Map)
1653 return 0;
1654
Douglas Gregorc34897d2009-04-09 22:27:44 +00001655 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1656 D != DEnd; ++D) {
1657 AddDeclarationName(D->first, Record);
1658 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1659 Record.push_back(Result.second - Result.first);
1660 for(; Result.first != Result.second; ++Result.first)
1661 AddDeclRef(*Result.first, Record);
1662 }
1663
1664 if (Record.size() == 0)
1665 return 0;
1666
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001667 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001668 return Offset;
1669}
1670
1671/// \brief Write a block containing all of the declarations.
1672void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001673 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001674 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001675
1676 // Emit all of the declarations.
1677 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001678 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001679 while (!DeclsToEmit.empty()) {
1680 // Pull the next declaration off the queue
1681 Decl *D = DeclsToEmit.front();
1682 DeclsToEmit.pop();
1683
1684 // If this declaration is also a DeclContext, write blocks for the
1685 // declarations that lexically stored inside its context and those
1686 // declarations that are visible from its context. These blocks
1687 // are written before the declaration itself so that we can put
1688 // their offsets into the record for the declaration.
1689 uint64_t LexicalOffset = 0;
1690 uint64_t VisibleOffset = 0;
1691 DeclContext *DC = dyn_cast<DeclContext>(D);
1692 if (DC) {
1693 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1694 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1695 }
1696
1697 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001698 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001699 if (ID == 0)
1700 ID = DeclIDs.size();
1701
1702 unsigned Index = ID - 1;
1703
1704 // Record the offset for this declaration
1705 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001706 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001707 else if (DeclOffsets.size() < Index) {
1708 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001709 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001710 }
1711
1712 // Build and emit a record for this declaration
1713 Record.clear();
1714 W.Code = (pch::DeclCode)0;
1715 W.Visit(D);
1716 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001717 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001718 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001719
Douglas Gregor1c507882009-04-15 21:30:51 +00001720 // If the declaration had any attributes, write them now.
1721 if (D->hasAttrs())
1722 WriteAttributeRecord(D->getAttrs());
1723
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001724 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001725 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001726
Douglas Gregor631f6c62009-04-14 00:24:19 +00001727 // Note external declarations so that we can add them to a record
1728 // in the PCH file later.
1729 if (isa<FileScopeAsmDecl>(D))
1730 ExternalDefinitions.push_back(ID);
1731 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1732 if (// Non-static file-scope variables with initializers or that
1733 // are tentative definitions.
1734 (Var->isFileVarDecl() &&
1735 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1736 // Out-of-line definitions of static data members (C++).
1737 (Var->getDeclContext()->isRecord() &&
1738 !Var->getLexicalDeclContext()->isRecord() &&
1739 Var->getStorageClass() == VarDecl::Static))
1740 ExternalDefinitions.push_back(ID);
1741 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00001742 if (Func->isThisDeclarationADefinition())
Douglas Gregor631f6c62009-04-14 00:24:19 +00001743 ExternalDefinitions.push_back(ID);
1744 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001745 }
1746
1747 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001748 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001749}
1750
Douglas Gregorff9a6092009-04-20 20:36:09 +00001751namespace {
1752class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1753 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001754 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001755
1756public:
1757 typedef const IdentifierInfo* key_type;
1758 typedef key_type key_type_ref;
1759
1760 typedef pch::IdentID data_type;
1761 typedef data_type data_type_ref;
1762
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001763 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1764 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001765
1766 static unsigned ComputeHash(const IdentifierInfo* II) {
1767 return clang::BernsteinHash(II->getName());
1768 }
1769
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001770 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001771 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1772 pch::IdentID ID) {
1773 unsigned KeyLen = strlen(II->getName()) + 1;
1774 clang::io::Emit16(Out, KeyLen);
Douglas Gregorc713da92009-04-21 22:25:48 +00001775 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1776 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001777 if (II->hasMacroDefinition() &&
1778 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1779 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001780 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1781 DEnd = IdentifierResolver::end();
1782 D != DEnd; ++D)
1783 DataLen += sizeof(pch::DeclID);
Douglas Gregorc713da92009-04-21 22:25:48 +00001784 clang::io::Emit16(Out, DataLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001785 return std::make_pair(KeyLen, DataLen);
1786 }
1787
1788 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1789 unsigned KeyLen) {
1790 // Record the location of the key data. This is used when generating
1791 // the mapping from persistent IDs to strings.
1792 Writer.SetIdentifierOffset(II, Out.tell());
1793 Out.write(II->getName(), KeyLen);
1794 }
1795
1796 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1797 pch::IdentID ID, unsigned) {
1798 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001799 bool hasMacroDefinition =
1800 II->hasMacroDefinition() &&
1801 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001802 Bits = Bits | (uint32_t)II->getTokenID();
1803 Bits = (Bits << 8) | (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001804 Bits = (Bits << 10) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001805 Bits = (Bits << 1) | II->isExtensionToken();
1806 Bits = (Bits << 1) | II->isPoisoned();
1807 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1808 clang::io::Emit32(Out, Bits);
1809 clang::io::Emit32(Out, ID);
1810
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001811 if (hasMacroDefinition)
1812 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1813
Douglas Gregorc713da92009-04-21 22:25:48 +00001814 // Emit the declaration IDs in reverse order, because the
1815 // IdentifierResolver provides the declarations as they would be
1816 // visible (e.g., the function "stat" would come before the struct
1817 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1818 // adds declarations to the end of the list (so we need to see the
1819 // struct "status" before the function "status").
1820 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1821 IdentifierResolver::end());
1822 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1823 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001824 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001825 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001826 }
1827};
1828} // end anonymous namespace
1829
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001830/// \brief Write the identifier table into the PCH file.
1831///
1832/// The identifier table consists of a blob containing string data
1833/// (the actual identifiers themselves) and a separate "offsets" index
1834/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001835void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001836 using namespace llvm;
1837
1838 // Create and write out the blob that contains the identifier
1839 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001840 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001841 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001842 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1843
1844 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001845 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1846 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1847 ID != IDEnd; ++ID) {
1848 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorff9a6092009-04-20 20:36:09 +00001849 Generator.insert(ID->first, ID->second);
1850 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001851
Douglas Gregorff9a6092009-04-20 20:36:09 +00001852 // Create the on-disk hash table in a buffer.
1853 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001854 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001855 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001856 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001857 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc713da92009-04-21 22:25:48 +00001858 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001859 }
1860
1861 // Create a blob abbreviation
1862 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1863 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001866 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001867
1868 // Write the identifier table
1869 RecordData Record;
1870 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001871 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001872 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1873 &IdentifierTable.front(),
1874 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001875 }
1876
1877 // Write the offsets table for identifier IDs.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001878 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001879}
1880
Douglas Gregor1c507882009-04-15 21:30:51 +00001881/// \brief Write a record containing the given attributes.
1882void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1883 RecordData Record;
1884 for (; Attr; Attr = Attr->getNext()) {
1885 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1886 Record.push_back(Attr->isInherited());
1887 switch (Attr->getKind()) {
1888 case Attr::Alias:
1889 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1890 break;
1891
1892 case Attr::Aligned:
1893 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1894 break;
1895
1896 case Attr::AlwaysInline:
1897 break;
1898
1899 case Attr::AnalyzerNoReturn:
1900 break;
1901
1902 case Attr::Annotate:
1903 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1904 break;
1905
1906 case Attr::AsmLabel:
1907 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1908 break;
1909
1910 case Attr::Blocks:
1911 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1912 break;
1913
1914 case Attr::Cleanup:
1915 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1916 break;
1917
1918 case Attr::Const:
1919 break;
1920
1921 case Attr::Constructor:
1922 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1923 break;
1924
1925 case Attr::DLLExport:
1926 case Attr::DLLImport:
1927 case Attr::Deprecated:
1928 break;
1929
1930 case Attr::Destructor:
1931 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1932 break;
1933
1934 case Attr::FastCall:
1935 break;
1936
1937 case Attr::Format: {
1938 const FormatAttr *Format = cast<FormatAttr>(Attr);
1939 AddString(Format->getType(), Record);
1940 Record.push_back(Format->getFormatIdx());
1941 Record.push_back(Format->getFirstArg());
1942 break;
1943 }
1944
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001945 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001946 case Attr::IBOutletKind:
1947 case Attr::NoReturn:
1948 case Attr::NoThrow:
1949 case Attr::Nodebug:
1950 case Attr::Noinline:
1951 break;
1952
1953 case Attr::NonNull: {
1954 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1955 Record.push_back(NonNull->size());
1956 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1957 break;
1958 }
1959
1960 case Attr::ObjCException:
1961 case Attr::ObjCNSObject:
1962 case Attr::Overloadable:
1963 break;
1964
1965 case Attr::Packed:
1966 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1967 break;
1968
1969 case Attr::Pure:
1970 break;
1971
1972 case Attr::Regparm:
1973 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1974 break;
1975
1976 case Attr::Section:
1977 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1978 break;
1979
1980 case Attr::StdCall:
1981 case Attr::TransparentUnion:
1982 case Attr::Unavailable:
1983 case Attr::Unused:
1984 case Attr::Used:
1985 break;
1986
1987 case Attr::Visibility:
1988 // FIXME: stable encoding
1989 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1990 break;
1991
1992 case Attr::WarnUnusedResult:
1993 case Attr::Weak:
1994 case Attr::WeakImport:
1995 break;
1996 }
1997 }
1998
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001999 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002000}
2001
2002void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2003 Record.push_back(Str.size());
2004 Record.insert(Record.end(), Str.begin(), Str.end());
2005}
2006
Douglas Gregorff9a6092009-04-20 20:36:09 +00002007/// \brief Note that the identifier II occurs at the given offset
2008/// within the identifier table.
2009void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
2010 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
2011}
2012
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002013PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002014 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
2015 NumStatements(0), NumMacros(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002016
Douglas Gregor87887da2009-04-20 15:53:59 +00002017void PCHWriter::WritePCH(Sema &SemaRef) {
2018 ASTContext &Context = SemaRef.Context;
2019 Preprocessor &PP = SemaRef.PP;
2020
Douglas Gregorc34897d2009-04-09 22:27:44 +00002021 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002022 Stream.Emit((unsigned)'C', 8);
2023 Stream.Emit((unsigned)'P', 8);
2024 Stream.Emit((unsigned)'C', 8);
2025 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002026
2027 // The translation unit is the first declaration we'll emit.
2028 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2029 DeclsToEmit.push(Context.getTranslationUnitDecl());
2030
2031 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002032 RecordData Record;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002033 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002034 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002035 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00002036 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002037 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002038 WriteTypesBlock(Context);
2039 WriteDeclsBlock(Context);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002040 WriteIdentifierTable(PP);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002041 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
2042 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregore01ad442009-04-18 05:55:16 +00002043
2044 // Write the record of special types.
2045 Record.clear();
2046 AddTypeRef(Context.getBuiltinVaListType(), Record);
2047 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2048
Douglas Gregor631f6c62009-04-14 00:24:19 +00002049 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002050 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor456e0952009-04-17 22:13:46 +00002051
2052 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002053 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002054 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002055 Record.push_back(NumMacros);
Douglas Gregor456e0952009-04-17 22:13:46 +00002056 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002057 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002058}
2059
2060void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2061 Record.push_back(Loc.getRawEncoding());
2062}
2063
2064void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2065 Record.push_back(Value.getBitWidth());
2066 unsigned N = Value.getNumWords();
2067 const uint64_t* Words = Value.getRawData();
2068 for (unsigned I = 0; I != N; ++I)
2069 Record.push_back(Words[I]);
2070}
2071
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002072void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2073 Record.push_back(Value.isUnsigned());
2074 AddAPInt(Value, Record);
2075}
2076
Douglas Gregore2f37202009-04-14 21:55:33 +00002077void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2078 AddAPInt(Value.bitcastToAPInt(), Record);
2079}
2080
Douglas Gregorc34897d2009-04-09 22:27:44 +00002081void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002082 if (II == 0) {
2083 Record.push_back(0);
2084 return;
2085 }
2086
2087 pch::IdentID &ID = IdentifierIDs[II];
2088 if (ID == 0)
2089 ID = IdentifierIDs.size();
2090
2091 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002092}
2093
2094void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2095 if (T.isNull()) {
2096 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2097 return;
2098 }
2099
2100 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002101 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002102 switch (BT->getKind()) {
2103 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2104 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2105 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2106 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2107 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2108 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2109 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2110 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2111 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2112 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2113 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2114 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2115 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2116 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2117 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2118 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2119 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2120 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2121 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2122 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2123 }
2124
2125 Record.push_back((ID << 3) | T.getCVRQualifiers());
2126 return;
2127 }
2128
Douglas Gregorac8f2802009-04-10 17:25:41 +00002129 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002130 if (ID == 0) // we haven't seen this type before
2131 ID = NextTypeID++;
2132
2133 // Encode the type qualifiers in the type reference.
2134 Record.push_back((ID << 3) | T.getCVRQualifiers());
2135}
2136
2137void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2138 if (D == 0) {
2139 Record.push_back(0);
2140 return;
2141 }
2142
Douglas Gregorac8f2802009-04-10 17:25:41 +00002143 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002144 if (ID == 0) {
2145 // We haven't seen this declaration before. Give it a new ID and
2146 // enqueue it in the list of declarations to emit.
2147 ID = DeclIDs.size();
2148 DeclsToEmit.push(const_cast<Decl *>(D));
2149 }
2150
2151 Record.push_back(ID);
2152}
2153
Douglas Gregorff9a6092009-04-20 20:36:09 +00002154pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2155 if (D == 0)
2156 return 0;
2157
2158 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2159 return DeclIDs[D];
2160}
2161
Douglas Gregorc34897d2009-04-09 22:27:44 +00002162void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2163 Record.push_back(Name.getNameKind());
2164 switch (Name.getNameKind()) {
2165 case DeclarationName::Identifier:
2166 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2167 break;
2168
2169 case DeclarationName::ObjCZeroArgSelector:
2170 case DeclarationName::ObjCOneArgSelector:
2171 case DeclarationName::ObjCMultiArgSelector:
2172 assert(false && "Serialization of Objective-C selectors unavailable");
2173 break;
2174
2175 case DeclarationName::CXXConstructorName:
2176 case DeclarationName::CXXDestructorName:
2177 case DeclarationName::CXXConversionFunctionName:
2178 AddTypeRef(Name.getCXXNameType(), Record);
2179 break;
2180
2181 case DeclarationName::CXXOperatorName:
2182 Record.push_back(Name.getCXXOverloadedOperator());
2183 break;
2184
2185 case DeclarationName::CXXUsingDirective:
2186 // No extra data to emit
2187 break;
2188 }
2189}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002190
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002191/// \brief Write the given substatement or subexpression to the
2192/// bitstream.
2193void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002194 RecordData Record;
2195 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002196 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002197
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002198 if (!S) {
2199 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002200 return;
2201 }
2202
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002203 Writer.Code = pch::STMT_NULL_PTR;
2204 Writer.Visit(S);
2205 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002206 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002207 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002208}
2209
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002210/// \brief Flush all of the statements that have been added to the
2211/// queue via AddStmt().
2212void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002213 RecordData Record;
2214 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002215
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002216 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002217 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002218 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002219
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002220 if (!S) {
2221 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002222 continue;
2223 }
2224
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002225 Writer.Code = pch::STMT_NULL_PTR;
2226 Writer.Visit(S);
2227 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002228 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002229 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002230
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002231 assert(N == StmtsToEmit.size() &&
2232 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002233
2234 // Note that we are at the end of a full expression. Any
2235 // expression records that follow this one are part of a different
2236 // expression.
2237 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002238 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002239 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002240
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002241 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002242 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002243}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002244
2245unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2246 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2247 "SwitchCase recorded twice");
2248 unsigned NextID = SwitchCaseIDs.size();
2249 SwitchCaseIDs[S] = NextID;
2250 return NextID;
2251}
2252
2253unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2254 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2255 "SwitchCase hasn't been seen yet");
2256 return SwitchCaseIDs[S];
2257}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002258
2259/// \brief Retrieve the ID for the given label statement, which may
2260/// or may not have been emitted yet.
2261unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2262 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2263 if (Pos != LabelIDs.end())
2264 return Pos->second;
2265
2266 unsigned NextID = LabelIDs.size();
2267 LabelIDs[S] = NextID;
2268 return NextID;
2269}