blob: 0a0a38d874013a599cb77eda79a4082ab7a16276 [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);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000481 Writer.AddTypeRef(D->getType(), Record);
482 // FIXME: stable encoding
483 Record.push_back((unsigned)D->getPropertyAttributes());
484 // FIXME: stable encoding
485 Record.push_back((unsigned)D->getPropertyImplementation());
486 Writer.AddDeclarationName(D->getGetterName(), Record);
487 Writer.AddDeclarationName(D->getSetterName(), Record);
488 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
489 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
490 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000491 Code = pch::DECL_OBJC_PROPERTY;
492}
493
494void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
495 VisitDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000496 Writer.AddDeclRef(D->getClassInterface(), Record);
497 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000498 // Abstract class (no need to define a stable pch::DECL code).
499}
500
501void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
502 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000503 Writer.AddIdentifierRef(D->getIdentifier(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000504 Code = pch::DECL_OBJC_CATEGORY_IMPL;
505}
506
507void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
508 VisitObjCImplDecl(D);
509 // FIXME: Implement.
510 Code = pch::DECL_OBJC_IMPLEMENTATION;
511}
512
513void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
514 VisitDecl(D);
515 // FIXME: Implement.
516 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff7333b492009-04-20 20:09:33 +0000517}
518
Douglas Gregor982365e2009-04-13 21:20:57 +0000519void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
520 VisitValueDecl(D);
521 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000522 Record.push_back(D->getBitWidth()? 1 : 0);
523 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000524 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000525 Code = pch::DECL_FIELD;
526}
527
Douglas Gregorc34897d2009-04-09 22:27:44 +0000528void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
529 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000530 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000531 Record.push_back(D->isThreadSpecified());
532 Record.push_back(D->hasCXXDirectInitializer());
533 Record.push_back(D->isDeclaredInCondition());
534 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
535 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000536 Record.push_back(D->getInit()? 1 : 0);
537 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000538 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000539 Code = pch::DECL_VAR;
540}
541
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000542void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
543 VisitVarDecl(D);
544 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000545 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000546 // FIXME: why isn't the "default argument" just stored as the initializer
547 // in VarDecl?
548 Code = pch::DECL_PARM_VAR;
549}
550
551void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
552 VisitParmVarDecl(D);
553 Writer.AddTypeRef(D->getOriginalType(), Record);
554 Code = pch::DECL_ORIGINAL_PARM_VAR;
555}
556
Douglas Gregor2a491792009-04-13 22:49:25 +0000557void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
558 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000559 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000560 Code = pch::DECL_FILE_SCOPE_ASM;
561}
562
563void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
564 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000565 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000566 Record.push_back(D->param_size());
567 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
568 P != PEnd; ++P)
569 Writer.AddDeclRef(*P, Record);
570 Code = pch::DECL_BLOCK;
571}
572
Douglas Gregorc34897d2009-04-09 22:27:44 +0000573/// \brief Emit the DeclContext part of a declaration context decl.
574///
575/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
576/// block for this declaration context is stored. May be 0 to indicate
577/// that there are no declarations stored within this context.
578///
579/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
580/// block for this declaration context is stored. May be 0 to indicate
581/// that there are no declarations visible from this context. Note
582/// that this value will not be emitted for non-primary declaration
583/// contexts.
584void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
585 uint64_t VisibleOffset) {
586 Record.push_back(LexicalOffset);
Douglas Gregor405b6432009-04-22 19:09:20 +0000587 Record.push_back(VisibleOffset);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000588}
589
590//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000591// Statement/expression serialization
592//===----------------------------------------------------------------------===//
593namespace {
594 class VISIBILITY_HIDDEN PCHStmtWriter
595 : public StmtVisitor<PCHStmtWriter, void> {
596
597 PCHWriter &Writer;
598 PCHWriter::RecordData &Record;
599
600 public:
601 pch::StmtCode Code;
602
603 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
604 : Writer(Writer), Record(Record) { }
605
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000606 void VisitStmt(Stmt *S);
607 void VisitNullStmt(NullStmt *S);
608 void VisitCompoundStmt(CompoundStmt *S);
609 void VisitSwitchCase(SwitchCase *S);
610 void VisitCaseStmt(CaseStmt *S);
611 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000612 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000613 void VisitIfStmt(IfStmt *S);
614 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000615 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000616 void VisitDoStmt(DoStmt *S);
617 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000618 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000619 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000620 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000621 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000622 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000623 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000624 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000625 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000626 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000627 void VisitDeclRefExpr(DeclRefExpr *E);
628 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000629 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000630 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000631 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000632 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000633 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000634 void VisitUnaryOperator(UnaryOperator *E);
635 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000636 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000637 void VisitCallExpr(CallExpr *E);
638 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000639 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000640 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000641 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
642 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000643 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000644 void VisitExplicitCastExpr(ExplicitCastExpr *E);
645 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000646 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000647 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000648 void VisitInitListExpr(InitListExpr *E);
649 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
650 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000651 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000652 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000653 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000654 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
655 void VisitChooseExpr(ChooseExpr *E);
656 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000657 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000658 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000659 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000660
661 // Objective-C
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000662 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000663 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000664 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
665 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000666 };
667}
668
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000669void PCHStmtWriter::VisitStmt(Stmt *S) {
670}
671
672void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
673 VisitStmt(S);
674 Writer.AddSourceLocation(S->getSemiLoc(), Record);
675 Code = pch::STMT_NULL;
676}
677
678void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
679 VisitStmt(S);
680 Record.push_back(S->size());
681 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
682 CS != CSEnd; ++CS)
683 Writer.WriteSubStmt(*CS);
684 Writer.AddSourceLocation(S->getLBracLoc(), Record);
685 Writer.AddSourceLocation(S->getRBracLoc(), Record);
686 Code = pch::STMT_COMPOUND;
687}
688
689void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
690 VisitStmt(S);
691 Record.push_back(Writer.RecordSwitchCaseID(S));
692}
693
694void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
695 VisitSwitchCase(S);
696 Writer.WriteSubStmt(S->getLHS());
697 Writer.WriteSubStmt(S->getRHS());
698 Writer.WriteSubStmt(S->getSubStmt());
699 Writer.AddSourceLocation(S->getCaseLoc(), Record);
700 Code = pch::STMT_CASE;
701}
702
703void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
704 VisitSwitchCase(S);
705 Writer.WriteSubStmt(S->getSubStmt());
706 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
707 Code = pch::STMT_DEFAULT;
708}
709
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000710void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
711 VisitStmt(S);
712 Writer.AddIdentifierRef(S->getID(), Record);
713 Writer.WriteSubStmt(S->getSubStmt());
714 Writer.AddSourceLocation(S->getIdentLoc(), Record);
715 Record.push_back(Writer.GetLabelID(S));
716 Code = pch::STMT_LABEL;
717}
718
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000719void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
720 VisitStmt(S);
721 Writer.WriteSubStmt(S->getCond());
722 Writer.WriteSubStmt(S->getThen());
723 Writer.WriteSubStmt(S->getElse());
724 Writer.AddSourceLocation(S->getIfLoc(), Record);
725 Code = pch::STMT_IF;
726}
727
728void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
729 VisitStmt(S);
730 Writer.WriteSubStmt(S->getCond());
731 Writer.WriteSubStmt(S->getBody());
732 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
733 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
734 SC = SC->getNextSwitchCase())
735 Record.push_back(Writer.getSwitchCaseID(SC));
736 Code = pch::STMT_SWITCH;
737}
738
Douglas Gregora6b503f2009-04-17 00:16:09 +0000739void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
740 VisitStmt(S);
741 Writer.WriteSubStmt(S->getCond());
742 Writer.WriteSubStmt(S->getBody());
743 Writer.AddSourceLocation(S->getWhileLoc(), Record);
744 Code = pch::STMT_WHILE;
745}
746
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000747void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
748 VisitStmt(S);
749 Writer.WriteSubStmt(S->getCond());
750 Writer.WriteSubStmt(S->getBody());
751 Writer.AddSourceLocation(S->getDoLoc(), Record);
752 Code = pch::STMT_DO;
753}
754
755void PCHStmtWriter::VisitForStmt(ForStmt *S) {
756 VisitStmt(S);
757 Writer.WriteSubStmt(S->getInit());
758 Writer.WriteSubStmt(S->getCond());
759 Writer.WriteSubStmt(S->getInc());
760 Writer.WriteSubStmt(S->getBody());
761 Writer.AddSourceLocation(S->getForLoc(), Record);
762 Code = pch::STMT_FOR;
763}
764
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000765void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
766 VisitStmt(S);
767 Record.push_back(Writer.GetLabelID(S->getLabel()));
768 Writer.AddSourceLocation(S->getGotoLoc(), Record);
769 Writer.AddSourceLocation(S->getLabelLoc(), Record);
770 Code = pch::STMT_GOTO;
771}
772
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000773void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
774 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000775 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000776 Writer.WriteSubStmt(S->getTarget());
777 Code = pch::STMT_INDIRECT_GOTO;
778}
779
Douglas Gregora6b503f2009-04-17 00:16:09 +0000780void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
781 VisitStmt(S);
782 Writer.AddSourceLocation(S->getContinueLoc(), Record);
783 Code = pch::STMT_CONTINUE;
784}
785
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000786void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
787 VisitStmt(S);
788 Writer.AddSourceLocation(S->getBreakLoc(), Record);
789 Code = pch::STMT_BREAK;
790}
791
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000792void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
793 VisitStmt(S);
794 Writer.WriteSubStmt(S->getRetValue());
795 Writer.AddSourceLocation(S->getReturnLoc(), Record);
796 Code = pch::STMT_RETURN;
797}
798
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000799void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
800 VisitStmt(S);
801 Writer.AddSourceLocation(S->getStartLoc(), Record);
802 Writer.AddSourceLocation(S->getEndLoc(), Record);
803 DeclGroupRef DG = S->getDeclGroup();
804 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
805 Writer.AddDeclRef(*D, Record);
806 Code = pch::STMT_DECL;
807}
808
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000809void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
810 VisitStmt(S);
811 Record.push_back(S->getNumOutputs());
812 Record.push_back(S->getNumInputs());
813 Record.push_back(S->getNumClobbers());
814 Writer.AddSourceLocation(S->getAsmLoc(), Record);
815 Writer.AddSourceLocation(S->getRParenLoc(), Record);
816 Record.push_back(S->isVolatile());
817 Record.push_back(S->isSimple());
818 Writer.WriteSubStmt(S->getAsmString());
819
820 // Outputs
821 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
822 Writer.AddString(S->getOutputName(I), Record);
823 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
824 Writer.WriteSubStmt(S->getOutputExpr(I));
825 }
826
827 // Inputs
828 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
829 Writer.AddString(S->getInputName(I), Record);
830 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
831 Writer.WriteSubStmt(S->getInputExpr(I));
832 }
833
834 // Clobbers
835 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
836 Writer.WriteSubStmt(S->getClobber(I));
837
838 Code = pch::STMT_ASM;
839}
840
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000841void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000842 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000843 Writer.AddTypeRef(E->getType(), Record);
844 Record.push_back(E->isTypeDependent());
845 Record.push_back(E->isValueDependent());
846}
847
Douglas Gregore2f37202009-04-14 21:55:33 +0000848void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
849 VisitExpr(E);
850 Writer.AddSourceLocation(E->getLocation(), Record);
851 Record.push_back(E->getIdentType()); // FIXME: stable encoding
852 Code = pch::EXPR_PREDEFINED;
853}
854
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000855void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
856 VisitExpr(E);
857 Writer.AddDeclRef(E->getDecl(), Record);
858 Writer.AddSourceLocation(E->getLocation(), Record);
859 Code = pch::EXPR_DECL_REF;
860}
861
862void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
863 VisitExpr(E);
864 Writer.AddSourceLocation(E->getLocation(), Record);
865 Writer.AddAPInt(E->getValue(), Record);
866 Code = pch::EXPR_INTEGER_LITERAL;
867}
868
Douglas Gregore2f37202009-04-14 21:55:33 +0000869void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
870 VisitExpr(E);
871 Writer.AddAPFloat(E->getValue(), Record);
872 Record.push_back(E->isExact());
873 Writer.AddSourceLocation(E->getLocation(), Record);
874 Code = pch::EXPR_FLOATING_LITERAL;
875}
876
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000877void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
878 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000879 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000880 Code = pch::EXPR_IMAGINARY_LITERAL;
881}
882
Douglas Gregor596e0932009-04-15 16:35:07 +0000883void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
884 VisitExpr(E);
885 Record.push_back(E->getByteLength());
886 Record.push_back(E->getNumConcatenated());
887 Record.push_back(E->isWide());
888 // FIXME: String data should be stored as a blob at the end of the
889 // StringLiteral. However, we can't do so now because we have no
890 // provision for coping with abbreviations when we're jumping around
891 // the PCH file during deserialization.
892 Record.insert(Record.end(),
893 E->getStrData(), E->getStrData() + E->getByteLength());
894 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
895 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
896 Code = pch::EXPR_STRING_LITERAL;
897}
898
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000899void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
900 VisitExpr(E);
901 Record.push_back(E->getValue());
902 Writer.AddSourceLocation(E->getLoc(), Record);
903 Record.push_back(E->isWide());
904 Code = pch::EXPR_CHARACTER_LITERAL;
905}
906
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000907void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
908 VisitExpr(E);
909 Writer.AddSourceLocation(E->getLParen(), Record);
910 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000911 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000912 Code = pch::EXPR_PAREN;
913}
914
Douglas Gregor12d74052009-04-15 15:58:59 +0000915void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
916 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000917 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000918 Record.push_back(E->getOpcode()); // FIXME: stable encoding
919 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
920 Code = pch::EXPR_UNARY_OPERATOR;
921}
922
923void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
924 VisitExpr(E);
925 Record.push_back(E->isSizeOf());
926 if (E->isArgumentType())
927 Writer.AddTypeRef(E->getArgumentType(), Record);
928 else {
929 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000930 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000931 }
932 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
933 Writer.AddSourceLocation(E->getRParenLoc(), Record);
934 Code = pch::EXPR_SIZEOF_ALIGN_OF;
935}
936
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000937void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
938 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000939 Writer.WriteSubStmt(E->getLHS());
940 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000941 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
942 Code = pch::EXPR_ARRAY_SUBSCRIPT;
943}
944
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000945void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
946 VisitExpr(E);
947 Record.push_back(E->getNumArgs());
948 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000949 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000950 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
951 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000952 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000953 Code = pch::EXPR_CALL;
954}
955
956void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
957 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000958 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000959 Writer.AddDeclRef(E->getMemberDecl(), Record);
960 Writer.AddSourceLocation(E->getMemberLoc(), Record);
961 Record.push_back(E->isArrow());
962 Code = pch::EXPR_MEMBER;
963}
964
Douglas Gregora151ba42009-04-14 23:32:43 +0000965void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
966 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000967 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000968}
969
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000970void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
971 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000972 Writer.WriteSubStmt(E->getLHS());
973 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000974 Record.push_back(E->getOpcode()); // FIXME: stable encoding
975 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
976 Code = pch::EXPR_BINARY_OPERATOR;
977}
978
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000979void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
980 VisitBinaryOperator(E);
981 Writer.AddTypeRef(E->getComputationLHSType(), Record);
982 Writer.AddTypeRef(E->getComputationResultType(), Record);
983 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
984}
985
986void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
987 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000988 Writer.WriteSubStmt(E->getCond());
989 Writer.WriteSubStmt(E->getLHS());
990 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000991 Code = pch::EXPR_CONDITIONAL_OPERATOR;
992}
993
Douglas Gregora151ba42009-04-14 23:32:43 +0000994void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
995 VisitCastExpr(E);
996 Record.push_back(E->isLvalueCast());
997 Code = pch::EXPR_IMPLICIT_CAST;
998}
999
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001000void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1001 VisitCastExpr(E);
1002 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1003}
1004
1005void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1006 VisitExplicitCastExpr(E);
1007 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1008 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1009 Code = pch::EXPR_CSTYLE_CAST;
1010}
1011
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001012void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1013 VisitExpr(E);
1014 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001015 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001016 Record.push_back(E->isFileScope());
1017 Code = pch::EXPR_COMPOUND_LITERAL;
1018}
1019
Douglas Gregorec0b8292009-04-15 23:02:49 +00001020void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1021 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001022 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001023 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1024 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1025 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1026}
1027
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001028void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1029 VisitExpr(E);
1030 Record.push_back(E->getNumInits());
1031 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001032 Writer.WriteSubStmt(E->getInit(I));
1033 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001034 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1035 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1036 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1037 Record.push_back(E->hadArrayRangeDesignator());
1038 Code = pch::EXPR_INIT_LIST;
1039}
1040
1041void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1042 VisitExpr(E);
1043 Record.push_back(E->getNumSubExprs());
1044 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001045 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001046 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1047 Record.push_back(E->usesGNUSyntax());
1048 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1049 DEnd = E->designators_end();
1050 D != DEnd; ++D) {
1051 if (D->isFieldDesignator()) {
1052 if (FieldDecl *Field = D->getField()) {
1053 Record.push_back(pch::DESIG_FIELD_DECL);
1054 Writer.AddDeclRef(Field, Record);
1055 } else {
1056 Record.push_back(pch::DESIG_FIELD_NAME);
1057 Writer.AddIdentifierRef(D->getFieldName(), Record);
1058 }
1059 Writer.AddSourceLocation(D->getDotLoc(), Record);
1060 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1061 } else if (D->isArrayDesignator()) {
1062 Record.push_back(pch::DESIG_ARRAY);
1063 Record.push_back(D->getFirstExprIndex());
1064 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1065 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1066 } else {
1067 assert(D->isArrayRangeDesignator() && "Unknown designator");
1068 Record.push_back(pch::DESIG_ARRAY_RANGE);
1069 Record.push_back(D->getFirstExprIndex());
1070 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1071 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1072 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1073 }
1074 }
1075 Code = pch::EXPR_DESIGNATED_INIT;
1076}
1077
1078void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1079 VisitExpr(E);
1080 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1081}
1082
Douglas Gregorec0b8292009-04-15 23:02:49 +00001083void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1084 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001085 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001086 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1087 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1088 Code = pch::EXPR_VA_ARG;
1089}
1090
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001091void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1092 VisitExpr(E);
1093 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1094 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1095 Record.push_back(Writer.GetLabelID(E->getLabel()));
1096 Code = pch::EXPR_ADDR_LABEL;
1097}
1098
Douglas Gregoreca12f62009-04-17 19:05:30 +00001099void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1100 VisitExpr(E);
1101 Writer.WriteSubStmt(E->getSubStmt());
1102 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1103 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1104 Code = pch::EXPR_STMT;
1105}
1106
Douglas Gregor209d4622009-04-15 23:33:31 +00001107void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1108 VisitExpr(E);
1109 Writer.AddTypeRef(E->getArgType1(), Record);
1110 Writer.AddTypeRef(E->getArgType2(), Record);
1111 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1112 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1113 Code = pch::EXPR_TYPES_COMPATIBLE;
1114}
1115
1116void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1117 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001118 Writer.WriteSubStmt(E->getCond());
1119 Writer.WriteSubStmt(E->getLHS());
1120 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001121 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1122 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1123 Code = pch::EXPR_CHOOSE;
1124}
1125
1126void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1127 VisitExpr(E);
1128 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1129 Code = pch::EXPR_GNU_NULL;
1130}
1131
Douglas Gregor725e94b2009-04-16 00:01:45 +00001132void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1133 VisitExpr(E);
1134 Record.push_back(E->getNumSubExprs());
1135 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001136 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001137 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1138 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1139 Code = pch::EXPR_SHUFFLE_VECTOR;
1140}
1141
Douglas Gregore246b742009-04-17 19:21:43 +00001142void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1143 VisitExpr(E);
1144 Writer.AddDeclRef(E->getBlockDecl(), Record);
1145 Record.push_back(E->hasBlockDeclRefExprs());
1146 Code = pch::EXPR_BLOCK;
1147}
1148
Douglas Gregor725e94b2009-04-16 00:01:45 +00001149void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1150 VisitExpr(E);
1151 Writer.AddDeclRef(E->getDecl(), Record);
1152 Writer.AddSourceLocation(E->getLocation(), Record);
1153 Record.push_back(E->isByRef());
1154 Code = pch::EXPR_BLOCK_DECL_REF;
1155}
1156
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001157//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001158// Objective-C Expressions and Statements.
1159//===----------------------------------------------------------------------===//
1160
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001161void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1162 VisitExpr(E);
1163 Writer.WriteSubStmt(E->getString());
1164 Writer.AddSourceLocation(E->getAtLoc(), Record);
1165 Code = pch::EXPR_OBJC_STRING_LITERAL;
1166}
1167
Chris Lattner80f83c62009-04-22 05:57:30 +00001168void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1169 VisitExpr(E);
1170 Writer.AddTypeRef(E->getEncodedType(), Record);
1171 Writer.AddSourceLocation(E->getAtLoc(), Record);
1172 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1173 Code = pch::EXPR_OBJC_ENCODE;
1174}
1175
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001176void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1177 VisitExpr(E);
Chris Lattner9b9f2352009-04-22 06:40:03 +00001178 assert(0 && "Can't write a selector yet!");
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001179 // FIXME! Write selectors.
1180 //Writer.WriteSubStmt(E->getSelector());
1181 Writer.AddSourceLocation(E->getAtLoc(), Record);
1182 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1183 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1184}
1185
1186void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1187 VisitExpr(E);
1188 Writer.AddDeclRef(E->getProtocol(), Record);
1189 Writer.AddSourceLocation(E->getAtLoc(), Record);
1190 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1191 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1192}
1193
Chris Lattner80f83c62009-04-22 05:57:30 +00001194
1195//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001196// PCHWriter Implementation
1197//===----------------------------------------------------------------------===//
1198
Douglas Gregorb5887f32009-04-10 21:16:55 +00001199/// \brief Write the target triple (e.g., i686-apple-darwin9).
1200void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1201 using namespace llvm;
1202 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1203 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001205 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001206
1207 RecordData Record;
1208 Record.push_back(pch::TARGET_TRIPLE);
1209 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001210 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001211}
1212
1213/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001214void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1215 RecordData Record;
1216 Record.push_back(LangOpts.Trigraphs);
1217 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1218 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1219 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1220 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1221 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1222 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1223 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1224 Record.push_back(LangOpts.C99); // C99 Support
1225 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1226 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1227 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1228 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1229 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1230
1231 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1232 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1233 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1234
1235 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1236 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1237 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1238 Record.push_back(LangOpts.LaxVectorConversions);
1239 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1240
1241 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1242 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1243 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1244
1245 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1246 // by locks.
1247 Record.push_back(LangOpts.Blocks); // block extension to C
1248 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1249 // they are unused.
1250 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1251 // (modulo the platform support).
1252
1253 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1254 // signed integer arithmetic overflows.
1255
1256 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1257 // may be ripped out at any time.
1258
1259 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1260 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1261 // defined.
1262 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1263 // opposed to __DYNAMIC__).
1264 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1265
1266 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1267 // used (instead of C99 semantics).
1268 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1269 Record.push_back(LangOpts.getGCMode());
1270 Record.push_back(LangOpts.getVisibilityMode());
1271 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001272 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001273}
1274
Douglas Gregorab1cef72009-04-10 03:52:48 +00001275//===----------------------------------------------------------------------===//
1276// Source Manager Serialization
1277//===----------------------------------------------------------------------===//
1278
1279/// \brief Create an abbreviation for the SLocEntry that refers to a
1280/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001281static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001282 using namespace llvm;
1283 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1284 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001290 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001291}
1292
1293/// \brief Create an abbreviation for the SLocEntry that refers to a
1294/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001295static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001296 using namespace llvm;
1297 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1298 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001304 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001305}
1306
1307/// \brief Create an abbreviation for the SLocEntry that refers to a
1308/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001309static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001310 using namespace llvm;
1311 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1312 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001314 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001315}
1316
1317/// \brief Create an abbreviation for the SLocEntry that refers to an
1318/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001319static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001320 using namespace llvm;
1321 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1322 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1324 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001328 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001329}
1330
1331/// \brief Writes the block containing the serialized form of the
1332/// source manager.
1333///
1334/// TODO: We should probably use an on-disk hash table (stored in a
1335/// blob), indexed based on the file name, so that we only create
1336/// entries for files that we actually need. In the common case (no
1337/// errors), we probably won't have to create file entries for any of
1338/// the files in the AST.
1339void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001340 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001341 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001342
1343 // Abbreviations for the various kinds of source-location entries.
1344 int SLocFileAbbrv = -1;
1345 int SLocBufferAbbrv = -1;
1346 int SLocBufferBlobAbbrv = -1;
1347 int SLocInstantiationAbbrv = -1;
1348
1349 // Write out the source location entry table. We skip the first
1350 // entry, which is always the same dummy entry.
1351 RecordData Record;
1352 for (SourceManager::sloc_entry_iterator
1353 SLoc = SourceMgr.sloc_entry_begin() + 1,
1354 SLocEnd = SourceMgr.sloc_entry_end();
1355 SLoc != SLocEnd; ++SLoc) {
1356 // Figure out which record code to use.
1357 unsigned Code;
1358 if (SLoc->isFile()) {
1359 if (SLoc->getFile().getContentCache()->Entry)
1360 Code = pch::SM_SLOC_FILE_ENTRY;
1361 else
1362 Code = pch::SM_SLOC_BUFFER_ENTRY;
1363 } else
1364 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1365 Record.push_back(Code);
1366
1367 Record.push_back(SLoc->getOffset());
1368 if (SLoc->isFile()) {
1369 const SrcMgr::FileInfo &File = SLoc->getFile();
1370 Record.push_back(File.getIncludeLoc().getRawEncoding());
1371 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001372 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001373
1374 const SrcMgr::ContentCache *Content = File.getContentCache();
1375 if (Content->Entry) {
1376 // The source location entry is a file. The blob associated
1377 // with this entry is the file name.
1378 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001379 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1380 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001381 Content->Entry->getName(),
1382 strlen(Content->Entry->getName()));
1383 } else {
1384 // The source location entry is a buffer. The blob associated
1385 // with this entry contains the contents of the buffer.
1386 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001387 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1388 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001389 }
1390
1391 // We add one to the size so that we capture the trailing NULL
1392 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1393 // the reader side).
1394 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1395 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001396 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001397 Record.clear();
1398 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001399 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001400 Buffer->getBufferStart(),
1401 Buffer->getBufferSize() + 1);
1402 }
1403 } else {
1404 // The source location entry is an instantiation.
1405 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1406 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1407 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1408 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1409
Douglas Gregor364e5802009-04-15 18:05:10 +00001410 // Compute the token length for this macro expansion.
1411 unsigned NextOffset = SourceMgr.getNextOffset();
1412 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1413 if (++NextSLoc != SLocEnd)
1414 NextOffset = NextSLoc->getOffset();
1415 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1416
Douglas Gregorab1cef72009-04-10 03:52:48 +00001417 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001418 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1419 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001420 }
1421
1422 Record.clear();
1423 }
1424
Douglas Gregor635f97f2009-04-13 16:31:14 +00001425 // Write the line table.
1426 if (SourceMgr.hasLineTable()) {
1427 LineTableInfo &LineTable = SourceMgr.getLineTable();
1428
1429 // Emit the file names
1430 Record.push_back(LineTable.getNumFilenames());
1431 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1432 // Emit the file name
1433 const char *Filename = LineTable.getFilename(I);
1434 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1435 Record.push_back(FilenameLen);
1436 if (FilenameLen)
1437 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1438 }
1439
1440 // Emit the line entries
1441 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1442 L != LEnd; ++L) {
1443 // Emit the file ID
1444 Record.push_back(L->first);
1445
1446 // Emit the line entries
1447 Record.push_back(L->second.size());
1448 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1449 LEEnd = L->second.end();
1450 LE != LEEnd; ++LE) {
1451 Record.push_back(LE->FileOffset);
1452 Record.push_back(LE->LineNo);
1453 Record.push_back(LE->FilenameID);
1454 Record.push_back((unsigned)LE->FileKind);
1455 Record.push_back(LE->IncludeOffset);
1456 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001457 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001458 }
1459 }
1460
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001461 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001462}
1463
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001464/// \brief Writes the block containing the serialized form of the
1465/// preprocessor.
1466///
Chris Lattner850eabd2009-04-10 18:08:30 +00001467void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001468 // Enter the preprocessor block.
Douglas Gregorc713da92009-04-21 22:25:48 +00001469 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner84b04f12009-04-10 17:16:57 +00001470
Chris Lattner1b094952009-04-10 18:00:12 +00001471 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1472 // FIXME: use diagnostics subsystem for localization etc.
1473 if (PP.SawDateOrTime())
1474 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001475
Chris Lattner1b094952009-04-10 18:00:12 +00001476 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001477
Chris Lattner4b21c202009-04-13 01:29:17 +00001478 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1479 if (PP.getCounterValue() != 0) {
1480 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001481 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001482 Record.clear();
1483 }
1484
Chris Lattner1b094952009-04-10 18:00:12 +00001485 // Loop over all the macro definitions that are live at the end of the file,
1486 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001487 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1488 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001489 // FIXME: This emits macros in hash table order, we should do it in a stable
1490 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001491 MacroInfo *MI = I->second;
1492
1493 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1494 // been redefined by the header (in which case they are not isBuiltinMacro).
1495 if (MI->isBuiltinMacro())
1496 continue;
1497
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001498 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001499 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001500 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001501 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1502 Record.push_back(MI->isUsed());
1503
1504 unsigned Code;
1505 if (MI->isObjectLike()) {
1506 Code = pch::PP_MACRO_OBJECT_LIKE;
1507 } else {
1508 Code = pch::PP_MACRO_FUNCTION_LIKE;
1509
1510 Record.push_back(MI->isC99Varargs());
1511 Record.push_back(MI->isGNUVarargs());
1512 Record.push_back(MI->getNumArgs());
1513 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1514 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001515 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001516 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001517 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001518 Record.clear();
1519
Chris Lattner850eabd2009-04-10 18:08:30 +00001520 // Emit the tokens array.
1521 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1522 // Note that we know that the preprocessor does not have any annotation
1523 // tokens in it because they are created by the parser, and thus can't be
1524 // in a macro definition.
1525 const Token &Tok = MI->getReplacementToken(TokNo);
1526
1527 Record.push_back(Tok.getLocation().getRawEncoding());
1528 Record.push_back(Tok.getLength());
1529
Chris Lattner850eabd2009-04-10 18:08:30 +00001530 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1531 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001532 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001533
1534 // FIXME: Should translate token kind to a stable encoding.
1535 Record.push_back(Tok.getKind());
1536 // FIXME: Should translate token flags to a stable encoding.
1537 Record.push_back(Tok.getFlags());
1538
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001539 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001540 Record.clear();
1541 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001542 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001543 }
1544
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001545 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001546}
1547
1548
Douglas Gregorc34897d2009-04-09 22:27:44 +00001549/// \brief Write the representation of a type to the PCH stream.
1550void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001551 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001552 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001553 ID = NextTypeID++;
1554
1555 // Record the offset for this type.
1556 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001557 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001558 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1559 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001560 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001561 }
1562
1563 RecordData Record;
1564
1565 // Emit the type's representation.
1566 PCHTypeWriter W(*this, Record);
1567 switch (T->getTypeClass()) {
1568 // For all of the concrete, non-dependent types, call the
1569 // appropriate visitor function.
1570#define TYPE(Class, Base) \
1571 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1572#define ABSTRACT_TYPE(Class, Base)
1573#define DEPENDENT_TYPE(Class, Base)
1574#include "clang/AST/TypeNodes.def"
1575
1576 // For all of the dependent type nodes (which only occur in C++
1577 // templates), produce an error.
1578#define TYPE(Class, Base)
1579#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1580#include "clang/AST/TypeNodes.def"
1581 assert(false && "Cannot serialize dependent type nodes");
1582 break;
1583 }
1584
1585 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001586 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001587
1588 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001589 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001590}
1591
1592/// \brief Write a block containing all of the types.
1593void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001594 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001595 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001596
1597 // Emit all of the types in the ASTContext
1598 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1599 TEnd = Context.getTypes().end();
1600 T != TEnd; ++T) {
1601 // Builtin types are never serialized.
1602 if (isa<BuiltinType>(*T))
1603 continue;
1604
1605 WriteType(*T);
1606 }
1607
1608 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001609 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001610}
1611
1612/// \brief Write the block containing all of the declaration IDs
1613/// lexically declared within the given DeclContext.
1614///
1615/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1616/// bistream, or 0 if no block was written.
1617uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1618 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001619 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001620 return 0;
1621
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001622 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001623 RecordData Record;
1624 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1625 DEnd = DC->decls_end(Context);
1626 D != DEnd; ++D)
1627 AddDeclRef(*D, Record);
1628
Douglas Gregoraf136d92009-04-22 22:34:57 +00001629 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001630 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001631 return Offset;
1632}
1633
1634/// \brief Write the block containing all of the declaration IDs
1635/// visible from the given DeclContext.
1636///
1637/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1638/// bistream, or 0 if no block was written.
1639uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1640 DeclContext *DC) {
1641 if (DC->getPrimaryContext() != DC)
1642 return 0;
1643
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001644 // Since there is no name lookup into functions or methods, and we
1645 // perform name lookup for the translation unit via the
1646 // IdentifierInfo chains, don't bother to build a
1647 // visible-declarations table for these entities.
1648 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001649 return 0;
1650
Douglas Gregorc34897d2009-04-09 22:27:44 +00001651 // Force the DeclContext to build a its name-lookup table.
1652 DC->lookup(Context, DeclarationName());
1653
1654 // Serialize the contents of the mapping used for lookup. Note that,
1655 // although we have two very different code paths, the serialized
1656 // representation is the same for both cases: a declaration name,
1657 // followed by a size, followed by references to the visible
1658 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001659 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001660 RecordData Record;
1661 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001662 if (!Map)
1663 return 0;
1664
Douglas Gregorc34897d2009-04-09 22:27:44 +00001665 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1666 D != DEnd; ++D) {
1667 AddDeclarationName(D->first, Record);
1668 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1669 Record.push_back(Result.second - Result.first);
1670 for(; Result.first != Result.second; ++Result.first)
1671 AddDeclRef(*Result.first, Record);
1672 }
1673
1674 if (Record.size() == 0)
1675 return 0;
1676
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001677 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001678 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001679 return Offset;
1680}
1681
1682/// \brief Write a block containing all of the declarations.
1683void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001684 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001685 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001686
1687 // Emit all of the declarations.
1688 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001689 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001690 while (!DeclsToEmit.empty()) {
1691 // Pull the next declaration off the queue
1692 Decl *D = DeclsToEmit.front();
1693 DeclsToEmit.pop();
1694
1695 // If this declaration is also a DeclContext, write blocks for the
1696 // declarations that lexically stored inside its context and those
1697 // declarations that are visible from its context. These blocks
1698 // are written before the declaration itself so that we can put
1699 // their offsets into the record for the declaration.
1700 uint64_t LexicalOffset = 0;
1701 uint64_t VisibleOffset = 0;
1702 DeclContext *DC = dyn_cast<DeclContext>(D);
1703 if (DC) {
1704 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1705 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1706 }
1707
1708 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001709 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001710 if (ID == 0)
1711 ID = DeclIDs.size();
1712
1713 unsigned Index = ID - 1;
1714
1715 // Record the offset for this declaration
1716 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001717 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001718 else if (DeclOffsets.size() < Index) {
1719 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001720 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001721 }
1722
1723 // Build and emit a record for this declaration
1724 Record.clear();
1725 W.Code = (pch::DeclCode)0;
1726 W.Visit(D);
1727 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001728
1729 if (!W.Code) {
1730 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1731 D->getDeclKindName());
1732 assert(false && "Unhandled declaration kind while generating PCH");
1733 exit(-1);
1734 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001735 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001736
Douglas Gregor1c507882009-04-15 21:30:51 +00001737 // If the declaration had any attributes, write them now.
1738 if (D->hasAttrs())
1739 WriteAttributeRecord(D->getAttrs());
1740
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001741 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001742 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001743
Douglas Gregor631f6c62009-04-14 00:24:19 +00001744 // Note external declarations so that we can add them to a record
1745 // in the PCH file later.
1746 if (isa<FileScopeAsmDecl>(D))
1747 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001748 }
1749
1750 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001751 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001752}
1753
Douglas Gregorff9a6092009-04-20 20:36:09 +00001754namespace {
1755class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1756 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001757 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001758
1759public:
1760 typedef const IdentifierInfo* key_type;
1761 typedef key_type key_type_ref;
1762
1763 typedef pch::IdentID data_type;
1764 typedef data_type data_type_ref;
1765
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001766 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1767 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001768
1769 static unsigned ComputeHash(const IdentifierInfo* II) {
1770 return clang::BernsteinHash(II->getName());
1771 }
1772
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001773 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001774 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1775 pch::IdentID ID) {
1776 unsigned KeyLen = strlen(II->getName()) + 1;
1777 clang::io::Emit16(Out, KeyLen);
Douglas Gregorc713da92009-04-21 22:25:48 +00001778 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1779 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001780 if (II->hasMacroDefinition() &&
1781 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1782 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001783 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1784 DEnd = IdentifierResolver::end();
1785 D != DEnd; ++D)
1786 DataLen += sizeof(pch::DeclID);
Douglas Gregorc713da92009-04-21 22:25:48 +00001787 clang::io::Emit16(Out, DataLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001788 return std::make_pair(KeyLen, DataLen);
1789 }
1790
1791 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1792 unsigned KeyLen) {
1793 // Record the location of the key data. This is used when generating
1794 // the mapping from persistent IDs to strings.
1795 Writer.SetIdentifierOffset(II, Out.tell());
1796 Out.write(II->getName(), KeyLen);
1797 }
1798
1799 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1800 pch::IdentID ID, unsigned) {
1801 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001802 bool hasMacroDefinition =
1803 II->hasMacroDefinition() &&
1804 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001805 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001806 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
1807 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001808 Bits = (Bits << 1) | II->isExtensionToken();
1809 Bits = (Bits << 1) | II->isPoisoned();
1810 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1811 clang::io::Emit32(Out, Bits);
1812 clang::io::Emit32(Out, ID);
1813
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001814 if (hasMacroDefinition)
1815 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1816
Douglas Gregorc713da92009-04-21 22:25:48 +00001817 // Emit the declaration IDs in reverse order, because the
1818 // IdentifierResolver provides the declarations as they would be
1819 // visible (e.g., the function "stat" would come before the struct
1820 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1821 // adds declarations to the end of the list (so we need to see the
1822 // struct "status" before the function "status").
1823 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1824 IdentifierResolver::end());
1825 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1826 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001827 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001828 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001829 }
1830};
1831} // end anonymous namespace
1832
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001833/// \brief Write the identifier table into the PCH file.
1834///
1835/// The identifier table consists of a blob containing string data
1836/// (the actual identifiers themselves) and a separate "offsets" index
1837/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001838void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001839 using namespace llvm;
1840
1841 // Create and write out the blob that contains the identifier
1842 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001843 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001844 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001845 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1846
1847 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001848 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1849 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1850 ID != IDEnd; ++ID) {
1851 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorff9a6092009-04-20 20:36:09 +00001852 Generator.insert(ID->first, ID->second);
1853 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001854
Douglas Gregorff9a6092009-04-20 20:36:09 +00001855 // Create the on-disk hash table in a buffer.
1856 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001857 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001858 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001859 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001860 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc713da92009-04-21 22:25:48 +00001861 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001862 }
1863
1864 // Create a blob abbreviation
1865 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1866 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001869 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001870
1871 // Write the identifier table
1872 RecordData Record;
1873 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001874 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001875 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1876 &IdentifierTable.front(),
1877 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001878 }
1879
1880 // Write the offsets table for identifier IDs.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001881 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001882}
1883
Douglas Gregor1c507882009-04-15 21:30:51 +00001884/// \brief Write a record containing the given attributes.
1885void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1886 RecordData Record;
1887 for (; Attr; Attr = Attr->getNext()) {
1888 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1889 Record.push_back(Attr->isInherited());
1890 switch (Attr->getKind()) {
1891 case Attr::Alias:
1892 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1893 break;
1894
1895 case Attr::Aligned:
1896 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1897 break;
1898
1899 case Attr::AlwaysInline:
1900 break;
1901
1902 case Attr::AnalyzerNoReturn:
1903 break;
1904
1905 case Attr::Annotate:
1906 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1907 break;
1908
1909 case Attr::AsmLabel:
1910 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1911 break;
1912
1913 case Attr::Blocks:
1914 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1915 break;
1916
1917 case Attr::Cleanup:
1918 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1919 break;
1920
1921 case Attr::Const:
1922 break;
1923
1924 case Attr::Constructor:
1925 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1926 break;
1927
1928 case Attr::DLLExport:
1929 case Attr::DLLImport:
1930 case Attr::Deprecated:
1931 break;
1932
1933 case Attr::Destructor:
1934 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1935 break;
1936
1937 case Attr::FastCall:
1938 break;
1939
1940 case Attr::Format: {
1941 const FormatAttr *Format = cast<FormatAttr>(Attr);
1942 AddString(Format->getType(), Record);
1943 Record.push_back(Format->getFormatIdx());
1944 Record.push_back(Format->getFirstArg());
1945 break;
1946 }
1947
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001948 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001949 case Attr::IBOutletKind:
1950 case Attr::NoReturn:
1951 case Attr::NoThrow:
1952 case Attr::Nodebug:
1953 case Attr::Noinline:
1954 break;
1955
1956 case Attr::NonNull: {
1957 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1958 Record.push_back(NonNull->size());
1959 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1960 break;
1961 }
1962
1963 case Attr::ObjCException:
1964 case Attr::ObjCNSObject:
1965 case Attr::Overloadable:
1966 break;
1967
1968 case Attr::Packed:
1969 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1970 break;
1971
1972 case Attr::Pure:
1973 break;
1974
1975 case Attr::Regparm:
1976 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1977 break;
1978
1979 case Attr::Section:
1980 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1981 break;
1982
1983 case Attr::StdCall:
1984 case Attr::TransparentUnion:
1985 case Attr::Unavailable:
1986 case Attr::Unused:
1987 case Attr::Used:
1988 break;
1989
1990 case Attr::Visibility:
1991 // FIXME: stable encoding
1992 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1993 break;
1994
1995 case Attr::WarnUnusedResult:
1996 case Attr::Weak:
1997 case Attr::WeakImport:
1998 break;
1999 }
2000 }
2001
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002002 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002003}
2004
2005void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2006 Record.push_back(Str.size());
2007 Record.insert(Record.end(), Str.begin(), Str.end());
2008}
2009
Douglas Gregorff9a6092009-04-20 20:36:09 +00002010/// \brief Note that the identifier II occurs at the given offset
2011/// within the identifier table.
2012void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
2013 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
2014}
2015
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002016PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002017 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002018 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2019 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002020
Douglas Gregor87887da2009-04-20 15:53:59 +00002021void PCHWriter::WritePCH(Sema &SemaRef) {
2022 ASTContext &Context = SemaRef.Context;
2023 Preprocessor &PP = SemaRef.PP;
2024
Douglas Gregorc34897d2009-04-09 22:27:44 +00002025 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002026 Stream.Emit((unsigned)'C', 8);
2027 Stream.Emit((unsigned)'P', 8);
2028 Stream.Emit((unsigned)'C', 8);
2029 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002030
2031 // The translation unit is the first declaration we'll emit.
2032 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2033 DeclsToEmit.push(Context.getTranslationUnitDecl());
2034
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002035 // Make sure that we emit IdentifierInfos (and any attached
2036 // declarations) for builtins.
2037 {
2038 IdentifierTable &Table = PP.getIdentifierTable();
2039 llvm::SmallVector<const char *, 32> BuiltinNames;
2040 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2041 Context.getLangOptions().NoBuiltin);
2042 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2043 getIdentifierRef(&Table.get(BuiltinNames[I]));
2044 }
2045
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002046 // Build a record containing all of the tentative definitions in
2047 // this header file. Generally, this record will be empty.
2048 RecordData TentativeDefinitions;
2049 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2050 TD = SemaRef.TentativeDefinitions.begin(),
2051 TDEnd = SemaRef.TentativeDefinitions.end();
2052 TD != TDEnd; ++TD)
2053 AddDeclRef(TD->second, TentativeDefinitions);
2054
Douglas Gregor062d9482009-04-22 22:18:58 +00002055 // Build a record containing all of the locally-scoped external
2056 // declarations in this header file. Generally, this record will be
2057 // empty.
2058 RecordData LocallyScopedExternalDecls;
2059 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2060 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2061 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2062 TD != TDEnd; ++TD)
2063 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2064
Douglas Gregorc34897d2009-04-09 22:27:44 +00002065 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002066 RecordData Record;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002067 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002068 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002069 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00002070 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002071 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002072 WriteTypesBlock(Context);
2073 WriteDeclsBlock(Context);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002074 WriteIdentifierTable(PP);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002075 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
2076 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregore01ad442009-04-18 05:55:16 +00002077
2078 // Write the record of special types.
2079 Record.clear();
2080 AddTypeRef(Context.getBuiltinVaListType(), Record);
2081 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2082
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002083 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002084 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002085 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002086
2087 // Write the record containing tentative definitions.
2088 if (!TentativeDefinitions.empty())
2089 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002090
2091 // Write the record containing locally-scoped external definitions.
2092 if (!LocallyScopedExternalDecls.empty())
2093 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2094 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002095
2096 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002097 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002098 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002099 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002100 Record.push_back(NumLexicalDeclContexts);
2101 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002102 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002103 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002104}
2105
2106void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2107 Record.push_back(Loc.getRawEncoding());
2108}
2109
2110void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2111 Record.push_back(Value.getBitWidth());
2112 unsigned N = Value.getNumWords();
2113 const uint64_t* Words = Value.getRawData();
2114 for (unsigned I = 0; I != N; ++I)
2115 Record.push_back(Words[I]);
2116}
2117
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002118void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2119 Record.push_back(Value.isUnsigned());
2120 AddAPInt(Value, Record);
2121}
2122
Douglas Gregore2f37202009-04-14 21:55:33 +00002123void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2124 AddAPInt(Value.bitcastToAPInt(), Record);
2125}
2126
Douglas Gregorc34897d2009-04-09 22:27:44 +00002127void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002128 Record.push_back(getIdentifierRef(II));
2129}
2130
2131pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2132 if (II == 0)
2133 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002134
2135 pch::IdentID &ID = IdentifierIDs[II];
2136 if (ID == 0)
2137 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002138 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002139}
2140
2141void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2142 if (T.isNull()) {
2143 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2144 return;
2145 }
2146
2147 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002148 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002149 switch (BT->getKind()) {
2150 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2151 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2152 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2153 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2154 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2155 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2156 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2157 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2158 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2159 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2160 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2161 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2162 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2163 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2164 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2165 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2166 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2167 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2168 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2169 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2170 }
2171
2172 Record.push_back((ID << 3) | T.getCVRQualifiers());
2173 return;
2174 }
2175
Douglas Gregorac8f2802009-04-10 17:25:41 +00002176 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002177 if (ID == 0) // we haven't seen this type before
2178 ID = NextTypeID++;
2179
2180 // Encode the type qualifiers in the type reference.
2181 Record.push_back((ID << 3) | T.getCVRQualifiers());
2182}
2183
2184void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2185 if (D == 0) {
2186 Record.push_back(0);
2187 return;
2188 }
2189
Douglas Gregorac8f2802009-04-10 17:25:41 +00002190 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002191 if (ID == 0) {
2192 // We haven't seen this declaration before. Give it a new ID and
2193 // enqueue it in the list of declarations to emit.
2194 ID = DeclIDs.size();
2195 DeclsToEmit.push(const_cast<Decl *>(D));
2196 }
2197
2198 Record.push_back(ID);
2199}
2200
Douglas Gregorff9a6092009-04-20 20:36:09 +00002201pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2202 if (D == 0)
2203 return 0;
2204
2205 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2206 return DeclIDs[D];
2207}
2208
Douglas Gregorc34897d2009-04-09 22:27:44 +00002209void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2210 Record.push_back(Name.getNameKind());
2211 switch (Name.getNameKind()) {
2212 case DeclarationName::Identifier:
2213 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2214 break;
2215
2216 case DeclarationName::ObjCZeroArgSelector:
2217 case DeclarationName::ObjCOneArgSelector:
2218 case DeclarationName::ObjCMultiArgSelector:
2219 assert(false && "Serialization of Objective-C selectors unavailable");
2220 break;
2221
2222 case DeclarationName::CXXConstructorName:
2223 case DeclarationName::CXXDestructorName:
2224 case DeclarationName::CXXConversionFunctionName:
2225 AddTypeRef(Name.getCXXNameType(), Record);
2226 break;
2227
2228 case DeclarationName::CXXOperatorName:
2229 Record.push_back(Name.getCXXOverloadedOperator());
2230 break;
2231
2232 case DeclarationName::CXXUsingDirective:
2233 // No extra data to emit
2234 break;
2235 }
2236}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002237
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002238/// \brief Write the given substatement or subexpression to the
2239/// bitstream.
2240void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002241 RecordData Record;
2242 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002243 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002244
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002245 if (!S) {
2246 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002247 return;
2248 }
2249
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002250 Writer.Code = pch::STMT_NULL_PTR;
2251 Writer.Visit(S);
2252 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002253 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002254 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002255}
2256
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002257/// \brief Flush all of the statements that have been added to the
2258/// queue via AddStmt().
2259void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002260 RecordData Record;
2261 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002262
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002263 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002264 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002265 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002266
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002267 if (!S) {
2268 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002269 continue;
2270 }
2271
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002272 Writer.Code = pch::STMT_NULL_PTR;
2273 Writer.Visit(S);
2274 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002275 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002276 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002277
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002278 assert(N == StmtsToEmit.size() &&
2279 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002280
2281 // Note that we are at the end of a full expression. Any
2282 // expression records that follow this one are part of a different
2283 // expression.
2284 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002285 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002286 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002287
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002288 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002289 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002290}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002291
2292unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2293 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2294 "SwitchCase recorded twice");
2295 unsigned NextID = SwitchCaseIDs.size();
2296 SwitchCaseIDs[S] = NextID;
2297 return NextID;
2298}
2299
2300unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2301 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2302 "SwitchCase hasn't been seen yet");
2303 return SwitchCaseIDs[S];
2304}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002305
2306/// \brief Retrieve the ID for the given label statement, which may
2307/// or may not have been emitted yet.
2308unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2309 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2310 if (Pos != LabelIDs.end())
2311 return Pos->second;
2312
2313 unsigned NextID = LabelIDs.size();
2314 LabelIDs[S] = NextID;
2315 return NextID;
2316}