blob: a908a8bfeea52f31a058dda86dfc94cc8e78e1a4 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorff9a6092009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
20#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000036#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000037using namespace clang;
38
39//===----------------------------------------------------------------------===//
40// Type serialization
41//===----------------------------------------------------------------------===//
42namespace {
43 class VISIBILITY_HIDDEN PCHTypeWriter {
44 PCHWriter &Writer;
45 PCHWriter::RecordData &Record;
46
47 public:
48 /// \brief Type code that corresponds to the record generated.
49 pch::TypeCode Code;
50
51 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
52 : Writer(Writer), Record(Record) { }
53
54 void VisitArrayType(const ArrayType *T);
55 void VisitFunctionType(const FunctionType *T);
56 void VisitTagType(const TagType *T);
57
58#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
59#define ABSTRACT_TYPE(Class, Base)
60#define DEPENDENT_TYPE(Class, Base)
61#include "clang/AST/TypeNodes.def"
62 };
63}
64
65void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
66 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
67 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
68 Record.push_back(T->getAddressSpace());
69 Code = pch::TYPE_EXT_QUAL;
70}
71
72void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
73 assert(false && "Built-in types are never serialized");
74}
75
76void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
77 Record.push_back(T->getWidth());
78 Record.push_back(T->isSigned());
79 Code = pch::TYPE_FIXED_WIDTH_INT;
80}
81
82void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
83 Writer.AddTypeRef(T->getElementType(), Record);
84 Code = pch::TYPE_COMPLEX;
85}
86
87void PCHTypeWriter::VisitPointerType(const PointerType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_POINTER;
90}
91
92void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_BLOCK_POINTER;
95}
96
97void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
98 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Code = pch::TYPE_LVALUE_REFERENCE;
100}
101
102void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
103 Writer.AddTypeRef(T->getPointeeType(), Record);
104 Code = pch::TYPE_RVALUE_REFERENCE;
105}
106
107void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
108 Writer.AddTypeRef(T->getPointeeType(), Record);
109 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
110 Code = pch::TYPE_MEMBER_POINTER;
111}
112
113void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
114 Writer.AddTypeRef(T->getElementType(), Record);
115 Record.push_back(T->getSizeModifier()); // FIXME: stable values
116 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
117}
118
119void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
120 VisitArrayType(T);
121 Writer.AddAPInt(T->getSize(), Record);
122 Code = pch::TYPE_CONSTANT_ARRAY;
123}
124
125void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
126 VisitArrayType(T);
127 Code = pch::TYPE_INCOMPLETE_ARRAY;
128}
129
130void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
131 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000132 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000133 Code = pch::TYPE_VARIABLE_ARRAY;
134}
135
136void PCHTypeWriter::VisitVectorType(const VectorType *T) {
137 Writer.AddTypeRef(T->getElementType(), Record);
138 Record.push_back(T->getNumElements());
139 Code = pch::TYPE_VECTOR;
140}
141
142void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
143 VisitVectorType(T);
144 Code = pch::TYPE_EXT_VECTOR;
145}
146
147void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
148 Writer.AddTypeRef(T->getResultType(), Record);
149}
150
151void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
152 VisitFunctionType(T);
153 Code = pch::TYPE_FUNCTION_NO_PROTO;
154}
155
156void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
157 VisitFunctionType(T);
158 Record.push_back(T->getNumArgs());
159 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
160 Writer.AddTypeRef(T->getArgType(I), Record);
161 Record.push_back(T->isVariadic());
162 Record.push_back(T->getTypeQuals());
163 Code = pch::TYPE_FUNCTION_PROTO;
164}
165
166void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
167 Writer.AddDeclRef(T->getDecl(), Record);
168 Code = pch::TYPE_TYPEDEF;
169}
170
171void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000172 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000173 Code = pch::TYPE_TYPEOF_EXPR;
174}
175
176void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
177 Writer.AddTypeRef(T->getUnderlyingType(), Record);
178 Code = pch::TYPE_TYPEOF;
179}
180
181void PCHTypeWriter::VisitTagType(const TagType *T) {
182 Writer.AddDeclRef(T->getDecl(), Record);
183 assert(!T->isBeingDefined() &&
184 "Cannot serialize in the middle of a type definition");
185}
186
187void PCHTypeWriter::VisitRecordType(const RecordType *T) {
188 VisitTagType(T);
189 Code = pch::TYPE_RECORD;
190}
191
192void PCHTypeWriter::VisitEnumType(const EnumType *T) {
193 VisitTagType(T);
194 Code = pch::TYPE_ENUM;
195}
196
197void
198PCHTypeWriter::VisitTemplateSpecializationType(
199 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000200 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000201 assert(false && "Cannot serialize template specialization types");
202}
203
204void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000205 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000206 assert(false && "Cannot serialize qualified name types");
207}
208
209void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
210 Writer.AddDeclRef(T->getDecl(), Record);
211 Code = pch::TYPE_OBJC_INTERFACE;
212}
213
214void
215PCHTypeWriter::VisitObjCQualifiedInterfaceType(
216 const ObjCQualifiedInterfaceType *T) {
217 VisitObjCInterfaceType(T);
218 Record.push_back(T->getNumProtocols());
219 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
220 Writer.AddDeclRef(T->getProtocol(I), Record);
221 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
222}
223
224void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
225 Record.push_back(T->getNumProtocols());
226 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
227 Writer.AddDeclRef(T->getProtocols(I), Record);
228 Code = pch::TYPE_OBJC_QUALIFIED_ID;
229}
230
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 Gregore3241e92009-04-18 00:02:19 +0000247 ASTContext &Context;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000248 PCHWriter::RecordData &Record;
249
250 public:
251 pch::DeclCode Code;
252
Douglas Gregore3241e92009-04-18 00:02:19 +0000253 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
254 PCHWriter::RecordData &Record)
255 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregorc34897d2009-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 Gregor47f1b2c2009-04-13 18:14:40 +0000262 void VisitTagDecl(TagDecl *D);
263 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000264 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000265 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000266 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000267 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000268 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000269 void VisitVarDecl(VarDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000270 void VisitParmVarDecl(ParmVarDecl *D);
271 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000272 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
273 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000274 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
275 uint64_t VisibleOffset);
Steve Naroff79ea0e02009-04-20 15:06:07 +0000276 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +0000277 void VisitObjCContainerDecl(ObjCContainerDecl *D);
278 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
279 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000280 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
281 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
282 void VisitObjCClassDecl(ObjCClassDecl *D);
283 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
284 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
285 void VisitObjCImplDecl(ObjCImplDecl *D);
286 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
287 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
288 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
289 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
290 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000291 };
292}
293
294void PCHDeclWriter::VisitDecl(Decl *D) {
295 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
296 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
297 Writer.AddSourceLocation(D->getLocation(), Record);
298 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000299 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000300 Record.push_back(D->isImplicit());
301 Record.push_back(D->getAccess());
302}
303
304void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
305 VisitDecl(D);
306 Code = pch::DECL_TRANSLATION_UNIT;
307}
308
309void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
310 VisitDecl(D);
311 Writer.AddDeclarationName(D->getDeclName(), Record);
312}
313
314void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
315 VisitNamedDecl(D);
316 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
317}
318
319void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
320 VisitTypeDecl(D);
321 Writer.AddTypeRef(D->getUnderlyingType(), Record);
322 Code = pch::DECL_TYPEDEF;
323}
324
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000325void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
326 VisitTypeDecl(D);
327 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
328 Record.push_back(D->isDefinition());
329 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
330}
331
332void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
333 VisitTagDecl(D);
334 Writer.AddTypeRef(D->getIntegerType(), Record);
335 Code = pch::DECL_ENUM;
336}
337
Douglas Gregor982365e2009-04-13 21:20:57 +0000338void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
339 VisitTagDecl(D);
340 Record.push_back(D->hasFlexibleArrayMember());
341 Record.push_back(D->isAnonymousStructOrUnion());
342 Code = pch::DECL_RECORD;
343}
344
Douglas Gregorc34897d2009-04-09 22:27:44 +0000345void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
346 VisitNamedDecl(D);
347 Writer.AddTypeRef(D->getType(), Record);
348}
349
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000350void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
351 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000352 Record.push_back(D->getInitExpr()? 1 : 0);
353 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000354 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000355 Writer.AddAPSInt(D->getInitVal(), Record);
356 Code = pch::DECL_ENUM_CONSTANT;
357}
358
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000359void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
360 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000361 Record.push_back(D->isThisDeclarationADefinition());
362 if (D->isThisDeclarationADefinition())
Douglas Gregore3241e92009-04-18 00:02:19 +0000363 Writer.AddStmt(D->getBody(Context));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000364 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
365 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
366 Record.push_back(D->isInline());
367 Record.push_back(D->isVirtual());
368 Record.push_back(D->isPure());
369 Record.push_back(D->inheritedPrototype());
370 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
371 Record.push_back(D->isDeleted());
372 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
373 Record.push_back(D->param_size());
374 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
375 P != PEnd; ++P)
376 Writer.AddDeclRef(*P, Record);
377 Code = pch::DECL_FUNCTION;
378}
379
Steve Naroff79ea0e02009-04-20 15:06:07 +0000380void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
381 VisitNamedDecl(D);
382 // FIXME: convert to LazyStmtPtr?
383 // Unlike C/C++, method bodies will never be in header files.
384 Record.push_back(D->getBody() != 0);
385 if (D->getBody() != 0) {
386 Writer.AddStmt(D->getBody(Context));
387 Writer.AddDeclRef(D->getSelfDecl(), Record);
388 Writer.AddDeclRef(D->getCmdDecl(), Record);
389 }
390 Record.push_back(D->isInstanceMethod());
391 Record.push_back(D->isVariadic());
392 Record.push_back(D->isSynthesized());
393 // FIXME: stable encoding for @required/@optional
394 Record.push_back(D->getImplementationControl());
395 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
396 Record.push_back(D->getObjCDeclQualifier());
397 Writer.AddTypeRef(D->getResultType(), Record);
398 Writer.AddSourceLocation(D->getLocEnd(), Record);
399 Record.push_back(D->param_size());
400 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
401 PEnd = D->param_end(); P != PEnd; ++P)
402 Writer.AddDeclRef(*P, Record);
403 Code = pch::DECL_OBJC_METHOD;
404}
405
Steve Naroff7333b492009-04-20 20:09:33 +0000406void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
407 VisitNamedDecl(D);
408 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
409 // Abstract class (no need to define a stable pch::DECL code).
410}
411
412void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
413 VisitObjCContainerDecl(D);
414 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
415 Writer.AddDeclRef(D->getSuperClass(), Record);
416 Record.push_back(D->ivar_size());
417 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
418 IEnd = D->ivar_end(); I != IEnd; ++I)
419 Writer.AddDeclRef(*I, Record);
420 Record.push_back(D->isForwardDecl());
421 Record.push_back(D->isImplicitInterfaceDecl());
422 Writer.AddSourceLocation(D->getClassLoc(), Record);
423 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
424 Writer.AddSourceLocation(D->getLocEnd(), Record);
425 // FIXME: add protocols, categories.
Steve Naroff97b53bd2009-04-21 15:12:33 +0000426 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff7333b492009-04-20 20:09:33 +0000427}
428
429void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
430 VisitFieldDecl(D);
431 // FIXME: stable encoding for @public/@private/@protected/@package
432 Record.push_back(D->getAccessControl());
Steve Naroff97b53bd2009-04-21 15:12:33 +0000433 Code = pch::DECL_OBJC_IVAR;
434}
435
436void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
437 VisitObjCContainerDecl(D);
438 Record.push_back(D->isForwardDecl());
439 Writer.AddSourceLocation(D->getLocEnd(), Record);
440 Record.push_back(D->protocol_size());
441 for (ObjCProtocolDecl::protocol_iterator
442 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
443 Writer.AddDeclRef(*I, Record);
444 Code = pch::DECL_OBJC_PROTOCOL;
445}
446
447void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
448 VisitFieldDecl(D);
449 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
450}
451
452void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
453 VisitDecl(D);
454 Record.push_back(D->size());
455 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
456 Writer.AddDeclRef(*I, Record);
457 Code = pch::DECL_OBJC_CLASS;
458}
459
460void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
461 VisitDecl(D);
462 Record.push_back(D->protocol_size());
463 for (ObjCProtocolDecl::protocol_iterator
464 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
465 Writer.AddDeclRef(*I, Record);
466 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
467}
468
469void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
470 VisitObjCContainerDecl(D);
471 Writer.AddDeclRef(D->getClassInterface(), Record);
472 Record.push_back(D->protocol_size());
473 for (ObjCProtocolDecl::protocol_iterator
474 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
475 Writer.AddDeclRef(*I, Record);
476 Writer.AddDeclRef(D->getNextClassCategory(), Record);
477 Writer.AddSourceLocation(D->getLocEnd(), Record);
478 Code = pch::DECL_OBJC_CATEGORY;
479}
480
481void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
482 VisitNamedDecl(D);
483 Writer.AddDeclRef(D->getClassInterface(), Record);
484 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
485}
486
487void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
488 VisitNamedDecl(D);
489 // FIXME: Implement.
490 Code = pch::DECL_OBJC_PROPERTY;
491}
492
493void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
494 VisitDecl(D);
495 // FIXME: Implement.
496 // Abstract class (no need to define a stable pch::DECL code).
497}
498
499void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
500 VisitObjCImplDecl(D);
501 // FIXME: Implement.
502 Code = pch::DECL_OBJC_CATEGORY_IMPL;
503}
504
505void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
506 VisitObjCImplDecl(D);
507 // FIXME: Implement.
508 Code = pch::DECL_OBJC_IMPLEMENTATION;
509}
510
511void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
512 VisitDecl(D);
513 // FIXME: Implement.
514 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff7333b492009-04-20 20:09:33 +0000515}
516
Douglas Gregor982365e2009-04-13 21:20:57 +0000517void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
518 VisitValueDecl(D);
519 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000520 Record.push_back(D->getBitWidth()? 1 : 0);
521 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000522 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000523 Code = pch::DECL_FIELD;
524}
525
Douglas Gregorc34897d2009-04-09 22:27:44 +0000526void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
527 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000528 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000529 Record.push_back(D->isThreadSpecified());
530 Record.push_back(D->hasCXXDirectInitializer());
531 Record.push_back(D->isDeclaredInCondition());
532 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
533 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000534 Record.push_back(D->getInit()? 1 : 0);
535 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000536 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000537 Code = pch::DECL_VAR;
538}
539
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000540void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
541 VisitVarDecl(D);
542 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000543 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000544 // FIXME: why isn't the "default argument" just stored as the initializer
545 // in VarDecl?
546 Code = pch::DECL_PARM_VAR;
547}
548
549void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
550 VisitParmVarDecl(D);
551 Writer.AddTypeRef(D->getOriginalType(), Record);
552 Code = pch::DECL_ORIGINAL_PARM_VAR;
553}
554
Douglas Gregor2a491792009-04-13 22:49:25 +0000555void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
556 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000557 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000558 Code = pch::DECL_FILE_SCOPE_ASM;
559}
560
561void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
562 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000563 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000564 Record.push_back(D->param_size());
565 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
566 P != PEnd; ++P)
567 Writer.AddDeclRef(*P, Record);
568 Code = pch::DECL_BLOCK;
569}
570
Douglas Gregorc34897d2009-04-09 22:27:44 +0000571/// \brief Emit the DeclContext part of a declaration context decl.
572///
573/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
574/// block for this declaration context is stored. May be 0 to indicate
575/// that there are no declarations stored within this context.
576///
577/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
578/// block for this declaration context is stored. May be 0 to indicate
579/// that there are no declarations visible from this context. Note
580/// that this value will not be emitted for non-primary declaration
581/// contexts.
582void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
583 uint64_t VisibleOffset) {
584 Record.push_back(LexicalOffset);
585 if (DC->getPrimaryContext() == DC)
586 Record.push_back(VisibleOffset);
587}
588
589//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000590// Statement/expression serialization
591//===----------------------------------------------------------------------===//
592namespace {
593 class VISIBILITY_HIDDEN PCHStmtWriter
594 : public StmtVisitor<PCHStmtWriter, void> {
595
596 PCHWriter &Writer;
597 PCHWriter::RecordData &Record;
598
599 public:
600 pch::StmtCode Code;
601
602 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
603 : Writer(Writer), Record(Record) { }
604
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000605 void VisitStmt(Stmt *S);
606 void VisitNullStmt(NullStmt *S);
607 void VisitCompoundStmt(CompoundStmt *S);
608 void VisitSwitchCase(SwitchCase *S);
609 void VisitCaseStmt(CaseStmt *S);
610 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000611 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000612 void VisitIfStmt(IfStmt *S);
613 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000614 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000615 void VisitDoStmt(DoStmt *S);
616 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000617 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000618 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000619 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000620 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000621 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000622 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000623 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000624 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000625 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000626 void VisitDeclRefExpr(DeclRefExpr *E);
627 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000628 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000629 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000630 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000631 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000632 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000633 void VisitUnaryOperator(UnaryOperator *E);
634 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000635 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000636 void VisitCallExpr(CallExpr *E);
637 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000638 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000639 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000640 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
641 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000642 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000643 void VisitExplicitCastExpr(ExplicitCastExpr *E);
644 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000645 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000646 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000647 void VisitInitListExpr(InitListExpr *E);
648 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
649 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000650 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000651 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000652 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000653 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
654 void VisitChooseExpr(ChooseExpr *E);
655 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000656 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000657 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000658 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000659
660 // Objective-C
661 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
662
663
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000664 };
665}
666
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000667void PCHStmtWriter::VisitStmt(Stmt *S) {
668}
669
670void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
671 VisitStmt(S);
672 Writer.AddSourceLocation(S->getSemiLoc(), Record);
673 Code = pch::STMT_NULL;
674}
675
676void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
677 VisitStmt(S);
678 Record.push_back(S->size());
679 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
680 CS != CSEnd; ++CS)
681 Writer.WriteSubStmt(*CS);
682 Writer.AddSourceLocation(S->getLBracLoc(), Record);
683 Writer.AddSourceLocation(S->getRBracLoc(), Record);
684 Code = pch::STMT_COMPOUND;
685}
686
687void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
688 VisitStmt(S);
689 Record.push_back(Writer.RecordSwitchCaseID(S));
690}
691
692void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
693 VisitSwitchCase(S);
694 Writer.WriteSubStmt(S->getLHS());
695 Writer.WriteSubStmt(S->getRHS());
696 Writer.WriteSubStmt(S->getSubStmt());
697 Writer.AddSourceLocation(S->getCaseLoc(), Record);
698 Code = pch::STMT_CASE;
699}
700
701void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
702 VisitSwitchCase(S);
703 Writer.WriteSubStmt(S->getSubStmt());
704 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
705 Code = pch::STMT_DEFAULT;
706}
707
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000708void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
709 VisitStmt(S);
710 Writer.AddIdentifierRef(S->getID(), Record);
711 Writer.WriteSubStmt(S->getSubStmt());
712 Writer.AddSourceLocation(S->getIdentLoc(), Record);
713 Record.push_back(Writer.GetLabelID(S));
714 Code = pch::STMT_LABEL;
715}
716
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000717void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
718 VisitStmt(S);
719 Writer.WriteSubStmt(S->getCond());
720 Writer.WriteSubStmt(S->getThen());
721 Writer.WriteSubStmt(S->getElse());
722 Writer.AddSourceLocation(S->getIfLoc(), Record);
723 Code = pch::STMT_IF;
724}
725
726void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
727 VisitStmt(S);
728 Writer.WriteSubStmt(S->getCond());
729 Writer.WriteSubStmt(S->getBody());
730 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
731 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
732 SC = SC->getNextSwitchCase())
733 Record.push_back(Writer.getSwitchCaseID(SC));
734 Code = pch::STMT_SWITCH;
735}
736
Douglas Gregora6b503f2009-04-17 00:16:09 +0000737void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
738 VisitStmt(S);
739 Writer.WriteSubStmt(S->getCond());
740 Writer.WriteSubStmt(S->getBody());
741 Writer.AddSourceLocation(S->getWhileLoc(), Record);
742 Code = pch::STMT_WHILE;
743}
744
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000745void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
746 VisitStmt(S);
747 Writer.WriteSubStmt(S->getCond());
748 Writer.WriteSubStmt(S->getBody());
749 Writer.AddSourceLocation(S->getDoLoc(), Record);
750 Code = pch::STMT_DO;
751}
752
753void PCHStmtWriter::VisitForStmt(ForStmt *S) {
754 VisitStmt(S);
755 Writer.WriteSubStmt(S->getInit());
756 Writer.WriteSubStmt(S->getCond());
757 Writer.WriteSubStmt(S->getInc());
758 Writer.WriteSubStmt(S->getBody());
759 Writer.AddSourceLocation(S->getForLoc(), Record);
760 Code = pch::STMT_FOR;
761}
762
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000763void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
764 VisitStmt(S);
765 Record.push_back(Writer.GetLabelID(S->getLabel()));
766 Writer.AddSourceLocation(S->getGotoLoc(), Record);
767 Writer.AddSourceLocation(S->getLabelLoc(), Record);
768 Code = pch::STMT_GOTO;
769}
770
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000771void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
772 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000773 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000774 Writer.WriteSubStmt(S->getTarget());
775 Code = pch::STMT_INDIRECT_GOTO;
776}
777
Douglas Gregora6b503f2009-04-17 00:16:09 +0000778void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
779 VisitStmt(S);
780 Writer.AddSourceLocation(S->getContinueLoc(), Record);
781 Code = pch::STMT_CONTINUE;
782}
783
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000784void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
785 VisitStmt(S);
786 Writer.AddSourceLocation(S->getBreakLoc(), Record);
787 Code = pch::STMT_BREAK;
788}
789
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000790void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
791 VisitStmt(S);
792 Writer.WriteSubStmt(S->getRetValue());
793 Writer.AddSourceLocation(S->getReturnLoc(), Record);
794 Code = pch::STMT_RETURN;
795}
796
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000797void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
798 VisitStmt(S);
799 Writer.AddSourceLocation(S->getStartLoc(), Record);
800 Writer.AddSourceLocation(S->getEndLoc(), Record);
801 DeclGroupRef DG = S->getDeclGroup();
802 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
803 Writer.AddDeclRef(*D, Record);
804 Code = pch::STMT_DECL;
805}
806
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000807void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
808 VisitStmt(S);
809 Record.push_back(S->getNumOutputs());
810 Record.push_back(S->getNumInputs());
811 Record.push_back(S->getNumClobbers());
812 Writer.AddSourceLocation(S->getAsmLoc(), Record);
813 Writer.AddSourceLocation(S->getRParenLoc(), Record);
814 Record.push_back(S->isVolatile());
815 Record.push_back(S->isSimple());
816 Writer.WriteSubStmt(S->getAsmString());
817
818 // Outputs
819 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
820 Writer.AddString(S->getOutputName(I), Record);
821 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
822 Writer.WriteSubStmt(S->getOutputExpr(I));
823 }
824
825 // Inputs
826 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
827 Writer.AddString(S->getInputName(I), Record);
828 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
829 Writer.WriteSubStmt(S->getInputExpr(I));
830 }
831
832 // Clobbers
833 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
834 Writer.WriteSubStmt(S->getClobber(I));
835
836 Code = pch::STMT_ASM;
837}
838
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000839void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000840 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000841 Writer.AddTypeRef(E->getType(), Record);
842 Record.push_back(E->isTypeDependent());
843 Record.push_back(E->isValueDependent());
844}
845
Douglas Gregore2f37202009-04-14 21:55:33 +0000846void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
847 VisitExpr(E);
848 Writer.AddSourceLocation(E->getLocation(), Record);
849 Record.push_back(E->getIdentType()); // FIXME: stable encoding
850 Code = pch::EXPR_PREDEFINED;
851}
852
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000853void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
854 VisitExpr(E);
855 Writer.AddDeclRef(E->getDecl(), Record);
856 Writer.AddSourceLocation(E->getLocation(), Record);
857 Code = pch::EXPR_DECL_REF;
858}
859
860void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
861 VisitExpr(E);
862 Writer.AddSourceLocation(E->getLocation(), Record);
863 Writer.AddAPInt(E->getValue(), Record);
864 Code = pch::EXPR_INTEGER_LITERAL;
865}
866
Douglas Gregore2f37202009-04-14 21:55:33 +0000867void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
868 VisitExpr(E);
869 Writer.AddAPFloat(E->getValue(), Record);
870 Record.push_back(E->isExact());
871 Writer.AddSourceLocation(E->getLocation(), Record);
872 Code = pch::EXPR_FLOATING_LITERAL;
873}
874
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000875void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
876 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000877 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000878 Code = pch::EXPR_IMAGINARY_LITERAL;
879}
880
Douglas Gregor596e0932009-04-15 16:35:07 +0000881void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
882 VisitExpr(E);
883 Record.push_back(E->getByteLength());
884 Record.push_back(E->getNumConcatenated());
885 Record.push_back(E->isWide());
886 // FIXME: String data should be stored as a blob at the end of the
887 // StringLiteral. However, we can't do so now because we have no
888 // provision for coping with abbreviations when we're jumping around
889 // the PCH file during deserialization.
890 Record.insert(Record.end(),
891 E->getStrData(), E->getStrData() + E->getByteLength());
892 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
893 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
894 Code = pch::EXPR_STRING_LITERAL;
895}
896
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000897void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
898 VisitExpr(E);
899 Record.push_back(E->getValue());
900 Writer.AddSourceLocation(E->getLoc(), Record);
901 Record.push_back(E->isWide());
902 Code = pch::EXPR_CHARACTER_LITERAL;
903}
904
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000905void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
906 VisitExpr(E);
907 Writer.AddSourceLocation(E->getLParen(), Record);
908 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000909 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000910 Code = pch::EXPR_PAREN;
911}
912
Douglas Gregor12d74052009-04-15 15:58:59 +0000913void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
914 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000915 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000916 Record.push_back(E->getOpcode()); // FIXME: stable encoding
917 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
918 Code = pch::EXPR_UNARY_OPERATOR;
919}
920
921void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
922 VisitExpr(E);
923 Record.push_back(E->isSizeOf());
924 if (E->isArgumentType())
925 Writer.AddTypeRef(E->getArgumentType(), Record);
926 else {
927 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000928 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000929 }
930 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
931 Writer.AddSourceLocation(E->getRParenLoc(), Record);
932 Code = pch::EXPR_SIZEOF_ALIGN_OF;
933}
934
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000935void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
936 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000937 Writer.WriteSubStmt(E->getLHS());
938 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000939 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
940 Code = pch::EXPR_ARRAY_SUBSCRIPT;
941}
942
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000943void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
944 VisitExpr(E);
945 Record.push_back(E->getNumArgs());
946 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000947 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000948 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
949 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000950 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000951 Code = pch::EXPR_CALL;
952}
953
954void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
955 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000956 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000957 Writer.AddDeclRef(E->getMemberDecl(), Record);
958 Writer.AddSourceLocation(E->getMemberLoc(), Record);
959 Record.push_back(E->isArrow());
960 Code = pch::EXPR_MEMBER;
961}
962
Douglas Gregora151ba42009-04-14 23:32:43 +0000963void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
964 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000965 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000966}
967
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000968void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
969 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000970 Writer.WriteSubStmt(E->getLHS());
971 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000972 Record.push_back(E->getOpcode()); // FIXME: stable encoding
973 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
974 Code = pch::EXPR_BINARY_OPERATOR;
975}
976
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000977void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
978 VisitBinaryOperator(E);
979 Writer.AddTypeRef(E->getComputationLHSType(), Record);
980 Writer.AddTypeRef(E->getComputationResultType(), Record);
981 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
982}
983
984void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
985 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000986 Writer.WriteSubStmt(E->getCond());
987 Writer.WriteSubStmt(E->getLHS());
988 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000989 Code = pch::EXPR_CONDITIONAL_OPERATOR;
990}
991
Douglas Gregora151ba42009-04-14 23:32:43 +0000992void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
993 VisitCastExpr(E);
994 Record.push_back(E->isLvalueCast());
995 Code = pch::EXPR_IMPLICIT_CAST;
996}
997
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000998void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
999 VisitCastExpr(E);
1000 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1001}
1002
1003void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1004 VisitExplicitCastExpr(E);
1005 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1006 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1007 Code = pch::EXPR_CSTYLE_CAST;
1008}
1009
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001010void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1011 VisitExpr(E);
1012 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001013 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001014 Record.push_back(E->isFileScope());
1015 Code = pch::EXPR_COMPOUND_LITERAL;
1016}
1017
Douglas Gregorec0b8292009-04-15 23:02:49 +00001018void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1019 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001020 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001021 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1022 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1023 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1024}
1025
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001026void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1027 VisitExpr(E);
1028 Record.push_back(E->getNumInits());
1029 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001030 Writer.WriteSubStmt(E->getInit(I));
1031 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001032 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1033 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1034 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1035 Record.push_back(E->hadArrayRangeDesignator());
1036 Code = pch::EXPR_INIT_LIST;
1037}
1038
1039void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1040 VisitExpr(E);
1041 Record.push_back(E->getNumSubExprs());
1042 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001043 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001044 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1045 Record.push_back(E->usesGNUSyntax());
1046 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1047 DEnd = E->designators_end();
1048 D != DEnd; ++D) {
1049 if (D->isFieldDesignator()) {
1050 if (FieldDecl *Field = D->getField()) {
1051 Record.push_back(pch::DESIG_FIELD_DECL);
1052 Writer.AddDeclRef(Field, Record);
1053 } else {
1054 Record.push_back(pch::DESIG_FIELD_NAME);
1055 Writer.AddIdentifierRef(D->getFieldName(), Record);
1056 }
1057 Writer.AddSourceLocation(D->getDotLoc(), Record);
1058 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1059 } else if (D->isArrayDesignator()) {
1060 Record.push_back(pch::DESIG_ARRAY);
1061 Record.push_back(D->getFirstExprIndex());
1062 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1063 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1064 } else {
1065 assert(D->isArrayRangeDesignator() && "Unknown designator");
1066 Record.push_back(pch::DESIG_ARRAY_RANGE);
1067 Record.push_back(D->getFirstExprIndex());
1068 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1069 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1070 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1071 }
1072 }
1073 Code = pch::EXPR_DESIGNATED_INIT;
1074}
1075
1076void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1077 VisitExpr(E);
1078 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1079}
1080
Douglas Gregorec0b8292009-04-15 23:02:49 +00001081void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1082 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001083 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001084 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1085 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1086 Code = pch::EXPR_VA_ARG;
1087}
1088
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001089void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1090 VisitExpr(E);
1091 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1092 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1093 Record.push_back(Writer.GetLabelID(E->getLabel()));
1094 Code = pch::EXPR_ADDR_LABEL;
1095}
1096
Douglas Gregoreca12f62009-04-17 19:05:30 +00001097void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1098 VisitExpr(E);
1099 Writer.WriteSubStmt(E->getSubStmt());
1100 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1101 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1102 Code = pch::EXPR_STMT;
1103}
1104
Douglas Gregor209d4622009-04-15 23:33:31 +00001105void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1106 VisitExpr(E);
1107 Writer.AddTypeRef(E->getArgType1(), Record);
1108 Writer.AddTypeRef(E->getArgType2(), Record);
1109 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1110 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1111 Code = pch::EXPR_TYPES_COMPATIBLE;
1112}
1113
1114void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1115 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001116 Writer.WriteSubStmt(E->getCond());
1117 Writer.WriteSubStmt(E->getLHS());
1118 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001119 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1120 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1121 Code = pch::EXPR_CHOOSE;
1122}
1123
1124void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1125 VisitExpr(E);
1126 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1127 Code = pch::EXPR_GNU_NULL;
1128}
1129
Douglas Gregor725e94b2009-04-16 00:01:45 +00001130void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1131 VisitExpr(E);
1132 Record.push_back(E->getNumSubExprs());
1133 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001134 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001135 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1136 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1137 Code = pch::EXPR_SHUFFLE_VECTOR;
1138}
1139
Douglas Gregore246b742009-04-17 19:21:43 +00001140void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1141 VisitExpr(E);
1142 Writer.AddDeclRef(E->getBlockDecl(), Record);
1143 Record.push_back(E->hasBlockDeclRefExprs());
1144 Code = pch::EXPR_BLOCK;
1145}
1146
Douglas Gregor725e94b2009-04-16 00:01:45 +00001147void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1148 VisitExpr(E);
1149 Writer.AddDeclRef(E->getDecl(), Record);
1150 Writer.AddSourceLocation(E->getLocation(), Record);
1151 Record.push_back(E->isByRef());
1152 Code = pch::EXPR_BLOCK_DECL_REF;
1153}
1154
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001155//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001156// Objective-C Expressions and Statements.
1157//===----------------------------------------------------------------------===//
1158
1159void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1160 VisitExpr(E);
1161 Writer.AddTypeRef(E->getEncodedType(), Record);
1162 Writer.AddSourceLocation(E->getAtLoc(), Record);
1163 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1164 Code = pch::EXPR_OBJC_ENCODE;
1165}
1166
1167
1168//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001169// PCHWriter Implementation
1170//===----------------------------------------------------------------------===//
1171
Douglas Gregorb5887f32009-04-10 21:16:55 +00001172/// \brief Write the target triple (e.g., i686-apple-darwin9).
1173void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1174 using namespace llvm;
1175 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1176 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001178 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001179
1180 RecordData Record;
1181 Record.push_back(pch::TARGET_TRIPLE);
1182 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001183 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001184}
1185
1186/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001187void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1188 RecordData Record;
1189 Record.push_back(LangOpts.Trigraphs);
1190 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1191 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1192 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1193 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1194 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1195 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1196 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1197 Record.push_back(LangOpts.C99); // C99 Support
1198 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1199 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1200 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1201 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1202 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1203
1204 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1205 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1206 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1207
1208 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1209 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1210 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1211 Record.push_back(LangOpts.LaxVectorConversions);
1212 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1213
1214 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1215 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1216 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1217
1218 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1219 // by locks.
1220 Record.push_back(LangOpts.Blocks); // block extension to C
1221 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1222 // they are unused.
1223 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1224 // (modulo the platform support).
1225
1226 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1227 // signed integer arithmetic overflows.
1228
1229 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1230 // may be ripped out at any time.
1231
1232 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1233 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1234 // defined.
1235 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1236 // opposed to __DYNAMIC__).
1237 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1238
1239 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1240 // used (instead of C99 semantics).
1241 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1242 Record.push_back(LangOpts.getGCMode());
1243 Record.push_back(LangOpts.getVisibilityMode());
1244 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001245 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001246}
1247
Douglas Gregorab1cef72009-04-10 03:52:48 +00001248//===----------------------------------------------------------------------===//
1249// Source Manager Serialization
1250//===----------------------------------------------------------------------===//
1251
1252/// \brief Create an abbreviation for the SLocEntry that refers to a
1253/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001254static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001255 using namespace llvm;
1256 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1257 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001263 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001264}
1265
1266/// \brief Create an abbreviation for the SLocEntry that refers to a
1267/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001268static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001269 using namespace llvm;
1270 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1271 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001277 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001278}
1279
1280/// \brief Create an abbreviation for the SLocEntry that refers to a
1281/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001282static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001283 using namespace llvm;
1284 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1285 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001287 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001288}
1289
1290/// \brief Create an abbreviation for the SLocEntry that refers to an
1291/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001292static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001293 using namespace llvm;
1294 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1295 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001301 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001302}
1303
1304/// \brief Writes the block containing the serialized form of the
1305/// source manager.
1306///
1307/// TODO: We should probably use an on-disk hash table (stored in a
1308/// blob), indexed based on the file name, so that we only create
1309/// entries for files that we actually need. In the common case (no
1310/// errors), we probably won't have to create file entries for any of
1311/// the files in the AST.
1312void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001313 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001314 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001315
1316 // Abbreviations for the various kinds of source-location entries.
1317 int SLocFileAbbrv = -1;
1318 int SLocBufferAbbrv = -1;
1319 int SLocBufferBlobAbbrv = -1;
1320 int SLocInstantiationAbbrv = -1;
1321
1322 // Write out the source location entry table. We skip the first
1323 // entry, which is always the same dummy entry.
1324 RecordData Record;
1325 for (SourceManager::sloc_entry_iterator
1326 SLoc = SourceMgr.sloc_entry_begin() + 1,
1327 SLocEnd = SourceMgr.sloc_entry_end();
1328 SLoc != SLocEnd; ++SLoc) {
1329 // Figure out which record code to use.
1330 unsigned Code;
1331 if (SLoc->isFile()) {
1332 if (SLoc->getFile().getContentCache()->Entry)
1333 Code = pch::SM_SLOC_FILE_ENTRY;
1334 else
1335 Code = pch::SM_SLOC_BUFFER_ENTRY;
1336 } else
1337 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1338 Record.push_back(Code);
1339
1340 Record.push_back(SLoc->getOffset());
1341 if (SLoc->isFile()) {
1342 const SrcMgr::FileInfo &File = SLoc->getFile();
1343 Record.push_back(File.getIncludeLoc().getRawEncoding());
1344 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001345 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001346
1347 const SrcMgr::ContentCache *Content = File.getContentCache();
1348 if (Content->Entry) {
1349 // The source location entry is a file. The blob associated
1350 // with this entry is the file name.
1351 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001352 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1353 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001354 Content->Entry->getName(),
1355 strlen(Content->Entry->getName()));
1356 } else {
1357 // The source location entry is a buffer. The blob associated
1358 // with this entry contains the contents of the buffer.
1359 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001360 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1361 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001362 }
1363
1364 // We add one to the size so that we capture the trailing NULL
1365 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1366 // the reader side).
1367 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1368 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001369 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001370 Record.clear();
1371 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001372 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001373 Buffer->getBufferStart(),
1374 Buffer->getBufferSize() + 1);
1375 }
1376 } else {
1377 // The source location entry is an instantiation.
1378 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1379 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1380 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1381 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1382
Douglas Gregor364e5802009-04-15 18:05:10 +00001383 // Compute the token length for this macro expansion.
1384 unsigned NextOffset = SourceMgr.getNextOffset();
1385 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1386 if (++NextSLoc != SLocEnd)
1387 NextOffset = NextSLoc->getOffset();
1388 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1389
Douglas Gregorab1cef72009-04-10 03:52:48 +00001390 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001391 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1392 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001393 }
1394
1395 Record.clear();
1396 }
1397
Douglas Gregor635f97f2009-04-13 16:31:14 +00001398 // Write the line table.
1399 if (SourceMgr.hasLineTable()) {
1400 LineTableInfo &LineTable = SourceMgr.getLineTable();
1401
1402 // Emit the file names
1403 Record.push_back(LineTable.getNumFilenames());
1404 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1405 // Emit the file name
1406 const char *Filename = LineTable.getFilename(I);
1407 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1408 Record.push_back(FilenameLen);
1409 if (FilenameLen)
1410 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1411 }
1412
1413 // Emit the line entries
1414 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1415 L != LEnd; ++L) {
1416 // Emit the file ID
1417 Record.push_back(L->first);
1418
1419 // Emit the line entries
1420 Record.push_back(L->second.size());
1421 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1422 LEEnd = L->second.end();
1423 LE != LEEnd; ++LE) {
1424 Record.push_back(LE->FileOffset);
1425 Record.push_back(LE->LineNo);
1426 Record.push_back(LE->FilenameID);
1427 Record.push_back((unsigned)LE->FileKind);
1428 Record.push_back(LE->IncludeOffset);
1429 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001430 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001431 }
1432 }
1433
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001434 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001435}
1436
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001437/// \brief Writes the block containing the serialized form of the
1438/// preprocessor.
1439///
Chris Lattner850eabd2009-04-10 18:08:30 +00001440void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001441 // Enter the preprocessor block.
Douglas Gregorc713da92009-04-21 22:25:48 +00001442 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner84b04f12009-04-10 17:16:57 +00001443
Chris Lattner1b094952009-04-10 18:00:12 +00001444 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1445 // FIXME: use diagnostics subsystem for localization etc.
1446 if (PP.SawDateOrTime())
1447 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001448
Chris Lattner1b094952009-04-10 18:00:12 +00001449 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001450
Chris Lattner4b21c202009-04-13 01:29:17 +00001451 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1452 if (PP.getCounterValue() != 0) {
1453 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001454 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001455 Record.clear();
1456 }
1457
Chris Lattner1b094952009-04-10 18:00:12 +00001458 // Loop over all the macro definitions that are live at the end of the file,
1459 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001460 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1461 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001462 // FIXME: This emits macros in hash table order, we should do it in a stable
1463 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001464 MacroInfo *MI = I->second;
1465
1466 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1467 // been redefined by the header (in which case they are not isBuiltinMacro).
1468 if (MI->isBuiltinMacro())
1469 continue;
1470
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001471 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001472 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001473 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001474 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1475 Record.push_back(MI->isUsed());
1476
1477 unsigned Code;
1478 if (MI->isObjectLike()) {
1479 Code = pch::PP_MACRO_OBJECT_LIKE;
1480 } else {
1481 Code = pch::PP_MACRO_FUNCTION_LIKE;
1482
1483 Record.push_back(MI->isC99Varargs());
1484 Record.push_back(MI->isGNUVarargs());
1485 Record.push_back(MI->getNumArgs());
1486 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1487 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001488 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001489 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001490 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001491 Record.clear();
1492
Chris Lattner850eabd2009-04-10 18:08:30 +00001493 // Emit the tokens array.
1494 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1495 // Note that we know that the preprocessor does not have any annotation
1496 // tokens in it because they are created by the parser, and thus can't be
1497 // in a macro definition.
1498 const Token &Tok = MI->getReplacementToken(TokNo);
1499
1500 Record.push_back(Tok.getLocation().getRawEncoding());
1501 Record.push_back(Tok.getLength());
1502
Chris Lattner850eabd2009-04-10 18:08:30 +00001503 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1504 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001505 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001506
1507 // FIXME: Should translate token kind to a stable encoding.
1508 Record.push_back(Tok.getKind());
1509 // FIXME: Should translate token flags to a stable encoding.
1510 Record.push_back(Tok.getFlags());
1511
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001512 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001513 Record.clear();
1514 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001515 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001516 }
1517
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001518 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001519}
1520
1521
Douglas Gregorc34897d2009-04-09 22:27:44 +00001522/// \brief Write the representation of a type to the PCH stream.
1523void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001524 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001525 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001526 ID = NextTypeID++;
1527
1528 // Record the offset for this type.
1529 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001530 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001531 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1532 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001533 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001534 }
1535
1536 RecordData Record;
1537
1538 // Emit the type's representation.
1539 PCHTypeWriter W(*this, Record);
1540 switch (T->getTypeClass()) {
1541 // For all of the concrete, non-dependent types, call the
1542 // appropriate visitor function.
1543#define TYPE(Class, Base) \
1544 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1545#define ABSTRACT_TYPE(Class, Base)
1546#define DEPENDENT_TYPE(Class, Base)
1547#include "clang/AST/TypeNodes.def"
1548
1549 // For all of the dependent type nodes (which only occur in C++
1550 // templates), produce an error.
1551#define TYPE(Class, Base)
1552#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1553#include "clang/AST/TypeNodes.def"
1554 assert(false && "Cannot serialize dependent type nodes");
1555 break;
1556 }
1557
1558 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001559 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001560
1561 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001562 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001563}
1564
1565/// \brief Write a block containing all of the types.
1566void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001567 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001568 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001569
1570 // Emit all of the types in the ASTContext
1571 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1572 TEnd = Context.getTypes().end();
1573 T != TEnd; ++T) {
1574 // Builtin types are never serialized.
1575 if (isa<BuiltinType>(*T))
1576 continue;
1577
1578 WriteType(*T);
1579 }
1580
1581 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001582 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001583}
1584
1585/// \brief Write the block containing all of the declaration IDs
1586/// lexically declared within the given DeclContext.
1587///
1588/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1589/// bistream, or 0 if no block was written.
1590uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1591 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001592 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001593 return 0;
1594
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001595 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001596 RecordData Record;
1597 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1598 DEnd = DC->decls_end(Context);
1599 D != DEnd; ++D)
1600 AddDeclRef(*D, Record);
1601
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001602 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001603 return Offset;
1604}
1605
1606/// \brief Write the block containing all of the declaration IDs
1607/// visible from the given DeclContext.
1608///
1609/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1610/// bistream, or 0 if no block was written.
1611uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1612 DeclContext *DC) {
1613 if (DC->getPrimaryContext() != DC)
1614 return 0;
1615
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001616 // Since there is no name lookup into functions or methods, and we
1617 // perform name lookup for the translation unit via the
1618 // IdentifierInfo chains, don't bother to build a
1619 // visible-declarations table for these entities.
1620 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001621 return 0;
1622
Douglas Gregorc34897d2009-04-09 22:27:44 +00001623 // Force the DeclContext to build a its name-lookup table.
1624 DC->lookup(Context, DeclarationName());
1625
1626 // Serialize the contents of the mapping used for lookup. Note that,
1627 // although we have two very different code paths, the serialized
1628 // representation is the same for both cases: a declaration name,
1629 // followed by a size, followed by references to the visible
1630 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001631 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001632 RecordData Record;
1633 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001634 if (!Map)
1635 return 0;
1636
Douglas Gregorc34897d2009-04-09 22:27:44 +00001637 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1638 D != DEnd; ++D) {
1639 AddDeclarationName(D->first, Record);
1640 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1641 Record.push_back(Result.second - Result.first);
1642 for(; Result.first != Result.second; ++Result.first)
1643 AddDeclRef(*Result.first, Record);
1644 }
1645
1646 if (Record.size() == 0)
1647 return 0;
1648
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001649 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001650 return Offset;
1651}
1652
1653/// \brief Write a block containing all of the declarations.
1654void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001655 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001656 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001657
1658 // Emit all of the declarations.
1659 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001660 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001661 while (!DeclsToEmit.empty()) {
1662 // Pull the next declaration off the queue
1663 Decl *D = DeclsToEmit.front();
1664 DeclsToEmit.pop();
1665
1666 // If this declaration is also a DeclContext, write blocks for the
1667 // declarations that lexically stored inside its context and those
1668 // declarations that are visible from its context. These blocks
1669 // are written before the declaration itself so that we can put
1670 // their offsets into the record for the declaration.
1671 uint64_t LexicalOffset = 0;
1672 uint64_t VisibleOffset = 0;
1673 DeclContext *DC = dyn_cast<DeclContext>(D);
1674 if (DC) {
1675 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1676 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1677 }
1678
1679 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001680 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001681 if (ID == 0)
1682 ID = DeclIDs.size();
1683
1684 unsigned Index = ID - 1;
1685
1686 // Record the offset for this declaration
1687 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001688 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001689 else if (DeclOffsets.size() < Index) {
1690 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001691 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001692 }
1693
1694 // Build and emit a record for this declaration
1695 Record.clear();
1696 W.Code = (pch::DeclCode)0;
1697 W.Visit(D);
1698 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001699 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001700 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001701
Douglas Gregor1c507882009-04-15 21:30:51 +00001702 // If the declaration had any attributes, write them now.
1703 if (D->hasAttrs())
1704 WriteAttributeRecord(D->getAttrs());
1705
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001706 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001707 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001708
Douglas Gregor631f6c62009-04-14 00:24:19 +00001709 // Note external declarations so that we can add them to a record
1710 // in the PCH file later.
1711 if (isa<FileScopeAsmDecl>(D))
1712 ExternalDefinitions.push_back(ID);
1713 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1714 if (// Non-static file-scope variables with initializers or that
1715 // are tentative definitions.
1716 (Var->isFileVarDecl() &&
1717 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1718 // Out-of-line definitions of static data members (C++).
1719 (Var->getDeclContext()->isRecord() &&
1720 !Var->getLexicalDeclContext()->isRecord() &&
1721 Var->getStorageClass() == VarDecl::Static))
1722 ExternalDefinitions.push_back(ID);
1723 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00001724 if (Func->isThisDeclarationADefinition())
Douglas Gregor631f6c62009-04-14 00:24:19 +00001725 ExternalDefinitions.push_back(ID);
1726 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001727 }
1728
1729 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001730 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001731}
1732
Douglas Gregorff9a6092009-04-20 20:36:09 +00001733namespace {
1734class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1735 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001736 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001737
1738public:
1739 typedef const IdentifierInfo* key_type;
1740 typedef key_type key_type_ref;
1741
1742 typedef pch::IdentID data_type;
1743 typedef data_type data_type_ref;
1744
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001745 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1746 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001747
1748 static unsigned ComputeHash(const IdentifierInfo* II) {
1749 return clang::BernsteinHash(II->getName());
1750 }
1751
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001752 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001753 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1754 pch::IdentID ID) {
1755 unsigned KeyLen = strlen(II->getName()) + 1;
1756 clang::io::Emit16(Out, KeyLen);
Douglas Gregorc713da92009-04-21 22:25:48 +00001757 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1758 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001759 if (II->hasMacroDefinition() &&
1760 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1761 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001762 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1763 DEnd = IdentifierResolver::end();
1764 D != DEnd; ++D)
1765 DataLen += sizeof(pch::DeclID);
Douglas Gregorc713da92009-04-21 22:25:48 +00001766 clang::io::Emit16(Out, DataLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001767 return std::make_pair(KeyLen, DataLen);
1768 }
1769
1770 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1771 unsigned KeyLen) {
1772 // Record the location of the key data. This is used when generating
1773 // the mapping from persistent IDs to strings.
1774 Writer.SetIdentifierOffset(II, Out.tell());
1775 Out.write(II->getName(), KeyLen);
1776 }
1777
1778 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1779 pch::IdentID ID, unsigned) {
1780 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001781 bool hasMacroDefinition =
1782 II->hasMacroDefinition() &&
1783 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001784 Bits = Bits | (uint32_t)II->getTokenID();
1785 Bits = (Bits << 8) | (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001786 Bits = (Bits << 10) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001787 Bits = (Bits << 1) | II->isExtensionToken();
1788 Bits = (Bits << 1) | II->isPoisoned();
1789 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1790 clang::io::Emit32(Out, Bits);
1791 clang::io::Emit32(Out, ID);
1792
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001793 if (hasMacroDefinition)
1794 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1795
Douglas Gregorc713da92009-04-21 22:25:48 +00001796 // Emit the declaration IDs in reverse order, because the
1797 // IdentifierResolver provides the declarations as they would be
1798 // visible (e.g., the function "stat" would come before the struct
1799 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1800 // adds declarations to the end of the list (so we need to see the
1801 // struct "status" before the function "status").
1802 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1803 IdentifierResolver::end());
1804 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1805 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001806 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001807 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001808 }
1809};
1810} // end anonymous namespace
1811
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001812/// \brief Write the identifier table into the PCH file.
1813///
1814/// The identifier table consists of a blob containing string data
1815/// (the actual identifiers themselves) and a separate "offsets" index
1816/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001817void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001818 using namespace llvm;
1819
1820 // Create and write out the blob that contains the identifier
1821 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001822 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001823 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001824 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1825
1826 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001827 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1828 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1829 ID != IDEnd; ++ID) {
1830 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorff9a6092009-04-20 20:36:09 +00001831 Generator.insert(ID->first, ID->second);
1832 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001833
Douglas Gregorff9a6092009-04-20 20:36:09 +00001834 // Create the on-disk hash table in a buffer.
1835 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001836 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001837 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001838 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001839 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc713da92009-04-21 22:25:48 +00001840 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001841 }
1842
1843 // Create a blob abbreviation
1844 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1845 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001848 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001849
1850 // Write the identifier table
1851 RecordData Record;
1852 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001853 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001854 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1855 &IdentifierTable.front(),
1856 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001857 }
1858
1859 // Write the offsets table for identifier IDs.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001860 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001861}
1862
Douglas Gregor1c507882009-04-15 21:30:51 +00001863/// \brief Write a record containing the given attributes.
1864void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1865 RecordData Record;
1866 for (; Attr; Attr = Attr->getNext()) {
1867 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1868 Record.push_back(Attr->isInherited());
1869 switch (Attr->getKind()) {
1870 case Attr::Alias:
1871 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1872 break;
1873
1874 case Attr::Aligned:
1875 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1876 break;
1877
1878 case Attr::AlwaysInline:
1879 break;
1880
1881 case Attr::AnalyzerNoReturn:
1882 break;
1883
1884 case Attr::Annotate:
1885 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1886 break;
1887
1888 case Attr::AsmLabel:
1889 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1890 break;
1891
1892 case Attr::Blocks:
1893 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1894 break;
1895
1896 case Attr::Cleanup:
1897 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1898 break;
1899
1900 case Attr::Const:
1901 break;
1902
1903 case Attr::Constructor:
1904 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1905 break;
1906
1907 case Attr::DLLExport:
1908 case Attr::DLLImport:
1909 case Attr::Deprecated:
1910 break;
1911
1912 case Attr::Destructor:
1913 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1914 break;
1915
1916 case Attr::FastCall:
1917 break;
1918
1919 case Attr::Format: {
1920 const FormatAttr *Format = cast<FormatAttr>(Attr);
1921 AddString(Format->getType(), Record);
1922 Record.push_back(Format->getFormatIdx());
1923 Record.push_back(Format->getFirstArg());
1924 break;
1925 }
1926
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001927 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001928 case Attr::IBOutletKind:
1929 case Attr::NoReturn:
1930 case Attr::NoThrow:
1931 case Attr::Nodebug:
1932 case Attr::Noinline:
1933 break;
1934
1935 case Attr::NonNull: {
1936 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1937 Record.push_back(NonNull->size());
1938 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1939 break;
1940 }
1941
1942 case Attr::ObjCException:
1943 case Attr::ObjCNSObject:
1944 case Attr::Overloadable:
1945 break;
1946
1947 case Attr::Packed:
1948 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1949 break;
1950
1951 case Attr::Pure:
1952 break;
1953
1954 case Attr::Regparm:
1955 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1956 break;
1957
1958 case Attr::Section:
1959 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1960 break;
1961
1962 case Attr::StdCall:
1963 case Attr::TransparentUnion:
1964 case Attr::Unavailable:
1965 case Attr::Unused:
1966 case Attr::Used:
1967 break;
1968
1969 case Attr::Visibility:
1970 // FIXME: stable encoding
1971 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1972 break;
1973
1974 case Attr::WarnUnusedResult:
1975 case Attr::Weak:
1976 case Attr::WeakImport:
1977 break;
1978 }
1979 }
1980
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001981 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001982}
1983
1984void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1985 Record.push_back(Str.size());
1986 Record.insert(Record.end(), Str.begin(), Str.end());
1987}
1988
Douglas Gregorff9a6092009-04-20 20:36:09 +00001989/// \brief Note that the identifier II occurs at the given offset
1990/// within the identifier table.
1991void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
1992 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
1993}
1994
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001995PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001996 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
1997 NumStatements(0), NumMacros(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001998
Douglas Gregor87887da2009-04-20 15:53:59 +00001999void PCHWriter::WritePCH(Sema &SemaRef) {
2000 ASTContext &Context = SemaRef.Context;
2001 Preprocessor &PP = SemaRef.PP;
2002
Douglas Gregorc34897d2009-04-09 22:27:44 +00002003 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002004 Stream.Emit((unsigned)'C', 8);
2005 Stream.Emit((unsigned)'P', 8);
2006 Stream.Emit((unsigned)'C', 8);
2007 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002008
2009 // The translation unit is the first declaration we'll emit.
2010 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2011 DeclsToEmit.push(Context.getTranslationUnitDecl());
2012
2013 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002014 RecordData Record;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002015 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002016 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002017 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00002018 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002019 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002020 WriteTypesBlock(Context);
2021 WriteDeclsBlock(Context);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002022 WriteIdentifierTable(PP);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002023 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
2024 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregore01ad442009-04-18 05:55:16 +00002025
2026 // Write the record of special types.
2027 Record.clear();
2028 AddTypeRef(Context.getBuiltinVaListType(), Record);
2029 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2030
Douglas Gregor631f6c62009-04-14 00:24:19 +00002031 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002032 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor456e0952009-04-17 22:13:46 +00002033
2034 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002035 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002036 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002037 Record.push_back(NumMacros);
Douglas Gregor456e0952009-04-17 22:13:46 +00002038 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002039 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002040}
2041
2042void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2043 Record.push_back(Loc.getRawEncoding());
2044}
2045
2046void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2047 Record.push_back(Value.getBitWidth());
2048 unsigned N = Value.getNumWords();
2049 const uint64_t* Words = Value.getRawData();
2050 for (unsigned I = 0; I != N; ++I)
2051 Record.push_back(Words[I]);
2052}
2053
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002054void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2055 Record.push_back(Value.isUnsigned());
2056 AddAPInt(Value, Record);
2057}
2058
Douglas Gregore2f37202009-04-14 21:55:33 +00002059void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2060 AddAPInt(Value.bitcastToAPInt(), Record);
2061}
2062
Douglas Gregorc34897d2009-04-09 22:27:44 +00002063void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002064 if (II == 0) {
2065 Record.push_back(0);
2066 return;
2067 }
2068
2069 pch::IdentID &ID = IdentifierIDs[II];
2070 if (ID == 0)
2071 ID = IdentifierIDs.size();
2072
2073 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002074}
2075
2076void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2077 if (T.isNull()) {
2078 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2079 return;
2080 }
2081
2082 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002083 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002084 switch (BT->getKind()) {
2085 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2086 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2087 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2088 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2089 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2090 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2091 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2092 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2093 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2094 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2095 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2096 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2097 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2098 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2099 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2100 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2101 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2102 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2103 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2104 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2105 }
2106
2107 Record.push_back((ID << 3) | T.getCVRQualifiers());
2108 return;
2109 }
2110
Douglas Gregorac8f2802009-04-10 17:25:41 +00002111 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002112 if (ID == 0) // we haven't seen this type before
2113 ID = NextTypeID++;
2114
2115 // Encode the type qualifiers in the type reference.
2116 Record.push_back((ID << 3) | T.getCVRQualifiers());
2117}
2118
2119void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2120 if (D == 0) {
2121 Record.push_back(0);
2122 return;
2123 }
2124
Douglas Gregorac8f2802009-04-10 17:25:41 +00002125 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002126 if (ID == 0) {
2127 // We haven't seen this declaration before. Give it a new ID and
2128 // enqueue it in the list of declarations to emit.
2129 ID = DeclIDs.size();
2130 DeclsToEmit.push(const_cast<Decl *>(D));
2131 }
2132
2133 Record.push_back(ID);
2134}
2135
Douglas Gregorff9a6092009-04-20 20:36:09 +00002136pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2137 if (D == 0)
2138 return 0;
2139
2140 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2141 return DeclIDs[D];
2142}
2143
Douglas Gregorc34897d2009-04-09 22:27:44 +00002144void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2145 Record.push_back(Name.getNameKind());
2146 switch (Name.getNameKind()) {
2147 case DeclarationName::Identifier:
2148 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2149 break;
2150
2151 case DeclarationName::ObjCZeroArgSelector:
2152 case DeclarationName::ObjCOneArgSelector:
2153 case DeclarationName::ObjCMultiArgSelector:
2154 assert(false && "Serialization of Objective-C selectors unavailable");
2155 break;
2156
2157 case DeclarationName::CXXConstructorName:
2158 case DeclarationName::CXXDestructorName:
2159 case DeclarationName::CXXConversionFunctionName:
2160 AddTypeRef(Name.getCXXNameType(), Record);
2161 break;
2162
2163 case DeclarationName::CXXOperatorName:
2164 Record.push_back(Name.getCXXOverloadedOperator());
2165 break;
2166
2167 case DeclarationName::CXXUsingDirective:
2168 // No extra data to emit
2169 break;
2170 }
2171}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002172
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002173/// \brief Write the given substatement or subexpression to the
2174/// bitstream.
2175void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002176 RecordData Record;
2177 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002178 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002179
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002180 if (!S) {
2181 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002182 return;
2183 }
2184
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002185 Writer.Code = pch::STMT_NULL_PTR;
2186 Writer.Visit(S);
2187 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002188 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002189 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002190}
2191
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002192/// \brief Flush all of the statements that have been added to the
2193/// queue via AddStmt().
2194void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002195 RecordData Record;
2196 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002197
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002198 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002199 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002200 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002201
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002202 if (!S) {
2203 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002204 continue;
2205 }
2206
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002207 Writer.Code = pch::STMT_NULL_PTR;
2208 Writer.Visit(S);
2209 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002210 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002211 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002212
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002213 assert(N == StmtsToEmit.size() &&
2214 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002215
2216 // Note that we are at the end of a full expression. Any
2217 // expression records that follow this one are part of a different
2218 // expression.
2219 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002220 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002221 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002222
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002223 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002224 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002225}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002226
2227unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2228 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2229 "SwitchCase recorded twice");
2230 unsigned NextID = SwitchCaseIDs.size();
2231 SwitchCaseIDs[S] = NextID;
2232 return NextID;
2233}
2234
2235unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2236 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2237 "SwitchCase hasn't been seen yet");
2238 return SwitchCaseIDs[S];
2239}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002240
2241/// \brief Retrieve the ID for the given label statement, which may
2242/// or may not have been emitted yet.
2243unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2244 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2245 if (Pos != LabelIDs.end())
2246 return Pos->second;
2247
2248 unsigned NextID = LabelIDs.size();
2249 LabelIDs[S] = NextID;
2250 return NextID;
2251}