blob: a6faf9ba999145cb0fa638623a311c14711dee98 [file] [log] [blame]
Douglas Gregor2cf26342009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregore7785042009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump1eb44332009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000033#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000045
Douglas Gregor2cf26342009-04-09 22:27:44 +000046namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000047 class PCHTypeWriter {
Douglas Gregor2cf26342009-04-09 22:27:44 +000048 PCHWriter &Writer;
49 PCHWriter::RecordData &Record;
50
51 public:
52 /// \brief Type code that corresponds to the record generated.
53 pch::TypeCode Code;
54
Mike Stump1eb44332009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
58 void VisitArrayType(const ArrayType *T);
59 void VisitFunctionType(const FunctionType *T);
60 void VisitTagType(const TagType *T);
61
62#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63#define ABSTRACT_TYPE(Class, Base)
64#define DEPENDENT_TYPE(Class, Base)
65#include "clang/AST/TypeNodes.def"
66 };
67}
68
Douglas Gregor2cf26342009-04-09 22:27:44 +000069void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
Douglas Gregor2cf26342009-04-09 22:27:44 +000073void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
74 Writer.AddTypeRef(T->getElementType(), Record);
75 Code = pch::TYPE_COMPLEX;
76}
77
78void PCHTypeWriter::VisitPointerType(const PointerType *T) {
79 Writer.AddTypeRef(T->getPointeeType(), Record);
80 Code = pch::TYPE_POINTER;
81}
82
83void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000084 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000085 Code = pch::TYPE_BLOCK_POINTER;
86}
87
88void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_LVALUE_REFERENCE;
91}
92
93void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_RVALUE_REFERENCE;
96}
97
98void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000099 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101 Code = pch::TYPE_MEMBER_POINTER;
102}
103
104void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
105 Writer.AddTypeRef(T->getElementType(), Record);
106 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000107 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
110void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
111 VisitArrayType(T);
112 Writer.AddAPInt(T->getSize(), Record);
113 Code = pch::TYPE_CONSTANT_ARRAY;
114}
115
116void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
117 VisitArrayType(T);
118 Code = pch::TYPE_INCOMPLETE_ARRAY;
119}
120
121void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
122 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000123 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
124 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000125 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126 Code = pch::TYPE_VARIABLE_ARRAY;
127}
128
129void PCHTypeWriter::VisitVectorType(const VectorType *T) {
130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getNumElements());
John Thompson82287d12010-02-05 00:12:22 +0000132 Record.push_back(T->isAltiVec());
133 Record.push_back(T->isPixel());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 Code = pch::TYPE_VECTOR;
135}
136
137void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
138 VisitVectorType(T);
139 Code = pch::TYPE_EXT_VECTOR;
140}
141
142void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
143 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000144 FunctionType::ExtInfo C = T->getExtInfo();
145 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000146 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000147 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000148 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000149}
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());
Sebastian Redl465226e2009-05-27 22:11:52 +0000163 Record.push_back(T->hasExceptionSpec());
164 Record.push_back(T->hasAnyExceptionSpec());
165 Record.push_back(T->getNumExceptions());
166 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
167 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 Code = pch::TYPE_FUNCTION_PROTO;
169}
170
John McCalled976492009-12-04 22:46:56 +0000171#if 0
172// For when we want it....
173void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
174 Writer.AddDeclRef(T->getDecl(), Record);
175 Code = pch::TYPE_UNRESOLVED_USING;
176}
177#endif
178
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
180 Writer.AddDeclRef(T->getDecl(), Record);
181 Code = pch::TYPE_TYPEDEF;
182}
183
184void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000185 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000186 Code = pch::TYPE_TYPEOF_EXPR;
187}
188
189void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
190 Writer.AddTypeRef(T->getUnderlyingType(), Record);
191 Code = pch::TYPE_TYPEOF;
192}
193
Anders Carlsson395b4752009-06-24 19:06:50 +0000194void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
195 Writer.AddStmt(T->getUnderlyingExpr());
196 Code = pch::TYPE_DECLTYPE;
197}
198
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199void PCHTypeWriter::VisitTagType(const TagType *T) {
200 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000201 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000202 "Cannot serialize in the middle of a type definition");
203}
204
205void PCHTypeWriter::VisitRecordType(const RecordType *T) {
206 VisitTagType(T);
207 Code = pch::TYPE_RECORD;
208}
209
210void PCHTypeWriter::VisitEnumType(const EnumType *T) {
211 VisitTagType(T);
212 Code = pch::TYPE_ENUM;
213}
214
John McCall7da24312009-09-05 00:15:47 +0000215void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
216 Writer.AddTypeRef(T->getUnderlyingType(), Record);
217 Record.push_back(T->getTagKind());
218 Code = pch::TYPE_ELABORATED;
219}
220
Mike Stump1eb44332009-09-09 15:08:12 +0000221void
John McCall49a832b2009-10-18 09:09:24 +0000222PCHTypeWriter::VisitSubstTemplateTypeParmType(
223 const SubstTemplateTypeParmType *T) {
224 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
225 Writer.AddTypeRef(T->getReplacementType(), Record);
226 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
227}
228
229void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000230PCHTypeWriter::VisitTemplateSpecializationType(
231 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000232 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000233 assert(false && "Cannot serialize template specialization types");
234}
235
236void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000237 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000238 assert(false && "Cannot serialize qualified name types");
239}
240
John McCall3cb0ebd2010-03-10 03:28:59 +0000241void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
242 Writer.AddDeclRef(T->getDecl(), Record);
243 Writer.AddTypeRef(T->getUnderlyingType(), Record);
244 Code = pch::TYPE_INJECTED_CLASS_NAME;
245}
246
Douglas Gregor2cf26342009-04-09 22:27:44 +0000247void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
248 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000249 Record.push_back(T->getNumProtocols());
Steve Naroff446ee4e2009-05-27 16:21:00 +0000250 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
251 E = T->qual_end(); I != E; ++I)
252 Writer.AddDeclRef(*I, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000253 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000254}
255
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000256void
257PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000258 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000259 Record.push_back(T->getNumProtocols());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000260 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000261 E = T->qual_end(); I != E; ++I)
262 Writer.AddDeclRef(*I, Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000263 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264}
265
John McCalla1ee0c52009-10-16 21:56:05 +0000266namespace {
267
268class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
269 PCHWriter &Writer;
270 PCHWriter::RecordData &Record;
271
272public:
273 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
274 : Writer(Writer), Record(Record) { }
275
John McCall51bd8032009-10-18 01:05:36 +0000276#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000277#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000278 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000279#include "clang/AST/TypeLocNodes.def"
280
John McCall51bd8032009-10-18 01:05:36 +0000281 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
282 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000283};
284
285}
286
John McCall51bd8032009-10-18 01:05:36 +0000287void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
288 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000289}
John McCall51bd8032009-10-18 01:05:36 +0000290void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000291 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
292 if (TL.needsExtraLocalData()) {
293 Record.push_back(TL.getWrittenTypeSpec());
294 Record.push_back(TL.getWrittenSignSpec());
295 Record.push_back(TL.getWrittenWidthSpec());
296 Record.push_back(TL.hasModeAttr());
297 }
John McCalla1ee0c52009-10-16 21:56:05 +0000298}
John McCall51bd8032009-10-18 01:05:36 +0000299void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
300 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000301}
John McCall51bd8032009-10-18 01:05:36 +0000302void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
303 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000304}
John McCall51bd8032009-10-18 01:05:36 +0000305void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
306 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000307}
John McCall51bd8032009-10-18 01:05:36 +0000308void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
309 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000310}
John McCall51bd8032009-10-18 01:05:36 +0000311void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
312 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000313}
John McCall51bd8032009-10-18 01:05:36 +0000314void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
315 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000316}
John McCall51bd8032009-10-18 01:05:36 +0000317void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
318 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
319 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
320 Record.push_back(TL.getSizeExpr() ? 1 : 0);
321 if (TL.getSizeExpr())
322 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000323}
John McCall51bd8032009-10-18 01:05:36 +0000324void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
325 VisitArrayTypeLoc(TL);
326}
327void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
328 VisitArrayTypeLoc(TL);
329}
330void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
331 VisitArrayTypeLoc(TL);
332}
333void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
334 DependentSizedArrayTypeLoc TL) {
335 VisitArrayTypeLoc(TL);
336}
337void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
338 DependentSizedExtVectorTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getNameLoc(), Record);
340}
341void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
342 Writer.AddSourceLocation(TL.getNameLoc(), Record);
343}
344void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
345 Writer.AddSourceLocation(TL.getNameLoc(), Record);
346}
347void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
348 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
349 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
350 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
351 Writer.AddDeclRef(TL.getArg(i), Record);
352}
353void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
354 VisitFunctionTypeLoc(TL);
355}
356void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
357 VisitFunctionTypeLoc(TL);
358}
John McCalled976492009-12-04 22:46:56 +0000359void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
360 Writer.AddSourceLocation(TL.getNameLoc(), Record);
361}
John McCall51bd8032009-10-18 01:05:36 +0000362void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
363 Writer.AddSourceLocation(TL.getNameLoc(), Record);
364}
365void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000366 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
367 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
368 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000369}
370void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000371 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
372 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
373 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
374 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000375}
376void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
382void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getNameLoc(), Record);
384}
385void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getNameLoc(), Record);
387}
388void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
389 Writer.AddSourceLocation(TL.getNameLoc(), Record);
390}
John McCall49a832b2009-10-18 09:09:24 +0000391void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
392 SubstTemplateTypeParmTypeLoc TL) {
393 Writer.AddSourceLocation(TL.getNameLoc(), Record);
394}
John McCall51bd8032009-10-18 01:05:36 +0000395void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
396 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000397 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
398 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
399 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
400 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
401 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000402}
403void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getNameLoc(), Record);
405}
John McCall3cb0ebd2010-03-10 03:28:59 +0000406void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getNameLoc(), Record);
408}
Douglas Gregor4714c122010-03-31 17:34:00 +0000409void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
John McCall51bd8032009-10-18 01:05:36 +0000410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
411}
412void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000414 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
415 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
416 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
417 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000418}
John McCall54e14c42009-10-22 22:37:11 +0000419void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
420 Writer.AddSourceLocation(TL.getStarLoc(), Record);
421 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
422 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
423 Record.push_back(TL.hasBaseTypeAsWritten());
424 Record.push_back(TL.hasProtocolsAsWritten());
425 if (TL.hasProtocolsAsWritten())
426 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
427 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
428}
John McCalla1ee0c52009-10-16 21:56:05 +0000429
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000430//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000431// PCHWriter Implementation
432//===----------------------------------------------------------------------===//
433
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000434static void EmitBlockID(unsigned ID, const char *Name,
435 llvm::BitstreamWriter &Stream,
436 PCHWriter::RecordData &Record) {
437 Record.clear();
438 Record.push_back(ID);
439 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
440
441 // Emit the block name if present.
442 if (Name == 0 || Name[0] == 0) return;
443 Record.clear();
444 while (*Name)
445 Record.push_back(*Name++);
446 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
447}
448
449static void EmitRecordID(unsigned ID, const char *Name,
450 llvm::BitstreamWriter &Stream,
451 PCHWriter::RecordData &Record) {
452 Record.clear();
453 Record.push_back(ID);
454 while (*Name)
455 Record.push_back(*Name++);
456 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000457}
458
459static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
460 PCHWriter::RecordData &Record) {
461#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
462 RECORD(STMT_STOP);
463 RECORD(STMT_NULL_PTR);
464 RECORD(STMT_NULL);
465 RECORD(STMT_COMPOUND);
466 RECORD(STMT_CASE);
467 RECORD(STMT_DEFAULT);
468 RECORD(STMT_LABEL);
469 RECORD(STMT_IF);
470 RECORD(STMT_SWITCH);
471 RECORD(STMT_WHILE);
472 RECORD(STMT_DO);
473 RECORD(STMT_FOR);
474 RECORD(STMT_GOTO);
475 RECORD(STMT_INDIRECT_GOTO);
476 RECORD(STMT_CONTINUE);
477 RECORD(STMT_BREAK);
478 RECORD(STMT_RETURN);
479 RECORD(STMT_DECL);
480 RECORD(STMT_ASM);
481 RECORD(EXPR_PREDEFINED);
482 RECORD(EXPR_DECL_REF);
483 RECORD(EXPR_INTEGER_LITERAL);
484 RECORD(EXPR_FLOATING_LITERAL);
485 RECORD(EXPR_IMAGINARY_LITERAL);
486 RECORD(EXPR_STRING_LITERAL);
487 RECORD(EXPR_CHARACTER_LITERAL);
488 RECORD(EXPR_PAREN);
489 RECORD(EXPR_UNARY_OPERATOR);
490 RECORD(EXPR_SIZEOF_ALIGN_OF);
491 RECORD(EXPR_ARRAY_SUBSCRIPT);
492 RECORD(EXPR_CALL);
493 RECORD(EXPR_MEMBER);
494 RECORD(EXPR_BINARY_OPERATOR);
495 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
496 RECORD(EXPR_CONDITIONAL_OPERATOR);
497 RECORD(EXPR_IMPLICIT_CAST);
498 RECORD(EXPR_CSTYLE_CAST);
499 RECORD(EXPR_COMPOUND_LITERAL);
500 RECORD(EXPR_EXT_VECTOR_ELEMENT);
501 RECORD(EXPR_INIT_LIST);
502 RECORD(EXPR_DESIGNATED_INIT);
503 RECORD(EXPR_IMPLICIT_VALUE_INIT);
504 RECORD(EXPR_VA_ARG);
505 RECORD(EXPR_ADDR_LABEL);
506 RECORD(EXPR_STMT);
507 RECORD(EXPR_TYPES_COMPATIBLE);
508 RECORD(EXPR_CHOOSE);
509 RECORD(EXPR_GNU_NULL);
510 RECORD(EXPR_SHUFFLE_VECTOR);
511 RECORD(EXPR_BLOCK);
512 RECORD(EXPR_BLOCK_DECL_REF);
513 RECORD(EXPR_OBJC_STRING_LITERAL);
514 RECORD(EXPR_OBJC_ENCODE);
515 RECORD(EXPR_OBJC_SELECTOR_EXPR);
516 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
517 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
518 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
519 RECORD(EXPR_OBJC_KVC_REF_EXPR);
520 RECORD(EXPR_OBJC_MESSAGE_EXPR);
521 RECORD(EXPR_OBJC_SUPER_EXPR);
522 RECORD(STMT_OBJC_FOR_COLLECTION);
523 RECORD(STMT_OBJC_CATCH);
524 RECORD(STMT_OBJC_FINALLY);
525 RECORD(STMT_OBJC_AT_TRY);
526 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
527 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000528 RECORD(EXPR_CXX_OPERATOR_CALL);
529 RECORD(EXPR_CXX_CONSTRUCT);
530 RECORD(EXPR_CXX_STATIC_CAST);
531 RECORD(EXPR_CXX_DYNAMIC_CAST);
532 RECORD(EXPR_CXX_REINTERPRET_CAST);
533 RECORD(EXPR_CXX_CONST_CAST);
534 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
535 RECORD(EXPR_CXX_BOOL_LITERAL);
536 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000537#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000538}
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000540void PCHWriter::WriteBlockInfoBlock() {
541 RecordData Record;
542 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattner2f4efd12009-04-27 00:40:25 +0000544#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000545#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000547 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000548 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000549 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000550 RECORD(TYPE_OFFSET);
551 RECORD(DECL_OFFSET);
552 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000553 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000554 RECORD(IDENTIFIER_OFFSET);
555 RECORD(IDENTIFIER_TABLE);
556 RECORD(EXTERNAL_DEFINITIONS);
557 RECORD(SPECIAL_TYPES);
558 RECORD(STATISTICS);
559 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000560 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000561 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
562 RECORD(SELECTOR_OFFSETS);
563 RECORD(METHOD_POOL);
564 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000565 RECORD(SOURCE_LOCATION_OFFSETS);
566 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000567 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000568 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000569 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000570 RECORD(UNUSED_STATIC_FUNCS);
571 RECORD(MACRO_DEFINITION_OFFSETS);
572
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000573 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000574 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000575 RECORD(SM_SLOC_FILE_ENTRY);
576 RECORD(SM_SLOC_BUFFER_ENTRY);
577 RECORD(SM_SLOC_BUFFER_BLOB);
578 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
579 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000581 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000582 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000583 RECORD(PP_MACRO_OBJECT_LIKE);
584 RECORD(PP_MACRO_FUNCTION_LIKE);
585 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000586 RECORD(PP_MACRO_INSTANTIATION);
587 RECORD(PP_MACRO_DEFINITION);
588
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000589 // Decls and Types block.
590 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000591 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000592 RECORD(TYPE_COMPLEX);
593 RECORD(TYPE_POINTER);
594 RECORD(TYPE_BLOCK_POINTER);
595 RECORD(TYPE_LVALUE_REFERENCE);
596 RECORD(TYPE_RVALUE_REFERENCE);
597 RECORD(TYPE_MEMBER_POINTER);
598 RECORD(TYPE_CONSTANT_ARRAY);
599 RECORD(TYPE_INCOMPLETE_ARRAY);
600 RECORD(TYPE_VARIABLE_ARRAY);
601 RECORD(TYPE_VECTOR);
602 RECORD(TYPE_EXT_VECTOR);
603 RECORD(TYPE_FUNCTION_PROTO);
604 RECORD(TYPE_FUNCTION_NO_PROTO);
605 RECORD(TYPE_TYPEDEF);
606 RECORD(TYPE_TYPEOF_EXPR);
607 RECORD(TYPE_TYPEOF);
608 RECORD(TYPE_RECORD);
609 RECORD(TYPE_ENUM);
610 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000611 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000612 RECORD(DECL_ATTR);
613 RECORD(DECL_TRANSLATION_UNIT);
614 RECORD(DECL_TYPEDEF);
615 RECORD(DECL_ENUM);
616 RECORD(DECL_RECORD);
617 RECORD(DECL_ENUM_CONSTANT);
618 RECORD(DECL_FUNCTION);
619 RECORD(DECL_OBJC_METHOD);
620 RECORD(DECL_OBJC_INTERFACE);
621 RECORD(DECL_OBJC_PROTOCOL);
622 RECORD(DECL_OBJC_IVAR);
623 RECORD(DECL_OBJC_AT_DEFS_FIELD);
624 RECORD(DECL_OBJC_CLASS);
625 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
626 RECORD(DECL_OBJC_CATEGORY);
627 RECORD(DECL_OBJC_CATEGORY_IMPL);
628 RECORD(DECL_OBJC_IMPLEMENTATION);
629 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
630 RECORD(DECL_OBJC_PROPERTY);
631 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 RECORD(DECL_FIELD);
633 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000634 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000635 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000636 RECORD(DECL_FILE_SCOPE_ASM);
637 RECORD(DECL_BLOCK);
638 RECORD(DECL_CONTEXT_LEXICAL);
639 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000640 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000641 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000642#undef RECORD
643#undef BLOCK
644 Stream.ExitBlock();
645}
646
Douglas Gregore650c8c2009-07-07 00:12:59 +0000647/// \brief Adjusts the given filename to only write out the portion of the
648/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000649///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000650/// \param Filename the file name to adjust.
651///
652/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
653/// the returned filename will be adjusted by this system root.
654///
655/// \returns either the original filename (if it needs no adjustment) or the
656/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000657static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000658adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
659 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Douglas Gregore650c8c2009-07-07 00:12:59 +0000661 if (!isysroot)
662 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Douglas Gregore650c8c2009-07-07 00:12:59 +0000664 // Verify that the filename and the system root have the same prefix.
665 unsigned Pos = 0;
666 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
667 if (Filename[Pos] != isysroot[Pos])
668 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Douglas Gregore650c8c2009-07-07 00:12:59 +0000670 // We hit the end of the filename before we hit the end of the system root.
671 if (!Filename[Pos])
672 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Douglas Gregore650c8c2009-07-07 00:12:59 +0000674 // If the file name has a '/' at the current position, skip over the '/'.
675 // We distinguish sysroot-based includes from absolute includes by the
676 // absence of '/' at the beginning of sysroot-based includes.
677 if (Filename[Pos] == '/')
678 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Douglas Gregore650c8c2009-07-07 00:12:59 +0000680 return Filename + Pos;
681}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000682
Douglas Gregorab41e632009-04-27 22:23:34 +0000683/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000684void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000685 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000686
Douglas Gregore650c8c2009-07-07 00:12:59 +0000687 // Metadata
688 const TargetInfo &Target = Context.Target;
689 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
690 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
691 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
692 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
693 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
694 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
695 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
696 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
697 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Douglas Gregore650c8c2009-07-07 00:12:59 +0000699 RecordData Record;
700 Record.push_back(pch::METADATA);
701 Record.push_back(pch::VERSION_MAJOR);
702 Record.push_back(pch::VERSION_MINOR);
703 Record.push_back(CLANG_VERSION_MAJOR);
704 Record.push_back(CLANG_VERSION_MINOR);
705 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000706 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000707 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Douglas Gregorb64c1932009-05-12 01:31:05 +0000709 // Original file name
710 SourceManager &SM = Context.getSourceManager();
711 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
712 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
713 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
714 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
715 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
716
717 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000719 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000720
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000721 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000722 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000723 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000724 RecordData Record;
725 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000726 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000727 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000728
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000729 // Repository branch/version information.
730 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
731 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
732 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
733 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000734 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000735 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000736 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
737 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000738}
739
740/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000741void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
742 RecordData Record;
743 Record.push_back(LangOpts.Trigraphs);
744 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
745 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
746 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
747 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000748 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000749 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
750 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
751 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
752 Record.push_back(LangOpts.C99); // C99 Support
753 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
754 Record.push_back(LangOpts.CPlusPlus); // C++ Support
755 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000756 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000758 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
759 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000760 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000761 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000762 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000763 // modern abi enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000765 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000766 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
767 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000768 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000769 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000770 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000771
772 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
773 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
774 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
775
Chris Lattnerea5ce472009-04-27 07:35:58 +0000776 // Whether static initializers are protected by locks.
777 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000778 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000779 Record.push_back(LangOpts.Blocks); // block extension to C
780 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
781 // they are unused.
782 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
783 // (modulo the platform support).
784
785 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
786 // signed integer arithmetic overflows.
787
788 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
789 // may be ripped out at any time.
790
791 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000792 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000793 // defined.
794 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
795 // opposed to __DYNAMIC__).
796 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
797
798 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
799 // used (instead of C99 semantics).
800 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000801 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
802 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000803 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
804 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000805 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000806 Record.push_back(LangOpts.getGCMode());
807 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000808 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000809 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000810 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000811 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000812 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000813 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000814}
815
Douglas Gregor14f79002009-04-10 03:52:48 +0000816//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000817// stat cache Serialization
818//===----------------------------------------------------------------------===//
819
820namespace {
821// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000822class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000823public:
824 typedef const char * key_type;
825 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000827 typedef std::pair<int, struct stat> data_type;
828 typedef const data_type& data_type_ref;
829
830 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000831 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
834 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000835 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
836 data_type_ref Data) {
837 unsigned StrLen = strlen(path);
838 clang::io::Emit16(Out, StrLen);
839 unsigned DataLen = 1; // result value
840 if (Data.first == 0)
841 DataLen += 4 + 4 + 2 + 8 + 8;
842 clang::io::Emit8(Out, DataLen);
843 return std::make_pair(StrLen + 1, DataLen);
844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000846 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
847 Out.write(path, KeyLen);
848 }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000850 void EmitData(llvm::raw_ostream& Out, key_type_ref,
851 data_type_ref Data, unsigned DataLen) {
852 using namespace clang::io;
853 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000855 // Result of stat()
856 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000858 if (Data.first == 0) {
859 Emit32(Out, (uint32_t) Data.second.st_ino);
860 Emit32(Out, (uint32_t) Data.second.st_dev);
861 Emit16(Out, (uint16_t) Data.second.st_mode);
862 Emit64(Out, (uint64_t) Data.second.st_mtime);
863 Emit64(Out, (uint64_t) Data.second.st_size);
864 }
865
866 assert(Out.tell() - Start == DataLen && "Wrong data length");
867 }
868};
869} // end anonymous namespace
870
871/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000872void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
873 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000874 // Build the on-disk hash table containing information about every
875 // stat() call.
876 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
877 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000878 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000879 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000880 Stat != StatEnd; ++Stat, ++NumStatEntries) {
881 const char *Filename = Stat->first();
882 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
883 Generator.insert(Filename, Stat->second);
884 }
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000886 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000887 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000888 uint32_t BucketOffset;
889 {
890 llvm::raw_svector_ostream Out(StatCacheData);
891 // Make sure that no bucket is at offset 0
892 clang::io::Emit32(Out, 0);
893 BucketOffset = Generator.Emit(Out);
894 }
895
896 // Create a blob abbreviation
897 using namespace llvm;
898 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
899 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
903 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
904
905 // Write the stat cache
906 RecordData Record;
907 Record.push_back(pch::STAT_CACHE);
908 Record.push_back(BucketOffset);
909 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000910 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000911}
912
913//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000914// Source Manager Serialization
915//===----------------------------------------------------------------------===//
916
917/// \brief Create an abbreviation for the SLocEntry that refers to a
918/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000919static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000920 using namespace llvm;
921 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
922 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000927 // FileEntry fields.
928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000930 // HeaderFileInfo fields.
931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000936 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000937}
938
939/// \brief Create an abbreviation for the SLocEntry that refers to a
940/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000941static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000942 using namespace llvm;
943 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
944 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000950 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000951}
952
953/// \brief Create an abbreviation for the SLocEntry that refers to a
954/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000955static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000956 using namespace llvm;
957 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
958 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000960 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000961}
962
963/// \brief Create an abbreviation for the SLocEntry that refers to an
964/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000965static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000966 using namespace llvm;
967 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
968 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000974 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000975}
976
977/// \brief Writes the block containing the serialized form of the
978/// source manager.
979///
980/// TODO: We should probably use an on-disk hash table (stored in a
981/// blob), indexed based on the file name, so that we only create
982/// entries for files that we actually need. In the common case (no
983/// errors), we probably won't have to create file entries for any of
984/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000985void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000986 const Preprocessor &PP,
987 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000988 RecordData Record;
989
Chris Lattnerf04ad692009-04-10 17:16:57 +0000990 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000991 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000992
993 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000994 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
995 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
996 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
997 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000998
Douglas Gregorbd945002009-04-13 16:31:14 +0000999 // Write the line table.
1000 if (SourceMgr.hasLineTable()) {
1001 LineTableInfo &LineTable = SourceMgr.getLineTable();
1002
1003 // Emit the file names
1004 Record.push_back(LineTable.getNumFilenames());
1005 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1006 // Emit the file name
1007 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001008 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001009 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1010 Record.push_back(FilenameLen);
1011 if (FilenameLen)
1012 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1013 }
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregorbd945002009-04-13 16:31:14 +00001015 // Emit the line entries
1016 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1017 L != LEnd; ++L) {
1018 // Emit the file ID
1019 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Douglas Gregorbd945002009-04-13 16:31:14 +00001021 // Emit the line entries
1022 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001023 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001024 LEEnd = L->second.end();
1025 LE != LEEnd; ++LE) {
1026 Record.push_back(LE->FileOffset);
1027 Record.push_back(LE->LineNo);
1028 Record.push_back(LE->FilenameID);
1029 Record.push_back((unsigned)LE->FileKind);
1030 Record.push_back(LE->IncludeOffset);
1031 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001032 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001033 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001034 }
1035
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001036 // Write out the source location entry table. We skip the first
1037 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001038 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001039 RecordData PreloadSLocs;
1040 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001041 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1042 // Get this source location entry.
1043 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001044
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001045 // Record the offset of this source-location entry.
1046 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1047
1048 // Figure out which record code to use.
1049 unsigned Code;
1050 if (SLoc->isFile()) {
1051 if (SLoc->getFile().getContentCache()->Entry)
1052 Code = pch::SM_SLOC_FILE_ENTRY;
1053 else
1054 Code = pch::SM_SLOC_BUFFER_ENTRY;
1055 } else
1056 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1057 Record.clear();
1058 Record.push_back(Code);
1059
1060 Record.push_back(SLoc->getOffset());
1061 if (SLoc->isFile()) {
1062 const SrcMgr::FileInfo &File = SLoc->getFile();
1063 Record.push_back(File.getIncludeLoc().getRawEncoding());
1064 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1065 Record.push_back(File.hasLineDirectives());
1066
1067 const SrcMgr::ContentCache *Content = File.getContentCache();
1068 if (Content->Entry) {
1069 // The source location entry is a file. The blob associated
1070 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Douglas Gregor2d52be52010-03-21 22:49:54 +00001072 // Emit size/modification time for this file.
1073 Record.push_back(Content->Entry->getSize());
1074 Record.push_back(Content->Entry->getModificationTime());
1075
Douglas Gregor12fab312010-03-16 16:35:32 +00001076 // Emit header-search information associated with this file.
1077 HeaderFileInfo HFI;
1078 HeaderSearch &HS = PP.getHeaderSearchInfo();
1079 if (Content->Entry->getUID() < HS.header_file_size())
1080 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1081 Record.push_back(HFI.isImport);
1082 Record.push_back(HFI.DirInfo);
1083 Record.push_back(HFI.NumIncludes);
1084 AddIdentifierRef(HFI.ControllingMacro, Record);
1085
Douglas Gregore650c8c2009-07-07 00:12:59 +00001086 // Turn the file name into an absolute path, if it isn't already.
1087 const char *Filename = Content->Entry->getName();
1088 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001089 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001090 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Douglas Gregore650c8c2009-07-07 00:12:59 +00001092 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001093 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001094
1095 // FIXME: For now, preload all file source locations, so that
1096 // we get the appropriate File entries in the reader. This is
1097 // a temporary measure.
1098 PreloadSLocs.push_back(SLocEntryOffsets.size());
1099 } else {
1100 // The source location entry is a buffer. The blob associated
1101 // with this entry contains the contents of the buffer.
1102
1103 // We add one to the size so that we capture the trailing NULL
1104 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1105 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001106 const llvm::MemoryBuffer *Buffer
1107 = Content->getBuffer(PP.getDiagnostics());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001108 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001109 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1110 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001111 Record.clear();
1112 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1113 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001114 llvm::StringRef(Buffer->getBufferStart(),
1115 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001116
1117 if (strcmp(Name, "<built-in>") == 0)
1118 PreloadSLocs.push_back(SLocEntryOffsets.size());
1119 }
1120 } else {
1121 // The source location entry is an instantiation.
1122 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1123 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1124 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1125 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1126
1127 // Compute the token length for this macro expansion.
1128 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001129 if (I + 1 != N)
1130 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1132 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1133 }
1134 }
1135
Douglas Gregorc9490c02009-04-16 22:23:12 +00001136 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001137
1138 if (SLocEntryOffsets.empty())
1139 return;
1140
1141 // Write the source-location offsets table into the PCH block. This
1142 // table is used for lazily loading source-location information.
1143 using namespace llvm;
1144 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1145 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1149 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001151 Record.clear();
1152 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1153 Record.push_back(SLocEntryOffsets.size());
1154 Record.push_back(SourceMgr.getNextOffset());
1155 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001156 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001157 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001158
1159 // Write the source location entry preloads array, telling the PCH
1160 // reader which source locations entries it should load eagerly.
1161 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001162}
1163
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001164//===----------------------------------------------------------------------===//
1165// Preprocessor Serialization
1166//===----------------------------------------------------------------------===//
1167
Chris Lattner0b1fb982009-04-10 17:15:23 +00001168/// \brief Writes the block containing the serialized form of the
1169/// preprocessor.
1170///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001171void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001172 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001173
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001174 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1175 if (PP.getCounterValue() != 0) {
1176 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001177 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001178 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001179 }
1180
1181 // Enter the preprocessor block.
1182 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001184 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1185 // FIXME: use diagnostics subsystem for localization etc.
1186 if (PP.SawDateOrTime())
1187 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001189 // Loop over all the macro definitions that are live at the end of the file,
1190 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001191 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001192 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1193 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001194 // FIXME: This emits macros in hash table order, we should do it in a stable
1195 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001196 MacroInfo *MI = I->second;
1197
1198 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1199 // been redefined by the header (in which case they are not isBuiltinMacro).
1200 if (MI->isBuiltinMacro())
1201 continue;
1202
Chris Lattner7356a312009-04-11 21:15:38 +00001203 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001204 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001205 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1206 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001208 unsigned Code;
1209 if (MI->isObjectLike()) {
1210 Code = pch::PP_MACRO_OBJECT_LIKE;
1211 } else {
1212 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001214 Record.push_back(MI->isC99Varargs());
1215 Record.push_back(MI->isGNUVarargs());
1216 Record.push_back(MI->getNumArgs());
1217 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1218 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001219 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001220 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001221
1222 // If we have a detailed preprocessing record, record the macro definition
1223 // ID that corresponds to this macro.
1224 if (PPRec)
1225 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1226
Douglas Gregorc9490c02009-04-16 22:23:12 +00001227 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001228 Record.clear();
1229
Chris Lattnerdf961c22009-04-10 18:08:30 +00001230 // Emit the tokens array.
1231 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1232 // Note that we know that the preprocessor does not have any annotation
1233 // tokens in it because they are created by the parser, and thus can't be
1234 // in a macro definition.
1235 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Chris Lattnerdf961c22009-04-10 18:08:30 +00001237 Record.push_back(Tok.getLocation().getRawEncoding());
1238 Record.push_back(Tok.getLength());
1239
Chris Lattnerdf961c22009-04-10 18:08:30 +00001240 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1241 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001242 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Chris Lattnerdf961c22009-04-10 18:08:30 +00001244 // FIXME: Should translate token kind to a stable encoding.
1245 Record.push_back(Tok.getKind());
1246 // FIXME: Should translate token flags to a stable encoding.
1247 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Douglas Gregorc9490c02009-04-16 22:23:12 +00001249 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001250 Record.clear();
1251 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001252 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001253 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001254
1255 // If the preprocessor has a preprocessing record, emit it.
1256 unsigned NumPreprocessingRecords = 0;
1257 if (PPRec) {
1258 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1259 E != EEnd; ++E) {
1260 Record.clear();
1261
1262 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1263 Record.push_back(NumPreprocessingRecords++);
1264 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1265 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1266 AddIdentifierRef(MI->getName(), Record);
1267 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1268 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1269 continue;
1270 }
1271
1272 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1273 // Record this macro definition's location.
1274 pch::IdentID ID = getMacroDefinitionID(MD);
1275 if (ID != MacroDefinitionOffsets.size()) {
1276 if (ID > MacroDefinitionOffsets.size())
1277 MacroDefinitionOffsets.resize(ID + 1);
1278
1279 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1280 } else
1281 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1282
1283 Record.push_back(NumPreprocessingRecords++);
1284 Record.push_back(ID);
1285 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1286 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1287 AddIdentifierRef(MD->getName(), Record);
1288 AddSourceLocation(MD->getLocation(), Record);
1289 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1290 continue;
1291 }
1292 }
1293 }
1294
Douglas Gregorc9490c02009-04-16 22:23:12 +00001295 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001296
1297 // Write the offsets table for the preprocessing record.
1298 if (NumPreprocessingRecords > 0) {
1299 // Write the offsets table for identifier IDs.
1300 using namespace llvm;
1301 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1302 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1304 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1306 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1307
1308 Record.clear();
1309 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1310 Record.push_back(NumPreprocessingRecords);
1311 Record.push_back(MacroDefinitionOffsets.size());
1312 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1313 (const char *)&MacroDefinitionOffsets.front(),
1314 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1315 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001316}
1317
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001318//===----------------------------------------------------------------------===//
1319// Type Serialization
1320//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001321
Douglas Gregor2cf26342009-04-09 22:27:44 +00001322/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001323void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001324 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001325 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001326 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Douglas Gregor2cf26342009-04-09 22:27:44 +00001328 // Record the offset for this type.
1329 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001330 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001331 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1332 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001333 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001334 }
1335
1336 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Douglas Gregor2cf26342009-04-09 22:27:44 +00001338 // Emit the type's representation.
1339 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001340
Douglas Gregora4923eb2009-11-16 21:35:15 +00001341 if (T.hasLocalNonFastQualifiers()) {
1342 Qualifiers Qs = T.getLocalQualifiers();
1343 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001344 Record.push_back(Qs.getAsOpaqueValue());
1345 W.Code = pch::TYPE_EXT_QUAL;
1346 } else {
1347 switch (T->getTypeClass()) {
1348 // For all of the concrete, non-dependent types, call the
1349 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001350#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001351 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001352#define ABSTRACT_TYPE(Class, Base)
1353#define DEPENDENT_TYPE(Class, Base)
1354#include "clang/AST/TypeNodes.def"
1355
John McCall0953e762009-09-24 19:53:00 +00001356 // For all of the dependent type nodes (which only occur in C++
1357 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001358#define TYPE(Class, Base)
1359#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1360#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001361 assert(false && "Cannot serialize dependent type nodes");
1362 break;
1363 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001364 }
1365
1366 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001367 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001368
1369 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001370 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001371}
1372
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001373//===----------------------------------------------------------------------===//
1374// Declaration Serialization
1375//===----------------------------------------------------------------------===//
1376
Douglas Gregor2cf26342009-04-09 22:27:44 +00001377/// \brief Write the block containing all of the declaration IDs
1378/// lexically declared within the given DeclContext.
1379///
1380/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1381/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001382uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001383 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001384 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001385 return 0;
1386
Douglas Gregorc9490c02009-04-16 22:23:12 +00001387 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001388 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001389 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1390 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001391 AddDeclRef(*D, Record);
1392
Douglas Gregor25123082009-04-22 22:34:57 +00001393 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001394 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001395 return Offset;
1396}
1397
1398/// \brief Write the block containing all of the declaration IDs
1399/// visible from the given DeclContext.
1400///
1401/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1402/// bistream, or 0 if no block was written.
1403uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1404 DeclContext *DC) {
1405 if (DC->getPrimaryContext() != DC)
1406 return 0;
1407
Douglas Gregoraff22df2009-04-21 22:32:33 +00001408 // Since there is no name lookup into functions or methods, and we
1409 // perform name lookup for the translation unit via the
1410 // IdentifierInfo chains, don't bother to build a
1411 // visible-declarations table for these entities.
1412 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001413 return 0;
1414
Douglas Gregor2cf26342009-04-09 22:27:44 +00001415 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001416 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001417
1418 // Serialize the contents of the mapping used for lookup. Note that,
1419 // although we have two very different code paths, the serialized
1420 // representation is the same for both cases: a declaration name,
1421 // followed by a size, followed by references to the visible
1422 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001423 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001424 RecordData Record;
1425 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001426 if (!Map)
1427 return 0;
1428
Douglas Gregor2cf26342009-04-09 22:27:44 +00001429 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1430 D != DEnd; ++D) {
1431 AddDeclarationName(D->first, Record);
1432 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1433 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001434 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001435 AddDeclRef(*Result.first, Record);
1436 }
1437
1438 if (Record.size() == 0)
1439 return 0;
1440
Douglas Gregorc9490c02009-04-16 22:23:12 +00001441 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001442 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001443 return Offset;
1444}
1445
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001446//===----------------------------------------------------------------------===//
1447// Global Method Pool and Selector Serialization
1448//===----------------------------------------------------------------------===//
1449
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001450namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001451// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001452class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001453 PCHWriter &Writer;
1454
1455public:
1456 typedef Selector key_type;
1457 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001459 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1460 typedef const data_type& data_type_ref;
1461
1462 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001464 static unsigned ComputeHash(Selector Sel) {
1465 unsigned N = Sel.getNumArgs();
1466 if (N == 0)
1467 ++N;
1468 unsigned R = 5381;
1469 for (unsigned I = 0; I != N; ++I)
1470 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001471 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001472 return R;
1473 }
Mike Stump1eb44332009-09-09 15:08:12 +00001474
1475 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001476 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1477 data_type_ref Methods) {
1478 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1479 clang::io::Emit16(Out, KeyLen);
1480 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001481 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001482 Method = Method->Next)
1483 if (Method->Method)
1484 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001485 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001486 Method = Method->Next)
1487 if (Method->Method)
1488 DataLen += 4;
1489 clang::io::Emit16(Out, DataLen);
1490 return std::make_pair(KeyLen, DataLen);
1491 }
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Douglas Gregor83941df2009-04-25 17:48:32 +00001493 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001494 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001495 assert((Start >> 32) == 0 && "Selector key offset too large");
1496 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001497 unsigned N = Sel.getNumArgs();
1498 clang::io::Emit16(Out, N);
1499 if (N == 0)
1500 N = 1;
1501 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001502 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001503 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001506 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001507 data_type_ref Methods, unsigned DataLen) {
1508 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001509 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001510 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001511 Method = Method->Next)
1512 if (Method->Method)
1513 ++NumInstanceMethods;
1514
1515 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001516 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001517 Method = Method->Next)
1518 if (Method->Method)
1519 ++NumFactoryMethods;
1520
1521 clang::io::Emit16(Out, NumInstanceMethods);
1522 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001523 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001524 Method = Method->Next)
1525 if (Method->Method)
1526 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001527 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001528 Method = Method->Next)
1529 if (Method->Method)
1530 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001531
1532 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001533 }
1534};
1535} // end anonymous namespace
1536
1537/// \brief Write the method pool into the PCH file.
1538///
1539/// The method pool contains both instance and factory methods, stored
1540/// in an on-disk hash table indexed by the selector.
1541void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1542 using namespace llvm;
1543
1544 // Create and write out the blob that contains the instance and
1545 // factor method pools.
1546 bool Empty = true;
1547 {
1548 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001550 // Create the on-disk hash table representation. Start by
1551 // iterating through the instance method pool.
1552 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001553 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001554 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001555 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001556 InstanceEnd = SemaRef.InstanceMethodPool.end();
1557 Instance != InstanceEnd; ++Instance) {
1558 // Check whether there is a factory method with the same
1559 // selector.
1560 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1561 = SemaRef.FactoryMethodPool.find(Instance->first);
1562
1563 if (Factory == SemaRef.FactoryMethodPool.end())
1564 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001565 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001566 ObjCMethodList()));
1567 else
1568 Generator.insert(Instance->first,
1569 std::make_pair(Instance->second, Factory->second));
1570
Douglas Gregor83941df2009-04-25 17:48:32 +00001571 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001572 Empty = false;
1573 }
1574
1575 // Now iterate through the factory method pool, to pick up any
1576 // selectors that weren't already in the instance method pool.
1577 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001578 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001579 FactoryEnd = SemaRef.FactoryMethodPool.end();
1580 Factory != FactoryEnd; ++Factory) {
1581 // Check whether there is an instance method with the same
1582 // selector. If so, there is no work to do here.
1583 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1584 = SemaRef.InstanceMethodPool.find(Factory->first);
1585
Douglas Gregor83941df2009-04-25 17:48:32 +00001586 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001587 Generator.insert(Factory->first,
1588 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001589 ++NumSelectorsInMethodPool;
1590 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001591
1592 Empty = false;
1593 }
1594
Douglas Gregor83941df2009-04-25 17:48:32 +00001595 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001596 return;
1597
1598 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001599 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001601 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001602 {
1603 PCHMethodPoolTrait Trait(*this);
1604 llvm::raw_svector_ostream Out(MethodPool);
1605 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001606 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001607 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001608
1609 // For every selector that we have seen but which was not
1610 // written into the hash table, write the selector itself and
1611 // record it's offset.
1612 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1613 if (SelectorOffsets[I] == 0)
1614 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001615 }
1616
1617 // Create a blob abbreviation
1618 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1619 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1623 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1624
Douglas Gregor83941df2009-04-25 17:48:32 +00001625 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001626 RecordData Record;
1627 Record.push_back(pch::METHOD_POOL);
1628 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001629 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001630 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001631
1632 // Create a blob abbreviation for the selector table offsets.
1633 Abbrev = new BitCodeAbbrev();
1634 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1636 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1637 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1638
1639 // Write the selector offsets table.
1640 Record.clear();
1641 Record.push_back(pch::SELECTOR_OFFSETS);
1642 Record.push_back(SelectorOffsets.size());
1643 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1644 (const char *)&SelectorOffsets.front(),
1645 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001646 }
1647}
1648
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001649//===----------------------------------------------------------------------===//
1650// Identifier Table Serialization
1651//===----------------------------------------------------------------------===//
1652
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001653namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001654class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001655 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001656 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001657
Douglas Gregora92193e2009-04-28 21:18:29 +00001658 /// \brief Determines whether this is an "interesting" identifier
1659 /// that needs a full IdentifierInfo structure written into the hash
1660 /// table.
1661 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1662 return II->isPoisoned() ||
1663 II->isExtensionToken() ||
1664 II->hasMacroDefinition() ||
1665 II->getObjCOrBuiltinID() ||
1666 II->getFETokenInfo<void>();
1667 }
1668
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001669public:
1670 typedef const IdentifierInfo* key_type;
1671 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001673 typedef pch::IdentID data_type;
1674 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001675
1676 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001677 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001678
1679 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001680 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001681 }
Mike Stump1eb44332009-09-09 15:08:12 +00001682
1683 std::pair<unsigned,unsigned>
1684 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001685 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001686 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001687 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1688 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001689 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001690 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001691 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001692 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001693 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1694 DEnd = IdentifierResolver::end();
1695 D != DEnd; ++D)
1696 DataLen += sizeof(pch::DeclID);
1697 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001698 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001699 // We emit the key length after the data length so that every
1700 // string is preceded by a 16-bit length. This matches the PTH
1701 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001702 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001703 return std::make_pair(KeyLen, DataLen);
1704 }
Mike Stump1eb44332009-09-09 15:08:12 +00001705
1706 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001707 unsigned KeyLen) {
1708 // Record the location of the key data. This is used when generating
1709 // the mapping from persistent IDs to strings.
1710 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001711 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001712 }
Mike Stump1eb44332009-09-09 15:08:12 +00001713
1714 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001715 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001716 if (!isInterestingIdentifier(II)) {
1717 clang::io::Emit32(Out, ID << 1);
1718 return;
1719 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001720
Douglas Gregora92193e2009-04-28 21:18:29 +00001721 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001722 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001723 bool hasMacroDefinition =
1724 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001725 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001726 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001727 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1728 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1729 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1730 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001731 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001732
Douglas Gregor37e26842009-04-21 23:56:24 +00001733 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001734 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001735
Douglas Gregor668c1a42009-04-21 22:25:48 +00001736 // Emit the declaration IDs in reverse order, because the
1737 // IdentifierResolver provides the declarations as they would be
1738 // visible (e.g., the function "stat" would come before the struct
1739 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1740 // adds declarations to the end of the list (so we need to see the
1741 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001742 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001743 IdentifierResolver::end());
1744 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1745 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001746 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001747 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001748 }
1749};
1750} // end anonymous namespace
1751
Douglas Gregorafaf3082009-04-11 00:14:32 +00001752/// \brief Write the identifier table into the PCH file.
1753///
1754/// The identifier table consists of a blob containing string data
1755/// (the actual identifiers themselves) and a separate "offsets" index
1756/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001757void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001758 using namespace llvm;
1759
1760 // Create and write out the blob that contains the identifier
1761 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001762 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001763 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Douglas Gregor92b059e2009-04-28 20:33:11 +00001765 // Look for any identifiers that were named while processing the
1766 // headers, but are otherwise not needed. We add these to the hash
1767 // table to enable checking of the predefines buffer in the case
1768 // where the user adds new macro definitions when building the PCH
1769 // file.
1770 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1771 IDEnd = PP.getIdentifierTable().end();
1772 ID != IDEnd; ++ID)
1773 getIdentifierRef(ID->second);
1774
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001775 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001776 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001777 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1778 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1779 ID != IDEnd; ++ID) {
1780 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001781 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001782 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001783
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001784 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001785 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001786 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001787 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001788 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001789 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001790 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001791 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001792 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001793 }
1794
1795 // Create a blob abbreviation
1796 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1797 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001800 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001801
1802 // Write the identifier table
1803 RecordData Record;
1804 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001805 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001806 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001807 }
1808
1809 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001810 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1811 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1814 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1815
1816 RecordData Record;
1817 Record.push_back(pch::IDENTIFIER_OFFSET);
1818 Record.push_back(IdentifierOffsets.size());
1819 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1820 (const char *)&IdentifierOffsets.front(),
1821 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001822}
1823
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001824//===----------------------------------------------------------------------===//
1825// General Serialization Routines
1826//===----------------------------------------------------------------------===//
1827
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001828/// \brief Write a record containing the given attributes.
1829void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1830 RecordData Record;
1831 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001832 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001833 Record.push_back(Attr->isInherited());
1834 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001835 default:
1836 assert(0 && "Does not support PCH writing for this attribute yet!");
1837 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001838 case Attr::Alias:
1839 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1840 break;
1841
1842 case Attr::Aligned:
1843 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1844 break;
1845
1846 case Attr::AlwaysInline:
1847 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001849 case Attr::AnalyzerNoReturn:
1850 break;
1851
1852 case Attr::Annotate:
1853 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1854 break;
1855
1856 case Attr::AsmLabel:
1857 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1858 break;
1859
Sean Hunt7725e672009-11-25 04:20:27 +00001860 case Attr::BaseCheck:
1861 break;
1862
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001863 case Attr::Blocks:
1864 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1865 break;
1866
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001867 case Attr::CDecl:
1868 break;
1869
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001870 case Attr::Cleanup:
1871 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1872 break;
1873
1874 case Attr::Const:
1875 break;
1876
1877 case Attr::Constructor:
1878 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1879 break;
1880
1881 case Attr::DLLExport:
1882 case Attr::DLLImport:
1883 case Attr::Deprecated:
1884 break;
1885
1886 case Attr::Destructor:
1887 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1888 break;
1889
1890 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001891 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001892 break;
1893
1894 case Attr::Format: {
1895 const FormatAttr *Format = cast<FormatAttr>(Attr);
1896 AddString(Format->getType(), Record);
1897 Record.push_back(Format->getFormatIdx());
1898 Record.push_back(Format->getFirstArg());
1899 break;
1900 }
1901
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001902 case Attr::FormatArg: {
1903 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1904 Record.push_back(Format->getFormatIdx());
1905 break;
1906 }
1907
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001908 case Attr::Sentinel : {
1909 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1910 Record.push_back(Sentinel->getSentinel());
1911 Record.push_back(Sentinel->getNullPos());
1912 break;
1913 }
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Chris Lattnercf2a7212009-04-20 19:12:28 +00001915 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001916 case Attr::Hiding:
Ted Kremenekefbddd22010-02-17 02:37:45 +00001917 case Attr::IBActionKind:
Ted Kremenek47e69902010-02-18 00:05:52 +00001918 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001919 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001920 case Attr::NoDebug:
Ted Kremenek47e69902010-02-18 00:05:52 +00001921 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001922 case Attr::NoReturn:
1923 case Attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001924 break;
1925
1926 case Attr::NonNull: {
1927 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1928 Record.push_back(NonNull->size());
1929 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1930 break;
1931 }
1932
Ted Kremenek31c780d2010-02-18 00:05:45 +00001933 case Attr::CFReturnsNotRetained:
1934 case Attr::CFReturnsRetained:
1935 case Attr::NSReturnsNotRetained:
1936 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001937 case Attr::ObjCException:
1938 case Attr::ObjCNSObject:
1939 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001940 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001941 break;
1942
Anders Carlssona860e752009-08-08 18:23:56 +00001943 case Attr::PragmaPack:
1944 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001945 break;
1946
Anders Carlssona860e752009-08-08 18:23:56 +00001947 case Attr::Packed:
1948 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001949
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001950 case Attr::Pure:
1951 break;
1952
1953 case Attr::Regparm:
1954 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1955 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Nate Begeman6f3d8382009-06-26 06:32:41 +00001957 case Attr::ReqdWorkGroupSize:
1958 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1959 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1960 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1961 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001962
1963 case Attr::Section:
1964 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1965 break;
1966
1967 case Attr::StdCall:
1968 case Attr::TransparentUnion:
1969 case Attr::Unavailable:
1970 case Attr::Unused:
1971 case Attr::Used:
1972 break;
1973
1974 case Attr::Visibility:
1975 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001976 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001977 break;
1978
1979 case Attr::WarnUnusedResult:
1980 case Attr::Weak:
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001981 case Attr::WeakRef:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001982 case Attr::WeakImport:
1983 break;
1984 }
1985 }
1986
Douglas Gregorc9490c02009-04-16 22:23:12 +00001987 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001988}
1989
1990void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1991 Record.push_back(Str.size());
1992 Record.insert(Record.end(), Str.begin(), Str.end());
1993}
1994
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001995/// \brief Note that the identifier II occurs at the given offset
1996/// within the identifier table.
1997void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001998 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001999}
2000
Douglas Gregor83941df2009-04-25 17:48:32 +00002001/// \brief Note that the selector Sel occurs at the given offset
2002/// within the method pool/selector table.
2003void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2004 unsigned ID = SelectorIDs[Sel];
2005 assert(ID && "Unknown selector");
2006 SelectorOffsets[ID - 1] = Offset;
2007}
2008
Mike Stump1eb44332009-09-09 15:08:12 +00002009PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2010 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00002011 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2012 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002013
Douglas Gregore650c8c2009-07-07 00:12:59 +00002014void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2015 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002016 using namespace llvm;
2017
Douglas Gregore7785042009-04-20 15:53:59 +00002018 ASTContext &Context = SemaRef.Context;
2019 Preprocessor &PP = SemaRef.PP;
2020
Douglas Gregor2cf26342009-04-09 22:27:44 +00002021 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002022 Stream.Emit((unsigned)'C', 8);
2023 Stream.Emit((unsigned)'P', 8);
2024 Stream.Emit((unsigned)'C', 8);
2025 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002026
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002027 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002028
2029 // The translation unit is the first declaration we'll emit.
2030 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002031 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002032
Douglas Gregor2deaea32009-04-22 18:49:13 +00002033 // Make sure that we emit IdentifierInfos (and any attached
2034 // declarations) for builtins.
2035 {
2036 IdentifierTable &Table = PP.getIdentifierTable();
2037 llvm::SmallVector<const char *, 32> BuiltinNames;
2038 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2039 Context.getLangOptions().NoBuiltin);
2040 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2041 getIdentifierRef(&Table.get(BuiltinNames[I]));
2042 }
2043
Chris Lattner63d65f82009-09-08 18:19:27 +00002044 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002045 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002046 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002047 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002048 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2049 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002050 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002051
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002052 // Build a record containing all of the static unused functions in this file.
2053 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002054 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002055 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002056
Douglas Gregor14c22f22009-04-22 22:18:58 +00002057 // Build a record containing all of the locally-scoped external
2058 // declarations in this header file. Generally, this record will be
2059 // empty.
2060 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002061 // FIXME: This is filling in the PCH file in densemap order which is
2062 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002063 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002064 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2065 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2066 TD != TDEnd; ++TD)
2067 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2068
Douglas Gregorb81c1702009-04-27 20:06:05 +00002069 // Build a record containing all of the ext_vector declarations.
2070 RecordData ExtVectorDecls;
2071 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2072 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2073
Douglas Gregor2cf26342009-04-09 22:27:44 +00002074 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002075 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002076 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002077 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002078 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002079 if (StatCalls && !isysroot)
2080 WriteStatCache(*StatCalls, isysroot);
2081 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002082 // Write the record of special types.
2083 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002084
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002085 AddTypeRef(Context.getBuiltinVaListType(), Record);
2086 AddTypeRef(Context.getObjCIdType(), Record);
2087 AddTypeRef(Context.getObjCSelType(), Record);
2088 AddTypeRef(Context.getObjCProtoType(), Record);
2089 AddTypeRef(Context.getObjCClassType(), Record);
2090 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2091 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2092 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002093 AddTypeRef(Context.getjmp_bufType(), Record);
2094 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002095 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2096 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002097#if 0
2098 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00002099 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002100#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00002101 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002102 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002103 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Douglas Gregor366809a2009-04-26 03:49:13 +00002105 // Keep writing types and declarations until all types and
2106 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002107 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2108 WriteDeclsBlockAbbrevs();
2109 while (!DeclTypesToEmit.empty()) {
2110 DeclOrType DOT = DeclTypesToEmit.front();
2111 DeclTypesToEmit.pop();
2112 if (DOT.isType())
2113 WriteType(DOT.getType());
2114 else
2115 WriteDecl(Context, DOT.getDecl());
2116 }
2117 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002118
Douglas Gregor813a97b2009-10-17 17:25:45 +00002119 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002120 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002121 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002122
2123 // Write the type offsets array
2124 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2125 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2128 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2129 Record.clear();
2130 Record.push_back(pch::TYPE_OFFSET);
2131 Record.push_back(TypeOffsets.size());
2132 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002133 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002134 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002136 // Write the declaration offsets array
2137 Abbrev = new BitCodeAbbrev();
2138 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2139 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2140 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2141 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2142 Record.clear();
2143 Record.push_back(pch::DECL_OFFSET);
2144 Record.push_back(DeclOffsets.size());
2145 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002146 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002147 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002148
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002149 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002150 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002151 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002152
2153 // Write the record containing tentative definitions.
2154 if (!TentativeDefinitions.empty())
2155 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002156
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002157 // Write the record containing unused static functions.
2158 if (!UnusedStaticFuncs.empty())
2159 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002160
Douglas Gregor14c22f22009-04-22 22:18:58 +00002161 // Write the record containing locally-scoped external definitions.
2162 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002163 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002164 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002165
2166 // Write the record containing ext_vector type names.
2167 if (!ExtVectorDecls.empty())
2168 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002169
Douglas Gregor3e1af842009-04-17 22:13:46 +00002170 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002171 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002172 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002173 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002174 Record.push_back(NumLexicalDeclContexts);
2175 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002176 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002177 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002178}
2179
2180void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2181 Record.push_back(Loc.getRawEncoding());
2182}
2183
2184void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2185 Record.push_back(Value.getBitWidth());
2186 unsigned N = Value.getNumWords();
2187 const uint64_t* Words = Value.getRawData();
2188 for (unsigned I = 0; I != N; ++I)
2189 Record.push_back(Words[I]);
2190}
2191
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002192void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2193 Record.push_back(Value.isUnsigned());
2194 AddAPInt(Value, Record);
2195}
2196
Douglas Gregor17fc2232009-04-14 21:55:33 +00002197void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2198 AddAPInt(Value.bitcastToAPInt(), Record);
2199}
2200
Douglas Gregor2cf26342009-04-09 22:27:44 +00002201void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002202 Record.push_back(getIdentifierRef(II));
2203}
2204
2205pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2206 if (II == 0)
2207 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002208
2209 pch::IdentID &ID = IdentifierIDs[II];
2210 if (ID == 0)
2211 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002212 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002213}
2214
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002215pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2216 if (MD == 0)
2217 return 0;
2218
2219 pch::IdentID &ID = MacroDefinitions[MD];
2220 if (ID == 0)
2221 ID = MacroDefinitions.size();
2222 return ID;
2223}
2224
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002225void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2226 if (SelRef.getAsOpaquePtr() == 0) {
2227 Record.push_back(0);
2228 return;
2229 }
2230
2231 pch::SelectorID &SID = SelectorIDs[SelRef];
2232 if (SID == 0) {
2233 SID = SelectorIDs.size();
2234 SelVector.push_back(SelRef);
2235 }
2236 Record.push_back(SID);
2237}
2238
John McCall833ca992009-10-29 08:12:44 +00002239void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2240 RecordData &Record) {
2241 switch (Arg.getArgument().getKind()) {
2242 case TemplateArgument::Expression:
2243 AddStmt(Arg.getLocInfo().getAsExpr());
2244 break;
2245 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002246 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002247 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002248 case TemplateArgument::Template:
2249 Record.push_back(
2250 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2251 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2252 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2253 break;
John McCall833ca992009-10-29 08:12:44 +00002254 case TemplateArgument::Null:
2255 case TemplateArgument::Integral:
2256 case TemplateArgument::Declaration:
2257 case TemplateArgument::Pack:
2258 break;
2259 }
2260}
2261
John McCalla93c9342009-12-07 02:54:59 +00002262void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2263 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002264 AddTypeRef(QualType(), Record);
2265 return;
2266 }
2267
John McCalla93c9342009-12-07 02:54:59 +00002268 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002269 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002270 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002271 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002272}
2273
Douglas Gregor2cf26342009-04-09 22:27:44 +00002274void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2275 if (T.isNull()) {
2276 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2277 return;
2278 }
2279
Douglas Gregora4923eb2009-11-16 21:35:15 +00002280 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002281 T.removeFastQualifiers();
2282
Douglas Gregora4923eb2009-11-16 21:35:15 +00002283 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002284 pch::TypeID &ID = TypeIDs[T];
2285 if (ID == 0) {
2286 // We haven't seen these qualifiers applied to this type before.
2287 // Assign it a new ID. This is the only time we enqueue a
2288 // qualified type, and it has no CV qualifiers.
2289 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002290 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002291 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002292
John McCall0953e762009-09-24 19:53:00 +00002293 // Encode the type qualifiers in the type reference.
2294 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2295 return;
2296 }
2297
Douglas Gregora4923eb2009-11-16 21:35:15 +00002298 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002299
Douglas Gregor2cf26342009-04-09 22:27:44 +00002300 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002301 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002302 switch (BT->getKind()) {
2303 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2304 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2305 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2306 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2307 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2308 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2309 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2310 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002311 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002312 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2313 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2314 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2315 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2316 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2317 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2318 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002319 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002320 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2321 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2322 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002323 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002324 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2325 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002326 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2327 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002328 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2329 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002330 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002331 case BuiltinType::UndeducedAuto:
2332 assert(0 && "Should not see undeduced auto here");
2333 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002334 }
2335
John McCall0953e762009-09-24 19:53:00 +00002336 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002337 return;
2338 }
2339
John McCall0953e762009-09-24 19:53:00 +00002340 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002341 if (ID == 0) {
2342 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002343 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002344 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002345 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002346 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002347
2348 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002349 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002350}
2351
2352void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2353 if (D == 0) {
2354 Record.push_back(0);
2355 return;
2356 }
2357
Douglas Gregor8038d512009-04-10 17:25:41 +00002358 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002359 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002360 // We haven't seen this declaration before. Give it a new ID and
2361 // enqueue it in the list of declarations to emit.
2362 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002363 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002364 }
2365
2366 Record.push_back(ID);
2367}
2368
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002369pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2370 if (D == 0)
2371 return 0;
2372
2373 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2374 return DeclIDs[D];
2375}
2376
Douglas Gregor2cf26342009-04-09 22:27:44 +00002377void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002378 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002379 Record.push_back(Name.getNameKind());
2380 switch (Name.getNameKind()) {
2381 case DeclarationName::Identifier:
2382 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2383 break;
2384
2385 case DeclarationName::ObjCZeroArgSelector:
2386 case DeclarationName::ObjCOneArgSelector:
2387 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002388 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002389 break;
2390
2391 case DeclarationName::CXXConstructorName:
2392 case DeclarationName::CXXDestructorName:
2393 case DeclarationName::CXXConversionFunctionName:
2394 AddTypeRef(Name.getCXXNameType(), Record);
2395 break;
2396
2397 case DeclarationName::CXXOperatorName:
2398 Record.push_back(Name.getCXXOverloadedOperator());
2399 break;
2400
Sean Hunt3e518bd2009-11-29 07:34:05 +00002401 case DeclarationName::CXXLiteralOperatorName:
2402 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2403 break;
2404
Douglas Gregor2cf26342009-04-09 22:27:44 +00002405 case DeclarationName::CXXUsingDirective:
2406 // No extra data to emit
2407 break;
2408 }
2409}