blob: 760578164b3a9d48ff5aaf3220963c945c4a8510 [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
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000661 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000662 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000663 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
664 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000665 };
666}
667
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000668void PCHStmtWriter::VisitStmt(Stmt *S) {
669}
670
671void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
672 VisitStmt(S);
673 Writer.AddSourceLocation(S->getSemiLoc(), Record);
674 Code = pch::STMT_NULL;
675}
676
677void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
678 VisitStmt(S);
679 Record.push_back(S->size());
680 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
681 CS != CSEnd; ++CS)
682 Writer.WriteSubStmt(*CS);
683 Writer.AddSourceLocation(S->getLBracLoc(), Record);
684 Writer.AddSourceLocation(S->getRBracLoc(), Record);
685 Code = pch::STMT_COMPOUND;
686}
687
688void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
689 VisitStmt(S);
690 Record.push_back(Writer.RecordSwitchCaseID(S));
691}
692
693void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
694 VisitSwitchCase(S);
695 Writer.WriteSubStmt(S->getLHS());
696 Writer.WriteSubStmt(S->getRHS());
697 Writer.WriteSubStmt(S->getSubStmt());
698 Writer.AddSourceLocation(S->getCaseLoc(), Record);
699 Code = pch::STMT_CASE;
700}
701
702void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
703 VisitSwitchCase(S);
704 Writer.WriteSubStmt(S->getSubStmt());
705 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
706 Code = pch::STMT_DEFAULT;
707}
708
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000709void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
710 VisitStmt(S);
711 Writer.AddIdentifierRef(S->getID(), Record);
712 Writer.WriteSubStmt(S->getSubStmt());
713 Writer.AddSourceLocation(S->getIdentLoc(), Record);
714 Record.push_back(Writer.GetLabelID(S));
715 Code = pch::STMT_LABEL;
716}
717
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000718void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
719 VisitStmt(S);
720 Writer.WriteSubStmt(S->getCond());
721 Writer.WriteSubStmt(S->getThen());
722 Writer.WriteSubStmt(S->getElse());
723 Writer.AddSourceLocation(S->getIfLoc(), Record);
724 Code = pch::STMT_IF;
725}
726
727void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
728 VisitStmt(S);
729 Writer.WriteSubStmt(S->getCond());
730 Writer.WriteSubStmt(S->getBody());
731 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
732 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
733 SC = SC->getNextSwitchCase())
734 Record.push_back(Writer.getSwitchCaseID(SC));
735 Code = pch::STMT_SWITCH;
736}
737
Douglas Gregora6b503f2009-04-17 00:16:09 +0000738void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
739 VisitStmt(S);
740 Writer.WriteSubStmt(S->getCond());
741 Writer.WriteSubStmt(S->getBody());
742 Writer.AddSourceLocation(S->getWhileLoc(), Record);
743 Code = pch::STMT_WHILE;
744}
745
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000746void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
747 VisitStmt(S);
748 Writer.WriteSubStmt(S->getCond());
749 Writer.WriteSubStmt(S->getBody());
750 Writer.AddSourceLocation(S->getDoLoc(), Record);
751 Code = pch::STMT_DO;
752}
753
754void PCHStmtWriter::VisitForStmt(ForStmt *S) {
755 VisitStmt(S);
756 Writer.WriteSubStmt(S->getInit());
757 Writer.WriteSubStmt(S->getCond());
758 Writer.WriteSubStmt(S->getInc());
759 Writer.WriteSubStmt(S->getBody());
760 Writer.AddSourceLocation(S->getForLoc(), Record);
761 Code = pch::STMT_FOR;
762}
763
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000764void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
765 VisitStmt(S);
766 Record.push_back(Writer.GetLabelID(S->getLabel()));
767 Writer.AddSourceLocation(S->getGotoLoc(), Record);
768 Writer.AddSourceLocation(S->getLabelLoc(), Record);
769 Code = pch::STMT_GOTO;
770}
771
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000772void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
773 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000774 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000775 Writer.WriteSubStmt(S->getTarget());
776 Code = pch::STMT_INDIRECT_GOTO;
777}
778
Douglas Gregora6b503f2009-04-17 00:16:09 +0000779void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
780 VisitStmt(S);
781 Writer.AddSourceLocation(S->getContinueLoc(), Record);
782 Code = pch::STMT_CONTINUE;
783}
784
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000785void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
786 VisitStmt(S);
787 Writer.AddSourceLocation(S->getBreakLoc(), Record);
788 Code = pch::STMT_BREAK;
789}
790
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000791void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
792 VisitStmt(S);
793 Writer.WriteSubStmt(S->getRetValue());
794 Writer.AddSourceLocation(S->getReturnLoc(), Record);
795 Code = pch::STMT_RETURN;
796}
797
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000798void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
799 VisitStmt(S);
800 Writer.AddSourceLocation(S->getStartLoc(), Record);
801 Writer.AddSourceLocation(S->getEndLoc(), Record);
802 DeclGroupRef DG = S->getDeclGroup();
803 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
804 Writer.AddDeclRef(*D, Record);
805 Code = pch::STMT_DECL;
806}
807
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000808void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
809 VisitStmt(S);
810 Record.push_back(S->getNumOutputs());
811 Record.push_back(S->getNumInputs());
812 Record.push_back(S->getNumClobbers());
813 Writer.AddSourceLocation(S->getAsmLoc(), Record);
814 Writer.AddSourceLocation(S->getRParenLoc(), Record);
815 Record.push_back(S->isVolatile());
816 Record.push_back(S->isSimple());
817 Writer.WriteSubStmt(S->getAsmString());
818
819 // Outputs
820 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
821 Writer.AddString(S->getOutputName(I), Record);
822 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
823 Writer.WriteSubStmt(S->getOutputExpr(I));
824 }
825
826 // Inputs
827 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
828 Writer.AddString(S->getInputName(I), Record);
829 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
830 Writer.WriteSubStmt(S->getInputExpr(I));
831 }
832
833 // Clobbers
834 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
835 Writer.WriteSubStmt(S->getClobber(I));
836
837 Code = pch::STMT_ASM;
838}
839
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000840void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000841 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000842 Writer.AddTypeRef(E->getType(), Record);
843 Record.push_back(E->isTypeDependent());
844 Record.push_back(E->isValueDependent());
845}
846
Douglas Gregore2f37202009-04-14 21:55:33 +0000847void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
848 VisitExpr(E);
849 Writer.AddSourceLocation(E->getLocation(), Record);
850 Record.push_back(E->getIdentType()); // FIXME: stable encoding
851 Code = pch::EXPR_PREDEFINED;
852}
853
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000854void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
855 VisitExpr(E);
856 Writer.AddDeclRef(E->getDecl(), Record);
857 Writer.AddSourceLocation(E->getLocation(), Record);
858 Code = pch::EXPR_DECL_REF;
859}
860
861void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
862 VisitExpr(E);
863 Writer.AddSourceLocation(E->getLocation(), Record);
864 Writer.AddAPInt(E->getValue(), Record);
865 Code = pch::EXPR_INTEGER_LITERAL;
866}
867
Douglas Gregore2f37202009-04-14 21:55:33 +0000868void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
869 VisitExpr(E);
870 Writer.AddAPFloat(E->getValue(), Record);
871 Record.push_back(E->isExact());
872 Writer.AddSourceLocation(E->getLocation(), Record);
873 Code = pch::EXPR_FLOATING_LITERAL;
874}
875
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000876void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
877 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000878 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000879 Code = pch::EXPR_IMAGINARY_LITERAL;
880}
881
Douglas Gregor596e0932009-04-15 16:35:07 +0000882void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
883 VisitExpr(E);
884 Record.push_back(E->getByteLength());
885 Record.push_back(E->getNumConcatenated());
886 Record.push_back(E->isWide());
887 // FIXME: String data should be stored as a blob at the end of the
888 // StringLiteral. However, we can't do so now because we have no
889 // provision for coping with abbreviations when we're jumping around
890 // the PCH file during deserialization.
891 Record.insert(Record.end(),
892 E->getStrData(), E->getStrData() + E->getByteLength());
893 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
894 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
895 Code = pch::EXPR_STRING_LITERAL;
896}
897
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000898void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
899 VisitExpr(E);
900 Record.push_back(E->getValue());
901 Writer.AddSourceLocation(E->getLoc(), Record);
902 Record.push_back(E->isWide());
903 Code = pch::EXPR_CHARACTER_LITERAL;
904}
905
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000906void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
907 VisitExpr(E);
908 Writer.AddSourceLocation(E->getLParen(), Record);
909 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000910 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000911 Code = pch::EXPR_PAREN;
912}
913
Douglas Gregor12d74052009-04-15 15:58:59 +0000914void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
915 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000916 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000917 Record.push_back(E->getOpcode()); // FIXME: stable encoding
918 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
919 Code = pch::EXPR_UNARY_OPERATOR;
920}
921
922void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
923 VisitExpr(E);
924 Record.push_back(E->isSizeOf());
925 if (E->isArgumentType())
926 Writer.AddTypeRef(E->getArgumentType(), Record);
927 else {
928 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000929 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000930 }
931 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
932 Writer.AddSourceLocation(E->getRParenLoc(), Record);
933 Code = pch::EXPR_SIZEOF_ALIGN_OF;
934}
935
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000936void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
937 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000938 Writer.WriteSubStmt(E->getLHS());
939 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000940 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
941 Code = pch::EXPR_ARRAY_SUBSCRIPT;
942}
943
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000944void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
945 VisitExpr(E);
946 Record.push_back(E->getNumArgs());
947 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000948 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000949 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
950 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000951 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000952 Code = pch::EXPR_CALL;
953}
954
955void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
956 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000957 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000958 Writer.AddDeclRef(E->getMemberDecl(), Record);
959 Writer.AddSourceLocation(E->getMemberLoc(), Record);
960 Record.push_back(E->isArrow());
961 Code = pch::EXPR_MEMBER;
962}
963
Douglas Gregora151ba42009-04-14 23:32:43 +0000964void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
965 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000966 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000967}
968
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000969void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
970 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000971 Writer.WriteSubStmt(E->getLHS());
972 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000973 Record.push_back(E->getOpcode()); // FIXME: stable encoding
974 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
975 Code = pch::EXPR_BINARY_OPERATOR;
976}
977
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000978void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
979 VisitBinaryOperator(E);
980 Writer.AddTypeRef(E->getComputationLHSType(), Record);
981 Writer.AddTypeRef(E->getComputationResultType(), Record);
982 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
983}
984
985void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
986 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000987 Writer.WriteSubStmt(E->getCond());
988 Writer.WriteSubStmt(E->getLHS());
989 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000990 Code = pch::EXPR_CONDITIONAL_OPERATOR;
991}
992
Douglas Gregora151ba42009-04-14 23:32:43 +0000993void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
994 VisitCastExpr(E);
995 Record.push_back(E->isLvalueCast());
996 Code = pch::EXPR_IMPLICIT_CAST;
997}
998
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000999void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1000 VisitCastExpr(E);
1001 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1002}
1003
1004void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1005 VisitExplicitCastExpr(E);
1006 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1007 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1008 Code = pch::EXPR_CSTYLE_CAST;
1009}
1010
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001011void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1012 VisitExpr(E);
1013 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001014 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001015 Record.push_back(E->isFileScope());
1016 Code = pch::EXPR_COMPOUND_LITERAL;
1017}
1018
Douglas Gregorec0b8292009-04-15 23:02:49 +00001019void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1020 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001021 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001022 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1023 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1024 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1025}
1026
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001027void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1028 VisitExpr(E);
1029 Record.push_back(E->getNumInits());
1030 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001031 Writer.WriteSubStmt(E->getInit(I));
1032 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001033 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1034 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1035 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1036 Record.push_back(E->hadArrayRangeDesignator());
1037 Code = pch::EXPR_INIT_LIST;
1038}
1039
1040void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1041 VisitExpr(E);
1042 Record.push_back(E->getNumSubExprs());
1043 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001044 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001045 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1046 Record.push_back(E->usesGNUSyntax());
1047 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1048 DEnd = E->designators_end();
1049 D != DEnd; ++D) {
1050 if (D->isFieldDesignator()) {
1051 if (FieldDecl *Field = D->getField()) {
1052 Record.push_back(pch::DESIG_FIELD_DECL);
1053 Writer.AddDeclRef(Field, Record);
1054 } else {
1055 Record.push_back(pch::DESIG_FIELD_NAME);
1056 Writer.AddIdentifierRef(D->getFieldName(), Record);
1057 }
1058 Writer.AddSourceLocation(D->getDotLoc(), Record);
1059 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1060 } else if (D->isArrayDesignator()) {
1061 Record.push_back(pch::DESIG_ARRAY);
1062 Record.push_back(D->getFirstExprIndex());
1063 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1064 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1065 } else {
1066 assert(D->isArrayRangeDesignator() && "Unknown designator");
1067 Record.push_back(pch::DESIG_ARRAY_RANGE);
1068 Record.push_back(D->getFirstExprIndex());
1069 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1070 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1071 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1072 }
1073 }
1074 Code = pch::EXPR_DESIGNATED_INIT;
1075}
1076
1077void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1078 VisitExpr(E);
1079 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1080}
1081
Douglas Gregorec0b8292009-04-15 23:02:49 +00001082void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1083 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001084 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001085 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1086 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1087 Code = pch::EXPR_VA_ARG;
1088}
1089
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001090void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1091 VisitExpr(E);
1092 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1093 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1094 Record.push_back(Writer.GetLabelID(E->getLabel()));
1095 Code = pch::EXPR_ADDR_LABEL;
1096}
1097
Douglas Gregoreca12f62009-04-17 19:05:30 +00001098void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1099 VisitExpr(E);
1100 Writer.WriteSubStmt(E->getSubStmt());
1101 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1102 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1103 Code = pch::EXPR_STMT;
1104}
1105
Douglas Gregor209d4622009-04-15 23:33:31 +00001106void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1107 VisitExpr(E);
1108 Writer.AddTypeRef(E->getArgType1(), Record);
1109 Writer.AddTypeRef(E->getArgType2(), Record);
1110 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1111 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1112 Code = pch::EXPR_TYPES_COMPATIBLE;
1113}
1114
1115void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1116 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001117 Writer.WriteSubStmt(E->getCond());
1118 Writer.WriteSubStmt(E->getLHS());
1119 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001120 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1121 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1122 Code = pch::EXPR_CHOOSE;
1123}
1124
1125void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1126 VisitExpr(E);
1127 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1128 Code = pch::EXPR_GNU_NULL;
1129}
1130
Douglas Gregor725e94b2009-04-16 00:01:45 +00001131void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1132 VisitExpr(E);
1133 Record.push_back(E->getNumSubExprs());
1134 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001135 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001136 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1137 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1138 Code = pch::EXPR_SHUFFLE_VECTOR;
1139}
1140
Douglas Gregore246b742009-04-17 19:21:43 +00001141void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1142 VisitExpr(E);
1143 Writer.AddDeclRef(E->getBlockDecl(), Record);
1144 Record.push_back(E->hasBlockDeclRefExprs());
1145 Code = pch::EXPR_BLOCK;
1146}
1147
Douglas Gregor725e94b2009-04-16 00:01:45 +00001148void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1149 VisitExpr(E);
1150 Writer.AddDeclRef(E->getDecl(), Record);
1151 Writer.AddSourceLocation(E->getLocation(), Record);
1152 Record.push_back(E->isByRef());
1153 Code = pch::EXPR_BLOCK_DECL_REF;
1154}
1155
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001156//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001157// Objective-C Expressions and Statements.
1158//===----------------------------------------------------------------------===//
1159
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001160void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1161 VisitExpr(E);
1162 Writer.WriteSubStmt(E->getString());
1163 Writer.AddSourceLocation(E->getAtLoc(), Record);
1164 Code = pch::EXPR_OBJC_STRING_LITERAL;
1165}
1166
Chris Lattner80f83c62009-04-22 05:57:30 +00001167void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1168 VisitExpr(E);
1169 Writer.AddTypeRef(E->getEncodedType(), Record);
1170 Writer.AddSourceLocation(E->getAtLoc(), Record);
1171 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1172 Code = pch::EXPR_OBJC_ENCODE;
1173}
1174
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001175void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1176 VisitExpr(E);
Chris Lattner9b9f2352009-04-22 06:40:03 +00001177 assert(0 && "Can't write a selector yet!");
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001178 // FIXME! Write selectors.
1179 //Writer.WriteSubStmt(E->getSelector());
1180 Writer.AddSourceLocation(E->getAtLoc(), Record);
1181 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1182 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1183}
1184
1185void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1186 VisitExpr(E);
1187 Writer.AddDeclRef(E->getProtocol(), Record);
1188 Writer.AddSourceLocation(E->getAtLoc(), Record);
1189 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1190 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1191}
1192
Chris Lattner80f83c62009-04-22 05:57:30 +00001193
1194//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001195// PCHWriter Implementation
1196//===----------------------------------------------------------------------===//
1197
Douglas Gregorb5887f32009-04-10 21:16:55 +00001198/// \brief Write the target triple (e.g., i686-apple-darwin9).
1199void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1200 using namespace llvm;
1201 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1202 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001204 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001205
1206 RecordData Record;
1207 Record.push_back(pch::TARGET_TRIPLE);
1208 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001209 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001210}
1211
1212/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001213void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1214 RecordData Record;
1215 Record.push_back(LangOpts.Trigraphs);
1216 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1217 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1218 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1219 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1220 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1221 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1222 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1223 Record.push_back(LangOpts.C99); // C99 Support
1224 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1225 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1226 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1227 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1228 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1229
1230 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1231 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1232 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1233
1234 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1235 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1236 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1237 Record.push_back(LangOpts.LaxVectorConversions);
1238 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1239
1240 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1241 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1242 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1243
1244 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1245 // by locks.
1246 Record.push_back(LangOpts.Blocks); // block extension to C
1247 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1248 // they are unused.
1249 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1250 // (modulo the platform support).
1251
1252 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1253 // signed integer arithmetic overflows.
1254
1255 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1256 // may be ripped out at any time.
1257
1258 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1259 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1260 // defined.
1261 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1262 // opposed to __DYNAMIC__).
1263 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1264
1265 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1266 // used (instead of C99 semantics).
1267 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1268 Record.push_back(LangOpts.getGCMode());
1269 Record.push_back(LangOpts.getVisibilityMode());
1270 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001271 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001272}
1273
Douglas Gregorab1cef72009-04-10 03:52:48 +00001274//===----------------------------------------------------------------------===//
1275// Source Manager Serialization
1276//===----------------------------------------------------------------------===//
1277
1278/// \brief Create an abbreviation for the SLocEntry that refers to a
1279/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001280static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001281 using namespace llvm;
1282 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1283 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001289 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001290}
1291
1292/// \brief Create an abbreviation for the SLocEntry that refers to a
1293/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001294static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001295 using namespace llvm;
1296 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1297 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001303 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001304}
1305
1306/// \brief Create an abbreviation for the SLocEntry that refers to a
1307/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001308static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001309 using namespace llvm;
1310 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1311 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001313 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001314}
1315
1316/// \brief Create an abbreviation for the SLocEntry that refers to an
1317/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001318static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001319 using namespace llvm;
1320 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1321 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1324 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001327 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001328}
1329
1330/// \brief Writes the block containing the serialized form of the
1331/// source manager.
1332///
1333/// TODO: We should probably use an on-disk hash table (stored in a
1334/// blob), indexed based on the file name, so that we only create
1335/// entries for files that we actually need. In the common case (no
1336/// errors), we probably won't have to create file entries for any of
1337/// the files in the AST.
1338void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001339 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001340 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001341
1342 // Abbreviations for the various kinds of source-location entries.
1343 int SLocFileAbbrv = -1;
1344 int SLocBufferAbbrv = -1;
1345 int SLocBufferBlobAbbrv = -1;
1346 int SLocInstantiationAbbrv = -1;
1347
1348 // Write out the source location entry table. We skip the first
1349 // entry, which is always the same dummy entry.
1350 RecordData Record;
1351 for (SourceManager::sloc_entry_iterator
1352 SLoc = SourceMgr.sloc_entry_begin() + 1,
1353 SLocEnd = SourceMgr.sloc_entry_end();
1354 SLoc != SLocEnd; ++SLoc) {
1355 // Figure out which record code to use.
1356 unsigned Code;
1357 if (SLoc->isFile()) {
1358 if (SLoc->getFile().getContentCache()->Entry)
1359 Code = pch::SM_SLOC_FILE_ENTRY;
1360 else
1361 Code = pch::SM_SLOC_BUFFER_ENTRY;
1362 } else
1363 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1364 Record.push_back(Code);
1365
1366 Record.push_back(SLoc->getOffset());
1367 if (SLoc->isFile()) {
1368 const SrcMgr::FileInfo &File = SLoc->getFile();
1369 Record.push_back(File.getIncludeLoc().getRawEncoding());
1370 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001371 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001372
1373 const SrcMgr::ContentCache *Content = File.getContentCache();
1374 if (Content->Entry) {
1375 // The source location entry is a file. The blob associated
1376 // with this entry is the file name.
1377 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001378 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1379 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001380 Content->Entry->getName(),
1381 strlen(Content->Entry->getName()));
1382 } else {
1383 // The source location entry is a buffer. The blob associated
1384 // with this entry contains the contents of the buffer.
1385 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001386 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1387 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001388 }
1389
1390 // We add one to the size so that we capture the trailing NULL
1391 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1392 // the reader side).
1393 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1394 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001395 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001396 Record.clear();
1397 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001398 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001399 Buffer->getBufferStart(),
1400 Buffer->getBufferSize() + 1);
1401 }
1402 } else {
1403 // The source location entry is an instantiation.
1404 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1405 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1406 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1407 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1408
Douglas Gregor364e5802009-04-15 18:05:10 +00001409 // Compute the token length for this macro expansion.
1410 unsigned NextOffset = SourceMgr.getNextOffset();
1411 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1412 if (++NextSLoc != SLocEnd)
1413 NextOffset = NextSLoc->getOffset();
1414 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1415
Douglas Gregorab1cef72009-04-10 03:52:48 +00001416 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001417 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1418 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001419 }
1420
1421 Record.clear();
1422 }
1423
Douglas Gregor635f97f2009-04-13 16:31:14 +00001424 // Write the line table.
1425 if (SourceMgr.hasLineTable()) {
1426 LineTableInfo &LineTable = SourceMgr.getLineTable();
1427
1428 // Emit the file names
1429 Record.push_back(LineTable.getNumFilenames());
1430 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1431 // Emit the file name
1432 const char *Filename = LineTable.getFilename(I);
1433 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1434 Record.push_back(FilenameLen);
1435 if (FilenameLen)
1436 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1437 }
1438
1439 // Emit the line entries
1440 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1441 L != LEnd; ++L) {
1442 // Emit the file ID
1443 Record.push_back(L->first);
1444
1445 // Emit the line entries
1446 Record.push_back(L->second.size());
1447 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1448 LEEnd = L->second.end();
1449 LE != LEEnd; ++LE) {
1450 Record.push_back(LE->FileOffset);
1451 Record.push_back(LE->LineNo);
1452 Record.push_back(LE->FilenameID);
1453 Record.push_back((unsigned)LE->FileKind);
1454 Record.push_back(LE->IncludeOffset);
1455 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001456 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001457 }
1458 }
1459
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001460 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001461}
1462
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001463/// \brief Writes the block containing the serialized form of the
1464/// preprocessor.
1465///
Chris Lattner850eabd2009-04-10 18:08:30 +00001466void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001467 // Enter the preprocessor block.
Douglas Gregorc713da92009-04-21 22:25:48 +00001468 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner84b04f12009-04-10 17:16:57 +00001469
Chris Lattner1b094952009-04-10 18:00:12 +00001470 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1471 // FIXME: use diagnostics subsystem for localization etc.
1472 if (PP.SawDateOrTime())
1473 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001474
Chris Lattner1b094952009-04-10 18:00:12 +00001475 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001476
Chris Lattner4b21c202009-04-13 01:29:17 +00001477 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1478 if (PP.getCounterValue() != 0) {
1479 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001480 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001481 Record.clear();
1482 }
1483
Chris Lattner1b094952009-04-10 18:00:12 +00001484 // Loop over all the macro definitions that are live at the end of the file,
1485 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001486 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1487 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001488 // FIXME: This emits macros in hash table order, we should do it in a stable
1489 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001490 MacroInfo *MI = I->second;
1491
1492 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1493 // been redefined by the header (in which case they are not isBuiltinMacro).
1494 if (MI->isBuiltinMacro())
1495 continue;
1496
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001497 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001498 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001499 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001500 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1501 Record.push_back(MI->isUsed());
1502
1503 unsigned Code;
1504 if (MI->isObjectLike()) {
1505 Code = pch::PP_MACRO_OBJECT_LIKE;
1506 } else {
1507 Code = pch::PP_MACRO_FUNCTION_LIKE;
1508
1509 Record.push_back(MI->isC99Varargs());
1510 Record.push_back(MI->isGNUVarargs());
1511 Record.push_back(MI->getNumArgs());
1512 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1513 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001514 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001515 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001516 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001517 Record.clear();
1518
Chris Lattner850eabd2009-04-10 18:08:30 +00001519 // Emit the tokens array.
1520 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1521 // Note that we know that the preprocessor does not have any annotation
1522 // tokens in it because they are created by the parser, and thus can't be
1523 // in a macro definition.
1524 const Token &Tok = MI->getReplacementToken(TokNo);
1525
1526 Record.push_back(Tok.getLocation().getRawEncoding());
1527 Record.push_back(Tok.getLength());
1528
Chris Lattner850eabd2009-04-10 18:08:30 +00001529 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1530 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001531 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001532
1533 // FIXME: Should translate token kind to a stable encoding.
1534 Record.push_back(Tok.getKind());
1535 // FIXME: Should translate token flags to a stable encoding.
1536 Record.push_back(Tok.getFlags());
1537
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001538 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001539 Record.clear();
1540 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001541 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001542 }
1543
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001544 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001545}
1546
1547
Douglas Gregorc34897d2009-04-09 22:27:44 +00001548/// \brief Write the representation of a type to the PCH stream.
1549void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001550 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001551 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001552 ID = NextTypeID++;
1553
1554 // Record the offset for this type.
1555 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001556 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001557 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1558 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001559 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001560 }
1561
1562 RecordData Record;
1563
1564 // Emit the type's representation.
1565 PCHTypeWriter W(*this, Record);
1566 switch (T->getTypeClass()) {
1567 // For all of the concrete, non-dependent types, call the
1568 // appropriate visitor function.
1569#define TYPE(Class, Base) \
1570 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1571#define ABSTRACT_TYPE(Class, Base)
1572#define DEPENDENT_TYPE(Class, Base)
1573#include "clang/AST/TypeNodes.def"
1574
1575 // For all of the dependent type nodes (which only occur in C++
1576 // templates), produce an error.
1577#define TYPE(Class, Base)
1578#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1579#include "clang/AST/TypeNodes.def"
1580 assert(false && "Cannot serialize dependent type nodes");
1581 break;
1582 }
1583
1584 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001585 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001586
1587 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001588 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001589}
1590
1591/// \brief Write a block containing all of the types.
1592void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001593 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001594 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001595
1596 // Emit all of the types in the ASTContext
1597 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1598 TEnd = Context.getTypes().end();
1599 T != TEnd; ++T) {
1600 // Builtin types are never serialized.
1601 if (isa<BuiltinType>(*T))
1602 continue;
1603
1604 WriteType(*T);
1605 }
1606
1607 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001608 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001609}
1610
1611/// \brief Write the block containing all of the declaration IDs
1612/// lexically declared within the given DeclContext.
1613///
1614/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1615/// bistream, or 0 if no block was written.
1616uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1617 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001618 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001619 return 0;
1620
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001621 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001622 RecordData Record;
1623 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1624 DEnd = DC->decls_end(Context);
1625 D != DEnd; ++D)
1626 AddDeclRef(*D, Record);
1627
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001628 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001629 return Offset;
1630}
1631
1632/// \brief Write the block containing all of the declaration IDs
1633/// visible from the given DeclContext.
1634///
1635/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1636/// bistream, or 0 if no block was written.
1637uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1638 DeclContext *DC) {
1639 if (DC->getPrimaryContext() != DC)
1640 return 0;
1641
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001642 // Since there is no name lookup into functions or methods, and we
1643 // perform name lookup for the translation unit via the
1644 // IdentifierInfo chains, don't bother to build a
1645 // visible-declarations table for these entities.
1646 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001647 return 0;
1648
Douglas Gregorc34897d2009-04-09 22:27:44 +00001649 // Force the DeclContext to build a its name-lookup table.
1650 DC->lookup(Context, DeclarationName());
1651
1652 // Serialize the contents of the mapping used for lookup. Note that,
1653 // although we have two very different code paths, the serialized
1654 // representation is the same for both cases: a declaration name,
1655 // followed by a size, followed by references to the visible
1656 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001657 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001658 RecordData Record;
1659 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001660 if (!Map)
1661 return 0;
1662
Douglas Gregorc34897d2009-04-09 22:27:44 +00001663 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1664 D != DEnd; ++D) {
1665 AddDeclarationName(D->first, Record);
1666 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1667 Record.push_back(Result.second - Result.first);
1668 for(; Result.first != Result.second; ++Result.first)
1669 AddDeclRef(*Result.first, Record);
1670 }
1671
1672 if (Record.size() == 0)
1673 return 0;
1674
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001675 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001676 return Offset;
1677}
1678
1679/// \brief Write a block containing all of the declarations.
1680void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001681 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001682 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001683
1684 // Emit all of the declarations.
1685 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001686 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001687 while (!DeclsToEmit.empty()) {
1688 // Pull the next declaration off the queue
1689 Decl *D = DeclsToEmit.front();
1690 DeclsToEmit.pop();
1691
1692 // If this declaration is also a DeclContext, write blocks for the
1693 // declarations that lexically stored inside its context and those
1694 // declarations that are visible from its context. These blocks
1695 // are written before the declaration itself so that we can put
1696 // their offsets into the record for the declaration.
1697 uint64_t LexicalOffset = 0;
1698 uint64_t VisibleOffset = 0;
1699 DeclContext *DC = dyn_cast<DeclContext>(D);
1700 if (DC) {
1701 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1702 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1703 }
1704
1705 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001706 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001707 if (ID == 0)
1708 ID = DeclIDs.size();
1709
1710 unsigned Index = ID - 1;
1711
1712 // Record the offset for this declaration
1713 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001714 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001715 else if (DeclOffsets.size() < Index) {
1716 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001717 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001718 }
1719
1720 // Build and emit a record for this declaration
1721 Record.clear();
1722 W.Code = (pch::DeclCode)0;
1723 W.Visit(D);
1724 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001725 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001726 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001727
Douglas Gregor1c507882009-04-15 21:30:51 +00001728 // If the declaration had any attributes, write them now.
1729 if (D->hasAttrs())
1730 WriteAttributeRecord(D->getAttrs());
1731
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001732 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001733 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001734
Douglas Gregor631f6c62009-04-14 00:24:19 +00001735 // Note external declarations so that we can add them to a record
1736 // in the PCH file later.
1737 if (isa<FileScopeAsmDecl>(D))
1738 ExternalDefinitions.push_back(ID);
1739 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1740 if (// Non-static file-scope variables with initializers or that
1741 // are tentative definitions.
1742 (Var->isFileVarDecl() &&
1743 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1744 // Out-of-line definitions of static data members (C++).
1745 (Var->getDeclContext()->isRecord() &&
1746 !Var->getLexicalDeclContext()->isRecord() &&
1747 Var->getStorageClass() == VarDecl::Static))
1748 ExternalDefinitions.push_back(ID);
1749 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00001750 if (Func->isThisDeclarationADefinition())
Douglas Gregor631f6c62009-04-14 00:24:19 +00001751 ExternalDefinitions.push_back(ID);
1752 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001753 }
1754
1755 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001756 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001757}
1758
Douglas Gregorff9a6092009-04-20 20:36:09 +00001759namespace {
1760class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1761 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001762 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001763
1764public:
1765 typedef const IdentifierInfo* key_type;
1766 typedef key_type key_type_ref;
1767
1768 typedef pch::IdentID data_type;
1769 typedef data_type data_type_ref;
1770
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001771 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1772 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001773
1774 static unsigned ComputeHash(const IdentifierInfo* II) {
1775 return clang::BernsteinHash(II->getName());
1776 }
1777
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001778 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001779 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1780 pch::IdentID ID) {
1781 unsigned KeyLen = strlen(II->getName()) + 1;
1782 clang::io::Emit16(Out, KeyLen);
Douglas Gregorc713da92009-04-21 22:25:48 +00001783 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1784 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001785 if (II->hasMacroDefinition() &&
1786 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1787 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001788 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1789 DEnd = IdentifierResolver::end();
1790 D != DEnd; ++D)
1791 DataLen += sizeof(pch::DeclID);
Douglas Gregorc713da92009-04-21 22:25:48 +00001792 clang::io::Emit16(Out, DataLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001793 return std::make_pair(KeyLen, DataLen);
1794 }
1795
1796 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1797 unsigned KeyLen) {
1798 // Record the location of the key data. This is used when generating
1799 // the mapping from persistent IDs to strings.
1800 Writer.SetIdentifierOffset(II, Out.tell());
1801 Out.write(II->getName(), KeyLen);
1802 }
1803
1804 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1805 pch::IdentID ID, unsigned) {
1806 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001807 bool hasMacroDefinition =
1808 II->hasMacroDefinition() &&
1809 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001810 Bits = Bits | (uint32_t)II->getTokenID();
1811 Bits = (Bits << 8) | (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001812 Bits = (Bits << 10) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001813 Bits = (Bits << 1) | II->isExtensionToken();
1814 Bits = (Bits << 1) | II->isPoisoned();
1815 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1816 clang::io::Emit32(Out, Bits);
1817 clang::io::Emit32(Out, ID);
1818
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001819 if (hasMacroDefinition)
1820 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1821
Douglas Gregorc713da92009-04-21 22:25:48 +00001822 // Emit the declaration IDs in reverse order, because the
1823 // IdentifierResolver provides the declarations as they would be
1824 // visible (e.g., the function "stat" would come before the struct
1825 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1826 // adds declarations to the end of the list (so we need to see the
1827 // struct "status" before the function "status").
1828 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1829 IdentifierResolver::end());
1830 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1831 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001832 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001833 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001834 }
1835};
1836} // end anonymous namespace
1837
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001838/// \brief Write the identifier table into the PCH file.
1839///
1840/// The identifier table consists of a blob containing string data
1841/// (the actual identifiers themselves) and a separate "offsets" index
1842/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001843void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001844 using namespace llvm;
1845
1846 // Create and write out the blob that contains the identifier
1847 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001848 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001849 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001850 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1851
1852 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001853 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1854 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1855 ID != IDEnd; ++ID) {
1856 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorff9a6092009-04-20 20:36:09 +00001857 Generator.insert(ID->first, ID->second);
1858 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001859
Douglas Gregorff9a6092009-04-20 20:36:09 +00001860 // Create the on-disk hash table in a buffer.
1861 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001862 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001863 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001864 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001865 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc713da92009-04-21 22:25:48 +00001866 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001867 }
1868
1869 // Create a blob abbreviation
1870 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1871 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001873 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001874 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001875
1876 // Write the identifier table
1877 RecordData Record;
1878 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001879 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001880 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1881 &IdentifierTable.front(),
1882 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001883 }
1884
1885 // Write the offsets table for identifier IDs.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001886 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001887}
1888
Douglas Gregor1c507882009-04-15 21:30:51 +00001889/// \brief Write a record containing the given attributes.
1890void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1891 RecordData Record;
1892 for (; Attr; Attr = Attr->getNext()) {
1893 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1894 Record.push_back(Attr->isInherited());
1895 switch (Attr->getKind()) {
1896 case Attr::Alias:
1897 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1898 break;
1899
1900 case Attr::Aligned:
1901 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1902 break;
1903
1904 case Attr::AlwaysInline:
1905 break;
1906
1907 case Attr::AnalyzerNoReturn:
1908 break;
1909
1910 case Attr::Annotate:
1911 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1912 break;
1913
1914 case Attr::AsmLabel:
1915 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1916 break;
1917
1918 case Attr::Blocks:
1919 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1920 break;
1921
1922 case Attr::Cleanup:
1923 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1924 break;
1925
1926 case Attr::Const:
1927 break;
1928
1929 case Attr::Constructor:
1930 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1931 break;
1932
1933 case Attr::DLLExport:
1934 case Attr::DLLImport:
1935 case Attr::Deprecated:
1936 break;
1937
1938 case Attr::Destructor:
1939 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1940 break;
1941
1942 case Attr::FastCall:
1943 break;
1944
1945 case Attr::Format: {
1946 const FormatAttr *Format = cast<FormatAttr>(Attr);
1947 AddString(Format->getType(), Record);
1948 Record.push_back(Format->getFormatIdx());
1949 Record.push_back(Format->getFirstArg());
1950 break;
1951 }
1952
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001953 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001954 case Attr::IBOutletKind:
1955 case Attr::NoReturn:
1956 case Attr::NoThrow:
1957 case Attr::Nodebug:
1958 case Attr::Noinline:
1959 break;
1960
1961 case Attr::NonNull: {
1962 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1963 Record.push_back(NonNull->size());
1964 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1965 break;
1966 }
1967
1968 case Attr::ObjCException:
1969 case Attr::ObjCNSObject:
1970 case Attr::Overloadable:
1971 break;
1972
1973 case Attr::Packed:
1974 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1975 break;
1976
1977 case Attr::Pure:
1978 break;
1979
1980 case Attr::Regparm:
1981 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1982 break;
1983
1984 case Attr::Section:
1985 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1986 break;
1987
1988 case Attr::StdCall:
1989 case Attr::TransparentUnion:
1990 case Attr::Unavailable:
1991 case Attr::Unused:
1992 case Attr::Used:
1993 break;
1994
1995 case Attr::Visibility:
1996 // FIXME: stable encoding
1997 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1998 break;
1999
2000 case Attr::WarnUnusedResult:
2001 case Attr::Weak:
2002 case Attr::WeakImport:
2003 break;
2004 }
2005 }
2006
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002007 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002008}
2009
2010void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2011 Record.push_back(Str.size());
2012 Record.insert(Record.end(), Str.begin(), Str.end());
2013}
2014
Douglas Gregorff9a6092009-04-20 20:36:09 +00002015/// \brief Note that the identifier II occurs at the given offset
2016/// within the identifier table.
2017void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
2018 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
2019}
2020
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002021PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002022 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
2023 NumStatements(0), NumMacros(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002024
Douglas Gregor87887da2009-04-20 15:53:59 +00002025void PCHWriter::WritePCH(Sema &SemaRef) {
2026 ASTContext &Context = SemaRef.Context;
2027 Preprocessor &PP = SemaRef.PP;
2028
Douglas Gregorc34897d2009-04-09 22:27:44 +00002029 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002030 Stream.Emit((unsigned)'C', 8);
2031 Stream.Emit((unsigned)'P', 8);
2032 Stream.Emit((unsigned)'C', 8);
2033 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002034
2035 // The translation unit is the first declaration we'll emit.
2036 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2037 DeclsToEmit.push(Context.getTranslationUnitDecl());
2038
2039 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002040 RecordData Record;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002041 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002042 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002043 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00002044 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002045 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002046 WriteTypesBlock(Context);
2047 WriteDeclsBlock(Context);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002048 WriteIdentifierTable(PP);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002049 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
2050 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregore01ad442009-04-18 05:55:16 +00002051
2052 // Write the record of special types.
2053 Record.clear();
2054 AddTypeRef(Context.getBuiltinVaListType(), Record);
2055 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2056
Douglas Gregor631f6c62009-04-14 00:24:19 +00002057 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002058 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor456e0952009-04-17 22:13:46 +00002059
2060 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002061 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002062 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002063 Record.push_back(NumMacros);
Douglas Gregor456e0952009-04-17 22:13:46 +00002064 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002065 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002066}
2067
2068void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2069 Record.push_back(Loc.getRawEncoding());
2070}
2071
2072void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2073 Record.push_back(Value.getBitWidth());
2074 unsigned N = Value.getNumWords();
2075 const uint64_t* Words = Value.getRawData();
2076 for (unsigned I = 0; I != N; ++I)
2077 Record.push_back(Words[I]);
2078}
2079
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002080void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2081 Record.push_back(Value.isUnsigned());
2082 AddAPInt(Value, Record);
2083}
2084
Douglas Gregore2f37202009-04-14 21:55:33 +00002085void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2086 AddAPInt(Value.bitcastToAPInt(), Record);
2087}
2088
Douglas Gregorc34897d2009-04-09 22:27:44 +00002089void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002090 if (II == 0) {
2091 Record.push_back(0);
2092 return;
2093 }
2094
2095 pch::IdentID &ID = IdentifierIDs[II];
2096 if (ID == 0)
2097 ID = IdentifierIDs.size();
2098
2099 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002100}
2101
2102void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2103 if (T.isNull()) {
2104 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2105 return;
2106 }
2107
2108 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002109 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002110 switch (BT->getKind()) {
2111 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2112 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2113 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2114 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2115 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2116 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2117 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2118 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2119 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2120 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2121 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2122 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2123 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2124 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2125 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2126 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2127 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2128 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2129 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2130 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2131 }
2132
2133 Record.push_back((ID << 3) | T.getCVRQualifiers());
2134 return;
2135 }
2136
Douglas Gregorac8f2802009-04-10 17:25:41 +00002137 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002138 if (ID == 0) // we haven't seen this type before
2139 ID = NextTypeID++;
2140
2141 // Encode the type qualifiers in the type reference.
2142 Record.push_back((ID << 3) | T.getCVRQualifiers());
2143}
2144
2145void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2146 if (D == 0) {
2147 Record.push_back(0);
2148 return;
2149 }
2150
Douglas Gregorac8f2802009-04-10 17:25:41 +00002151 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002152 if (ID == 0) {
2153 // We haven't seen this declaration before. Give it a new ID and
2154 // enqueue it in the list of declarations to emit.
2155 ID = DeclIDs.size();
2156 DeclsToEmit.push(const_cast<Decl *>(D));
2157 }
2158
2159 Record.push_back(ID);
2160}
2161
Douglas Gregorff9a6092009-04-20 20:36:09 +00002162pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2163 if (D == 0)
2164 return 0;
2165
2166 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2167 return DeclIDs[D];
2168}
2169
Douglas Gregorc34897d2009-04-09 22:27:44 +00002170void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2171 Record.push_back(Name.getNameKind());
2172 switch (Name.getNameKind()) {
2173 case DeclarationName::Identifier:
2174 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2175 break;
2176
2177 case DeclarationName::ObjCZeroArgSelector:
2178 case DeclarationName::ObjCOneArgSelector:
2179 case DeclarationName::ObjCMultiArgSelector:
2180 assert(false && "Serialization of Objective-C selectors unavailable");
2181 break;
2182
2183 case DeclarationName::CXXConstructorName:
2184 case DeclarationName::CXXDestructorName:
2185 case DeclarationName::CXXConversionFunctionName:
2186 AddTypeRef(Name.getCXXNameType(), Record);
2187 break;
2188
2189 case DeclarationName::CXXOperatorName:
2190 Record.push_back(Name.getCXXOverloadedOperator());
2191 break;
2192
2193 case DeclarationName::CXXUsingDirective:
2194 // No extra data to emit
2195 break;
2196 }
2197}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002198
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002199/// \brief Write the given substatement or subexpression to the
2200/// bitstream.
2201void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002202 RecordData Record;
2203 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002204 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002205
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002206 if (!S) {
2207 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002208 return;
2209 }
2210
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002211 Writer.Code = pch::STMT_NULL_PTR;
2212 Writer.Visit(S);
2213 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002214 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002215 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002216}
2217
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002218/// \brief Flush all of the statements that have been added to the
2219/// queue via AddStmt().
2220void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002221 RecordData Record;
2222 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002223
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002224 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002225 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002226 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002227
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002228 if (!S) {
2229 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002230 continue;
2231 }
2232
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002233 Writer.Code = pch::STMT_NULL_PTR;
2234 Writer.Visit(S);
2235 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002236 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002237 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002238
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002239 assert(N == StmtsToEmit.size() &&
2240 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002241
2242 // Note that we are at the end of a full expression. Any
2243 // expression records that follow this one are part of a different
2244 // expression.
2245 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002246 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002247 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002248
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002249 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002250 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002251}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002252
2253unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2254 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2255 "SwitchCase recorded twice");
2256 unsigned NextID = SwitchCaseIDs.size();
2257 SwitchCaseIDs[S] = NextID;
2258 return NextID;
2259}
2260
2261unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2262 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2263 "SwitchCase hasn't been seen yet");
2264 return SwitchCaseIDs[S];
2265}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002266
2267/// \brief Retrieve the ID for the given label statement, which may
2268/// or may not have been emitted yet.
2269unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2270 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2271 if (Pos != LabelIDs.end())
2272 return Pos->second;
2273
2274 unsigned NextID = LabelIDs.size();
2275 LabelIDs[S] = NextID;
2276 return NextID;
2277}