blob: ea2f359e5036173d32eb2c75234f6030ccc5d194 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorff9a6092009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
20#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000036#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000037using namespace clang;
38
39//===----------------------------------------------------------------------===//
40// Type serialization
41//===----------------------------------------------------------------------===//
42namespace {
43 class VISIBILITY_HIDDEN PCHTypeWriter {
44 PCHWriter &Writer;
45 PCHWriter::RecordData &Record;
46
47 public:
48 /// \brief Type code that corresponds to the record generated.
49 pch::TypeCode Code;
50
51 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
52 : Writer(Writer), Record(Record) { }
53
54 void VisitArrayType(const ArrayType *T);
55 void VisitFunctionType(const FunctionType *T);
56 void VisitTagType(const TagType *T);
57
58#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
59#define ABSTRACT_TYPE(Class, Base)
60#define DEPENDENT_TYPE(Class, Base)
61#include "clang/AST/TypeNodes.def"
62 };
63}
64
65void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
66 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
67 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
68 Record.push_back(T->getAddressSpace());
69 Code = pch::TYPE_EXT_QUAL;
70}
71
72void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
73 assert(false && "Built-in types are never serialized");
74}
75
76void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
77 Record.push_back(T->getWidth());
78 Record.push_back(T->isSigned());
79 Code = pch::TYPE_FIXED_WIDTH_INT;
80}
81
82void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
83 Writer.AddTypeRef(T->getElementType(), Record);
84 Code = pch::TYPE_COMPLEX;
85}
86
87void PCHTypeWriter::VisitPointerType(const PointerType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_POINTER;
90}
91
92void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_BLOCK_POINTER;
95}
96
97void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
98 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Code = pch::TYPE_LVALUE_REFERENCE;
100}
101
102void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
103 Writer.AddTypeRef(T->getPointeeType(), Record);
104 Code = pch::TYPE_RVALUE_REFERENCE;
105}
106
107void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
108 Writer.AddTypeRef(T->getPointeeType(), Record);
109 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
110 Code = pch::TYPE_MEMBER_POINTER;
111}
112
113void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
114 Writer.AddTypeRef(T->getElementType(), Record);
115 Record.push_back(T->getSizeModifier()); // FIXME: stable values
116 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
117}
118
119void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
120 VisitArrayType(T);
121 Writer.AddAPInt(T->getSize(), Record);
122 Code = pch::TYPE_CONSTANT_ARRAY;
123}
124
125void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
126 VisitArrayType(T);
127 Code = pch::TYPE_INCOMPLETE_ARRAY;
128}
129
130void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
131 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000132 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000133 Code = pch::TYPE_VARIABLE_ARRAY;
134}
135
136void PCHTypeWriter::VisitVectorType(const VectorType *T) {
137 Writer.AddTypeRef(T->getElementType(), Record);
138 Record.push_back(T->getNumElements());
139 Code = pch::TYPE_VECTOR;
140}
141
142void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
143 VisitVectorType(T);
144 Code = pch::TYPE_EXT_VECTOR;
145}
146
147void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
148 Writer.AddTypeRef(T->getResultType(), Record);
149}
150
151void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
152 VisitFunctionType(T);
153 Code = pch::TYPE_FUNCTION_NO_PROTO;
154}
155
156void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
157 VisitFunctionType(T);
158 Record.push_back(T->getNumArgs());
159 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
160 Writer.AddTypeRef(T->getArgType(I), Record);
161 Record.push_back(T->isVariadic());
162 Record.push_back(T->getTypeQuals());
163 Code = pch::TYPE_FUNCTION_PROTO;
164}
165
166void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
167 Writer.AddDeclRef(T->getDecl(), Record);
168 Code = pch::TYPE_TYPEDEF;
169}
170
171void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000172 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000173 Code = pch::TYPE_TYPEOF_EXPR;
174}
175
176void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
177 Writer.AddTypeRef(T->getUnderlyingType(), Record);
178 Code = pch::TYPE_TYPEOF;
179}
180
181void PCHTypeWriter::VisitTagType(const TagType *T) {
182 Writer.AddDeclRef(T->getDecl(), Record);
183 assert(!T->isBeingDefined() &&
184 "Cannot serialize in the middle of a type definition");
185}
186
187void PCHTypeWriter::VisitRecordType(const RecordType *T) {
188 VisitTagType(T);
189 Code = pch::TYPE_RECORD;
190}
191
192void PCHTypeWriter::VisitEnumType(const EnumType *T) {
193 VisitTagType(T);
194 Code = pch::TYPE_ENUM;
195}
196
197void
198PCHTypeWriter::VisitTemplateSpecializationType(
199 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000200 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000201 assert(false && "Cannot serialize template specialization types");
202}
203
204void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000205 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000206 assert(false && "Cannot serialize qualified name types");
207}
208
209void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
210 Writer.AddDeclRef(T->getDecl(), Record);
211 Code = pch::TYPE_OBJC_INTERFACE;
212}
213
214void
215PCHTypeWriter::VisitObjCQualifiedInterfaceType(
216 const ObjCQualifiedInterfaceType *T) {
217 VisitObjCInterfaceType(T);
218 Record.push_back(T->getNumProtocols());
219 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
220 Writer.AddDeclRef(T->getProtocol(I), Record);
221 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
222}
223
224void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
225 Record.push_back(T->getNumProtocols());
226 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
227 Writer.AddDeclRef(T->getProtocols(I), Record);
228 Code = pch::TYPE_OBJC_QUALIFIED_ID;
229}
230
Douglas Gregorc34897d2009-04-09 22:27:44 +0000231//===----------------------------------------------------------------------===//
232// Declaration serialization
233//===----------------------------------------------------------------------===//
234namespace {
235 class VISIBILITY_HIDDEN PCHDeclWriter
236 : public DeclVisitor<PCHDeclWriter, void> {
237
238 PCHWriter &Writer;
Douglas Gregore3241e92009-04-18 00:02:19 +0000239 ASTContext &Context;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000240 PCHWriter::RecordData &Record;
241
242 public:
243 pch::DeclCode Code;
244
Douglas Gregore3241e92009-04-18 00:02:19 +0000245 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
246 PCHWriter::RecordData &Record)
247 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000248
249 void VisitDecl(Decl *D);
250 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
251 void VisitNamedDecl(NamedDecl *D);
252 void VisitTypeDecl(TypeDecl *D);
253 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000254 void VisitTagDecl(TagDecl *D);
255 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000256 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000257 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000258 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000259 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000260 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000261 void VisitVarDecl(VarDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000262 void VisitParmVarDecl(ParmVarDecl *D);
263 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000264 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
265 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000266 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
267 uint64_t VisibleOffset);
Steve Naroff79ea0e02009-04-20 15:06:07 +0000268 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +0000269 void VisitObjCContainerDecl(ObjCContainerDecl *D);
270 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
271 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000272 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
273 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
274 void VisitObjCClassDecl(ObjCClassDecl *D);
275 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
276 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
277 void VisitObjCImplDecl(ObjCImplDecl *D);
278 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
279 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
280 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
281 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
282 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000283 };
284}
285
286void PCHDeclWriter::VisitDecl(Decl *D) {
287 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
288 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
289 Writer.AddSourceLocation(D->getLocation(), Record);
290 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000291 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000292 Record.push_back(D->isImplicit());
293 Record.push_back(D->getAccess());
294}
295
296void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
297 VisitDecl(D);
298 Code = pch::DECL_TRANSLATION_UNIT;
299}
300
301void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
302 VisitDecl(D);
303 Writer.AddDeclarationName(D->getDeclName(), Record);
304}
305
306void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
307 VisitNamedDecl(D);
308 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
309}
310
311void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
312 VisitTypeDecl(D);
313 Writer.AddTypeRef(D->getUnderlyingType(), Record);
314 Code = pch::DECL_TYPEDEF;
315}
316
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000317void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
318 VisitTypeDecl(D);
319 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
320 Record.push_back(D->isDefinition());
321 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
322}
323
324void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
325 VisitTagDecl(D);
326 Writer.AddTypeRef(D->getIntegerType(), Record);
327 Code = pch::DECL_ENUM;
328}
329
Douglas Gregor982365e2009-04-13 21:20:57 +0000330void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
331 VisitTagDecl(D);
332 Record.push_back(D->hasFlexibleArrayMember());
333 Record.push_back(D->isAnonymousStructOrUnion());
334 Code = pch::DECL_RECORD;
335}
336
Douglas Gregorc34897d2009-04-09 22:27:44 +0000337void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
338 VisitNamedDecl(D);
339 Writer.AddTypeRef(D->getType(), Record);
340}
341
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000342void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
343 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000344 Record.push_back(D->getInitExpr()? 1 : 0);
345 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000346 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000347 Writer.AddAPSInt(D->getInitVal(), Record);
348 Code = pch::DECL_ENUM_CONSTANT;
349}
350
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000351void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
352 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000353 Record.push_back(D->isThisDeclarationADefinition());
354 if (D->isThisDeclarationADefinition())
Douglas Gregore3241e92009-04-18 00:02:19 +0000355 Writer.AddStmt(D->getBody(Context));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000356 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
357 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
358 Record.push_back(D->isInline());
359 Record.push_back(D->isVirtual());
360 Record.push_back(D->isPure());
361 Record.push_back(D->inheritedPrototype());
362 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
363 Record.push_back(D->isDeleted());
364 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
365 Record.push_back(D->param_size());
366 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
367 P != PEnd; ++P)
368 Writer.AddDeclRef(*P, Record);
369 Code = pch::DECL_FUNCTION;
370}
371
Steve Naroff79ea0e02009-04-20 15:06:07 +0000372void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
373 VisitNamedDecl(D);
374 // FIXME: convert to LazyStmtPtr?
375 // Unlike C/C++, method bodies will never be in header files.
376 Record.push_back(D->getBody() != 0);
377 if (D->getBody() != 0) {
378 Writer.AddStmt(D->getBody(Context));
379 Writer.AddDeclRef(D->getSelfDecl(), Record);
380 Writer.AddDeclRef(D->getCmdDecl(), Record);
381 }
382 Record.push_back(D->isInstanceMethod());
383 Record.push_back(D->isVariadic());
384 Record.push_back(D->isSynthesized());
385 // FIXME: stable encoding for @required/@optional
386 Record.push_back(D->getImplementationControl());
387 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
388 Record.push_back(D->getObjCDeclQualifier());
389 Writer.AddTypeRef(D->getResultType(), Record);
390 Writer.AddSourceLocation(D->getLocEnd(), Record);
391 Record.push_back(D->param_size());
392 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
393 PEnd = D->param_end(); P != PEnd; ++P)
394 Writer.AddDeclRef(*P, Record);
395 Code = pch::DECL_OBJC_METHOD;
396}
397
Steve Naroff7333b492009-04-20 20:09:33 +0000398void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
399 VisitNamedDecl(D);
400 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
401 // Abstract class (no need to define a stable pch::DECL code).
402}
403
404void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
405 VisitObjCContainerDecl(D);
406 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
407 Writer.AddDeclRef(D->getSuperClass(), Record);
408 Record.push_back(D->ivar_size());
409 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
410 IEnd = D->ivar_end(); I != IEnd; ++I)
411 Writer.AddDeclRef(*I, Record);
412 Record.push_back(D->isForwardDecl());
413 Record.push_back(D->isImplicitInterfaceDecl());
414 Writer.AddSourceLocation(D->getClassLoc(), Record);
415 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
416 Writer.AddSourceLocation(D->getLocEnd(), Record);
417 // FIXME: add protocols, categories.
Steve Naroff97b53bd2009-04-21 15:12:33 +0000418 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff7333b492009-04-20 20:09:33 +0000419}
420
421void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
422 VisitFieldDecl(D);
423 // FIXME: stable encoding for @public/@private/@protected/@package
424 Record.push_back(D->getAccessControl());
Steve Naroff97b53bd2009-04-21 15:12:33 +0000425 Code = pch::DECL_OBJC_IVAR;
426}
427
428void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
429 VisitObjCContainerDecl(D);
430 Record.push_back(D->isForwardDecl());
431 Writer.AddSourceLocation(D->getLocEnd(), Record);
432 Record.push_back(D->protocol_size());
433 for (ObjCProtocolDecl::protocol_iterator
434 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
435 Writer.AddDeclRef(*I, Record);
436 Code = pch::DECL_OBJC_PROTOCOL;
437}
438
439void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
440 VisitFieldDecl(D);
441 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
442}
443
444void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
445 VisitDecl(D);
446 Record.push_back(D->size());
447 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
448 Writer.AddDeclRef(*I, Record);
449 Code = pch::DECL_OBJC_CLASS;
450}
451
452void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
453 VisitDecl(D);
454 Record.push_back(D->protocol_size());
455 for (ObjCProtocolDecl::protocol_iterator
456 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
457 Writer.AddDeclRef(*I, Record);
458 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
459}
460
461void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
462 VisitObjCContainerDecl(D);
463 Writer.AddDeclRef(D->getClassInterface(), Record);
464 Record.push_back(D->protocol_size());
465 for (ObjCProtocolDecl::protocol_iterator
466 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
467 Writer.AddDeclRef(*I, Record);
468 Writer.AddDeclRef(D->getNextClassCategory(), Record);
469 Writer.AddSourceLocation(D->getLocEnd(), Record);
470 Code = pch::DECL_OBJC_CATEGORY;
471}
472
473void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
474 VisitNamedDecl(D);
475 Writer.AddDeclRef(D->getClassInterface(), Record);
476 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
477}
478
479void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
480 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000481 Writer.AddTypeRef(D->getType(), Record);
482 // FIXME: stable encoding
483 Record.push_back((unsigned)D->getPropertyAttributes());
484 // FIXME: stable encoding
485 Record.push_back((unsigned)D->getPropertyImplementation());
486 Writer.AddDeclarationName(D->getGetterName(), Record);
487 Writer.AddDeclarationName(D->getSetterName(), Record);
488 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
489 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
490 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000491 Code = pch::DECL_OBJC_PROPERTY;
492}
493
494void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
495 VisitDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000496 Writer.AddDeclRef(D->getClassInterface(), Record);
497 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000498 // Abstract class (no need to define a stable pch::DECL code).
499}
500
501void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
502 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000503 Writer.AddIdentifierRef(D->getIdentifier(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000504 Code = pch::DECL_OBJC_CATEGORY_IMPL;
505}
506
507void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
508 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000509 Writer.AddDeclRef(D->getSuperClass(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000510 Code = pch::DECL_OBJC_IMPLEMENTATION;
511}
512
513void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
514 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000515 Writer.AddSourceLocation(D->getLocStart(), Record);
516 Writer.AddDeclRef(D->getPropertyDecl(), Record);
517 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000518 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff7333b492009-04-20 20:09:33 +0000519}
520
Douglas Gregor982365e2009-04-13 21:20:57 +0000521void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
522 VisitValueDecl(D);
523 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000524 Record.push_back(D->getBitWidth()? 1 : 0);
525 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000526 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000527 Code = pch::DECL_FIELD;
528}
529
Douglas Gregorc34897d2009-04-09 22:27:44 +0000530void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
531 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000532 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000533 Record.push_back(D->isThreadSpecified());
534 Record.push_back(D->hasCXXDirectInitializer());
535 Record.push_back(D->isDeclaredInCondition());
536 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
537 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000538 Record.push_back(D->getInit()? 1 : 0);
539 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000540 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000541 Code = pch::DECL_VAR;
542}
543
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000544void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
545 VisitVarDecl(D);
546 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000547 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000548 // FIXME: why isn't the "default argument" just stored as the initializer
549 // in VarDecl?
550 Code = pch::DECL_PARM_VAR;
551}
552
553void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
554 VisitParmVarDecl(D);
555 Writer.AddTypeRef(D->getOriginalType(), Record);
556 Code = pch::DECL_ORIGINAL_PARM_VAR;
557}
558
Douglas Gregor2a491792009-04-13 22:49:25 +0000559void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
560 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000561 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000562 Code = pch::DECL_FILE_SCOPE_ASM;
563}
564
565void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
566 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000567 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000568 Record.push_back(D->param_size());
569 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
570 P != PEnd; ++P)
571 Writer.AddDeclRef(*P, Record);
572 Code = pch::DECL_BLOCK;
573}
574
Douglas Gregorc34897d2009-04-09 22:27:44 +0000575/// \brief Emit the DeclContext part of a declaration context decl.
576///
577/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
578/// block for this declaration context is stored. May be 0 to indicate
579/// that there are no declarations stored within this context.
580///
581/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
582/// block for this declaration context is stored. May be 0 to indicate
583/// that there are no declarations visible from this context. Note
584/// that this value will not be emitted for non-primary declaration
585/// contexts.
586void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
587 uint64_t VisibleOffset) {
588 Record.push_back(LexicalOffset);
Douglas Gregor405b6432009-04-22 19:09:20 +0000589 Record.push_back(VisibleOffset);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000590}
591
592//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000593// Statement/expression serialization
594//===----------------------------------------------------------------------===//
595namespace {
596 class VISIBILITY_HIDDEN PCHStmtWriter
597 : public StmtVisitor<PCHStmtWriter, void> {
598
599 PCHWriter &Writer;
600 PCHWriter::RecordData &Record;
601
602 public:
603 pch::StmtCode Code;
604
605 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
606 : Writer(Writer), Record(Record) { }
607
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000608 void VisitStmt(Stmt *S);
609 void VisitNullStmt(NullStmt *S);
610 void VisitCompoundStmt(CompoundStmt *S);
611 void VisitSwitchCase(SwitchCase *S);
612 void VisitCaseStmt(CaseStmt *S);
613 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000614 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000615 void VisitIfStmt(IfStmt *S);
616 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000617 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000618 void VisitDoStmt(DoStmt *S);
619 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000620 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000621 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000622 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000623 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000624 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000625 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000626 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000627 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000628 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000629 void VisitDeclRefExpr(DeclRefExpr *E);
630 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000631 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000632 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000633 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000634 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000635 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000636 void VisitUnaryOperator(UnaryOperator *E);
637 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000638 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000639 void VisitCallExpr(CallExpr *E);
640 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000641 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000642 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000643 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
644 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000645 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000646 void VisitExplicitCastExpr(ExplicitCastExpr *E);
647 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000648 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000649 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000650 void VisitInitListExpr(InitListExpr *E);
651 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
652 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000653 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000654 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000655 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000656 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
657 void VisitChooseExpr(ChooseExpr *E);
658 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000659 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000660 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000661 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000662
663 // Objective-C
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000664 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000665 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000666 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
667 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000668 };
669}
670
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000671void PCHStmtWriter::VisitStmt(Stmt *S) {
672}
673
674void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
675 VisitStmt(S);
676 Writer.AddSourceLocation(S->getSemiLoc(), Record);
677 Code = pch::STMT_NULL;
678}
679
680void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
681 VisitStmt(S);
682 Record.push_back(S->size());
683 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
684 CS != CSEnd; ++CS)
685 Writer.WriteSubStmt(*CS);
686 Writer.AddSourceLocation(S->getLBracLoc(), Record);
687 Writer.AddSourceLocation(S->getRBracLoc(), Record);
688 Code = pch::STMT_COMPOUND;
689}
690
691void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
692 VisitStmt(S);
693 Record.push_back(Writer.RecordSwitchCaseID(S));
694}
695
696void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
697 VisitSwitchCase(S);
698 Writer.WriteSubStmt(S->getLHS());
699 Writer.WriteSubStmt(S->getRHS());
700 Writer.WriteSubStmt(S->getSubStmt());
701 Writer.AddSourceLocation(S->getCaseLoc(), Record);
702 Code = pch::STMT_CASE;
703}
704
705void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
706 VisitSwitchCase(S);
707 Writer.WriteSubStmt(S->getSubStmt());
708 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
709 Code = pch::STMT_DEFAULT;
710}
711
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000712void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
713 VisitStmt(S);
714 Writer.AddIdentifierRef(S->getID(), Record);
715 Writer.WriteSubStmt(S->getSubStmt());
716 Writer.AddSourceLocation(S->getIdentLoc(), Record);
717 Record.push_back(Writer.GetLabelID(S));
718 Code = pch::STMT_LABEL;
719}
720
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000721void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
722 VisitStmt(S);
723 Writer.WriteSubStmt(S->getCond());
724 Writer.WriteSubStmt(S->getThen());
725 Writer.WriteSubStmt(S->getElse());
726 Writer.AddSourceLocation(S->getIfLoc(), Record);
727 Code = pch::STMT_IF;
728}
729
730void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
731 VisitStmt(S);
732 Writer.WriteSubStmt(S->getCond());
733 Writer.WriteSubStmt(S->getBody());
734 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
735 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
736 SC = SC->getNextSwitchCase())
737 Record.push_back(Writer.getSwitchCaseID(SC));
738 Code = pch::STMT_SWITCH;
739}
740
Douglas Gregora6b503f2009-04-17 00:16:09 +0000741void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
742 VisitStmt(S);
743 Writer.WriteSubStmt(S->getCond());
744 Writer.WriteSubStmt(S->getBody());
745 Writer.AddSourceLocation(S->getWhileLoc(), Record);
746 Code = pch::STMT_WHILE;
747}
748
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000749void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
750 VisitStmt(S);
751 Writer.WriteSubStmt(S->getCond());
752 Writer.WriteSubStmt(S->getBody());
753 Writer.AddSourceLocation(S->getDoLoc(), Record);
754 Code = pch::STMT_DO;
755}
756
757void PCHStmtWriter::VisitForStmt(ForStmt *S) {
758 VisitStmt(S);
759 Writer.WriteSubStmt(S->getInit());
760 Writer.WriteSubStmt(S->getCond());
761 Writer.WriteSubStmt(S->getInc());
762 Writer.WriteSubStmt(S->getBody());
763 Writer.AddSourceLocation(S->getForLoc(), Record);
764 Code = pch::STMT_FOR;
765}
766
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000767void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
768 VisitStmt(S);
769 Record.push_back(Writer.GetLabelID(S->getLabel()));
770 Writer.AddSourceLocation(S->getGotoLoc(), Record);
771 Writer.AddSourceLocation(S->getLabelLoc(), Record);
772 Code = pch::STMT_GOTO;
773}
774
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000775void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
776 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000777 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000778 Writer.WriteSubStmt(S->getTarget());
779 Code = pch::STMT_INDIRECT_GOTO;
780}
781
Douglas Gregora6b503f2009-04-17 00:16:09 +0000782void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
783 VisitStmt(S);
784 Writer.AddSourceLocation(S->getContinueLoc(), Record);
785 Code = pch::STMT_CONTINUE;
786}
787
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000788void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
789 VisitStmt(S);
790 Writer.AddSourceLocation(S->getBreakLoc(), Record);
791 Code = pch::STMT_BREAK;
792}
793
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000794void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
795 VisitStmt(S);
796 Writer.WriteSubStmt(S->getRetValue());
797 Writer.AddSourceLocation(S->getReturnLoc(), Record);
798 Code = pch::STMT_RETURN;
799}
800
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000801void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
802 VisitStmt(S);
803 Writer.AddSourceLocation(S->getStartLoc(), Record);
804 Writer.AddSourceLocation(S->getEndLoc(), Record);
805 DeclGroupRef DG = S->getDeclGroup();
806 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
807 Writer.AddDeclRef(*D, Record);
808 Code = pch::STMT_DECL;
809}
810
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000811void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
812 VisitStmt(S);
813 Record.push_back(S->getNumOutputs());
814 Record.push_back(S->getNumInputs());
815 Record.push_back(S->getNumClobbers());
816 Writer.AddSourceLocation(S->getAsmLoc(), Record);
817 Writer.AddSourceLocation(S->getRParenLoc(), Record);
818 Record.push_back(S->isVolatile());
819 Record.push_back(S->isSimple());
820 Writer.WriteSubStmt(S->getAsmString());
821
822 // Outputs
823 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
824 Writer.AddString(S->getOutputName(I), Record);
825 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
826 Writer.WriteSubStmt(S->getOutputExpr(I));
827 }
828
829 // Inputs
830 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
831 Writer.AddString(S->getInputName(I), Record);
832 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
833 Writer.WriteSubStmt(S->getInputExpr(I));
834 }
835
836 // Clobbers
837 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
838 Writer.WriteSubStmt(S->getClobber(I));
839
840 Code = pch::STMT_ASM;
841}
842
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000843void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000844 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000845 Writer.AddTypeRef(E->getType(), Record);
846 Record.push_back(E->isTypeDependent());
847 Record.push_back(E->isValueDependent());
848}
849
Douglas Gregore2f37202009-04-14 21:55:33 +0000850void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
851 VisitExpr(E);
852 Writer.AddSourceLocation(E->getLocation(), Record);
853 Record.push_back(E->getIdentType()); // FIXME: stable encoding
854 Code = pch::EXPR_PREDEFINED;
855}
856
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000857void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
858 VisitExpr(E);
859 Writer.AddDeclRef(E->getDecl(), Record);
860 Writer.AddSourceLocation(E->getLocation(), Record);
861 Code = pch::EXPR_DECL_REF;
862}
863
864void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
865 VisitExpr(E);
866 Writer.AddSourceLocation(E->getLocation(), Record);
867 Writer.AddAPInt(E->getValue(), Record);
868 Code = pch::EXPR_INTEGER_LITERAL;
869}
870
Douglas Gregore2f37202009-04-14 21:55:33 +0000871void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
872 VisitExpr(E);
873 Writer.AddAPFloat(E->getValue(), Record);
874 Record.push_back(E->isExact());
875 Writer.AddSourceLocation(E->getLocation(), Record);
876 Code = pch::EXPR_FLOATING_LITERAL;
877}
878
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000879void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
880 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000881 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000882 Code = pch::EXPR_IMAGINARY_LITERAL;
883}
884
Douglas Gregor596e0932009-04-15 16:35:07 +0000885void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
886 VisitExpr(E);
887 Record.push_back(E->getByteLength());
888 Record.push_back(E->getNumConcatenated());
889 Record.push_back(E->isWide());
890 // FIXME: String data should be stored as a blob at the end of the
891 // StringLiteral. However, we can't do so now because we have no
892 // provision for coping with abbreviations when we're jumping around
893 // the PCH file during deserialization.
894 Record.insert(Record.end(),
895 E->getStrData(), E->getStrData() + E->getByteLength());
896 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
897 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
898 Code = pch::EXPR_STRING_LITERAL;
899}
900
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000901void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
902 VisitExpr(E);
903 Record.push_back(E->getValue());
904 Writer.AddSourceLocation(E->getLoc(), Record);
905 Record.push_back(E->isWide());
906 Code = pch::EXPR_CHARACTER_LITERAL;
907}
908
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000909void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
910 VisitExpr(E);
911 Writer.AddSourceLocation(E->getLParen(), Record);
912 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000913 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000914 Code = pch::EXPR_PAREN;
915}
916
Douglas Gregor12d74052009-04-15 15:58:59 +0000917void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
918 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000919 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000920 Record.push_back(E->getOpcode()); // FIXME: stable encoding
921 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
922 Code = pch::EXPR_UNARY_OPERATOR;
923}
924
925void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
926 VisitExpr(E);
927 Record.push_back(E->isSizeOf());
928 if (E->isArgumentType())
929 Writer.AddTypeRef(E->getArgumentType(), Record);
930 else {
931 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000932 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000933 }
934 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
935 Writer.AddSourceLocation(E->getRParenLoc(), Record);
936 Code = pch::EXPR_SIZEOF_ALIGN_OF;
937}
938
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000939void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
940 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000941 Writer.WriteSubStmt(E->getLHS());
942 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000943 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
944 Code = pch::EXPR_ARRAY_SUBSCRIPT;
945}
946
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000947void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
948 VisitExpr(E);
949 Record.push_back(E->getNumArgs());
950 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000951 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000952 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
953 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000954 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000955 Code = pch::EXPR_CALL;
956}
957
958void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
959 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000960 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000961 Writer.AddDeclRef(E->getMemberDecl(), Record);
962 Writer.AddSourceLocation(E->getMemberLoc(), Record);
963 Record.push_back(E->isArrow());
964 Code = pch::EXPR_MEMBER;
965}
966
Douglas Gregora151ba42009-04-14 23:32:43 +0000967void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
968 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000969 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000970}
971
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000972void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
973 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000974 Writer.WriteSubStmt(E->getLHS());
975 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000976 Record.push_back(E->getOpcode()); // FIXME: stable encoding
977 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
978 Code = pch::EXPR_BINARY_OPERATOR;
979}
980
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000981void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
982 VisitBinaryOperator(E);
983 Writer.AddTypeRef(E->getComputationLHSType(), Record);
984 Writer.AddTypeRef(E->getComputationResultType(), Record);
985 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
986}
987
988void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
989 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000990 Writer.WriteSubStmt(E->getCond());
991 Writer.WriteSubStmt(E->getLHS());
992 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000993 Code = pch::EXPR_CONDITIONAL_OPERATOR;
994}
995
Douglas Gregora151ba42009-04-14 23:32:43 +0000996void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
997 VisitCastExpr(E);
998 Record.push_back(E->isLvalueCast());
999 Code = pch::EXPR_IMPLICIT_CAST;
1000}
1001
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001002void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1003 VisitCastExpr(E);
1004 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1005}
1006
1007void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1008 VisitExplicitCastExpr(E);
1009 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1010 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1011 Code = pch::EXPR_CSTYLE_CAST;
1012}
1013
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001014void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1015 VisitExpr(E);
1016 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001017 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001018 Record.push_back(E->isFileScope());
1019 Code = pch::EXPR_COMPOUND_LITERAL;
1020}
1021
Douglas Gregorec0b8292009-04-15 23:02:49 +00001022void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1023 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001024 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001025 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1026 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1027 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1028}
1029
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001030void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1031 VisitExpr(E);
1032 Record.push_back(E->getNumInits());
1033 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001034 Writer.WriteSubStmt(E->getInit(I));
1035 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001036 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1037 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1038 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1039 Record.push_back(E->hadArrayRangeDesignator());
1040 Code = pch::EXPR_INIT_LIST;
1041}
1042
1043void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1044 VisitExpr(E);
1045 Record.push_back(E->getNumSubExprs());
1046 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001047 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001048 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1049 Record.push_back(E->usesGNUSyntax());
1050 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1051 DEnd = E->designators_end();
1052 D != DEnd; ++D) {
1053 if (D->isFieldDesignator()) {
1054 if (FieldDecl *Field = D->getField()) {
1055 Record.push_back(pch::DESIG_FIELD_DECL);
1056 Writer.AddDeclRef(Field, Record);
1057 } else {
1058 Record.push_back(pch::DESIG_FIELD_NAME);
1059 Writer.AddIdentifierRef(D->getFieldName(), Record);
1060 }
1061 Writer.AddSourceLocation(D->getDotLoc(), Record);
1062 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1063 } else if (D->isArrayDesignator()) {
1064 Record.push_back(pch::DESIG_ARRAY);
1065 Record.push_back(D->getFirstExprIndex());
1066 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1067 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1068 } else {
1069 assert(D->isArrayRangeDesignator() && "Unknown designator");
1070 Record.push_back(pch::DESIG_ARRAY_RANGE);
1071 Record.push_back(D->getFirstExprIndex());
1072 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1073 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1074 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1075 }
1076 }
1077 Code = pch::EXPR_DESIGNATED_INIT;
1078}
1079
1080void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1081 VisitExpr(E);
1082 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1083}
1084
Douglas Gregorec0b8292009-04-15 23:02:49 +00001085void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1086 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001087 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001088 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1089 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1090 Code = pch::EXPR_VA_ARG;
1091}
1092
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001093void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1094 VisitExpr(E);
1095 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1096 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1097 Record.push_back(Writer.GetLabelID(E->getLabel()));
1098 Code = pch::EXPR_ADDR_LABEL;
1099}
1100
Douglas Gregoreca12f62009-04-17 19:05:30 +00001101void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1102 VisitExpr(E);
1103 Writer.WriteSubStmt(E->getSubStmt());
1104 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1105 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1106 Code = pch::EXPR_STMT;
1107}
1108
Douglas Gregor209d4622009-04-15 23:33:31 +00001109void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1110 VisitExpr(E);
1111 Writer.AddTypeRef(E->getArgType1(), Record);
1112 Writer.AddTypeRef(E->getArgType2(), Record);
1113 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1114 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1115 Code = pch::EXPR_TYPES_COMPATIBLE;
1116}
1117
1118void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1119 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001120 Writer.WriteSubStmt(E->getCond());
1121 Writer.WriteSubStmt(E->getLHS());
1122 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001123 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1124 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1125 Code = pch::EXPR_CHOOSE;
1126}
1127
1128void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1129 VisitExpr(E);
1130 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1131 Code = pch::EXPR_GNU_NULL;
1132}
1133
Douglas Gregor725e94b2009-04-16 00:01:45 +00001134void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1135 VisitExpr(E);
1136 Record.push_back(E->getNumSubExprs());
1137 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001138 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001139 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1140 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1141 Code = pch::EXPR_SHUFFLE_VECTOR;
1142}
1143
Douglas Gregore246b742009-04-17 19:21:43 +00001144void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1145 VisitExpr(E);
1146 Writer.AddDeclRef(E->getBlockDecl(), Record);
1147 Record.push_back(E->hasBlockDeclRefExprs());
1148 Code = pch::EXPR_BLOCK;
1149}
1150
Douglas Gregor725e94b2009-04-16 00:01:45 +00001151void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1152 VisitExpr(E);
1153 Writer.AddDeclRef(E->getDecl(), Record);
1154 Writer.AddSourceLocation(E->getLocation(), Record);
1155 Record.push_back(E->isByRef());
1156 Code = pch::EXPR_BLOCK_DECL_REF;
1157}
1158
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001159//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001160// Objective-C Expressions and Statements.
1161//===----------------------------------------------------------------------===//
1162
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001163void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1164 VisitExpr(E);
1165 Writer.WriteSubStmt(E->getString());
1166 Writer.AddSourceLocation(E->getAtLoc(), Record);
1167 Code = pch::EXPR_OBJC_STRING_LITERAL;
1168}
1169
Chris Lattner80f83c62009-04-22 05:57:30 +00001170void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1171 VisitExpr(E);
1172 Writer.AddTypeRef(E->getEncodedType(), Record);
1173 Writer.AddSourceLocation(E->getAtLoc(), Record);
1174 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1175 Code = pch::EXPR_OBJC_ENCODE;
1176}
1177
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001178void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1179 VisitExpr(E);
Chris Lattner9b9f2352009-04-22 06:40:03 +00001180 assert(0 && "Can't write a selector yet!");
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001181 // FIXME! Write selectors.
1182 //Writer.WriteSubStmt(E->getSelector());
1183 Writer.AddSourceLocation(E->getAtLoc(), Record);
1184 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1185 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1186}
1187
1188void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1189 VisitExpr(E);
1190 Writer.AddDeclRef(E->getProtocol(), Record);
1191 Writer.AddSourceLocation(E->getAtLoc(), Record);
1192 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1193 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1194}
1195
Chris Lattner80f83c62009-04-22 05:57:30 +00001196
1197//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001198// PCHWriter Implementation
1199//===----------------------------------------------------------------------===//
1200
Douglas Gregorb5887f32009-04-10 21:16:55 +00001201/// \brief Write the target triple (e.g., i686-apple-darwin9).
1202void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1203 using namespace llvm;
1204 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1205 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001207 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001208
1209 RecordData Record;
1210 Record.push_back(pch::TARGET_TRIPLE);
1211 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001212 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001213}
1214
1215/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001216void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1217 RecordData Record;
1218 Record.push_back(LangOpts.Trigraphs);
1219 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1220 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1221 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1222 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1223 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1224 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1225 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1226 Record.push_back(LangOpts.C99); // C99 Support
1227 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1228 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1229 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1230 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1231 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1232
1233 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1234 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1235 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1236
1237 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1238 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1239 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1240 Record.push_back(LangOpts.LaxVectorConversions);
1241 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1242
1243 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1244 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1245 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1246
1247 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1248 // by locks.
1249 Record.push_back(LangOpts.Blocks); // block extension to C
1250 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1251 // they are unused.
1252 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1253 // (modulo the platform support).
1254
1255 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1256 // signed integer arithmetic overflows.
1257
1258 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1259 // may be ripped out at any time.
1260
1261 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1262 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1263 // defined.
1264 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1265 // opposed to __DYNAMIC__).
1266 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1267
1268 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1269 // used (instead of C99 semantics).
1270 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1271 Record.push_back(LangOpts.getGCMode());
1272 Record.push_back(LangOpts.getVisibilityMode());
1273 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001274 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001275}
1276
Douglas Gregorab1cef72009-04-10 03:52:48 +00001277//===----------------------------------------------------------------------===//
1278// Source Manager Serialization
1279//===----------------------------------------------------------------------===//
1280
1281/// \brief Create an abbreviation for the SLocEntry that refers to a
1282/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001283static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001284 using namespace llvm;
1285 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1286 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001292 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001293}
1294
1295/// \brief Create an abbreviation for the SLocEntry that refers to a
1296/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001297static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001298 using namespace llvm;
1299 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1300 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1304 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001306 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001307}
1308
1309/// \brief Create an abbreviation for the SLocEntry that refers to a
1310/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001311static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001312 using namespace llvm;
1313 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1314 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001316 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001317}
1318
1319/// \brief Create an abbreviation for the SLocEntry that refers to an
1320/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001321static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001322 using namespace llvm;
1323 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1324 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001330 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001331}
1332
1333/// \brief Writes the block containing the serialized form of the
1334/// source manager.
1335///
1336/// TODO: We should probably use an on-disk hash table (stored in a
1337/// blob), indexed based on the file name, so that we only create
1338/// entries for files that we actually need. In the common case (no
1339/// errors), we probably won't have to create file entries for any of
1340/// the files in the AST.
1341void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001342 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001343 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001344
1345 // Abbreviations for the various kinds of source-location entries.
1346 int SLocFileAbbrv = -1;
1347 int SLocBufferAbbrv = -1;
1348 int SLocBufferBlobAbbrv = -1;
1349 int SLocInstantiationAbbrv = -1;
1350
1351 // Write out the source location entry table. We skip the first
1352 // entry, which is always the same dummy entry.
1353 RecordData Record;
1354 for (SourceManager::sloc_entry_iterator
1355 SLoc = SourceMgr.sloc_entry_begin() + 1,
1356 SLocEnd = SourceMgr.sloc_entry_end();
1357 SLoc != SLocEnd; ++SLoc) {
1358 // Figure out which record code to use.
1359 unsigned Code;
1360 if (SLoc->isFile()) {
1361 if (SLoc->getFile().getContentCache()->Entry)
1362 Code = pch::SM_SLOC_FILE_ENTRY;
1363 else
1364 Code = pch::SM_SLOC_BUFFER_ENTRY;
1365 } else
1366 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1367 Record.push_back(Code);
1368
1369 Record.push_back(SLoc->getOffset());
1370 if (SLoc->isFile()) {
1371 const SrcMgr::FileInfo &File = SLoc->getFile();
1372 Record.push_back(File.getIncludeLoc().getRawEncoding());
1373 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001374 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001375
1376 const SrcMgr::ContentCache *Content = File.getContentCache();
1377 if (Content->Entry) {
1378 // The source location entry is a file. The blob associated
1379 // with this entry is the file name.
1380 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001381 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1382 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001383 Content->Entry->getName(),
1384 strlen(Content->Entry->getName()));
1385 } else {
1386 // The source location entry is a buffer. The blob associated
1387 // with this entry contains the contents of the buffer.
1388 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001389 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1390 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001391 }
1392
1393 // We add one to the size so that we capture the trailing NULL
1394 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1395 // the reader side).
1396 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1397 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001398 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001399 Record.clear();
1400 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001401 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001402 Buffer->getBufferStart(),
1403 Buffer->getBufferSize() + 1);
1404 }
1405 } else {
1406 // The source location entry is an instantiation.
1407 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1408 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1409 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1410 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1411
Douglas Gregor364e5802009-04-15 18:05:10 +00001412 // Compute the token length for this macro expansion.
1413 unsigned NextOffset = SourceMgr.getNextOffset();
1414 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1415 if (++NextSLoc != SLocEnd)
1416 NextOffset = NextSLoc->getOffset();
1417 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1418
Douglas Gregorab1cef72009-04-10 03:52:48 +00001419 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001420 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1421 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001422 }
1423
1424 Record.clear();
1425 }
1426
Douglas Gregor635f97f2009-04-13 16:31:14 +00001427 // Write the line table.
1428 if (SourceMgr.hasLineTable()) {
1429 LineTableInfo &LineTable = SourceMgr.getLineTable();
1430
1431 // Emit the file names
1432 Record.push_back(LineTable.getNumFilenames());
1433 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1434 // Emit the file name
1435 const char *Filename = LineTable.getFilename(I);
1436 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1437 Record.push_back(FilenameLen);
1438 if (FilenameLen)
1439 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1440 }
1441
1442 // Emit the line entries
1443 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1444 L != LEnd; ++L) {
1445 // Emit the file ID
1446 Record.push_back(L->first);
1447
1448 // Emit the line entries
1449 Record.push_back(L->second.size());
1450 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1451 LEEnd = L->second.end();
1452 LE != LEEnd; ++LE) {
1453 Record.push_back(LE->FileOffset);
1454 Record.push_back(LE->LineNo);
1455 Record.push_back(LE->FilenameID);
1456 Record.push_back((unsigned)LE->FileKind);
1457 Record.push_back(LE->IncludeOffset);
1458 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001459 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001460 }
1461 }
1462
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001463 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001464}
1465
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001466/// \brief Writes the block containing the serialized form of the
1467/// preprocessor.
1468///
Chris Lattner850eabd2009-04-10 18:08:30 +00001469void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001470 // Enter the preprocessor block.
Douglas Gregorc713da92009-04-21 22:25:48 +00001471 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner84b04f12009-04-10 17:16:57 +00001472
Chris Lattner1b094952009-04-10 18:00:12 +00001473 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1474 // FIXME: use diagnostics subsystem for localization etc.
1475 if (PP.SawDateOrTime())
1476 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001477
Chris Lattner1b094952009-04-10 18:00:12 +00001478 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001479
Chris Lattner4b21c202009-04-13 01:29:17 +00001480 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1481 if (PP.getCounterValue() != 0) {
1482 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001483 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001484 Record.clear();
1485 }
1486
Chris Lattner1b094952009-04-10 18:00:12 +00001487 // Loop over all the macro definitions that are live at the end of the file,
1488 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001489 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1490 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001491 // FIXME: This emits macros in hash table order, we should do it in a stable
1492 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001493 MacroInfo *MI = I->second;
1494
1495 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1496 // been redefined by the header (in which case they are not isBuiltinMacro).
1497 if (MI->isBuiltinMacro())
1498 continue;
1499
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001500 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001501 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001502 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001503 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1504 Record.push_back(MI->isUsed());
1505
1506 unsigned Code;
1507 if (MI->isObjectLike()) {
1508 Code = pch::PP_MACRO_OBJECT_LIKE;
1509 } else {
1510 Code = pch::PP_MACRO_FUNCTION_LIKE;
1511
1512 Record.push_back(MI->isC99Varargs());
1513 Record.push_back(MI->isGNUVarargs());
1514 Record.push_back(MI->getNumArgs());
1515 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1516 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001517 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001518 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001519 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001520 Record.clear();
1521
Chris Lattner850eabd2009-04-10 18:08:30 +00001522 // Emit the tokens array.
1523 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1524 // Note that we know that the preprocessor does not have any annotation
1525 // tokens in it because they are created by the parser, and thus can't be
1526 // in a macro definition.
1527 const Token &Tok = MI->getReplacementToken(TokNo);
1528
1529 Record.push_back(Tok.getLocation().getRawEncoding());
1530 Record.push_back(Tok.getLength());
1531
Chris Lattner850eabd2009-04-10 18:08:30 +00001532 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1533 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001534 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001535
1536 // FIXME: Should translate token kind to a stable encoding.
1537 Record.push_back(Tok.getKind());
1538 // FIXME: Should translate token flags to a stable encoding.
1539 Record.push_back(Tok.getFlags());
1540
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001541 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001542 Record.clear();
1543 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001544 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001545 }
1546
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001547 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001548}
1549
1550
Douglas Gregorc34897d2009-04-09 22:27:44 +00001551/// \brief Write the representation of a type to the PCH stream.
1552void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001553 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001554 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001555 ID = NextTypeID++;
1556
1557 // Record the offset for this type.
1558 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001559 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001560 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1561 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001562 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001563 }
1564
1565 RecordData Record;
1566
1567 // Emit the type's representation.
1568 PCHTypeWriter W(*this, Record);
1569 switch (T->getTypeClass()) {
1570 // For all of the concrete, non-dependent types, call the
1571 // appropriate visitor function.
1572#define TYPE(Class, Base) \
1573 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1574#define ABSTRACT_TYPE(Class, Base)
1575#define DEPENDENT_TYPE(Class, Base)
1576#include "clang/AST/TypeNodes.def"
1577
1578 // For all of the dependent type nodes (which only occur in C++
1579 // templates), produce an error.
1580#define TYPE(Class, Base)
1581#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1582#include "clang/AST/TypeNodes.def"
1583 assert(false && "Cannot serialize dependent type nodes");
1584 break;
1585 }
1586
1587 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001588 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001589
1590 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001591 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001592}
1593
1594/// \brief Write a block containing all of the types.
1595void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001596 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001597 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001598
1599 // Emit all of the types in the ASTContext
1600 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1601 TEnd = Context.getTypes().end();
1602 T != TEnd; ++T) {
1603 // Builtin types are never serialized.
1604 if (isa<BuiltinType>(*T))
1605 continue;
1606
1607 WriteType(*T);
1608 }
1609
1610 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001611 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001612}
1613
1614/// \brief Write the block containing all of the declaration IDs
1615/// lexically declared within the given DeclContext.
1616///
1617/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1618/// bistream, or 0 if no block was written.
1619uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1620 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001621 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001622 return 0;
1623
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001624 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001625 RecordData Record;
1626 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1627 DEnd = DC->decls_end(Context);
1628 D != DEnd; ++D)
1629 AddDeclRef(*D, Record);
1630
Douglas Gregoraf136d92009-04-22 22:34:57 +00001631 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001632 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001633 return Offset;
1634}
1635
1636/// \brief Write the block containing all of the declaration IDs
1637/// visible from the given DeclContext.
1638///
1639/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1640/// bistream, or 0 if no block was written.
1641uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1642 DeclContext *DC) {
1643 if (DC->getPrimaryContext() != DC)
1644 return 0;
1645
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001646 // Since there is no name lookup into functions or methods, and we
1647 // perform name lookup for the translation unit via the
1648 // IdentifierInfo chains, don't bother to build a
1649 // visible-declarations table for these entities.
1650 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001651 return 0;
1652
Douglas Gregorc34897d2009-04-09 22:27:44 +00001653 // Force the DeclContext to build a its name-lookup table.
1654 DC->lookup(Context, DeclarationName());
1655
1656 // Serialize the contents of the mapping used for lookup. Note that,
1657 // although we have two very different code paths, the serialized
1658 // representation is the same for both cases: a declaration name,
1659 // followed by a size, followed by references to the visible
1660 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001661 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001662 RecordData Record;
1663 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001664 if (!Map)
1665 return 0;
1666
Douglas Gregorc34897d2009-04-09 22:27:44 +00001667 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1668 D != DEnd; ++D) {
1669 AddDeclarationName(D->first, Record);
1670 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1671 Record.push_back(Result.second - Result.first);
1672 for(; Result.first != Result.second; ++Result.first)
1673 AddDeclRef(*Result.first, Record);
1674 }
1675
1676 if (Record.size() == 0)
1677 return 0;
1678
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001679 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001680 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001681 return Offset;
1682}
1683
1684/// \brief Write a block containing all of the declarations.
1685void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001686 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001687 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001688
1689 // Emit all of the declarations.
1690 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001691 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001692 while (!DeclsToEmit.empty()) {
1693 // Pull the next declaration off the queue
1694 Decl *D = DeclsToEmit.front();
1695 DeclsToEmit.pop();
1696
1697 // If this declaration is also a DeclContext, write blocks for the
1698 // declarations that lexically stored inside its context and those
1699 // declarations that are visible from its context. These blocks
1700 // are written before the declaration itself so that we can put
1701 // their offsets into the record for the declaration.
1702 uint64_t LexicalOffset = 0;
1703 uint64_t VisibleOffset = 0;
1704 DeclContext *DC = dyn_cast<DeclContext>(D);
1705 if (DC) {
1706 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1707 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1708 }
1709
1710 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001711 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001712 if (ID == 0)
1713 ID = DeclIDs.size();
1714
1715 unsigned Index = ID - 1;
1716
1717 // Record the offset for this declaration
1718 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001719 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001720 else if (DeclOffsets.size() < Index) {
1721 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001722 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001723 }
1724
1725 // Build and emit a record for this declaration
1726 Record.clear();
1727 W.Code = (pch::DeclCode)0;
1728 W.Visit(D);
1729 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001730
1731 if (!W.Code) {
1732 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1733 D->getDeclKindName());
1734 assert(false && "Unhandled declaration kind while generating PCH");
1735 exit(-1);
1736 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001737 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001738
Douglas Gregor1c507882009-04-15 21:30:51 +00001739 // If the declaration had any attributes, write them now.
1740 if (D->hasAttrs())
1741 WriteAttributeRecord(D->getAttrs());
1742
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001743 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001744 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001745
Douglas Gregor631f6c62009-04-14 00:24:19 +00001746 // Note external declarations so that we can add them to a record
1747 // in the PCH file later.
1748 if (isa<FileScopeAsmDecl>(D))
1749 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001750 }
1751
1752 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001753 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001754}
1755
Douglas Gregorff9a6092009-04-20 20:36:09 +00001756namespace {
1757class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1758 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001759 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001760
1761public:
1762 typedef const IdentifierInfo* key_type;
1763 typedef key_type key_type_ref;
1764
1765 typedef pch::IdentID data_type;
1766 typedef data_type data_type_ref;
1767
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001768 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1769 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001770
1771 static unsigned ComputeHash(const IdentifierInfo* II) {
1772 return clang::BernsteinHash(II->getName());
1773 }
1774
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001775 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001776 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1777 pch::IdentID ID) {
1778 unsigned KeyLen = strlen(II->getName()) + 1;
1779 clang::io::Emit16(Out, KeyLen);
Douglas Gregorc713da92009-04-21 22:25:48 +00001780 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1781 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001782 if (II->hasMacroDefinition() &&
1783 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1784 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001785 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1786 DEnd = IdentifierResolver::end();
1787 D != DEnd; ++D)
1788 DataLen += sizeof(pch::DeclID);
Douglas Gregorc713da92009-04-21 22:25:48 +00001789 clang::io::Emit16(Out, DataLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001790 return std::make_pair(KeyLen, DataLen);
1791 }
1792
1793 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1794 unsigned KeyLen) {
1795 // Record the location of the key data. This is used when generating
1796 // the mapping from persistent IDs to strings.
1797 Writer.SetIdentifierOffset(II, Out.tell());
1798 Out.write(II->getName(), KeyLen);
1799 }
1800
1801 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1802 pch::IdentID ID, unsigned) {
1803 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001804 bool hasMacroDefinition =
1805 II->hasMacroDefinition() &&
1806 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001807 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001808 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
1809 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001810 Bits = (Bits << 1) | II->isExtensionToken();
1811 Bits = (Bits << 1) | II->isPoisoned();
1812 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1813 clang::io::Emit32(Out, Bits);
1814 clang::io::Emit32(Out, ID);
1815
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001816 if (hasMacroDefinition)
1817 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1818
Douglas Gregorc713da92009-04-21 22:25:48 +00001819 // Emit the declaration IDs in reverse order, because the
1820 // IdentifierResolver provides the declarations as they would be
1821 // visible (e.g., the function "stat" would come before the struct
1822 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1823 // adds declarations to the end of the list (so we need to see the
1824 // struct "status" before the function "status").
1825 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1826 IdentifierResolver::end());
1827 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1828 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001829 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001830 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001831 }
1832};
1833} // end anonymous namespace
1834
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001835/// \brief Write the identifier table into the PCH file.
1836///
1837/// The identifier table consists of a blob containing string data
1838/// (the actual identifiers themselves) and a separate "offsets" index
1839/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001840void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001841 using namespace llvm;
1842
1843 // Create and write out the blob that contains the identifier
1844 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001845 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001846 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001847 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1848
1849 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001850 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1851 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1852 ID != IDEnd; ++ID) {
1853 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorff9a6092009-04-20 20:36:09 +00001854 Generator.insert(ID->first, ID->second);
1855 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001856
Douglas Gregorff9a6092009-04-20 20:36:09 +00001857 // Create the on-disk hash table in a buffer.
1858 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001859 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001860 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001861 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001862 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc713da92009-04-21 22:25:48 +00001863 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001864 }
1865
1866 // Create a blob abbreviation
1867 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1868 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001871 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001872
1873 // Write the identifier table
1874 RecordData Record;
1875 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001876 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001877 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1878 &IdentifierTable.front(),
1879 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001880 }
1881
1882 // Write the offsets table for identifier IDs.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001883 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001884}
1885
Douglas Gregor1c507882009-04-15 21:30:51 +00001886/// \brief Write a record containing the given attributes.
1887void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1888 RecordData Record;
1889 for (; Attr; Attr = Attr->getNext()) {
1890 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1891 Record.push_back(Attr->isInherited());
1892 switch (Attr->getKind()) {
1893 case Attr::Alias:
1894 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1895 break;
1896
1897 case Attr::Aligned:
1898 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1899 break;
1900
1901 case Attr::AlwaysInline:
1902 break;
1903
1904 case Attr::AnalyzerNoReturn:
1905 break;
1906
1907 case Attr::Annotate:
1908 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1909 break;
1910
1911 case Attr::AsmLabel:
1912 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1913 break;
1914
1915 case Attr::Blocks:
1916 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1917 break;
1918
1919 case Attr::Cleanup:
1920 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1921 break;
1922
1923 case Attr::Const:
1924 break;
1925
1926 case Attr::Constructor:
1927 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1928 break;
1929
1930 case Attr::DLLExport:
1931 case Attr::DLLImport:
1932 case Attr::Deprecated:
1933 break;
1934
1935 case Attr::Destructor:
1936 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1937 break;
1938
1939 case Attr::FastCall:
1940 break;
1941
1942 case Attr::Format: {
1943 const FormatAttr *Format = cast<FormatAttr>(Attr);
1944 AddString(Format->getType(), Record);
1945 Record.push_back(Format->getFormatIdx());
1946 Record.push_back(Format->getFirstArg());
1947 break;
1948 }
1949
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001950 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001951 case Attr::IBOutletKind:
1952 case Attr::NoReturn:
1953 case Attr::NoThrow:
1954 case Attr::Nodebug:
1955 case Attr::Noinline:
1956 break;
1957
1958 case Attr::NonNull: {
1959 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1960 Record.push_back(NonNull->size());
1961 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1962 break;
1963 }
1964
1965 case Attr::ObjCException:
1966 case Attr::ObjCNSObject:
1967 case Attr::Overloadable:
1968 break;
1969
1970 case Attr::Packed:
1971 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1972 break;
1973
1974 case Attr::Pure:
1975 break;
1976
1977 case Attr::Regparm:
1978 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1979 break;
1980
1981 case Attr::Section:
1982 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1983 break;
1984
1985 case Attr::StdCall:
1986 case Attr::TransparentUnion:
1987 case Attr::Unavailable:
1988 case Attr::Unused:
1989 case Attr::Used:
1990 break;
1991
1992 case Attr::Visibility:
1993 // FIXME: stable encoding
1994 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1995 break;
1996
1997 case Attr::WarnUnusedResult:
1998 case Attr::Weak:
1999 case Attr::WeakImport:
2000 break;
2001 }
2002 }
2003
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002004 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002005}
2006
2007void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2008 Record.push_back(Str.size());
2009 Record.insert(Record.end(), Str.begin(), Str.end());
2010}
2011
Douglas Gregorff9a6092009-04-20 20:36:09 +00002012/// \brief Note that the identifier II occurs at the given offset
2013/// within the identifier table.
2014void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
2015 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
2016}
2017
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002018PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002019 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002020 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2021 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002022
Douglas Gregor87887da2009-04-20 15:53:59 +00002023void PCHWriter::WritePCH(Sema &SemaRef) {
2024 ASTContext &Context = SemaRef.Context;
2025 Preprocessor &PP = SemaRef.PP;
2026
Douglas Gregorc34897d2009-04-09 22:27:44 +00002027 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002028 Stream.Emit((unsigned)'C', 8);
2029 Stream.Emit((unsigned)'P', 8);
2030 Stream.Emit((unsigned)'C', 8);
2031 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002032
2033 // The translation unit is the first declaration we'll emit.
2034 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2035 DeclsToEmit.push(Context.getTranslationUnitDecl());
2036
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002037 // Make sure that we emit IdentifierInfos (and any attached
2038 // declarations) for builtins.
2039 {
2040 IdentifierTable &Table = PP.getIdentifierTable();
2041 llvm::SmallVector<const char *, 32> BuiltinNames;
2042 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2043 Context.getLangOptions().NoBuiltin);
2044 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2045 getIdentifierRef(&Table.get(BuiltinNames[I]));
2046 }
2047
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002048 // Build a record containing all of the tentative definitions in
2049 // this header file. Generally, this record will be empty.
2050 RecordData TentativeDefinitions;
2051 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2052 TD = SemaRef.TentativeDefinitions.begin(),
2053 TDEnd = SemaRef.TentativeDefinitions.end();
2054 TD != TDEnd; ++TD)
2055 AddDeclRef(TD->second, TentativeDefinitions);
2056
Douglas Gregor062d9482009-04-22 22:18:58 +00002057 // Build a record containing all of the locally-scoped external
2058 // declarations in this header file. Generally, this record will be
2059 // empty.
2060 RecordData LocallyScopedExternalDecls;
2061 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2062 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2063 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2064 TD != TDEnd; ++TD)
2065 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2066
Douglas Gregorc34897d2009-04-09 22:27:44 +00002067 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002068 RecordData Record;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002069 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002070 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002071 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00002072 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002073 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002074 WriteTypesBlock(Context);
2075 WriteDeclsBlock(Context);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002076 WriteIdentifierTable(PP);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002077 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
2078 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregore01ad442009-04-18 05:55:16 +00002079
2080 // Write the record of special types.
2081 Record.clear();
2082 AddTypeRef(Context.getBuiltinVaListType(), Record);
2083 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2084
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002085 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002086 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002087 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002088
2089 // Write the record containing tentative definitions.
2090 if (!TentativeDefinitions.empty())
2091 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002092
2093 // Write the record containing locally-scoped external definitions.
2094 if (!LocallyScopedExternalDecls.empty())
2095 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2096 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002097
2098 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002099 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002100 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002101 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002102 Record.push_back(NumLexicalDeclContexts);
2103 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002104 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002105 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002106}
2107
2108void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2109 Record.push_back(Loc.getRawEncoding());
2110}
2111
2112void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2113 Record.push_back(Value.getBitWidth());
2114 unsigned N = Value.getNumWords();
2115 const uint64_t* Words = Value.getRawData();
2116 for (unsigned I = 0; I != N; ++I)
2117 Record.push_back(Words[I]);
2118}
2119
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002120void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2121 Record.push_back(Value.isUnsigned());
2122 AddAPInt(Value, Record);
2123}
2124
Douglas Gregore2f37202009-04-14 21:55:33 +00002125void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2126 AddAPInt(Value.bitcastToAPInt(), Record);
2127}
2128
Douglas Gregorc34897d2009-04-09 22:27:44 +00002129void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002130 Record.push_back(getIdentifierRef(II));
2131}
2132
2133pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2134 if (II == 0)
2135 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002136
2137 pch::IdentID &ID = IdentifierIDs[II];
2138 if (ID == 0)
2139 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002140 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002141}
2142
2143void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2144 if (T.isNull()) {
2145 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2146 return;
2147 }
2148
2149 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002150 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002151 switch (BT->getKind()) {
2152 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2153 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2154 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2155 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2156 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2157 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2158 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2159 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2160 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2161 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2162 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2163 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2164 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2165 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2166 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2167 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2168 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2169 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2170 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2171 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2172 }
2173
2174 Record.push_back((ID << 3) | T.getCVRQualifiers());
2175 return;
2176 }
2177
Douglas Gregorac8f2802009-04-10 17:25:41 +00002178 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002179 if (ID == 0) // we haven't seen this type before
2180 ID = NextTypeID++;
2181
2182 // Encode the type qualifiers in the type reference.
2183 Record.push_back((ID << 3) | T.getCVRQualifiers());
2184}
2185
2186void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2187 if (D == 0) {
2188 Record.push_back(0);
2189 return;
2190 }
2191
Douglas Gregorac8f2802009-04-10 17:25:41 +00002192 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002193 if (ID == 0) {
2194 // We haven't seen this declaration before. Give it a new ID and
2195 // enqueue it in the list of declarations to emit.
2196 ID = DeclIDs.size();
2197 DeclsToEmit.push(const_cast<Decl *>(D));
2198 }
2199
2200 Record.push_back(ID);
2201}
2202
Douglas Gregorff9a6092009-04-20 20:36:09 +00002203pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2204 if (D == 0)
2205 return 0;
2206
2207 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2208 return DeclIDs[D];
2209}
2210
Douglas Gregorc34897d2009-04-09 22:27:44 +00002211void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2212 Record.push_back(Name.getNameKind());
2213 switch (Name.getNameKind()) {
2214 case DeclarationName::Identifier:
2215 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2216 break;
2217
2218 case DeclarationName::ObjCZeroArgSelector:
2219 case DeclarationName::ObjCOneArgSelector:
2220 case DeclarationName::ObjCMultiArgSelector:
2221 assert(false && "Serialization of Objective-C selectors unavailable");
2222 break;
2223
2224 case DeclarationName::CXXConstructorName:
2225 case DeclarationName::CXXDestructorName:
2226 case DeclarationName::CXXConversionFunctionName:
2227 AddTypeRef(Name.getCXXNameType(), Record);
2228 break;
2229
2230 case DeclarationName::CXXOperatorName:
2231 Record.push_back(Name.getCXXOverloadedOperator());
2232 break;
2233
2234 case DeclarationName::CXXUsingDirective:
2235 // No extra data to emit
2236 break;
2237 }
2238}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002239
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002240/// \brief Write the given substatement or subexpression to the
2241/// bitstream.
2242void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002243 RecordData Record;
2244 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002245 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002246
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002247 if (!S) {
2248 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002249 return;
2250 }
2251
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002252 Writer.Code = pch::STMT_NULL_PTR;
2253 Writer.Visit(S);
2254 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002255 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002256 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002257}
2258
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002259/// \brief Flush all of the statements that have been added to the
2260/// queue via AddStmt().
2261void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002262 RecordData Record;
2263 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002264
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002265 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002266 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002267 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002268
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002269 if (!S) {
2270 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002271 continue;
2272 }
2273
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002274 Writer.Code = pch::STMT_NULL_PTR;
2275 Writer.Visit(S);
2276 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002277 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002278 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002279
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002280 assert(N == StmtsToEmit.size() &&
2281 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002282
2283 // Note that we are at the end of a full expression. Any
2284 // expression records that follow this one are part of a different
2285 // expression.
2286 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002287 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002288 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002289
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002290 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002291 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002292}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002293
2294unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2295 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2296 "SwitchCase recorded twice");
2297 unsigned NextID = SwitchCaseIDs.size();
2298 SwitchCaseIDs[S] = NextID;
2299 return NextID;
2300}
2301
2302unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2303 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2304 "SwitchCase hasn't been seen yet");
2305 return SwitchCaseIDs[S];
2306}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002307
2308/// \brief Retrieve the ID for the given label statement, which may
2309/// or may not have been emitted yet.
2310unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2311 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2312 if (Pos != LabelIDs.end())
2313 return Pos->second;
2314
2315 unsigned NextID = LabelIDs.size();
2316 LabelIDs[S] = NextID;
2317 return NextID;
2318}