blob: a3b1253a66531e46be0671b55a6f0555980239be [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);
496 // FIXME: Implement.
497 // Abstract class (no need to define a stable pch::DECL code).
498}
499
500void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
501 VisitObjCImplDecl(D);
502 // FIXME: Implement.
503 Code = pch::DECL_OBJC_CATEGORY_IMPL;
504}
505
506void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
507 VisitObjCImplDecl(D);
508 // FIXME: Implement.
509 Code = pch::DECL_OBJC_IMPLEMENTATION;
510}
511
512void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
513 VisitDecl(D);
514 // FIXME: Implement.
515 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff7333b492009-04-20 20:09:33 +0000516}
517
Douglas Gregor982365e2009-04-13 21:20:57 +0000518void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
519 VisitValueDecl(D);
520 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000521 Record.push_back(D->getBitWidth()? 1 : 0);
522 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000523 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000524 Code = pch::DECL_FIELD;
525}
526
Douglas Gregorc34897d2009-04-09 22:27:44 +0000527void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
528 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000529 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000530 Record.push_back(D->isThreadSpecified());
531 Record.push_back(D->hasCXXDirectInitializer());
532 Record.push_back(D->isDeclaredInCondition());
533 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
534 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000535 Record.push_back(D->getInit()? 1 : 0);
536 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000537 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000538 Code = pch::DECL_VAR;
539}
540
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000541void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
542 VisitVarDecl(D);
543 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000544 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000545 // FIXME: why isn't the "default argument" just stored as the initializer
546 // in VarDecl?
547 Code = pch::DECL_PARM_VAR;
548}
549
550void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
551 VisitParmVarDecl(D);
552 Writer.AddTypeRef(D->getOriginalType(), Record);
553 Code = pch::DECL_ORIGINAL_PARM_VAR;
554}
555
Douglas Gregor2a491792009-04-13 22:49:25 +0000556void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
557 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000558 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000559 Code = pch::DECL_FILE_SCOPE_ASM;
560}
561
562void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
563 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000564 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000565 Record.push_back(D->param_size());
566 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
567 P != PEnd; ++P)
568 Writer.AddDeclRef(*P, Record);
569 Code = pch::DECL_BLOCK;
570}
571
Douglas Gregorc34897d2009-04-09 22:27:44 +0000572/// \brief Emit the DeclContext part of a declaration context decl.
573///
574/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
575/// block for this declaration context is stored. May be 0 to indicate
576/// that there are no declarations stored within this context.
577///
578/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
579/// block for this declaration context is stored. May be 0 to indicate
580/// that there are no declarations visible from this context. Note
581/// that this value will not be emitted for non-primary declaration
582/// contexts.
583void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
584 uint64_t VisibleOffset) {
585 Record.push_back(LexicalOffset);
Douglas Gregor405b6432009-04-22 19:09:20 +0000586 Record.push_back(VisibleOffset);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000587}
588
589//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000590// Statement/expression serialization
591//===----------------------------------------------------------------------===//
592namespace {
593 class VISIBILITY_HIDDEN PCHStmtWriter
594 : public StmtVisitor<PCHStmtWriter, void> {
595
596 PCHWriter &Writer;
597 PCHWriter::RecordData &Record;
598
599 public:
600 pch::StmtCode Code;
601
602 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
603 : Writer(Writer), Record(Record) { }
604
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000605 void VisitStmt(Stmt *S);
606 void VisitNullStmt(NullStmt *S);
607 void VisitCompoundStmt(CompoundStmt *S);
608 void VisitSwitchCase(SwitchCase *S);
609 void VisitCaseStmt(CaseStmt *S);
610 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000611 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000612 void VisitIfStmt(IfStmt *S);
613 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000614 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000615 void VisitDoStmt(DoStmt *S);
616 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000617 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000618 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000619 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000620 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000621 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000622 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000623 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000624 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000625 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000626 void VisitDeclRefExpr(DeclRefExpr *E);
627 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000628 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000629 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000630 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000631 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000632 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000633 void VisitUnaryOperator(UnaryOperator *E);
634 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000635 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000636 void VisitCallExpr(CallExpr *E);
637 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000638 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000639 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000640 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
641 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000642 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000643 void VisitExplicitCastExpr(ExplicitCastExpr *E);
644 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000645 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000646 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000647 void VisitInitListExpr(InitListExpr *E);
648 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
649 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000650 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000651 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000652 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000653 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
654 void VisitChooseExpr(ChooseExpr *E);
655 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000656 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000657 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000658 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000659
660 // Objective-C
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000661 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000662 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000663 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
664 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000665 };
666}
667
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000668void PCHStmtWriter::VisitStmt(Stmt *S) {
669}
670
671void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
672 VisitStmt(S);
673 Writer.AddSourceLocation(S->getSemiLoc(), Record);
674 Code = pch::STMT_NULL;
675}
676
677void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
678 VisitStmt(S);
679 Record.push_back(S->size());
680 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
681 CS != CSEnd; ++CS)
682 Writer.WriteSubStmt(*CS);
683 Writer.AddSourceLocation(S->getLBracLoc(), Record);
684 Writer.AddSourceLocation(S->getRBracLoc(), Record);
685 Code = pch::STMT_COMPOUND;
686}
687
688void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
689 VisitStmt(S);
690 Record.push_back(Writer.RecordSwitchCaseID(S));
691}
692
693void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
694 VisitSwitchCase(S);
695 Writer.WriteSubStmt(S->getLHS());
696 Writer.WriteSubStmt(S->getRHS());
697 Writer.WriteSubStmt(S->getSubStmt());
698 Writer.AddSourceLocation(S->getCaseLoc(), Record);
699 Code = pch::STMT_CASE;
700}
701
702void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
703 VisitSwitchCase(S);
704 Writer.WriteSubStmt(S->getSubStmt());
705 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
706 Code = pch::STMT_DEFAULT;
707}
708
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000709void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
710 VisitStmt(S);
711 Writer.AddIdentifierRef(S->getID(), Record);
712 Writer.WriteSubStmt(S->getSubStmt());
713 Writer.AddSourceLocation(S->getIdentLoc(), Record);
714 Record.push_back(Writer.GetLabelID(S));
715 Code = pch::STMT_LABEL;
716}
717
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000718void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
719 VisitStmt(S);
720 Writer.WriteSubStmt(S->getCond());
721 Writer.WriteSubStmt(S->getThen());
722 Writer.WriteSubStmt(S->getElse());
723 Writer.AddSourceLocation(S->getIfLoc(), Record);
724 Code = pch::STMT_IF;
725}
726
727void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
728 VisitStmt(S);
729 Writer.WriteSubStmt(S->getCond());
730 Writer.WriteSubStmt(S->getBody());
731 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
732 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
733 SC = SC->getNextSwitchCase())
734 Record.push_back(Writer.getSwitchCaseID(SC));
735 Code = pch::STMT_SWITCH;
736}
737
Douglas Gregora6b503f2009-04-17 00:16:09 +0000738void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
739 VisitStmt(S);
740 Writer.WriteSubStmt(S->getCond());
741 Writer.WriteSubStmt(S->getBody());
742 Writer.AddSourceLocation(S->getWhileLoc(), Record);
743 Code = pch::STMT_WHILE;
744}
745
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000746void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
747 VisitStmt(S);
748 Writer.WriteSubStmt(S->getCond());
749 Writer.WriteSubStmt(S->getBody());
750 Writer.AddSourceLocation(S->getDoLoc(), Record);
751 Code = pch::STMT_DO;
752}
753
754void PCHStmtWriter::VisitForStmt(ForStmt *S) {
755 VisitStmt(S);
756 Writer.WriteSubStmt(S->getInit());
757 Writer.WriteSubStmt(S->getCond());
758 Writer.WriteSubStmt(S->getInc());
759 Writer.WriteSubStmt(S->getBody());
760 Writer.AddSourceLocation(S->getForLoc(), Record);
761 Code = pch::STMT_FOR;
762}
763
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000764void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
765 VisitStmt(S);
766 Record.push_back(Writer.GetLabelID(S->getLabel()));
767 Writer.AddSourceLocation(S->getGotoLoc(), Record);
768 Writer.AddSourceLocation(S->getLabelLoc(), Record);
769 Code = pch::STMT_GOTO;
770}
771
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000772void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
773 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000774 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000775 Writer.WriteSubStmt(S->getTarget());
776 Code = pch::STMT_INDIRECT_GOTO;
777}
778
Douglas Gregora6b503f2009-04-17 00:16:09 +0000779void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
780 VisitStmt(S);
781 Writer.AddSourceLocation(S->getContinueLoc(), Record);
782 Code = pch::STMT_CONTINUE;
783}
784
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000785void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
786 VisitStmt(S);
787 Writer.AddSourceLocation(S->getBreakLoc(), Record);
788 Code = pch::STMT_BREAK;
789}
790
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000791void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
792 VisitStmt(S);
793 Writer.WriteSubStmt(S->getRetValue());
794 Writer.AddSourceLocation(S->getReturnLoc(), Record);
795 Code = pch::STMT_RETURN;
796}
797
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000798void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
799 VisitStmt(S);
800 Writer.AddSourceLocation(S->getStartLoc(), Record);
801 Writer.AddSourceLocation(S->getEndLoc(), Record);
802 DeclGroupRef DG = S->getDeclGroup();
803 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
804 Writer.AddDeclRef(*D, Record);
805 Code = pch::STMT_DECL;
806}
807
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000808void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
809 VisitStmt(S);
810 Record.push_back(S->getNumOutputs());
811 Record.push_back(S->getNumInputs());
812 Record.push_back(S->getNumClobbers());
813 Writer.AddSourceLocation(S->getAsmLoc(), Record);
814 Writer.AddSourceLocation(S->getRParenLoc(), Record);
815 Record.push_back(S->isVolatile());
816 Record.push_back(S->isSimple());
817 Writer.WriteSubStmt(S->getAsmString());
818
819 // Outputs
820 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
821 Writer.AddString(S->getOutputName(I), Record);
822 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
823 Writer.WriteSubStmt(S->getOutputExpr(I));
824 }
825
826 // Inputs
827 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
828 Writer.AddString(S->getInputName(I), Record);
829 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
830 Writer.WriteSubStmt(S->getInputExpr(I));
831 }
832
833 // Clobbers
834 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
835 Writer.WriteSubStmt(S->getClobber(I));
836
837 Code = pch::STMT_ASM;
838}
839
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000840void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000841 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000842 Writer.AddTypeRef(E->getType(), Record);
843 Record.push_back(E->isTypeDependent());
844 Record.push_back(E->isValueDependent());
845}
846
Douglas Gregore2f37202009-04-14 21:55:33 +0000847void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
848 VisitExpr(E);
849 Writer.AddSourceLocation(E->getLocation(), Record);
850 Record.push_back(E->getIdentType()); // FIXME: stable encoding
851 Code = pch::EXPR_PREDEFINED;
852}
853
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000854void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
855 VisitExpr(E);
856 Writer.AddDeclRef(E->getDecl(), Record);
857 Writer.AddSourceLocation(E->getLocation(), Record);
858 Code = pch::EXPR_DECL_REF;
859}
860
861void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
862 VisitExpr(E);
863 Writer.AddSourceLocation(E->getLocation(), Record);
864 Writer.AddAPInt(E->getValue(), Record);
865 Code = pch::EXPR_INTEGER_LITERAL;
866}
867
Douglas Gregore2f37202009-04-14 21:55:33 +0000868void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
869 VisitExpr(E);
870 Writer.AddAPFloat(E->getValue(), Record);
871 Record.push_back(E->isExact());
872 Writer.AddSourceLocation(E->getLocation(), Record);
873 Code = pch::EXPR_FLOATING_LITERAL;
874}
875
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000876void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
877 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000878 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000879 Code = pch::EXPR_IMAGINARY_LITERAL;
880}
881
Douglas Gregor596e0932009-04-15 16:35:07 +0000882void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
883 VisitExpr(E);
884 Record.push_back(E->getByteLength());
885 Record.push_back(E->getNumConcatenated());
886 Record.push_back(E->isWide());
887 // FIXME: String data should be stored as a blob at the end of the
888 // StringLiteral. However, we can't do so now because we have no
889 // provision for coping with abbreviations when we're jumping around
890 // the PCH file during deserialization.
891 Record.insert(Record.end(),
892 E->getStrData(), E->getStrData() + E->getByteLength());
893 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
894 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
895 Code = pch::EXPR_STRING_LITERAL;
896}
897
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000898void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
899 VisitExpr(E);
900 Record.push_back(E->getValue());
901 Writer.AddSourceLocation(E->getLoc(), Record);
902 Record.push_back(E->isWide());
903 Code = pch::EXPR_CHARACTER_LITERAL;
904}
905
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000906void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
907 VisitExpr(E);
908 Writer.AddSourceLocation(E->getLParen(), Record);
909 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000910 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000911 Code = pch::EXPR_PAREN;
912}
913
Douglas Gregor12d74052009-04-15 15:58:59 +0000914void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
915 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000916 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000917 Record.push_back(E->getOpcode()); // FIXME: stable encoding
918 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
919 Code = pch::EXPR_UNARY_OPERATOR;
920}
921
922void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
923 VisitExpr(E);
924 Record.push_back(E->isSizeOf());
925 if (E->isArgumentType())
926 Writer.AddTypeRef(E->getArgumentType(), Record);
927 else {
928 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000929 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000930 }
931 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
932 Writer.AddSourceLocation(E->getRParenLoc(), Record);
933 Code = pch::EXPR_SIZEOF_ALIGN_OF;
934}
935
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000936void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
937 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000938 Writer.WriteSubStmt(E->getLHS());
939 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000940 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
941 Code = pch::EXPR_ARRAY_SUBSCRIPT;
942}
943
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000944void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
945 VisitExpr(E);
946 Record.push_back(E->getNumArgs());
947 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000948 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000949 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
950 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000951 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000952 Code = pch::EXPR_CALL;
953}
954
955void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
956 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000957 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000958 Writer.AddDeclRef(E->getMemberDecl(), Record);
959 Writer.AddSourceLocation(E->getMemberLoc(), Record);
960 Record.push_back(E->isArrow());
961 Code = pch::EXPR_MEMBER;
962}
963
Douglas Gregora151ba42009-04-14 23:32:43 +0000964void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
965 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000966 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000967}
968
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000969void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
970 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000971 Writer.WriteSubStmt(E->getLHS());
972 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000973 Record.push_back(E->getOpcode()); // FIXME: stable encoding
974 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
975 Code = pch::EXPR_BINARY_OPERATOR;
976}
977
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000978void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
979 VisitBinaryOperator(E);
980 Writer.AddTypeRef(E->getComputationLHSType(), Record);
981 Writer.AddTypeRef(E->getComputationResultType(), Record);
982 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
983}
984
985void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
986 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000987 Writer.WriteSubStmt(E->getCond());
988 Writer.WriteSubStmt(E->getLHS());
989 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000990 Code = pch::EXPR_CONDITIONAL_OPERATOR;
991}
992
Douglas Gregora151ba42009-04-14 23:32:43 +0000993void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
994 VisitCastExpr(E);
995 Record.push_back(E->isLvalueCast());
996 Code = pch::EXPR_IMPLICIT_CAST;
997}
998
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000999void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1000 VisitCastExpr(E);
1001 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1002}
1003
1004void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1005 VisitExplicitCastExpr(E);
1006 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1007 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1008 Code = pch::EXPR_CSTYLE_CAST;
1009}
1010
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001011void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1012 VisitExpr(E);
1013 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001014 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001015 Record.push_back(E->isFileScope());
1016 Code = pch::EXPR_COMPOUND_LITERAL;
1017}
1018
Douglas Gregorec0b8292009-04-15 23:02:49 +00001019void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1020 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001021 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001022 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1023 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1024 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1025}
1026
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001027void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1028 VisitExpr(E);
1029 Record.push_back(E->getNumInits());
1030 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001031 Writer.WriteSubStmt(E->getInit(I));
1032 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001033 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1034 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1035 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1036 Record.push_back(E->hadArrayRangeDesignator());
1037 Code = pch::EXPR_INIT_LIST;
1038}
1039
1040void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1041 VisitExpr(E);
1042 Record.push_back(E->getNumSubExprs());
1043 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001044 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001045 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1046 Record.push_back(E->usesGNUSyntax());
1047 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1048 DEnd = E->designators_end();
1049 D != DEnd; ++D) {
1050 if (D->isFieldDesignator()) {
1051 if (FieldDecl *Field = D->getField()) {
1052 Record.push_back(pch::DESIG_FIELD_DECL);
1053 Writer.AddDeclRef(Field, Record);
1054 } else {
1055 Record.push_back(pch::DESIG_FIELD_NAME);
1056 Writer.AddIdentifierRef(D->getFieldName(), Record);
1057 }
1058 Writer.AddSourceLocation(D->getDotLoc(), Record);
1059 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1060 } else if (D->isArrayDesignator()) {
1061 Record.push_back(pch::DESIG_ARRAY);
1062 Record.push_back(D->getFirstExprIndex());
1063 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1064 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1065 } else {
1066 assert(D->isArrayRangeDesignator() && "Unknown designator");
1067 Record.push_back(pch::DESIG_ARRAY_RANGE);
1068 Record.push_back(D->getFirstExprIndex());
1069 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1070 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1071 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1072 }
1073 }
1074 Code = pch::EXPR_DESIGNATED_INIT;
1075}
1076
1077void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1078 VisitExpr(E);
1079 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1080}
1081
Douglas Gregorec0b8292009-04-15 23:02:49 +00001082void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1083 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001084 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001085 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1086 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1087 Code = pch::EXPR_VA_ARG;
1088}
1089
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001090void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1091 VisitExpr(E);
1092 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1093 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1094 Record.push_back(Writer.GetLabelID(E->getLabel()));
1095 Code = pch::EXPR_ADDR_LABEL;
1096}
1097
Douglas Gregoreca12f62009-04-17 19:05:30 +00001098void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1099 VisitExpr(E);
1100 Writer.WriteSubStmt(E->getSubStmt());
1101 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1102 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1103 Code = pch::EXPR_STMT;
1104}
1105
Douglas Gregor209d4622009-04-15 23:33:31 +00001106void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1107 VisitExpr(E);
1108 Writer.AddTypeRef(E->getArgType1(), Record);
1109 Writer.AddTypeRef(E->getArgType2(), Record);
1110 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1111 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1112 Code = pch::EXPR_TYPES_COMPATIBLE;
1113}
1114
1115void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1116 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001117 Writer.WriteSubStmt(E->getCond());
1118 Writer.WriteSubStmt(E->getLHS());
1119 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001120 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1121 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1122 Code = pch::EXPR_CHOOSE;
1123}
1124
1125void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1126 VisitExpr(E);
1127 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1128 Code = pch::EXPR_GNU_NULL;
1129}
1130
Douglas Gregor725e94b2009-04-16 00:01:45 +00001131void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1132 VisitExpr(E);
1133 Record.push_back(E->getNumSubExprs());
1134 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001135 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001136 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1137 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1138 Code = pch::EXPR_SHUFFLE_VECTOR;
1139}
1140
Douglas Gregore246b742009-04-17 19:21:43 +00001141void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1142 VisitExpr(E);
1143 Writer.AddDeclRef(E->getBlockDecl(), Record);
1144 Record.push_back(E->hasBlockDeclRefExprs());
1145 Code = pch::EXPR_BLOCK;
1146}
1147
Douglas Gregor725e94b2009-04-16 00:01:45 +00001148void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1149 VisitExpr(E);
1150 Writer.AddDeclRef(E->getDecl(), Record);
1151 Writer.AddSourceLocation(E->getLocation(), Record);
1152 Record.push_back(E->isByRef());
1153 Code = pch::EXPR_BLOCK_DECL_REF;
1154}
1155
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001156//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001157// Objective-C Expressions and Statements.
1158//===----------------------------------------------------------------------===//
1159
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001160void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1161 VisitExpr(E);
1162 Writer.WriteSubStmt(E->getString());
1163 Writer.AddSourceLocation(E->getAtLoc(), Record);
1164 Code = pch::EXPR_OBJC_STRING_LITERAL;
1165}
1166
Chris Lattner80f83c62009-04-22 05:57:30 +00001167void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1168 VisitExpr(E);
1169 Writer.AddTypeRef(E->getEncodedType(), Record);
1170 Writer.AddSourceLocation(E->getAtLoc(), Record);
1171 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1172 Code = pch::EXPR_OBJC_ENCODE;
1173}
1174
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001175void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1176 VisitExpr(E);
Chris Lattner9b9f2352009-04-22 06:40:03 +00001177 assert(0 && "Can't write a selector yet!");
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001178 // FIXME! Write selectors.
1179 //Writer.WriteSubStmt(E->getSelector());
1180 Writer.AddSourceLocation(E->getAtLoc(), Record);
1181 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1182 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1183}
1184
1185void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1186 VisitExpr(E);
1187 Writer.AddDeclRef(E->getProtocol(), Record);
1188 Writer.AddSourceLocation(E->getAtLoc(), Record);
1189 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1190 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1191}
1192
Chris Lattner80f83c62009-04-22 05:57:30 +00001193
1194//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001195// PCHWriter Implementation
1196//===----------------------------------------------------------------------===//
1197
Douglas Gregorb5887f32009-04-10 21:16:55 +00001198/// \brief Write the target triple (e.g., i686-apple-darwin9).
1199void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1200 using namespace llvm;
1201 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1202 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001204 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001205
1206 RecordData Record;
1207 Record.push_back(pch::TARGET_TRIPLE);
1208 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001209 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001210}
1211
1212/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001213void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1214 RecordData Record;
1215 Record.push_back(LangOpts.Trigraphs);
1216 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1217 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1218 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1219 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1220 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1221 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1222 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1223 Record.push_back(LangOpts.C99); // C99 Support
1224 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1225 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1226 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1227 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1228 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1229
1230 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1231 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1232 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1233
1234 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1235 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1236 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1237 Record.push_back(LangOpts.LaxVectorConversions);
1238 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1239
1240 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1241 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1242 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1243
1244 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1245 // by locks.
1246 Record.push_back(LangOpts.Blocks); // block extension to C
1247 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1248 // they are unused.
1249 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1250 // (modulo the platform support).
1251
1252 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1253 // signed integer arithmetic overflows.
1254
1255 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1256 // may be ripped out at any time.
1257
1258 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1259 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1260 // defined.
1261 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1262 // opposed to __DYNAMIC__).
1263 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1264
1265 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1266 // used (instead of C99 semantics).
1267 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1268 Record.push_back(LangOpts.getGCMode());
1269 Record.push_back(LangOpts.getVisibilityMode());
1270 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001271 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001272}
1273
Douglas Gregorab1cef72009-04-10 03:52:48 +00001274//===----------------------------------------------------------------------===//
1275// Source Manager Serialization
1276//===----------------------------------------------------------------------===//
1277
1278/// \brief Create an abbreviation for the SLocEntry that refers to a
1279/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001280static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001281 using namespace llvm;
1282 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1283 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001289 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001290}
1291
1292/// \brief Create an abbreviation for the SLocEntry that refers to a
1293/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001294static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001295 using namespace llvm;
1296 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1297 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001303 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001304}
1305
1306/// \brief Create an abbreviation for the SLocEntry that refers to a
1307/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001308static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001309 using namespace llvm;
1310 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1311 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001313 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001314}
1315
1316/// \brief Create an abbreviation for the SLocEntry that refers to an
1317/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001318static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001319 using namespace llvm;
1320 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1321 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1324 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001327 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001328}
1329
1330/// \brief Writes the block containing the serialized form of the
1331/// source manager.
1332///
1333/// TODO: We should probably use an on-disk hash table (stored in a
1334/// blob), indexed based on the file name, so that we only create
1335/// entries for files that we actually need. In the common case (no
1336/// errors), we probably won't have to create file entries for any of
1337/// the files in the AST.
1338void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001339 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001340 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001341
1342 // Abbreviations for the various kinds of source-location entries.
1343 int SLocFileAbbrv = -1;
1344 int SLocBufferAbbrv = -1;
1345 int SLocBufferBlobAbbrv = -1;
1346 int SLocInstantiationAbbrv = -1;
1347
1348 // Write out the source location entry table. We skip the first
1349 // entry, which is always the same dummy entry.
1350 RecordData Record;
1351 for (SourceManager::sloc_entry_iterator
1352 SLoc = SourceMgr.sloc_entry_begin() + 1,
1353 SLocEnd = SourceMgr.sloc_entry_end();
1354 SLoc != SLocEnd; ++SLoc) {
1355 // Figure out which record code to use.
1356 unsigned Code;
1357 if (SLoc->isFile()) {
1358 if (SLoc->getFile().getContentCache()->Entry)
1359 Code = pch::SM_SLOC_FILE_ENTRY;
1360 else
1361 Code = pch::SM_SLOC_BUFFER_ENTRY;
1362 } else
1363 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1364 Record.push_back(Code);
1365
1366 Record.push_back(SLoc->getOffset());
1367 if (SLoc->isFile()) {
1368 const SrcMgr::FileInfo &File = SLoc->getFile();
1369 Record.push_back(File.getIncludeLoc().getRawEncoding());
1370 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001371 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001372
1373 const SrcMgr::ContentCache *Content = File.getContentCache();
1374 if (Content->Entry) {
1375 // The source location entry is a file. The blob associated
1376 // with this entry is the file name.
1377 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001378 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1379 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001380 Content->Entry->getName(),
1381 strlen(Content->Entry->getName()));
1382 } else {
1383 // The source location entry is a buffer. The blob associated
1384 // with this entry contains the contents of the buffer.
1385 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001386 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1387 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001388 }
1389
1390 // We add one to the size so that we capture the trailing NULL
1391 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1392 // the reader side).
1393 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1394 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001395 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001396 Record.clear();
1397 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001398 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001399 Buffer->getBufferStart(),
1400 Buffer->getBufferSize() + 1);
1401 }
1402 } else {
1403 // The source location entry is an instantiation.
1404 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1405 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1406 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1407 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1408
Douglas Gregor364e5802009-04-15 18:05:10 +00001409 // Compute the token length for this macro expansion.
1410 unsigned NextOffset = SourceMgr.getNextOffset();
1411 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1412 if (++NextSLoc != SLocEnd)
1413 NextOffset = NextSLoc->getOffset();
1414 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1415
Douglas Gregorab1cef72009-04-10 03:52:48 +00001416 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001417 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1418 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001419 }
1420
1421 Record.clear();
1422 }
1423
Douglas Gregor635f97f2009-04-13 16:31:14 +00001424 // Write the line table.
1425 if (SourceMgr.hasLineTable()) {
1426 LineTableInfo &LineTable = SourceMgr.getLineTable();
1427
1428 // Emit the file names
1429 Record.push_back(LineTable.getNumFilenames());
1430 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1431 // Emit the file name
1432 const char *Filename = LineTable.getFilename(I);
1433 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1434 Record.push_back(FilenameLen);
1435 if (FilenameLen)
1436 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1437 }
1438
1439 // Emit the line entries
1440 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1441 L != LEnd; ++L) {
1442 // Emit the file ID
1443 Record.push_back(L->first);
1444
1445 // Emit the line entries
1446 Record.push_back(L->second.size());
1447 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1448 LEEnd = L->second.end();
1449 LE != LEEnd; ++LE) {
1450 Record.push_back(LE->FileOffset);
1451 Record.push_back(LE->LineNo);
1452 Record.push_back(LE->FilenameID);
1453 Record.push_back((unsigned)LE->FileKind);
1454 Record.push_back(LE->IncludeOffset);
1455 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001456 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001457 }
1458 }
1459
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001460 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001461}
1462
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001463/// \brief Writes the block containing the serialized form of the
1464/// preprocessor.
1465///
Chris Lattner850eabd2009-04-10 18:08:30 +00001466void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001467 // Enter the preprocessor block.
Douglas Gregorc713da92009-04-21 22:25:48 +00001468 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner84b04f12009-04-10 17:16:57 +00001469
Chris Lattner1b094952009-04-10 18:00:12 +00001470 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1471 // FIXME: use diagnostics subsystem for localization etc.
1472 if (PP.SawDateOrTime())
1473 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001474
Chris Lattner1b094952009-04-10 18:00:12 +00001475 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001476
Chris Lattner4b21c202009-04-13 01:29:17 +00001477 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1478 if (PP.getCounterValue() != 0) {
1479 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001480 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001481 Record.clear();
1482 }
1483
Chris Lattner1b094952009-04-10 18:00:12 +00001484 // Loop over all the macro definitions that are live at the end of the file,
1485 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001486 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1487 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001488 // FIXME: This emits macros in hash table order, we should do it in a stable
1489 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001490 MacroInfo *MI = I->second;
1491
1492 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1493 // been redefined by the header (in which case they are not isBuiltinMacro).
1494 if (MI->isBuiltinMacro())
1495 continue;
1496
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001497 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001498 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001499 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001500 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1501 Record.push_back(MI->isUsed());
1502
1503 unsigned Code;
1504 if (MI->isObjectLike()) {
1505 Code = pch::PP_MACRO_OBJECT_LIKE;
1506 } else {
1507 Code = pch::PP_MACRO_FUNCTION_LIKE;
1508
1509 Record.push_back(MI->isC99Varargs());
1510 Record.push_back(MI->isGNUVarargs());
1511 Record.push_back(MI->getNumArgs());
1512 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1513 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001514 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001515 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001516 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001517 Record.clear();
1518
Chris Lattner850eabd2009-04-10 18:08:30 +00001519 // Emit the tokens array.
1520 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1521 // Note that we know that the preprocessor does not have any annotation
1522 // tokens in it because they are created by the parser, and thus can't be
1523 // in a macro definition.
1524 const Token &Tok = MI->getReplacementToken(TokNo);
1525
1526 Record.push_back(Tok.getLocation().getRawEncoding());
1527 Record.push_back(Tok.getLength());
1528
Chris Lattner850eabd2009-04-10 18:08:30 +00001529 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1530 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001531 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001532
1533 // FIXME: Should translate token kind to a stable encoding.
1534 Record.push_back(Tok.getKind());
1535 // FIXME: Should translate token flags to a stable encoding.
1536 Record.push_back(Tok.getFlags());
1537
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001538 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001539 Record.clear();
1540 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001541 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001542 }
1543
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001544 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001545}
1546
1547
Douglas Gregorc34897d2009-04-09 22:27:44 +00001548/// \brief Write the representation of a type to the PCH stream.
1549void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001550 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001551 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001552 ID = NextTypeID++;
1553
1554 // Record the offset for this type.
1555 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001556 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001557 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1558 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001559 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001560 }
1561
1562 RecordData Record;
1563
1564 // Emit the type's representation.
1565 PCHTypeWriter W(*this, Record);
1566 switch (T->getTypeClass()) {
1567 // For all of the concrete, non-dependent types, call the
1568 // appropriate visitor function.
1569#define TYPE(Class, Base) \
1570 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1571#define ABSTRACT_TYPE(Class, Base)
1572#define DEPENDENT_TYPE(Class, Base)
1573#include "clang/AST/TypeNodes.def"
1574
1575 // For all of the dependent type nodes (which only occur in C++
1576 // templates), produce an error.
1577#define TYPE(Class, Base)
1578#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1579#include "clang/AST/TypeNodes.def"
1580 assert(false && "Cannot serialize dependent type nodes");
1581 break;
1582 }
1583
1584 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001585 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001586
1587 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001588 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001589}
1590
1591/// \brief Write a block containing all of the types.
1592void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001593 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001594 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001595
1596 // Emit all of the types in the ASTContext
1597 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1598 TEnd = Context.getTypes().end();
1599 T != TEnd; ++T) {
1600 // Builtin types are never serialized.
1601 if (isa<BuiltinType>(*T))
1602 continue;
1603
1604 WriteType(*T);
1605 }
1606
1607 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001608 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001609}
1610
1611/// \brief Write the block containing all of the declaration IDs
1612/// lexically declared within the given DeclContext.
1613///
1614/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1615/// bistream, or 0 if no block was written.
1616uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1617 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001618 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001619 return 0;
1620
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001621 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001622 RecordData Record;
1623 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1624 DEnd = DC->decls_end(Context);
1625 D != DEnd; ++D)
1626 AddDeclRef(*D, Record);
1627
Douglas Gregoraf136d92009-04-22 22:34:57 +00001628 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001629 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001630 return Offset;
1631}
1632
1633/// \brief Write the block containing all of the declaration IDs
1634/// visible from the given DeclContext.
1635///
1636/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1637/// bistream, or 0 if no block was written.
1638uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1639 DeclContext *DC) {
1640 if (DC->getPrimaryContext() != DC)
1641 return 0;
1642
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001643 // Since there is no name lookup into functions or methods, and we
1644 // perform name lookup for the translation unit via the
1645 // IdentifierInfo chains, don't bother to build a
1646 // visible-declarations table for these entities.
1647 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001648 return 0;
1649
Douglas Gregorc34897d2009-04-09 22:27:44 +00001650 // Force the DeclContext to build a its name-lookup table.
1651 DC->lookup(Context, DeclarationName());
1652
1653 // Serialize the contents of the mapping used for lookup. Note that,
1654 // although we have two very different code paths, the serialized
1655 // representation is the same for both cases: a declaration name,
1656 // followed by a size, followed by references to the visible
1657 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001658 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001659 RecordData Record;
1660 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001661 if (!Map)
1662 return 0;
1663
Douglas Gregorc34897d2009-04-09 22:27:44 +00001664 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1665 D != DEnd; ++D) {
1666 AddDeclarationName(D->first, Record);
1667 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1668 Record.push_back(Result.second - Result.first);
1669 for(; Result.first != Result.second; ++Result.first)
1670 AddDeclRef(*Result.first, Record);
1671 }
1672
1673 if (Record.size() == 0)
1674 return 0;
1675
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001676 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001677 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001678 return Offset;
1679}
1680
1681/// \brief Write a block containing all of the declarations.
1682void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001683 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001684 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001685
1686 // Emit all of the declarations.
1687 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001688 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001689 while (!DeclsToEmit.empty()) {
1690 // Pull the next declaration off the queue
1691 Decl *D = DeclsToEmit.front();
1692 DeclsToEmit.pop();
1693
1694 // If this declaration is also a DeclContext, write blocks for the
1695 // declarations that lexically stored inside its context and those
1696 // declarations that are visible from its context. These blocks
1697 // are written before the declaration itself so that we can put
1698 // their offsets into the record for the declaration.
1699 uint64_t LexicalOffset = 0;
1700 uint64_t VisibleOffset = 0;
1701 DeclContext *DC = dyn_cast<DeclContext>(D);
1702 if (DC) {
1703 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1704 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1705 }
1706
1707 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001708 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001709 if (ID == 0)
1710 ID = DeclIDs.size();
1711
1712 unsigned Index = ID - 1;
1713
1714 // Record the offset for this declaration
1715 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001716 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001717 else if (DeclOffsets.size() < Index) {
1718 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001719 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001720 }
1721
1722 // Build and emit a record for this declaration
1723 Record.clear();
1724 W.Code = (pch::DeclCode)0;
1725 W.Visit(D);
1726 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001727
1728 if (!W.Code) {
1729 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1730 D->getDeclKindName());
1731 assert(false && "Unhandled declaration kind while generating PCH");
1732 exit(-1);
1733 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001734 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001735
Douglas Gregor1c507882009-04-15 21:30:51 +00001736 // If the declaration had any attributes, write them now.
1737 if (D->hasAttrs())
1738 WriteAttributeRecord(D->getAttrs());
1739
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001740 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001741 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001742
Douglas Gregor631f6c62009-04-14 00:24:19 +00001743 // Note external declarations so that we can add them to a record
1744 // in the PCH file later.
1745 if (isa<FileScopeAsmDecl>(D))
1746 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001747 }
1748
1749 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001750 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001751}
1752
Douglas Gregorff9a6092009-04-20 20:36:09 +00001753namespace {
1754class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1755 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001756 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001757
1758public:
1759 typedef const IdentifierInfo* key_type;
1760 typedef key_type key_type_ref;
1761
1762 typedef pch::IdentID data_type;
1763 typedef data_type data_type_ref;
1764
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001765 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1766 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001767
1768 static unsigned ComputeHash(const IdentifierInfo* II) {
1769 return clang::BernsteinHash(II->getName());
1770 }
1771
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001772 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001773 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1774 pch::IdentID ID) {
1775 unsigned KeyLen = strlen(II->getName()) + 1;
1776 clang::io::Emit16(Out, KeyLen);
Douglas Gregorc713da92009-04-21 22:25:48 +00001777 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1778 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001779 if (II->hasMacroDefinition() &&
1780 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1781 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001782 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1783 DEnd = IdentifierResolver::end();
1784 D != DEnd; ++D)
1785 DataLen += sizeof(pch::DeclID);
Douglas Gregorc713da92009-04-21 22:25:48 +00001786 clang::io::Emit16(Out, DataLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001787 return std::make_pair(KeyLen, DataLen);
1788 }
1789
1790 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1791 unsigned KeyLen) {
1792 // Record the location of the key data. This is used when generating
1793 // the mapping from persistent IDs to strings.
1794 Writer.SetIdentifierOffset(II, Out.tell());
1795 Out.write(II->getName(), KeyLen);
1796 }
1797
1798 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1799 pch::IdentID ID, unsigned) {
1800 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001801 bool hasMacroDefinition =
1802 II->hasMacroDefinition() &&
1803 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001804 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001805 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
1806 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001807 Bits = (Bits << 1) | II->isExtensionToken();
1808 Bits = (Bits << 1) | II->isPoisoned();
1809 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1810 clang::io::Emit32(Out, Bits);
1811 clang::io::Emit32(Out, ID);
1812
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001813 if (hasMacroDefinition)
1814 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1815
Douglas Gregorc713da92009-04-21 22:25:48 +00001816 // Emit the declaration IDs in reverse order, because the
1817 // IdentifierResolver provides the declarations as they would be
1818 // visible (e.g., the function "stat" would come before the struct
1819 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1820 // adds declarations to the end of the list (so we need to see the
1821 // struct "status" before the function "status").
1822 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1823 IdentifierResolver::end());
1824 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1825 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001826 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001827 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001828 }
1829};
1830} // end anonymous namespace
1831
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001832/// \brief Write the identifier table into the PCH file.
1833///
1834/// The identifier table consists of a blob containing string data
1835/// (the actual identifiers themselves) and a separate "offsets" index
1836/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001837void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001838 using namespace llvm;
1839
1840 // Create and write out the blob that contains the identifier
1841 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001842 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001843 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001844 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1845
1846 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001847 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1848 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1849 ID != IDEnd; ++ID) {
1850 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorff9a6092009-04-20 20:36:09 +00001851 Generator.insert(ID->first, ID->second);
1852 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001853
Douglas Gregorff9a6092009-04-20 20:36:09 +00001854 // Create the on-disk hash table in a buffer.
1855 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001856 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001857 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001858 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001859 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc713da92009-04-21 22:25:48 +00001860 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001861 }
1862
1863 // Create a blob abbreviation
1864 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1865 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001868 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001869
1870 // Write the identifier table
1871 RecordData Record;
1872 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001873 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001874 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1875 &IdentifierTable.front(),
1876 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001877 }
1878
1879 // Write the offsets table for identifier IDs.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001880 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001881}
1882
Douglas Gregor1c507882009-04-15 21:30:51 +00001883/// \brief Write a record containing the given attributes.
1884void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1885 RecordData Record;
1886 for (; Attr; Attr = Attr->getNext()) {
1887 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1888 Record.push_back(Attr->isInherited());
1889 switch (Attr->getKind()) {
1890 case Attr::Alias:
1891 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1892 break;
1893
1894 case Attr::Aligned:
1895 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1896 break;
1897
1898 case Attr::AlwaysInline:
1899 break;
1900
1901 case Attr::AnalyzerNoReturn:
1902 break;
1903
1904 case Attr::Annotate:
1905 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1906 break;
1907
1908 case Attr::AsmLabel:
1909 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1910 break;
1911
1912 case Attr::Blocks:
1913 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1914 break;
1915
1916 case Attr::Cleanup:
1917 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1918 break;
1919
1920 case Attr::Const:
1921 break;
1922
1923 case Attr::Constructor:
1924 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1925 break;
1926
1927 case Attr::DLLExport:
1928 case Attr::DLLImport:
1929 case Attr::Deprecated:
1930 break;
1931
1932 case Attr::Destructor:
1933 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1934 break;
1935
1936 case Attr::FastCall:
1937 break;
1938
1939 case Attr::Format: {
1940 const FormatAttr *Format = cast<FormatAttr>(Attr);
1941 AddString(Format->getType(), Record);
1942 Record.push_back(Format->getFormatIdx());
1943 Record.push_back(Format->getFirstArg());
1944 break;
1945 }
1946
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001947 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001948 case Attr::IBOutletKind:
1949 case Attr::NoReturn:
1950 case Attr::NoThrow:
1951 case Attr::Nodebug:
1952 case Attr::Noinline:
1953 break;
1954
1955 case Attr::NonNull: {
1956 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1957 Record.push_back(NonNull->size());
1958 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1959 break;
1960 }
1961
1962 case Attr::ObjCException:
1963 case Attr::ObjCNSObject:
1964 case Attr::Overloadable:
1965 break;
1966
1967 case Attr::Packed:
1968 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1969 break;
1970
1971 case Attr::Pure:
1972 break;
1973
1974 case Attr::Regparm:
1975 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1976 break;
1977
1978 case Attr::Section:
1979 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1980 break;
1981
1982 case Attr::StdCall:
1983 case Attr::TransparentUnion:
1984 case Attr::Unavailable:
1985 case Attr::Unused:
1986 case Attr::Used:
1987 break;
1988
1989 case Attr::Visibility:
1990 // FIXME: stable encoding
1991 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1992 break;
1993
1994 case Attr::WarnUnusedResult:
1995 case Attr::Weak:
1996 case Attr::WeakImport:
1997 break;
1998 }
1999 }
2000
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002001 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002002}
2003
2004void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2005 Record.push_back(Str.size());
2006 Record.insert(Record.end(), Str.begin(), Str.end());
2007}
2008
Douglas Gregorff9a6092009-04-20 20:36:09 +00002009/// \brief Note that the identifier II occurs at the given offset
2010/// within the identifier table.
2011void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
2012 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
2013}
2014
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002015PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002016 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002017 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2018 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002019
Douglas Gregor87887da2009-04-20 15:53:59 +00002020void PCHWriter::WritePCH(Sema &SemaRef) {
2021 ASTContext &Context = SemaRef.Context;
2022 Preprocessor &PP = SemaRef.PP;
2023
Douglas Gregorc34897d2009-04-09 22:27:44 +00002024 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002025 Stream.Emit((unsigned)'C', 8);
2026 Stream.Emit((unsigned)'P', 8);
2027 Stream.Emit((unsigned)'C', 8);
2028 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002029
2030 // The translation unit is the first declaration we'll emit.
2031 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2032 DeclsToEmit.push(Context.getTranslationUnitDecl());
2033
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002034 // Make sure that we emit IdentifierInfos (and any attached
2035 // declarations) for builtins.
2036 {
2037 IdentifierTable &Table = PP.getIdentifierTable();
2038 llvm::SmallVector<const char *, 32> BuiltinNames;
2039 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2040 Context.getLangOptions().NoBuiltin);
2041 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2042 getIdentifierRef(&Table.get(BuiltinNames[I]));
2043 }
2044
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002045 // Build a record containing all of the tentative definitions in
2046 // this header file. Generally, this record will be empty.
2047 RecordData TentativeDefinitions;
2048 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2049 TD = SemaRef.TentativeDefinitions.begin(),
2050 TDEnd = SemaRef.TentativeDefinitions.end();
2051 TD != TDEnd; ++TD)
2052 AddDeclRef(TD->second, TentativeDefinitions);
2053
Douglas Gregor062d9482009-04-22 22:18:58 +00002054 // Build a record containing all of the locally-scoped external
2055 // declarations in this header file. Generally, this record will be
2056 // empty.
2057 RecordData LocallyScopedExternalDecls;
2058 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2059 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2060 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2061 TD != TDEnd; ++TD)
2062 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2063
Douglas Gregorc34897d2009-04-09 22:27:44 +00002064 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002065 RecordData Record;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002066 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002067 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002068 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00002069 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002070 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002071 WriteTypesBlock(Context);
2072 WriteDeclsBlock(Context);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002073 WriteIdentifierTable(PP);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002074 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
2075 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregore01ad442009-04-18 05:55:16 +00002076
2077 // Write the record of special types.
2078 Record.clear();
2079 AddTypeRef(Context.getBuiltinVaListType(), Record);
2080 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2081
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002082 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002083 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002084 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002085
2086 // Write the record containing tentative definitions.
2087 if (!TentativeDefinitions.empty())
2088 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002089
2090 // Write the record containing locally-scoped external definitions.
2091 if (!LocallyScopedExternalDecls.empty())
2092 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2093 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002094
2095 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002096 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002097 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002098 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002099 Record.push_back(NumLexicalDeclContexts);
2100 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002101 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002102 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002103}
2104
2105void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2106 Record.push_back(Loc.getRawEncoding());
2107}
2108
2109void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2110 Record.push_back(Value.getBitWidth());
2111 unsigned N = Value.getNumWords();
2112 const uint64_t* Words = Value.getRawData();
2113 for (unsigned I = 0; I != N; ++I)
2114 Record.push_back(Words[I]);
2115}
2116
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002117void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2118 Record.push_back(Value.isUnsigned());
2119 AddAPInt(Value, Record);
2120}
2121
Douglas Gregore2f37202009-04-14 21:55:33 +00002122void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2123 AddAPInt(Value.bitcastToAPInt(), Record);
2124}
2125
Douglas Gregorc34897d2009-04-09 22:27:44 +00002126void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002127 Record.push_back(getIdentifierRef(II));
2128}
2129
2130pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2131 if (II == 0)
2132 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002133
2134 pch::IdentID &ID = IdentifierIDs[II];
2135 if (ID == 0)
2136 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002137 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002138}
2139
2140void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2141 if (T.isNull()) {
2142 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2143 return;
2144 }
2145
2146 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002147 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002148 switch (BT->getKind()) {
2149 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2150 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2151 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2152 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2153 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2154 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2155 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2156 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2157 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2158 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2159 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2160 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2161 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2162 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2163 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2164 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2165 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2166 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2167 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2168 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2169 }
2170
2171 Record.push_back((ID << 3) | T.getCVRQualifiers());
2172 return;
2173 }
2174
Douglas Gregorac8f2802009-04-10 17:25:41 +00002175 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002176 if (ID == 0) // we haven't seen this type before
2177 ID = NextTypeID++;
2178
2179 // Encode the type qualifiers in the type reference.
2180 Record.push_back((ID << 3) | T.getCVRQualifiers());
2181}
2182
2183void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2184 if (D == 0) {
2185 Record.push_back(0);
2186 return;
2187 }
2188
Douglas Gregorac8f2802009-04-10 17:25:41 +00002189 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002190 if (ID == 0) {
2191 // We haven't seen this declaration before. Give it a new ID and
2192 // enqueue it in the list of declarations to emit.
2193 ID = DeclIDs.size();
2194 DeclsToEmit.push(const_cast<Decl *>(D));
2195 }
2196
2197 Record.push_back(ID);
2198}
2199
Douglas Gregorff9a6092009-04-20 20:36:09 +00002200pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2201 if (D == 0)
2202 return 0;
2203
2204 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2205 return DeclIDs[D];
2206}
2207
Douglas Gregorc34897d2009-04-09 22:27:44 +00002208void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2209 Record.push_back(Name.getNameKind());
2210 switch (Name.getNameKind()) {
2211 case DeclarationName::Identifier:
2212 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2213 break;
2214
2215 case DeclarationName::ObjCZeroArgSelector:
2216 case DeclarationName::ObjCOneArgSelector:
2217 case DeclarationName::ObjCMultiArgSelector:
2218 assert(false && "Serialization of Objective-C selectors unavailable");
2219 break;
2220
2221 case DeclarationName::CXXConstructorName:
2222 case DeclarationName::CXXDestructorName:
2223 case DeclarationName::CXXConversionFunctionName:
2224 AddTypeRef(Name.getCXXNameType(), Record);
2225 break;
2226
2227 case DeclarationName::CXXOperatorName:
2228 Record.push_back(Name.getCXXOverloadedOperator());
2229 break;
2230
2231 case DeclarationName::CXXUsingDirective:
2232 // No extra data to emit
2233 break;
2234 }
2235}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002236
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002237/// \brief Write the given substatement or subexpression to the
2238/// bitstream.
2239void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002240 RecordData Record;
2241 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002242 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002243
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002244 if (!S) {
2245 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002246 return;
2247 }
2248
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002249 Writer.Code = pch::STMT_NULL_PTR;
2250 Writer.Visit(S);
2251 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002252 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002253 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002254}
2255
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002256/// \brief Flush all of the statements that have been added to the
2257/// queue via AddStmt().
2258void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002259 RecordData Record;
2260 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002261
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002262 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002263 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002264 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002265
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002266 if (!S) {
2267 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002268 continue;
2269 }
2270
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002271 Writer.Code = pch::STMT_NULL_PTR;
2272 Writer.Visit(S);
2273 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002274 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002275 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002276
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002277 assert(N == StmtsToEmit.size() &&
2278 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002279
2280 // Note that we are at the end of a full expression. Any
2281 // expression records that follow this one are part of a different
2282 // expression.
2283 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002284 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002285 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002286
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002287 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002288 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002289}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002290
2291unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2292 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2293 "SwitchCase recorded twice");
2294 unsigned NextID = SwitchCaseIDs.size();
2295 SwitchCaseIDs[S] = NextID;
2296 return NextID;
2297}
2298
2299unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2300 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2301 "SwitchCase hasn't been seen yet");
2302 return SwitchCaseIDs[S];
2303}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002304
2305/// \brief Retrieve the ID for the given label statement, which may
2306/// or may not have been emitted yet.
2307unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2308 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2309 if (Pos != LabelIDs.end())
2310 return Pos->second;
2311
2312 unsigned NextID = LabelIDs.size();
2313 LabelIDs[S] = NextID;
2314 return NextID;
2315}