blob: 643b8dbf7a1adb6a5f95a617c8d68c3f5e483c73 [file] [log] [blame]
Douglas Gregor2cf26342009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregore7785042009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregor3251ceb2009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000036#include <cstdio>
Douglas Gregor2cf26342009-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 Gregorc9490c02009-04-16 22:23:12 +0000132 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-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 Gregorc9490c02009-04-16 22:23:12 +0000172 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-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 Gregor6a2bfb22009-04-15 18:43:11 +0000200 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201 assert(false && "Cannot serialize template specialization types");
202}
203
204void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000205 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-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
231void
232PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) {
233 Record.push_back(T->getNumProtocols());
234 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
235 Writer.AddDeclRef(T->getProtocols(I), Record);
236 Code = pch::TYPE_OBJC_QUALIFIED_CLASS;
237}
238
239//===----------------------------------------------------------------------===//
240// Declaration serialization
241//===----------------------------------------------------------------------===//
242namespace {
243 class VISIBILITY_HIDDEN PCHDeclWriter
244 : public DeclVisitor<PCHDeclWriter, void> {
245
246 PCHWriter &Writer;
Douglas Gregor72971342009-04-18 00:02:19 +0000247 ASTContext &Context;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 PCHWriter::RecordData &Record;
249
250 public:
251 pch::DeclCode Code;
252
Douglas Gregor72971342009-04-18 00:02:19 +0000253 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
254 PCHWriter::RecordData &Record)
255 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256
257 void VisitDecl(Decl *D);
258 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
259 void VisitNamedDecl(NamedDecl *D);
260 void VisitTypeDecl(TypeDecl *D);
261 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000262 void VisitTagDecl(TagDecl *D);
263 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000264 void VisitRecordDecl(RecordDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000265 void VisitValueDecl(ValueDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000266 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000267 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000268 void VisitFieldDecl(FieldDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000269 void VisitVarDecl(VarDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000270 void VisitParmVarDecl(ParmVarDecl *D);
271 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor1028bc62009-04-13 22:49:25 +0000272 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
273 void VisitBlockDecl(BlockDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000274 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
275 uint64_t VisibleOffset);
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000276 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff33feeb02009-04-20 20:09:33 +0000277 void VisitObjCContainerDecl(ObjCContainerDecl *D);
278 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
279 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000280 };
281}
282
283void PCHDeclWriter::VisitDecl(Decl *D) {
284 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
285 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
286 Writer.AddSourceLocation(D->getLocation(), Record);
287 Record.push_back(D->isInvalidDecl());
Douglas Gregor68a2eb02009-04-15 21:30:51 +0000288 Record.push_back(D->hasAttrs());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000289 Record.push_back(D->isImplicit());
290 Record.push_back(D->getAccess());
291}
292
293void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
294 VisitDecl(D);
295 Code = pch::DECL_TRANSLATION_UNIT;
296}
297
298void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
299 VisitDecl(D);
300 Writer.AddDeclarationName(D->getDeclName(), Record);
301}
302
303void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
304 VisitNamedDecl(D);
305 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
306}
307
308void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
309 VisitTypeDecl(D);
310 Writer.AddTypeRef(D->getUnderlyingType(), Record);
311 Code = pch::DECL_TYPEDEF;
312}
313
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000314void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
315 VisitTypeDecl(D);
316 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
317 Record.push_back(D->isDefinition());
318 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
319}
320
321void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
322 VisitTagDecl(D);
323 Writer.AddTypeRef(D->getIntegerType(), Record);
324 Code = pch::DECL_ENUM;
325}
326
Douglas Gregor8c700062009-04-13 21:20:57 +0000327void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
328 VisitTagDecl(D);
329 Record.push_back(D->hasFlexibleArrayMember());
330 Record.push_back(D->isAnonymousStructOrUnion());
331 Code = pch::DECL_RECORD;
332}
333
Douglas Gregor2cf26342009-04-09 22:27:44 +0000334void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
335 VisitNamedDecl(D);
336 Writer.AddTypeRef(D->getType(), Record);
337}
338
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000339void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
340 VisitValueDecl(D);
Douglas Gregor0b748912009-04-14 21:18:50 +0000341 Record.push_back(D->getInitExpr()? 1 : 0);
342 if (D->getInitExpr())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000343 Writer.AddStmt(D->getInitExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000344 Writer.AddAPSInt(D->getInitVal(), Record);
345 Code = pch::DECL_ENUM_CONSTANT;
346}
347
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000348void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
349 VisitValueDecl(D);
Douglas Gregor025452f2009-04-17 00:04:06 +0000350 Record.push_back(D->isThisDeclarationADefinition());
351 if (D->isThisDeclarationADefinition())
Douglas Gregor72971342009-04-18 00:02:19 +0000352 Writer.AddStmt(D->getBody(Context));
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000353 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
354 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
355 Record.push_back(D->isInline());
356 Record.push_back(D->isVirtual());
357 Record.push_back(D->isPure());
358 Record.push_back(D->inheritedPrototype());
359 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
360 Record.push_back(D->isDeleted());
361 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
362 Record.push_back(D->param_size());
363 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
364 P != PEnd; ++P)
365 Writer.AddDeclRef(*P, Record);
366 Code = pch::DECL_FUNCTION;
367}
368
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000369void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
370 VisitNamedDecl(D);
371 // FIXME: convert to LazyStmtPtr?
372 // Unlike C/C++, method bodies will never be in header files.
373 Record.push_back(D->getBody() != 0);
374 if (D->getBody() != 0) {
375 Writer.AddStmt(D->getBody(Context));
376 Writer.AddDeclRef(D->getSelfDecl(), Record);
377 Writer.AddDeclRef(D->getCmdDecl(), Record);
378 }
379 Record.push_back(D->isInstanceMethod());
380 Record.push_back(D->isVariadic());
381 Record.push_back(D->isSynthesized());
382 // FIXME: stable encoding for @required/@optional
383 Record.push_back(D->getImplementationControl());
384 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
385 Record.push_back(D->getObjCDeclQualifier());
386 Writer.AddTypeRef(D->getResultType(), Record);
387 Writer.AddSourceLocation(D->getLocEnd(), Record);
388 Record.push_back(D->param_size());
389 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
390 PEnd = D->param_end(); P != PEnd; ++P)
391 Writer.AddDeclRef(*P, Record);
392 Code = pch::DECL_OBJC_METHOD;
393}
394
Steve Naroff33feeb02009-04-20 20:09:33 +0000395void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
396 VisitNamedDecl(D);
397 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
398 // Abstract class (no need to define a stable pch::DECL code).
399}
400
401void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
402 VisitObjCContainerDecl(D);
403 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
404 Writer.AddDeclRef(D->getSuperClass(), Record);
405 Record.push_back(D->ivar_size());
406 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
407 IEnd = D->ivar_end(); I != IEnd; ++I)
408 Writer.AddDeclRef(*I, Record);
409 Record.push_back(D->isForwardDecl());
410 Record.push_back(D->isImplicitInterfaceDecl());
411 Writer.AddSourceLocation(D->getClassLoc(), Record);
412 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
413 Writer.AddSourceLocation(D->getLocEnd(), Record);
414 // FIXME: add protocols, categories.
415 Code = pch::DECL_OBJC_INTERFACE_DECL;
416}
417
418void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
419 VisitFieldDecl(D);
420 // FIXME: stable encoding for @public/@private/@protected/@package
421 Record.push_back(D->getAccessControl());
422 Code = pch::DECL_OBJC_IVAR_DECL;
423}
424
Douglas Gregor8c700062009-04-13 21:20:57 +0000425void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
426 VisitValueDecl(D);
427 Record.push_back(D->isMutable());
Douglas Gregor0b748912009-04-14 21:18:50 +0000428 Record.push_back(D->getBitWidth()? 1 : 0);
429 if (D->getBitWidth())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000430 Writer.AddStmt(D->getBitWidth());
Douglas Gregor8c700062009-04-13 21:20:57 +0000431 Code = pch::DECL_FIELD;
432}
433
Douglas Gregor2cf26342009-04-09 22:27:44 +0000434void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
435 VisitValueDecl(D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000436 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor2cf26342009-04-09 22:27:44 +0000437 Record.push_back(D->isThreadSpecified());
438 Record.push_back(D->hasCXXDirectInitializer());
439 Record.push_back(D->isDeclaredInCondition());
440 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
441 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregor0b748912009-04-14 21:18:50 +0000442 Record.push_back(D->getInit()? 1 : 0);
443 if (D->getInit())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000444 Writer.AddStmt(D->getInit());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000445 Code = pch::DECL_VAR;
446}
447
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000448void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
449 VisitVarDecl(D);
450 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000451 // FIXME: emit default argument (C++)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000452 // FIXME: why isn't the "default argument" just stored as the initializer
453 // in VarDecl?
454 Code = pch::DECL_PARM_VAR;
455}
456
457void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
458 VisitParmVarDecl(D);
459 Writer.AddTypeRef(D->getOriginalType(), Record);
460 Code = pch::DECL_ORIGINAL_PARM_VAR;
461}
462
Douglas Gregor1028bc62009-04-13 22:49:25 +0000463void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
464 VisitDecl(D);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000465 Writer.AddStmt(D->getAsmString());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000466 Code = pch::DECL_FILE_SCOPE_ASM;
467}
468
469void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
470 VisitDecl(D);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000471 Writer.AddStmt(D->getBody());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000472 Record.push_back(D->param_size());
473 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
474 P != PEnd; ++P)
475 Writer.AddDeclRef(*P, Record);
476 Code = pch::DECL_BLOCK;
477}
478
Douglas Gregor2cf26342009-04-09 22:27:44 +0000479/// \brief Emit the DeclContext part of a declaration context decl.
480///
481/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
482/// block for this declaration context is stored. May be 0 to indicate
483/// that there are no declarations stored within this context.
484///
485/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
486/// block for this declaration context is stored. May be 0 to indicate
487/// that there are no declarations visible from this context. Note
488/// that this value will not be emitted for non-primary declaration
489/// contexts.
490void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
491 uint64_t VisibleOffset) {
492 Record.push_back(LexicalOffset);
493 if (DC->getPrimaryContext() == DC)
494 Record.push_back(VisibleOffset);
495}
496
497//===----------------------------------------------------------------------===//
Douglas Gregor0b748912009-04-14 21:18:50 +0000498// Statement/expression serialization
499//===----------------------------------------------------------------------===//
500namespace {
501 class VISIBILITY_HIDDEN PCHStmtWriter
502 : public StmtVisitor<PCHStmtWriter, void> {
503
504 PCHWriter &Writer;
505 PCHWriter::RecordData &Record;
506
507 public:
508 pch::StmtCode Code;
509
510 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
511 : Writer(Writer), Record(Record) { }
512
Douglas Gregor025452f2009-04-17 00:04:06 +0000513 void VisitStmt(Stmt *S);
514 void VisitNullStmt(NullStmt *S);
515 void VisitCompoundStmt(CompoundStmt *S);
516 void VisitSwitchCase(SwitchCase *S);
517 void VisitCaseStmt(CaseStmt *S);
518 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000519 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000520 void VisitIfStmt(IfStmt *S);
521 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000522 void VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000523 void VisitDoStmt(DoStmt *S);
524 void VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000525 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000526 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000527 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000528 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000529 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000530 void VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000531 void VisitAsmStmt(AsmStmt *S);
Douglas Gregor0b748912009-04-14 21:18:50 +0000532 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000533 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000534 void VisitDeclRefExpr(DeclRefExpr *E);
535 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000536 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000537 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000538 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000539 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000540 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000541 void VisitUnaryOperator(UnaryOperator *E);
542 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000543 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000544 void VisitCallExpr(CallExpr *E);
545 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000546 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000547 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000548 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
549 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000550 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000551 void VisitExplicitCastExpr(ExplicitCastExpr *E);
552 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000553 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000554 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000555 void VisitInitListExpr(InitListExpr *E);
556 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
557 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000558 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000559 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000560 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000561 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
562 void VisitChooseExpr(ChooseExpr *E);
563 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000564 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000565 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000566 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000567 };
568}
569
Douglas Gregor025452f2009-04-17 00:04:06 +0000570void PCHStmtWriter::VisitStmt(Stmt *S) {
571}
572
573void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
574 VisitStmt(S);
575 Writer.AddSourceLocation(S->getSemiLoc(), Record);
576 Code = pch::STMT_NULL;
577}
578
579void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
580 VisitStmt(S);
581 Record.push_back(S->size());
582 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
583 CS != CSEnd; ++CS)
584 Writer.WriteSubStmt(*CS);
585 Writer.AddSourceLocation(S->getLBracLoc(), Record);
586 Writer.AddSourceLocation(S->getRBracLoc(), Record);
587 Code = pch::STMT_COMPOUND;
588}
589
590void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
591 VisitStmt(S);
592 Record.push_back(Writer.RecordSwitchCaseID(S));
593}
594
595void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
596 VisitSwitchCase(S);
597 Writer.WriteSubStmt(S->getLHS());
598 Writer.WriteSubStmt(S->getRHS());
599 Writer.WriteSubStmt(S->getSubStmt());
600 Writer.AddSourceLocation(S->getCaseLoc(), Record);
601 Code = pch::STMT_CASE;
602}
603
604void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
605 VisitSwitchCase(S);
606 Writer.WriteSubStmt(S->getSubStmt());
607 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
608 Code = pch::STMT_DEFAULT;
609}
610
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000611void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
612 VisitStmt(S);
613 Writer.AddIdentifierRef(S->getID(), Record);
614 Writer.WriteSubStmt(S->getSubStmt());
615 Writer.AddSourceLocation(S->getIdentLoc(), Record);
616 Record.push_back(Writer.GetLabelID(S));
617 Code = pch::STMT_LABEL;
618}
619
Douglas Gregor025452f2009-04-17 00:04:06 +0000620void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
621 VisitStmt(S);
622 Writer.WriteSubStmt(S->getCond());
623 Writer.WriteSubStmt(S->getThen());
624 Writer.WriteSubStmt(S->getElse());
625 Writer.AddSourceLocation(S->getIfLoc(), Record);
626 Code = pch::STMT_IF;
627}
628
629void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
630 VisitStmt(S);
631 Writer.WriteSubStmt(S->getCond());
632 Writer.WriteSubStmt(S->getBody());
633 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
634 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
635 SC = SC->getNextSwitchCase())
636 Record.push_back(Writer.getSwitchCaseID(SC));
637 Code = pch::STMT_SWITCH;
638}
639
Douglas Gregord921cf92009-04-17 00:16:09 +0000640void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
641 VisitStmt(S);
642 Writer.WriteSubStmt(S->getCond());
643 Writer.WriteSubStmt(S->getBody());
644 Writer.AddSourceLocation(S->getWhileLoc(), Record);
645 Code = pch::STMT_WHILE;
646}
647
Douglas Gregor67d82492009-04-17 00:29:51 +0000648void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
649 VisitStmt(S);
650 Writer.WriteSubStmt(S->getCond());
651 Writer.WriteSubStmt(S->getBody());
652 Writer.AddSourceLocation(S->getDoLoc(), Record);
653 Code = pch::STMT_DO;
654}
655
656void PCHStmtWriter::VisitForStmt(ForStmt *S) {
657 VisitStmt(S);
658 Writer.WriteSubStmt(S->getInit());
659 Writer.WriteSubStmt(S->getCond());
660 Writer.WriteSubStmt(S->getInc());
661 Writer.WriteSubStmt(S->getBody());
662 Writer.AddSourceLocation(S->getForLoc(), Record);
663 Code = pch::STMT_FOR;
664}
665
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000666void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
667 VisitStmt(S);
668 Record.push_back(Writer.GetLabelID(S->getLabel()));
669 Writer.AddSourceLocation(S->getGotoLoc(), Record);
670 Writer.AddSourceLocation(S->getLabelLoc(), Record);
671 Code = pch::STMT_GOTO;
672}
673
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000674void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
675 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000676 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000677 Writer.WriteSubStmt(S->getTarget());
678 Code = pch::STMT_INDIRECT_GOTO;
679}
680
Douglas Gregord921cf92009-04-17 00:16:09 +0000681void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
682 VisitStmt(S);
683 Writer.AddSourceLocation(S->getContinueLoc(), Record);
684 Code = pch::STMT_CONTINUE;
685}
686
Douglas Gregor025452f2009-04-17 00:04:06 +0000687void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
688 VisitStmt(S);
689 Writer.AddSourceLocation(S->getBreakLoc(), Record);
690 Code = pch::STMT_BREAK;
691}
692
Douglas Gregor0de9d882009-04-17 16:34:57 +0000693void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
694 VisitStmt(S);
695 Writer.WriteSubStmt(S->getRetValue());
696 Writer.AddSourceLocation(S->getReturnLoc(), Record);
697 Code = pch::STMT_RETURN;
698}
699
Douglas Gregor84f21702009-04-17 16:55:36 +0000700void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
701 VisitStmt(S);
702 Writer.AddSourceLocation(S->getStartLoc(), Record);
703 Writer.AddSourceLocation(S->getEndLoc(), Record);
704 DeclGroupRef DG = S->getDeclGroup();
705 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
706 Writer.AddDeclRef(*D, Record);
707 Code = pch::STMT_DECL;
708}
709
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000710void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
711 VisitStmt(S);
712 Record.push_back(S->getNumOutputs());
713 Record.push_back(S->getNumInputs());
714 Record.push_back(S->getNumClobbers());
715 Writer.AddSourceLocation(S->getAsmLoc(), Record);
716 Writer.AddSourceLocation(S->getRParenLoc(), Record);
717 Record.push_back(S->isVolatile());
718 Record.push_back(S->isSimple());
719 Writer.WriteSubStmt(S->getAsmString());
720
721 // Outputs
722 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
723 Writer.AddString(S->getOutputName(I), Record);
724 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
725 Writer.WriteSubStmt(S->getOutputExpr(I));
726 }
727
728 // Inputs
729 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
730 Writer.AddString(S->getInputName(I), Record);
731 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
732 Writer.WriteSubStmt(S->getInputExpr(I));
733 }
734
735 // Clobbers
736 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
737 Writer.WriteSubStmt(S->getClobber(I));
738
739 Code = pch::STMT_ASM;
740}
741
Douglas Gregor0b748912009-04-14 21:18:50 +0000742void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000743 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000744 Writer.AddTypeRef(E->getType(), Record);
745 Record.push_back(E->isTypeDependent());
746 Record.push_back(E->isValueDependent());
747}
748
Douglas Gregor17fc2232009-04-14 21:55:33 +0000749void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
750 VisitExpr(E);
751 Writer.AddSourceLocation(E->getLocation(), Record);
752 Record.push_back(E->getIdentType()); // FIXME: stable encoding
753 Code = pch::EXPR_PREDEFINED;
754}
755
Douglas Gregor0b748912009-04-14 21:18:50 +0000756void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
757 VisitExpr(E);
758 Writer.AddDeclRef(E->getDecl(), Record);
759 Writer.AddSourceLocation(E->getLocation(), Record);
760 Code = pch::EXPR_DECL_REF;
761}
762
763void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
764 VisitExpr(E);
765 Writer.AddSourceLocation(E->getLocation(), Record);
766 Writer.AddAPInt(E->getValue(), Record);
767 Code = pch::EXPR_INTEGER_LITERAL;
768}
769
Douglas Gregor17fc2232009-04-14 21:55:33 +0000770void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
771 VisitExpr(E);
772 Writer.AddAPFloat(E->getValue(), Record);
773 Record.push_back(E->isExact());
774 Writer.AddSourceLocation(E->getLocation(), Record);
775 Code = pch::EXPR_FLOATING_LITERAL;
776}
777
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000778void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
779 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000780 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000781 Code = pch::EXPR_IMAGINARY_LITERAL;
782}
783
Douglas Gregor673ecd62009-04-15 16:35:07 +0000784void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
785 VisitExpr(E);
786 Record.push_back(E->getByteLength());
787 Record.push_back(E->getNumConcatenated());
788 Record.push_back(E->isWide());
789 // FIXME: String data should be stored as a blob at the end of the
790 // StringLiteral. However, we can't do so now because we have no
791 // provision for coping with abbreviations when we're jumping around
792 // the PCH file during deserialization.
793 Record.insert(Record.end(),
794 E->getStrData(), E->getStrData() + E->getByteLength());
795 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
796 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
797 Code = pch::EXPR_STRING_LITERAL;
798}
799
Douglas Gregor0b748912009-04-14 21:18:50 +0000800void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
801 VisitExpr(E);
802 Record.push_back(E->getValue());
803 Writer.AddSourceLocation(E->getLoc(), Record);
804 Record.push_back(E->isWide());
805 Code = pch::EXPR_CHARACTER_LITERAL;
806}
807
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000808void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
809 VisitExpr(E);
810 Writer.AddSourceLocation(E->getLParen(), Record);
811 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000812 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000813 Code = pch::EXPR_PAREN;
814}
815
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000816void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
817 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000818 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000819 Record.push_back(E->getOpcode()); // FIXME: stable encoding
820 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
821 Code = pch::EXPR_UNARY_OPERATOR;
822}
823
824void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
825 VisitExpr(E);
826 Record.push_back(E->isSizeOf());
827 if (E->isArgumentType())
828 Writer.AddTypeRef(E->getArgumentType(), Record);
829 else {
830 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000831 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000832 }
833 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
834 Writer.AddSourceLocation(E->getRParenLoc(), Record);
835 Code = pch::EXPR_SIZEOF_ALIGN_OF;
836}
837
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000838void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
839 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000840 Writer.WriteSubStmt(E->getLHS());
841 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000842 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
843 Code = pch::EXPR_ARRAY_SUBSCRIPT;
844}
845
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000846void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
847 VisitExpr(E);
848 Record.push_back(E->getNumArgs());
849 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000850 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000851 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
852 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000853 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000854 Code = pch::EXPR_CALL;
855}
856
857void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
858 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000859 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000860 Writer.AddDeclRef(E->getMemberDecl(), Record);
861 Writer.AddSourceLocation(E->getMemberLoc(), Record);
862 Record.push_back(E->isArrow());
863 Code = pch::EXPR_MEMBER;
864}
865
Douglas Gregor087fd532009-04-14 23:32:43 +0000866void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
867 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000868 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000869}
870
Douglas Gregordb600c32009-04-15 00:25:59 +0000871void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
872 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000873 Writer.WriteSubStmt(E->getLHS());
874 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000875 Record.push_back(E->getOpcode()); // FIXME: stable encoding
876 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
877 Code = pch::EXPR_BINARY_OPERATOR;
878}
879
Douglas Gregorad90e962009-04-15 22:40:36 +0000880void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
881 VisitBinaryOperator(E);
882 Writer.AddTypeRef(E->getComputationLHSType(), Record);
883 Writer.AddTypeRef(E->getComputationResultType(), Record);
884 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
885}
886
887void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
888 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000889 Writer.WriteSubStmt(E->getCond());
890 Writer.WriteSubStmt(E->getLHS());
891 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +0000892 Code = pch::EXPR_CONDITIONAL_OPERATOR;
893}
894
Douglas Gregor087fd532009-04-14 23:32:43 +0000895void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
896 VisitCastExpr(E);
897 Record.push_back(E->isLvalueCast());
898 Code = pch::EXPR_IMPLICIT_CAST;
899}
900
Douglas Gregordb600c32009-04-15 00:25:59 +0000901void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
902 VisitCastExpr(E);
903 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
904}
905
906void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
907 VisitExplicitCastExpr(E);
908 Writer.AddSourceLocation(E->getLParenLoc(), Record);
909 Writer.AddSourceLocation(E->getRParenLoc(), Record);
910 Code = pch::EXPR_CSTYLE_CAST;
911}
912
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000913void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
914 VisitExpr(E);
915 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000916 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000917 Record.push_back(E->isFileScope());
918 Code = pch::EXPR_COMPOUND_LITERAL;
919}
920
Douglas Gregord3c98a02009-04-15 23:02:49 +0000921void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
922 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000923 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000924 Writer.AddIdentifierRef(&E->getAccessor(), Record);
925 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
926 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
927}
928
Douglas Gregord077d752009-04-16 00:55:48 +0000929void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
930 VisitExpr(E);
931 Record.push_back(E->getNumInits());
932 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000933 Writer.WriteSubStmt(E->getInit(I));
934 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +0000935 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
936 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
937 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
938 Record.push_back(E->hadArrayRangeDesignator());
939 Code = pch::EXPR_INIT_LIST;
940}
941
942void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
943 VisitExpr(E);
944 Record.push_back(E->getNumSubExprs());
945 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000946 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +0000947 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
948 Record.push_back(E->usesGNUSyntax());
949 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
950 DEnd = E->designators_end();
951 D != DEnd; ++D) {
952 if (D->isFieldDesignator()) {
953 if (FieldDecl *Field = D->getField()) {
954 Record.push_back(pch::DESIG_FIELD_DECL);
955 Writer.AddDeclRef(Field, Record);
956 } else {
957 Record.push_back(pch::DESIG_FIELD_NAME);
958 Writer.AddIdentifierRef(D->getFieldName(), Record);
959 }
960 Writer.AddSourceLocation(D->getDotLoc(), Record);
961 Writer.AddSourceLocation(D->getFieldLoc(), Record);
962 } else if (D->isArrayDesignator()) {
963 Record.push_back(pch::DESIG_ARRAY);
964 Record.push_back(D->getFirstExprIndex());
965 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
966 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
967 } else {
968 assert(D->isArrayRangeDesignator() && "Unknown designator");
969 Record.push_back(pch::DESIG_ARRAY_RANGE);
970 Record.push_back(D->getFirstExprIndex());
971 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
972 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
973 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
974 }
975 }
976 Code = pch::EXPR_DESIGNATED_INIT;
977}
978
979void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
980 VisitExpr(E);
981 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
982}
983
Douglas Gregord3c98a02009-04-15 23:02:49 +0000984void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
985 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000986 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +0000987 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
988 Writer.AddSourceLocation(E->getRParenLoc(), Record);
989 Code = pch::EXPR_VA_ARG;
990}
991
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000992void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
993 VisitExpr(E);
994 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
995 Writer.AddSourceLocation(E->getLabelLoc(), Record);
996 Record.push_back(Writer.GetLabelID(E->getLabel()));
997 Code = pch::EXPR_ADDR_LABEL;
998}
999
Douglas Gregor6a2dd552009-04-17 19:05:30 +00001000void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1001 VisitExpr(E);
1002 Writer.WriteSubStmt(E->getSubStmt());
1003 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1004 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1005 Code = pch::EXPR_STMT;
1006}
1007
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001008void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1009 VisitExpr(E);
1010 Writer.AddTypeRef(E->getArgType1(), Record);
1011 Writer.AddTypeRef(E->getArgType2(), Record);
1012 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1013 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1014 Code = pch::EXPR_TYPES_COMPATIBLE;
1015}
1016
1017void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1018 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001019 Writer.WriteSubStmt(E->getCond());
1020 Writer.WriteSubStmt(E->getLHS());
1021 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001022 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1023 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1024 Code = pch::EXPR_CHOOSE;
1025}
1026
1027void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1028 VisitExpr(E);
1029 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1030 Code = pch::EXPR_GNU_NULL;
1031}
1032
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001033void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1034 VisitExpr(E);
1035 Record.push_back(E->getNumSubExprs());
1036 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001037 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001038 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1039 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1040 Code = pch::EXPR_SHUFFLE_VECTOR;
1041}
1042
Douglas Gregor84af7c22009-04-17 19:21:43 +00001043void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1044 VisitExpr(E);
1045 Writer.AddDeclRef(E->getBlockDecl(), Record);
1046 Record.push_back(E->hasBlockDeclRefExprs());
1047 Code = pch::EXPR_BLOCK;
1048}
1049
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001050void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1051 VisitExpr(E);
1052 Writer.AddDeclRef(E->getDecl(), Record);
1053 Writer.AddSourceLocation(E->getLocation(), Record);
1054 Record.push_back(E->isByRef());
1055 Code = pch::EXPR_BLOCK_DECL_REF;
1056}
1057
Douglas Gregor0b748912009-04-14 21:18:50 +00001058//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00001059// PCHWriter Implementation
1060//===----------------------------------------------------------------------===//
1061
Douglas Gregor2bec0412009-04-10 21:16:55 +00001062/// \brief Write the target triple (e.g., i686-apple-darwin9).
1063void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1064 using namespace llvm;
1065 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1066 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001068 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001069
1070 RecordData Record;
1071 Record.push_back(pch::TARGET_TRIPLE);
1072 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001073 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +00001074}
1075
1076/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001077void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1078 RecordData Record;
1079 Record.push_back(LangOpts.Trigraphs);
1080 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1081 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1082 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1083 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1084 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1085 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1086 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1087 Record.push_back(LangOpts.C99); // C99 Support
1088 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1089 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1090 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1091 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1092 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1093
1094 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1095 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1096 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1097
1098 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1099 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1100 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1101 Record.push_back(LangOpts.LaxVectorConversions);
1102 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1103
1104 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1105 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1106 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1107
1108 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1109 // by locks.
1110 Record.push_back(LangOpts.Blocks); // block extension to C
1111 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1112 // they are unused.
1113 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1114 // (modulo the platform support).
1115
1116 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1117 // signed integer arithmetic overflows.
1118
1119 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1120 // may be ripped out at any time.
1121
1122 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1123 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1124 // defined.
1125 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1126 // opposed to __DYNAMIC__).
1127 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1128
1129 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1130 // used (instead of C99 semantics).
1131 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1132 Record.push_back(LangOpts.getGCMode());
1133 Record.push_back(LangOpts.getVisibilityMode());
1134 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001135 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001136}
1137
Douglas Gregor14f79002009-04-10 03:52:48 +00001138//===----------------------------------------------------------------------===//
1139// Source Manager Serialization
1140//===----------------------------------------------------------------------===//
1141
1142/// \brief Create an abbreviation for the SLocEntry that refers to a
1143/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001144static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001145 using namespace llvm;
1146 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1147 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +00001152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001153 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001154}
1155
1156/// \brief Create an abbreviation for the SLocEntry that refers to a
1157/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001158static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001159 using namespace llvm;
1160 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1161 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001167 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001168}
1169
1170/// \brief Create an abbreviation for the SLocEntry that refers to a
1171/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001172static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001173 using namespace llvm;
1174 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1175 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001177 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001178}
1179
1180/// \brief Create an abbreviation for the SLocEntry that refers to an
1181/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001182static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001183 using namespace llvm;
1184 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1185 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001191 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001192}
1193
1194/// \brief Writes the block containing the serialized form of the
1195/// source manager.
1196///
1197/// TODO: We should probably use an on-disk hash table (stored in a
1198/// blob), indexed based on the file name, so that we only create
1199/// entries for files that we actually need. In the common case (no
1200/// errors), we probably won't have to create file entries for any of
1201/// the files in the AST.
1202void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001203 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001204 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001205
1206 // Abbreviations for the various kinds of source-location entries.
1207 int SLocFileAbbrv = -1;
1208 int SLocBufferAbbrv = -1;
1209 int SLocBufferBlobAbbrv = -1;
1210 int SLocInstantiationAbbrv = -1;
1211
1212 // Write out the source location entry table. We skip the first
1213 // entry, which is always the same dummy entry.
1214 RecordData Record;
1215 for (SourceManager::sloc_entry_iterator
1216 SLoc = SourceMgr.sloc_entry_begin() + 1,
1217 SLocEnd = SourceMgr.sloc_entry_end();
1218 SLoc != SLocEnd; ++SLoc) {
1219 // Figure out which record code to use.
1220 unsigned Code;
1221 if (SLoc->isFile()) {
1222 if (SLoc->getFile().getContentCache()->Entry)
1223 Code = pch::SM_SLOC_FILE_ENTRY;
1224 else
1225 Code = pch::SM_SLOC_BUFFER_ENTRY;
1226 } else
1227 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1228 Record.push_back(Code);
1229
1230 Record.push_back(SLoc->getOffset());
1231 if (SLoc->isFile()) {
1232 const SrcMgr::FileInfo &File = SLoc->getFile();
1233 Record.push_back(File.getIncludeLoc().getRawEncoding());
1234 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001235 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001236
1237 const SrcMgr::ContentCache *Content = File.getContentCache();
1238 if (Content->Entry) {
1239 // The source location entry is a file. The blob associated
1240 // with this entry is the file name.
1241 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001242 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1243 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001244 Content->Entry->getName(),
1245 strlen(Content->Entry->getName()));
1246 } else {
1247 // The source location entry is a buffer. The blob associated
1248 // with this entry contains the contents of the buffer.
1249 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001250 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1251 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001252 }
1253
1254 // We add one to the size so that we capture the trailing NULL
1255 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1256 // the reader side).
1257 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1258 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001259 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001260 Record.clear();
1261 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001262 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001263 Buffer->getBufferStart(),
1264 Buffer->getBufferSize() + 1);
1265 }
1266 } else {
1267 // The source location entry is an instantiation.
1268 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1269 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1270 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1271 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1272
Douglas Gregorf60e9912009-04-15 18:05:10 +00001273 // Compute the token length for this macro expansion.
1274 unsigned NextOffset = SourceMgr.getNextOffset();
1275 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1276 if (++NextSLoc != SLocEnd)
1277 NextOffset = NextSLoc->getOffset();
1278 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1279
Douglas Gregor14f79002009-04-10 03:52:48 +00001280 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001281 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1282 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001283 }
1284
1285 Record.clear();
1286 }
1287
Douglas Gregorbd945002009-04-13 16:31:14 +00001288 // Write the line table.
1289 if (SourceMgr.hasLineTable()) {
1290 LineTableInfo &LineTable = SourceMgr.getLineTable();
1291
1292 // Emit the file names
1293 Record.push_back(LineTable.getNumFilenames());
1294 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1295 // Emit the file name
1296 const char *Filename = LineTable.getFilename(I);
1297 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1298 Record.push_back(FilenameLen);
1299 if (FilenameLen)
1300 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1301 }
1302
1303 // Emit the line entries
1304 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1305 L != LEnd; ++L) {
1306 // Emit the file ID
1307 Record.push_back(L->first);
1308
1309 // Emit the line entries
1310 Record.push_back(L->second.size());
1311 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1312 LEEnd = L->second.end();
1313 LE != LEEnd; ++LE) {
1314 Record.push_back(LE->FileOffset);
1315 Record.push_back(LE->LineNo);
1316 Record.push_back(LE->FilenameID);
1317 Record.push_back((unsigned)LE->FileKind);
1318 Record.push_back(LE->IncludeOffset);
1319 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001320 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001321 }
1322 }
1323
Douglas Gregorc9490c02009-04-16 22:23:12 +00001324 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001325}
1326
Chris Lattner0b1fb982009-04-10 17:15:23 +00001327/// \brief Writes the block containing the serialized form of the
1328/// preprocessor.
1329///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001330void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001331 // Enter the preprocessor block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001332 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001333
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001334 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1335 // FIXME: use diagnostics subsystem for localization etc.
1336 if (PP.SawDateOrTime())
1337 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001338
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001339 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001340
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001341 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1342 if (PP.getCounterValue() != 0) {
1343 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001344 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001345 Record.clear();
1346 }
1347
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001348 // Loop over all the macro definitions that are live at the end of the file,
1349 // emitting each to the PP section.
1350 // FIXME: Eventually we want to emit an index so that we can lazily load
1351 // macros.
1352 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1353 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001354 // FIXME: This emits macros in hash table order, we should do it in a stable
1355 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001356 MacroInfo *MI = I->second;
1357
1358 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1359 // been redefined by the header (in which case they are not isBuiltinMacro).
1360 if (MI->isBuiltinMacro())
1361 continue;
1362
Chris Lattner7356a312009-04-11 21:15:38 +00001363 AddIdentifierRef(I->first, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001364 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1365 Record.push_back(MI->isUsed());
1366
1367 unsigned Code;
1368 if (MI->isObjectLike()) {
1369 Code = pch::PP_MACRO_OBJECT_LIKE;
1370 } else {
1371 Code = pch::PP_MACRO_FUNCTION_LIKE;
1372
1373 Record.push_back(MI->isC99Varargs());
1374 Record.push_back(MI->isGNUVarargs());
1375 Record.push_back(MI->getNumArgs());
1376 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1377 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001378 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001379 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001380 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001381 Record.clear();
1382
Chris Lattnerdf961c22009-04-10 18:08:30 +00001383 // Emit the tokens array.
1384 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1385 // Note that we know that the preprocessor does not have any annotation
1386 // tokens in it because they are created by the parser, and thus can't be
1387 // in a macro definition.
1388 const Token &Tok = MI->getReplacementToken(TokNo);
1389
1390 Record.push_back(Tok.getLocation().getRawEncoding());
1391 Record.push_back(Tok.getLength());
1392
Chris Lattnerdf961c22009-04-10 18:08:30 +00001393 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1394 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001395 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001396
1397 // FIXME: Should translate token kind to a stable encoding.
1398 Record.push_back(Tok.getKind());
1399 // FIXME: Should translate token flags to a stable encoding.
1400 Record.push_back(Tok.getFlags());
1401
Douglas Gregorc9490c02009-04-16 22:23:12 +00001402 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001403 Record.clear();
1404 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001405
1406 }
1407
Douglas Gregorc9490c02009-04-16 22:23:12 +00001408 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001409}
1410
1411
Douglas Gregor2cf26342009-04-09 22:27:44 +00001412/// \brief Write the representation of a type to the PCH stream.
1413void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001414 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001415 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001416 ID = NextTypeID++;
1417
1418 // Record the offset for this type.
1419 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001420 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001421 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1422 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001423 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001424 }
1425
1426 RecordData Record;
1427
1428 // Emit the type's representation.
1429 PCHTypeWriter W(*this, Record);
1430 switch (T->getTypeClass()) {
1431 // For all of the concrete, non-dependent types, call the
1432 // appropriate visitor function.
1433#define TYPE(Class, Base) \
1434 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1435#define ABSTRACT_TYPE(Class, Base)
1436#define DEPENDENT_TYPE(Class, Base)
1437#include "clang/AST/TypeNodes.def"
1438
1439 // For all of the dependent type nodes (which only occur in C++
1440 // templates), produce an error.
1441#define TYPE(Class, Base)
1442#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1443#include "clang/AST/TypeNodes.def"
1444 assert(false && "Cannot serialize dependent type nodes");
1445 break;
1446 }
1447
1448 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001449 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001450
1451 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001452 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001453}
1454
1455/// \brief Write a block containing all of the types.
1456void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001457 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001458 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001459
1460 // Emit all of the types in the ASTContext
1461 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1462 TEnd = Context.getTypes().end();
1463 T != TEnd; ++T) {
1464 // Builtin types are never serialized.
1465 if (isa<BuiltinType>(*T))
1466 continue;
1467
1468 WriteType(*T);
1469 }
1470
1471 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001472 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001473}
1474
1475/// \brief Write the block containing all of the declaration IDs
1476/// lexically declared within the given DeclContext.
1477///
1478/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1479/// bistream, or 0 if no block was written.
1480uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1481 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001482 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001483 return 0;
1484
Douglas Gregorc9490c02009-04-16 22:23:12 +00001485 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001486 RecordData Record;
1487 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1488 DEnd = DC->decls_end(Context);
1489 D != DEnd; ++D)
1490 AddDeclRef(*D, Record);
1491
Douglas Gregorc9490c02009-04-16 22:23:12 +00001492 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001493 return Offset;
1494}
1495
1496/// \brief Write the block containing all of the declaration IDs
1497/// visible from the given DeclContext.
1498///
1499/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1500/// bistream, or 0 if no block was written.
1501uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1502 DeclContext *DC) {
1503 if (DC->getPrimaryContext() != DC)
1504 return 0;
1505
Douglas Gregor58f06992009-04-18 15:49:20 +00001506 // Since there is no name lookup into functions or methods, don't
1507 // bother to build a visible-declarations table.
1508 if (DC->isFunctionOrMethod())
1509 return 0;
1510
Douglas Gregor2cf26342009-04-09 22:27:44 +00001511 // Force the DeclContext to build a its name-lookup table.
1512 DC->lookup(Context, DeclarationName());
1513
1514 // Serialize the contents of the mapping used for lookup. Note that,
1515 // although we have two very different code paths, the serialized
1516 // representation is the same for both cases: a declaration name,
1517 // followed by a size, followed by references to the visible
1518 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001519 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001520 RecordData Record;
1521 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001522 if (!Map)
1523 return 0;
1524
Douglas Gregor2cf26342009-04-09 22:27:44 +00001525 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1526 D != DEnd; ++D) {
1527 AddDeclarationName(D->first, Record);
1528 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1529 Record.push_back(Result.second - Result.first);
1530 for(; Result.first != Result.second; ++Result.first)
1531 AddDeclRef(*Result.first, Record);
1532 }
1533
1534 if (Record.size() == 0)
1535 return 0;
1536
Douglas Gregorc9490c02009-04-16 22:23:12 +00001537 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001538 return Offset;
1539}
1540
1541/// \brief Write a block containing all of the declarations.
1542void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001543 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001544 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001545
1546 // Emit all of the declarations.
1547 RecordData Record;
Douglas Gregor72971342009-04-18 00:02:19 +00001548 PCHDeclWriter W(*this, Context, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001549 while (!DeclsToEmit.empty()) {
1550 // Pull the next declaration off the queue
1551 Decl *D = DeclsToEmit.front();
1552 DeclsToEmit.pop();
1553
1554 // If this declaration is also a DeclContext, write blocks for the
1555 // declarations that lexically stored inside its context and those
1556 // declarations that are visible from its context. These blocks
1557 // are written before the declaration itself so that we can put
1558 // their offsets into the record for the declaration.
1559 uint64_t LexicalOffset = 0;
1560 uint64_t VisibleOffset = 0;
1561 DeclContext *DC = dyn_cast<DeclContext>(D);
1562 if (DC) {
1563 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1564 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1565 }
1566
1567 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001568 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001569 if (ID == 0)
1570 ID = DeclIDs.size();
1571
1572 unsigned Index = ID - 1;
1573
1574 // Record the offset for this declaration
1575 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001576 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001577 else if (DeclOffsets.size() < Index) {
1578 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001579 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001580 }
1581
1582 // Build and emit a record for this declaration
1583 Record.clear();
1584 W.Code = (pch::DeclCode)0;
1585 W.Visit(D);
1586 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001587 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc9490c02009-04-16 22:23:12 +00001588 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001589
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001590 // If the declaration had any attributes, write them now.
1591 if (D->hasAttrs())
1592 WriteAttributeRecord(D->getAttrs());
1593
Douglas Gregor0b748912009-04-14 21:18:50 +00001594 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001595 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001596
Douglas Gregorfdd01722009-04-14 00:24:19 +00001597 // Note external declarations so that we can add them to a record
1598 // in the PCH file later.
1599 if (isa<FileScopeAsmDecl>(D))
1600 ExternalDefinitions.push_back(ID);
1601 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1602 if (// Non-static file-scope variables with initializers or that
1603 // are tentative definitions.
1604 (Var->isFileVarDecl() &&
1605 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1606 // Out-of-line definitions of static data members (C++).
1607 (Var->getDeclContext()->isRecord() &&
1608 !Var->getLexicalDeclContext()->isRecord() &&
1609 Var->getStorageClass() == VarDecl::Static))
1610 ExternalDefinitions.push_back(ID);
1611 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00001612 if (Func->isThisDeclarationADefinition())
Douglas Gregorfdd01722009-04-14 00:24:19 +00001613 ExternalDefinitions.push_back(ID);
1614 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001615 }
1616
1617 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001618 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001619}
1620
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001621namespace {
1622class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1623 PCHWriter &Writer;
1624
1625public:
1626 typedef const IdentifierInfo* key_type;
1627 typedef key_type key_type_ref;
1628
1629 typedef pch::IdentID data_type;
1630 typedef data_type data_type_ref;
1631
1632 PCHIdentifierTableTrait(PCHWriter &Writer) : Writer(Writer) { }
1633
1634 static unsigned ComputeHash(const IdentifierInfo* II) {
1635 return clang::BernsteinHash(II->getName());
1636 }
1637
1638 static std::pair<unsigned,unsigned>
1639 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1640 pch::IdentID ID) {
1641 unsigned KeyLen = strlen(II->getName()) + 1;
1642 clang::io::Emit16(Out, KeyLen);
1643 unsigned DataLen = 4 + 4 + 2; // 4 bytes for token ID, builtin, flags
1644 // 4 bytes for the persistent ID
1645 // 2 bytes for the length of the decl chain
1646 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1647 DEnd = IdentifierResolver::end();
1648 D != DEnd; ++D)
1649 DataLen += sizeof(pch::DeclID);
1650 return std::make_pair(KeyLen, DataLen);
1651 }
1652
1653 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1654 unsigned KeyLen) {
1655 // Record the location of the key data. This is used when generating
1656 // the mapping from persistent IDs to strings.
1657 Writer.SetIdentifierOffset(II, Out.tell());
1658 Out.write(II->getName(), KeyLen);
1659 }
1660
1661 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1662 pch::IdentID ID, unsigned) {
1663 uint32_t Bits = 0;
1664 Bits = Bits | (uint32_t)II->getTokenID();
1665 Bits = (Bits << 8) | (uint32_t)II->getObjCOrBuiltinID();
1666 Bits = (Bits << 10) | II->hasMacroDefinition();
1667 Bits = (Bits << 1) | II->isExtensionToken();
1668 Bits = (Bits << 1) | II->isPoisoned();
1669 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1670 clang::io::Emit32(Out, Bits);
1671 clang::io::Emit32(Out, ID);
1672
1673 llvm::SmallVector<pch::DeclID, 8> Decls;
1674 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1675 DEnd = IdentifierResolver::end();
1676 D != DEnd; ++D)
1677 Decls.push_back(Writer.getDeclID(*D));
1678
1679 clang::io::Emit16(Out, Decls.size());
1680 for (unsigned I = 0; I < Decls.size(); ++I)
1681 clang::io::Emit32(Out, Decls[I]);
1682 }
1683};
1684} // end anonymous namespace
1685
Douglas Gregorafaf3082009-04-11 00:14:32 +00001686/// \brief Write the identifier table into the PCH file.
1687///
1688/// The identifier table consists of a blob containing string data
1689/// (the actual identifiers themselves) and a separate "offsets" index
1690/// that maps identifier IDs to locations within the blob.
1691void PCHWriter::WriteIdentifierTable() {
1692 using namespace llvm;
1693
1694 // Create and write out the blob that contains the identifier
1695 // strings.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001696 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001697 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001698 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1699
1700 // Create the on-disk hash table representation.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001701 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1702 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1703 ID != IDEnd; ++ID) {
1704 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001705 Generator.insert(ID->first, ID->second);
1706 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001707
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001708 // Create the on-disk hash table in a buffer.
1709 llvm::SmallVector<char, 4096> IdentifierTable;
1710 {
1711 PCHIdentifierTableTrait Trait(*this);
1712 llvm::raw_svector_ostream Out(IdentifierTable);
1713 Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001714 }
1715
1716 // Create a blob abbreviation
1717 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1718 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001720 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001721
1722 // Write the identifier table
1723 RecordData Record;
1724 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001725 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1726 &IdentifierTable.front(),
1727 IdentifierTable.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001728 }
1729
1730 // Write the offsets table for identifier IDs.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001731 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001732}
1733
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001734/// \brief Write a record containing the given attributes.
1735void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1736 RecordData Record;
1737 for (; Attr; Attr = Attr->getNext()) {
1738 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1739 Record.push_back(Attr->isInherited());
1740 switch (Attr->getKind()) {
1741 case Attr::Alias:
1742 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1743 break;
1744
1745 case Attr::Aligned:
1746 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1747 break;
1748
1749 case Attr::AlwaysInline:
1750 break;
1751
1752 case Attr::AnalyzerNoReturn:
1753 break;
1754
1755 case Attr::Annotate:
1756 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1757 break;
1758
1759 case Attr::AsmLabel:
1760 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1761 break;
1762
1763 case Attr::Blocks:
1764 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1765 break;
1766
1767 case Attr::Cleanup:
1768 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1769 break;
1770
1771 case Attr::Const:
1772 break;
1773
1774 case Attr::Constructor:
1775 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1776 break;
1777
1778 case Attr::DLLExport:
1779 case Attr::DLLImport:
1780 case Attr::Deprecated:
1781 break;
1782
1783 case Attr::Destructor:
1784 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1785 break;
1786
1787 case Attr::FastCall:
1788 break;
1789
1790 case Attr::Format: {
1791 const FormatAttr *Format = cast<FormatAttr>(Attr);
1792 AddString(Format->getType(), Record);
1793 Record.push_back(Format->getFormatIdx());
1794 Record.push_back(Format->getFirstArg());
1795 break;
1796 }
1797
Chris Lattnercf2a7212009-04-20 19:12:28 +00001798 case Attr::GNUInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001799 case Attr::IBOutletKind:
1800 case Attr::NoReturn:
1801 case Attr::NoThrow:
1802 case Attr::Nodebug:
1803 case Attr::Noinline:
1804 break;
1805
1806 case Attr::NonNull: {
1807 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1808 Record.push_back(NonNull->size());
1809 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1810 break;
1811 }
1812
1813 case Attr::ObjCException:
1814 case Attr::ObjCNSObject:
1815 case Attr::Overloadable:
1816 break;
1817
1818 case Attr::Packed:
1819 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1820 break;
1821
1822 case Attr::Pure:
1823 break;
1824
1825 case Attr::Regparm:
1826 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1827 break;
1828
1829 case Attr::Section:
1830 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1831 break;
1832
1833 case Attr::StdCall:
1834 case Attr::TransparentUnion:
1835 case Attr::Unavailable:
1836 case Attr::Unused:
1837 case Attr::Used:
1838 break;
1839
1840 case Attr::Visibility:
1841 // FIXME: stable encoding
1842 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1843 break;
1844
1845 case Attr::WarnUnusedResult:
1846 case Attr::Weak:
1847 case Attr::WeakImport:
1848 break;
1849 }
1850 }
1851
Douglas Gregorc9490c02009-04-16 22:23:12 +00001852 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001853}
1854
1855void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1856 Record.push_back(Str.size());
1857 Record.insert(Record.end(), Str.begin(), Str.end());
1858}
1859
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001860/// \brief Note that the identifier II occurs at the given offset
1861/// within the identifier table.
1862void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
1863 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
1864}
1865
Douglas Gregorc9490c02009-04-16 22:23:12 +00001866PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor3e1af842009-04-17 22:13:46 +00001867 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), NumStatements(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001868
Douglas Gregore7785042009-04-20 15:53:59 +00001869void PCHWriter::WritePCH(Sema &SemaRef) {
1870 ASTContext &Context = SemaRef.Context;
1871 Preprocessor &PP = SemaRef.PP;
1872
Douglas Gregor2cf26342009-04-09 22:27:44 +00001873 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001874 Stream.Emit((unsigned)'C', 8);
1875 Stream.Emit((unsigned)'P', 8);
1876 Stream.Emit((unsigned)'C', 8);
1877 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001878
1879 // The translation unit is the first declaration we'll emit.
1880 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1881 DeclsToEmit.push(Context.getTranslationUnitDecl());
1882
1883 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001884 RecordData Record;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001885 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001886 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001887 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00001888 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00001889 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001890 WriteTypesBlock(Context);
1891 WriteDeclsBlock(Context);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001892 WriteIdentifierTable();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001893 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1894 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorad1de002009-04-18 05:55:16 +00001895
1896 // Write the record of special types.
1897 Record.clear();
1898 AddTypeRef(Context.getBuiltinVaListType(), Record);
1899 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1900
Douglas Gregorfdd01722009-04-14 00:24:19 +00001901 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00001902 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001903
1904 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00001905 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00001906 Record.push_back(NumStatements);
1907 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001908 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001909}
1910
1911void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1912 Record.push_back(Loc.getRawEncoding());
1913}
1914
1915void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1916 Record.push_back(Value.getBitWidth());
1917 unsigned N = Value.getNumWords();
1918 const uint64_t* Words = Value.getRawData();
1919 for (unsigned I = 0; I != N; ++I)
1920 Record.push_back(Words[I]);
1921}
1922
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001923void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1924 Record.push_back(Value.isUnsigned());
1925 AddAPInt(Value, Record);
1926}
1927
Douglas Gregor17fc2232009-04-14 21:55:33 +00001928void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1929 AddAPInt(Value.bitcastToAPInt(), Record);
1930}
1931
Douglas Gregor2cf26342009-04-09 22:27:44 +00001932void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001933 if (II == 0) {
1934 Record.push_back(0);
1935 return;
1936 }
1937
1938 pch::IdentID &ID = IdentifierIDs[II];
1939 if (ID == 0)
1940 ID = IdentifierIDs.size();
1941
1942 Record.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001943}
1944
1945void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1946 if (T.isNull()) {
1947 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1948 return;
1949 }
1950
1951 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001952 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001953 switch (BT->getKind()) {
1954 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1955 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1956 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1957 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1958 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1959 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1960 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1961 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1962 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1963 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1964 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1965 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1966 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1967 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1968 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1969 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1970 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1971 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1972 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1973 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1974 }
1975
1976 Record.push_back((ID << 3) | T.getCVRQualifiers());
1977 return;
1978 }
1979
Douglas Gregor8038d512009-04-10 17:25:41 +00001980 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001981 if (ID == 0) // we haven't seen this type before
1982 ID = NextTypeID++;
1983
1984 // Encode the type qualifiers in the type reference.
1985 Record.push_back((ID << 3) | T.getCVRQualifiers());
1986}
1987
1988void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1989 if (D == 0) {
1990 Record.push_back(0);
1991 return;
1992 }
1993
Douglas Gregor8038d512009-04-10 17:25:41 +00001994 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001995 if (ID == 0) {
1996 // We haven't seen this declaration before. Give it a new ID and
1997 // enqueue it in the list of declarations to emit.
1998 ID = DeclIDs.size();
1999 DeclsToEmit.push(const_cast<Decl *>(D));
2000 }
2001
2002 Record.push_back(ID);
2003}
2004
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002005pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2006 if (D == 0)
2007 return 0;
2008
2009 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2010 return DeclIDs[D];
2011}
2012
Douglas Gregor2cf26342009-04-09 22:27:44 +00002013void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2014 Record.push_back(Name.getNameKind());
2015 switch (Name.getNameKind()) {
2016 case DeclarationName::Identifier:
2017 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2018 break;
2019
2020 case DeclarationName::ObjCZeroArgSelector:
2021 case DeclarationName::ObjCOneArgSelector:
2022 case DeclarationName::ObjCMultiArgSelector:
2023 assert(false && "Serialization of Objective-C selectors unavailable");
2024 break;
2025
2026 case DeclarationName::CXXConstructorName:
2027 case DeclarationName::CXXDestructorName:
2028 case DeclarationName::CXXConversionFunctionName:
2029 AddTypeRef(Name.getCXXNameType(), Record);
2030 break;
2031
2032 case DeclarationName::CXXOperatorName:
2033 Record.push_back(Name.getCXXOverloadedOperator());
2034 break;
2035
2036 case DeclarationName::CXXUsingDirective:
2037 // No extra data to emit
2038 break;
2039 }
2040}
Douglas Gregor0b748912009-04-14 21:18:50 +00002041
Douglas Gregorc9490c02009-04-16 22:23:12 +00002042/// \brief Write the given substatement or subexpression to the
2043/// bitstream.
2044void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00002045 RecordData Record;
2046 PCHStmtWriter Writer(*this, Record);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002047 ++NumStatements;
Douglas Gregor087fd532009-04-14 23:32:43 +00002048
Douglas Gregorc9490c02009-04-16 22:23:12 +00002049 if (!S) {
2050 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002051 return;
2052 }
2053
Douglas Gregorc9490c02009-04-16 22:23:12 +00002054 Writer.Code = pch::STMT_NULL_PTR;
2055 Writer.Visit(S);
2056 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00002057 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002058 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002059}
2060
Douglas Gregorc9490c02009-04-16 22:23:12 +00002061/// \brief Flush all of the statements that have been added to the
2062/// queue via AddStmt().
2063void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00002064 RecordData Record;
2065 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002066
Douglas Gregorc9490c02009-04-16 22:23:12 +00002067 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor3e1af842009-04-17 22:13:46 +00002068 ++NumStatements;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002069 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00002070
Douglas Gregorc9490c02009-04-16 22:23:12 +00002071 if (!S) {
2072 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002073 continue;
2074 }
2075
Douglas Gregorc9490c02009-04-16 22:23:12 +00002076 Writer.Code = pch::STMT_NULL_PTR;
2077 Writer.Visit(S);
2078 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00002079 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002080 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002081
Douglas Gregorc9490c02009-04-16 22:23:12 +00002082 assert(N == StmtsToEmit.size() &&
2083 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00002084
2085 // Note that we are at the end of a full expression. Any
2086 // expression records that follow this one are part of a different
2087 // expression.
2088 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00002089 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002090 }
Douglas Gregor087fd532009-04-14 23:32:43 +00002091
Douglas Gregorc9490c02009-04-16 22:23:12 +00002092 StmtsToEmit.clear();
Douglas Gregor0de9d882009-04-17 16:34:57 +00002093 SwitchCaseIDs.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00002094}
Douglas Gregor025452f2009-04-17 00:04:06 +00002095
2096unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2097 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2098 "SwitchCase recorded twice");
2099 unsigned NextID = SwitchCaseIDs.size();
2100 SwitchCaseIDs[S] = NextID;
2101 return NextID;
2102}
2103
2104unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2105 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2106 "SwitchCase hasn't been seen yet");
2107 return SwitchCaseIDs[S];
2108}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002109
2110/// \brief Retrieve the ID for the given label statement, which may
2111/// or may not have been emitted yet.
2112unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2113 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2114 if (Pos != LabelIDs.end())
2115 return Pos->second;
2116
2117 unsigned NextID = LabelIDs.size();
2118 LabelIDs[S] = NextID;
2119 return NextID;
2120}