blob: 4646f424b7c66eccfc3b6352d1907aa5275299a2 [file] [log] [blame]
Chris Lattnere127a0d2010-04-20 20:35:58 +00001//===--- PCHWriter.cpp - Precompiled Headers Writer -----------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
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"
Sebastian Redl77f46032010-07-09 21:00:24 +000023#include "clang/Frontend/PCHReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000025#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000026#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000027#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000029#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000031#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000032#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000034#include "llvm/ADT/APFloat.h"
35#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000036#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000037#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000038#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000039#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000040#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000041using namespace clang;
42
Sebastian Redlade50002010-07-30 17:03:48 +000043template <typename T, typename Allocator>
44T *data(std::vector<T, Allocator> &v) {
45 return v.empty() ? 0 : &v.front();
46}
47template <typename T, typename Allocator>
48const T *data(const std::vector<T, Allocator> &v) {
49 return v.empty() ? 0 : &v.front();
50}
51
Douglas Gregor2cf26342009-04-09 22:27:44 +000052//===----------------------------------------------------------------------===//
53// Type serialization
54//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000055
Douglas Gregor2cf26342009-04-09 22:27:44 +000056namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000057 class PCHTypeWriter {
Douglas Gregor2cf26342009-04-09 22:27:44 +000058 PCHWriter &Writer;
59 PCHWriter::RecordData &Record;
60
61 public:
62 /// \brief Type code that corresponds to the record generated.
63 pch::TypeCode Code;
64
Mike Stump1eb44332009-09-09 15:08:12 +000065 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000066 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000067
68 void VisitArrayType(const ArrayType *T);
69 void VisitFunctionType(const FunctionType *T);
70 void VisitTagType(const TagType *T);
71
72#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
73#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000074#include "clang/AST/TypeNodes.def"
75 };
76}
77
Douglas Gregor2cf26342009-04-09 22:27:44 +000078void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
79 assert(false && "Built-in types are never serialized");
80}
81
Douglas Gregor2cf26342009-04-09 22:27:44 +000082void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
83 Writer.AddTypeRef(T->getElementType(), Record);
84 Code = pch::TYPE_COMPLEX;
85}
86
87void PCHTypeWriter::VisitPointerType(const PointerType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_POINTER;
90}
91
92void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000093 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000094 Code = pch::TYPE_BLOCK_POINTER;
95}
96
97void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
98 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Code = pch::TYPE_LVALUE_REFERENCE;
100}
101
102void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
103 Writer.AddTypeRef(T->getPointeeType(), Record);
104 Code = pch::TYPE_RVALUE_REFERENCE;
105}
106
107void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
109 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110 Code = pch::TYPE_MEMBER_POINTER;
111}
112
113void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
114 Writer.AddTypeRef(T->getElementType(), Record);
115 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000116 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000117}
118
119void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
120 VisitArrayType(T);
121 Writer.AddAPInt(T->getSize(), Record);
122 Code = pch::TYPE_CONSTANT_ARRAY;
123}
124
125void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
126 VisitArrayType(T);
127 Code = pch::TYPE_INCOMPLETE_ARRAY;
128}
129
130void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
131 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000132 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
133 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000134 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135 Code = pch::TYPE_VARIABLE_ARRAY;
136}
137
138void PCHTypeWriter::VisitVectorType(const VectorType *T) {
139 Writer.AddTypeRef(T->getElementType(), Record);
140 Record.push_back(T->getNumElements());
Chris Lattner788b0fd2010-06-23 06:00:24 +0000141 Record.push_back(T->getAltiVecSpecific());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 Code = pch::TYPE_VECTOR;
143}
144
145void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
146 VisitVectorType(T);
147 Code = pch::TYPE_EXT_VECTOR;
148}
149
150void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
151 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000152 FunctionType::ExtInfo C = T->getExtInfo();
153 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000154 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000155 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000156 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000157}
158
159void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
160 VisitFunctionType(T);
161 Code = pch::TYPE_FUNCTION_NO_PROTO;
162}
163
164void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
165 VisitFunctionType(T);
166 Record.push_back(T->getNumArgs());
167 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
168 Writer.AddTypeRef(T->getArgType(I), Record);
169 Record.push_back(T->isVariadic());
170 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000171 Record.push_back(T->hasExceptionSpec());
172 Record.push_back(T->hasAnyExceptionSpec());
173 Record.push_back(T->getNumExceptions());
174 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
175 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176 Code = pch::TYPE_FUNCTION_PROTO;
177}
178
John McCalled976492009-12-04 22:46:56 +0000179void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
180 Writer.AddDeclRef(T->getDecl(), Record);
181 Code = pch::TYPE_UNRESOLVED_USING;
182}
John McCalled976492009-12-04 22:46:56 +0000183
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
185 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000186 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
187 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000188 Code = pch::TYPE_TYPEDEF;
189}
190
191void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000192 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193 Code = pch::TYPE_TYPEOF_EXPR;
194}
195
196void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
197 Writer.AddTypeRef(T->getUnderlyingType(), Record);
198 Code = pch::TYPE_TYPEOF;
199}
200
Anders Carlsson395b4752009-06-24 19:06:50 +0000201void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
202 Writer.AddStmt(T->getUnderlyingExpr());
203 Code = pch::TYPE_DECLTYPE;
204}
205
Douglas Gregor2cf26342009-04-09 22:27:44 +0000206void PCHTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000207 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000209 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000210 "Cannot serialize in the middle of a type definition");
211}
212
213void PCHTypeWriter::VisitRecordType(const RecordType *T) {
214 VisitTagType(T);
215 Code = pch::TYPE_RECORD;
216}
217
218void PCHTypeWriter::VisitEnumType(const EnumType *T) {
219 VisitTagType(T);
220 Code = pch::TYPE_ENUM;
221}
222
Mike Stump1eb44332009-09-09 15:08:12 +0000223void
John McCall49a832b2009-10-18 09:09:24 +0000224PCHTypeWriter::VisitSubstTemplateTypeParmType(
225 const SubstTemplateTypeParmType *T) {
226 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
227 Writer.AddTypeRef(T->getReplacementType(), Record);
228 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
229}
230
231void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000232PCHTypeWriter::VisitTemplateSpecializationType(
233 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000234 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000235 Writer.AddTemplateName(T->getTemplateName(), Record);
236 Record.push_back(T->getNumArgs());
237 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
238 ArgI != ArgE; ++ArgI)
239 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000240 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
241 : T->getCanonicalTypeInternal(),
242 Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000243 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
244}
245
246void
247PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000248 VisitArrayType(T);
249 Writer.AddStmt(T->getSizeExpr());
250 Writer.AddSourceRange(T->getBracketsRange(), Record);
251 Code = pch::TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000252}
253
254void
255PCHTypeWriter::VisitDependentSizedExtVectorType(
256 const DependentSizedExtVectorType *T) {
257 // FIXME: Serialize this type (C++ only)
258 assert(false && "Cannot serialize dependent sized extended vector types");
259}
260
261void
262PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
263 Record.push_back(T->getDepth());
264 Record.push_back(T->getIndex());
265 Record.push_back(T->isParameterPack());
266 Writer.AddIdentifierRef(T->getName(), Record);
267 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
268}
269
270void
271PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000272 Record.push_back(T->getKeyword());
273 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
274 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000275 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
276 : T->getCanonicalTypeInternal(),
277 Record);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000278 Code = pch::TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000279}
280
281void
282PCHTypeWriter::VisitDependentTemplateSpecializationType(
283 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000284 Record.push_back(T->getKeyword());
285 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
286 Writer.AddIdentifierRef(T->getIdentifier(), Record);
287 Record.push_back(T->getNumArgs());
288 for (DependentTemplateSpecializationType::iterator
289 I = T->begin(), E = T->end(); I != E; ++I)
290 Writer.AddTemplateArgument(*I, Record);
291 Code = pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000292}
293
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000294void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000295 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000296 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
297 Writer.AddTypeRef(T->getNamedType(), Record);
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000298 Code = pch::TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000299}
300
John McCall3cb0ebd2010-03-10 03:28:59 +0000301void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
302 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000303 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000304 Code = pch::TYPE_INJECTED_CLASS_NAME;
305}
306
Douglas Gregor2cf26342009-04-09 22:27:44 +0000307void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
308 Writer.AddDeclRef(T->getDecl(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000309 Code = pch::TYPE_OBJC_INTERFACE;
310}
311
312void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
313 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000314 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000315 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000316 E = T->qual_end(); I != E; ++I)
317 Writer.AddDeclRef(*I, Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000318 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000319}
320
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000321void
322PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000323 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000324 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000325}
326
John McCalla1ee0c52009-10-16 21:56:05 +0000327namespace {
328
329class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
330 PCHWriter &Writer;
331 PCHWriter::RecordData &Record;
332
333public:
334 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
335 : Writer(Writer), Record(Record) { }
336
John McCall51bd8032009-10-18 01:05:36 +0000337#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000338#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000339 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000340#include "clang/AST/TypeLocNodes.def"
341
John McCall51bd8032009-10-18 01:05:36 +0000342 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
343 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000344};
345
346}
347
John McCall51bd8032009-10-18 01:05:36 +0000348void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
349 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000350}
John McCall51bd8032009-10-18 01:05:36 +0000351void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000352 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
353 if (TL.needsExtraLocalData()) {
354 Record.push_back(TL.getWrittenTypeSpec());
355 Record.push_back(TL.getWrittenSignSpec());
356 Record.push_back(TL.getWrittenWidthSpec());
357 Record.push_back(TL.hasModeAttr());
358 }
John McCalla1ee0c52009-10-16 21:56:05 +0000359}
John McCall51bd8032009-10-18 01:05:36 +0000360void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000362}
John McCall51bd8032009-10-18 01:05:36 +0000363void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000365}
John McCall51bd8032009-10-18 01:05:36 +0000366void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000368}
John McCall51bd8032009-10-18 01:05:36 +0000369void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
370 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000371}
John McCall51bd8032009-10-18 01:05:36 +0000372void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
373 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000374}
John McCall51bd8032009-10-18 01:05:36 +0000375void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
376 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000377}
John McCall51bd8032009-10-18 01:05:36 +0000378void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
379 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
380 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
381 Record.push_back(TL.getSizeExpr() ? 1 : 0);
382 if (TL.getSizeExpr())
383 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000384}
John McCall51bd8032009-10-18 01:05:36 +0000385void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
386 VisitArrayTypeLoc(TL);
387}
388void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
389 VisitArrayTypeLoc(TL);
390}
391void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
392 VisitArrayTypeLoc(TL);
393}
394void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
395 DependentSizedArrayTypeLoc TL) {
396 VisitArrayTypeLoc(TL);
397}
398void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
399 DependentSizedExtVectorTypeLoc TL) {
400 Writer.AddSourceLocation(TL.getNameLoc(), Record);
401}
402void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
403 Writer.AddSourceLocation(TL.getNameLoc(), Record);
404}
405void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getNameLoc(), Record);
407}
408void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
410 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
411 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
412 Writer.AddDeclRef(TL.getArg(i), Record);
413}
414void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
415 VisitFunctionTypeLoc(TL);
416}
417void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
418 VisitFunctionTypeLoc(TL);
419}
John McCalled976492009-12-04 22:46:56 +0000420void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getNameLoc(), Record);
422}
John McCall51bd8032009-10-18 01:05:36 +0000423void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getNameLoc(), Record);
425}
426void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000427 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
428 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
429 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000430}
431void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000432 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
433 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
434 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
435 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000436}
437void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
439}
440void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
442}
443void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
John McCall51bd8032009-10-18 01:05:36 +0000446void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getNameLoc(), Record);
448}
John McCall49a832b2009-10-18 09:09:24 +0000449void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
450 SubstTemplateTypeParmTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
454 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000455 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
456 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
457 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
458 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000459 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
460 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000461}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000462void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000463 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
464 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000465}
John McCall3cb0ebd2010-03-10 03:28:59 +0000466void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
467 Writer.AddSourceLocation(TL.getNameLoc(), Record);
468}
Douglas Gregor4714c122010-03-31 17:34:00 +0000469void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000470 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
471 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000472 Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
John McCall33500952010-06-11 00:33:02 +0000474void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
475 DependentTemplateSpecializationTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
477 Writer.AddSourceRange(TL.getQualifierRange(), Record);
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
480 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
481 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000482 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
483 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000484}
John McCall51bd8032009-10-18 01:05:36 +0000485void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000487}
488void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
489 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000490 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
491 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
492 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
493 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000494}
John McCall54e14c42009-10-22 22:37:11 +0000495void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000497}
John McCalla1ee0c52009-10-16 21:56:05 +0000498
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000499//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000500// PCHWriter Implementation
501//===----------------------------------------------------------------------===//
502
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000503static void EmitBlockID(unsigned ID, const char *Name,
504 llvm::BitstreamWriter &Stream,
505 PCHWriter::RecordData &Record) {
506 Record.clear();
507 Record.push_back(ID);
508 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
509
510 // Emit the block name if present.
511 if (Name == 0 || Name[0] == 0) return;
512 Record.clear();
513 while (*Name)
514 Record.push_back(*Name++);
515 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
516}
517
518static void EmitRecordID(unsigned ID, const char *Name,
519 llvm::BitstreamWriter &Stream,
520 PCHWriter::RecordData &Record) {
521 Record.clear();
522 Record.push_back(ID);
523 while (*Name)
524 Record.push_back(*Name++);
525 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000526}
527
528static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
529 PCHWriter::RecordData &Record) {
530#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
531 RECORD(STMT_STOP);
532 RECORD(STMT_NULL_PTR);
533 RECORD(STMT_NULL);
534 RECORD(STMT_COMPOUND);
535 RECORD(STMT_CASE);
536 RECORD(STMT_DEFAULT);
537 RECORD(STMT_LABEL);
538 RECORD(STMT_IF);
539 RECORD(STMT_SWITCH);
540 RECORD(STMT_WHILE);
541 RECORD(STMT_DO);
542 RECORD(STMT_FOR);
543 RECORD(STMT_GOTO);
544 RECORD(STMT_INDIRECT_GOTO);
545 RECORD(STMT_CONTINUE);
546 RECORD(STMT_BREAK);
547 RECORD(STMT_RETURN);
548 RECORD(STMT_DECL);
549 RECORD(STMT_ASM);
550 RECORD(EXPR_PREDEFINED);
551 RECORD(EXPR_DECL_REF);
552 RECORD(EXPR_INTEGER_LITERAL);
553 RECORD(EXPR_FLOATING_LITERAL);
554 RECORD(EXPR_IMAGINARY_LITERAL);
555 RECORD(EXPR_STRING_LITERAL);
556 RECORD(EXPR_CHARACTER_LITERAL);
557 RECORD(EXPR_PAREN);
558 RECORD(EXPR_UNARY_OPERATOR);
559 RECORD(EXPR_SIZEOF_ALIGN_OF);
560 RECORD(EXPR_ARRAY_SUBSCRIPT);
561 RECORD(EXPR_CALL);
562 RECORD(EXPR_MEMBER);
563 RECORD(EXPR_BINARY_OPERATOR);
564 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
565 RECORD(EXPR_CONDITIONAL_OPERATOR);
566 RECORD(EXPR_IMPLICIT_CAST);
567 RECORD(EXPR_CSTYLE_CAST);
568 RECORD(EXPR_COMPOUND_LITERAL);
569 RECORD(EXPR_EXT_VECTOR_ELEMENT);
570 RECORD(EXPR_INIT_LIST);
571 RECORD(EXPR_DESIGNATED_INIT);
572 RECORD(EXPR_IMPLICIT_VALUE_INIT);
573 RECORD(EXPR_VA_ARG);
574 RECORD(EXPR_ADDR_LABEL);
575 RECORD(EXPR_STMT);
576 RECORD(EXPR_TYPES_COMPATIBLE);
577 RECORD(EXPR_CHOOSE);
578 RECORD(EXPR_GNU_NULL);
579 RECORD(EXPR_SHUFFLE_VECTOR);
580 RECORD(EXPR_BLOCK);
581 RECORD(EXPR_BLOCK_DECL_REF);
582 RECORD(EXPR_OBJC_STRING_LITERAL);
583 RECORD(EXPR_OBJC_ENCODE);
584 RECORD(EXPR_OBJC_SELECTOR_EXPR);
585 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
586 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
587 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
588 RECORD(EXPR_OBJC_KVC_REF_EXPR);
589 RECORD(EXPR_OBJC_MESSAGE_EXPR);
590 RECORD(EXPR_OBJC_SUPER_EXPR);
591 RECORD(STMT_OBJC_FOR_COLLECTION);
592 RECORD(STMT_OBJC_CATCH);
593 RECORD(STMT_OBJC_FINALLY);
594 RECORD(STMT_OBJC_AT_TRY);
595 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
596 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000597 RECORD(EXPR_CXX_OPERATOR_CALL);
598 RECORD(EXPR_CXX_CONSTRUCT);
599 RECORD(EXPR_CXX_STATIC_CAST);
600 RECORD(EXPR_CXX_DYNAMIC_CAST);
601 RECORD(EXPR_CXX_REINTERPRET_CAST);
602 RECORD(EXPR_CXX_CONST_CAST);
603 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
604 RECORD(EXPR_CXX_BOOL_LITERAL);
605 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000606#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000607}
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000609void PCHWriter::WriteBlockInfoBlock() {
610 RecordData Record;
611 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Chris Lattner2f4efd12009-04-27 00:40:25 +0000613#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000614#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000616 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000617 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000618 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000619 RECORD(TYPE_OFFSET);
620 RECORD(DECL_OFFSET);
621 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000622 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000623 RECORD(IDENTIFIER_OFFSET);
624 RECORD(IDENTIFIER_TABLE);
625 RECORD(EXTERNAL_DEFINITIONS);
626 RECORD(SPECIAL_TYPES);
627 RECORD(STATISTICS);
628 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000629 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000630 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
631 RECORD(SELECTOR_OFFSETS);
632 RECORD(METHOD_POOL);
633 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000634 RECORD(SOURCE_LOCATION_OFFSETS);
635 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000636 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000637 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000638 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000639 RECORD(UNUSED_STATIC_FUNCS);
640 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000641 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000642 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000643
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000644 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000645 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000646 RECORD(SM_SLOC_FILE_ENTRY);
647 RECORD(SM_SLOC_BUFFER_ENTRY);
648 RECORD(SM_SLOC_BUFFER_BLOB);
649 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
650 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000652 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000653 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000654 RECORD(PP_MACRO_OBJECT_LIKE);
655 RECORD(PP_MACRO_FUNCTION_LIKE);
656 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000657 RECORD(PP_MACRO_INSTANTIATION);
658 RECORD(PP_MACRO_DEFINITION);
659
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000660 // Decls and Types block.
661 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000662 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000663 RECORD(TYPE_COMPLEX);
664 RECORD(TYPE_POINTER);
665 RECORD(TYPE_BLOCK_POINTER);
666 RECORD(TYPE_LVALUE_REFERENCE);
667 RECORD(TYPE_RVALUE_REFERENCE);
668 RECORD(TYPE_MEMBER_POINTER);
669 RECORD(TYPE_CONSTANT_ARRAY);
670 RECORD(TYPE_INCOMPLETE_ARRAY);
671 RECORD(TYPE_VARIABLE_ARRAY);
672 RECORD(TYPE_VECTOR);
673 RECORD(TYPE_EXT_VECTOR);
674 RECORD(TYPE_FUNCTION_PROTO);
675 RECORD(TYPE_FUNCTION_NO_PROTO);
676 RECORD(TYPE_TYPEDEF);
677 RECORD(TYPE_TYPEOF_EXPR);
678 RECORD(TYPE_TYPEOF);
679 RECORD(TYPE_RECORD);
680 RECORD(TYPE_ENUM);
681 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000682 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000683 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000684 RECORD(DECL_ATTR);
685 RECORD(DECL_TRANSLATION_UNIT);
686 RECORD(DECL_TYPEDEF);
687 RECORD(DECL_ENUM);
688 RECORD(DECL_RECORD);
689 RECORD(DECL_ENUM_CONSTANT);
690 RECORD(DECL_FUNCTION);
691 RECORD(DECL_OBJC_METHOD);
692 RECORD(DECL_OBJC_INTERFACE);
693 RECORD(DECL_OBJC_PROTOCOL);
694 RECORD(DECL_OBJC_IVAR);
695 RECORD(DECL_OBJC_AT_DEFS_FIELD);
696 RECORD(DECL_OBJC_CLASS);
697 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
698 RECORD(DECL_OBJC_CATEGORY);
699 RECORD(DECL_OBJC_CATEGORY_IMPL);
700 RECORD(DECL_OBJC_IMPLEMENTATION);
701 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
702 RECORD(DECL_OBJC_PROPERTY);
703 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000704 RECORD(DECL_FIELD);
705 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000706 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000707 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000708 RECORD(DECL_FILE_SCOPE_ASM);
709 RECORD(DECL_BLOCK);
710 RECORD(DECL_CONTEXT_LEXICAL);
711 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000712 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000713 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000714#undef RECORD
715#undef BLOCK
716 Stream.ExitBlock();
717}
718
Douglas Gregore650c8c2009-07-07 00:12:59 +0000719/// \brief Adjusts the given filename to only write out the portion of the
720/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000721///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000722/// \param Filename the file name to adjust.
723///
724/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
725/// the returned filename will be adjusted by this system root.
726///
727/// \returns either the original filename (if it needs no adjustment) or the
728/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000729static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000730adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
731 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Douglas Gregore650c8c2009-07-07 00:12:59 +0000733 if (!isysroot)
734 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Douglas Gregore650c8c2009-07-07 00:12:59 +0000736 // Verify that the filename and the system root have the same prefix.
737 unsigned Pos = 0;
738 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
739 if (Filename[Pos] != isysroot[Pos])
740 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Douglas Gregore650c8c2009-07-07 00:12:59 +0000742 // We hit the end of the filename before we hit the end of the system root.
743 if (!Filename[Pos])
744 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Douglas Gregore650c8c2009-07-07 00:12:59 +0000746 // If the file name has a '/' at the current position, skip over the '/'.
747 // We distinguish sysroot-based includes from absolute includes by the
748 // absence of '/' at the beginning of sysroot-based includes.
749 if (Filename[Pos] == '/')
750 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Douglas Gregore650c8c2009-07-07 00:12:59 +0000752 return Filename + Pos;
753}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000754
Douglas Gregorab41e632009-04-27 22:23:34 +0000755/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Sebastian Redl30c514c2010-07-14 23:45:08 +0000756void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000757 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000758
Douglas Gregore650c8c2009-07-07 00:12:59 +0000759 // Metadata
760 const TargetInfo &Target = Context.Target;
761 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000762 MetaAbbrev->Add(BitCodeAbbrevOp(
763 Chain ? pch::CHAINED_METADATA : pch::METADATA));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000764 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
765 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
766 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000769 // Target triple or chained PCH name
770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000771 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Douglas Gregore650c8c2009-07-07 00:12:59 +0000773 RecordData Record;
Sebastian Redl77f46032010-07-09 21:00:24 +0000774 Record.push_back(Chain ? pch::CHAINED_METADATA : pch::METADATA);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000775 Record.push_back(pch::VERSION_MAJOR);
776 Record.push_back(pch::VERSION_MINOR);
777 Record.push_back(CLANG_VERSION_MAJOR);
778 Record.push_back(CLANG_VERSION_MINOR);
779 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000780 // FIXME: This writes the absolute path for chained headers.
781 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
782 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Douglas Gregorb64c1932009-05-12 01:31:05 +0000784 // Original file name
785 SourceManager &SM = Context.getSourceManager();
786 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
787 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
788 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
789 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
790 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
791
792 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000794 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000795
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000796 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000797 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000798 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000799 RecordData Record;
800 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000801 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000802 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000803
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000804 // Repository branch/version information.
805 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
806 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
807 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
808 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000809 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000810 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000811 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
812 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000813}
814
815/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000816void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
817 RecordData Record;
818 Record.push_back(LangOpts.Trigraphs);
819 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
820 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
821 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
822 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000823 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000824 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
825 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
826 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
827 Record.push_back(LangOpts.C99); // C99 Support
828 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
829 Record.push_back(LangOpts.CPlusPlus); // C++ Support
830 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000831 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000833 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
834 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000835 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000836 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000837 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000838 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000839 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000841 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000842 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
843 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000844 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000845 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000846 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000847
848 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
849 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
850 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
851
Chris Lattnerea5ce472009-04-27 07:35:58 +0000852 // Whether static initializers are protected by locks.
853 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000854 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000855 Record.push_back(LangOpts.Blocks); // block extension to C
856 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
857 // they are unused.
858 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
859 // (modulo the platform support).
860
Chris Lattnera4d71452010-06-26 21:25:03 +0000861 Record.push_back(LangOpts.getSignedOverflowBehavior());
862 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000863
864 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000865 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000866 // defined.
867 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
868 // opposed to __DYNAMIC__).
869 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
870
871 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
872 // used (instead of C99 semantics).
873 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000874 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
875 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000876 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
877 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000878 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000879 Record.push_back(LangOpts.getGCMode());
880 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000881 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000882 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000883 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000884 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000885 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000886 Record.push_back(LangOpts.SpellChecking);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000887 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000888}
889
Douglas Gregor14f79002009-04-10 03:52:48 +0000890//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000891// stat cache Serialization
892//===----------------------------------------------------------------------===//
893
894namespace {
895// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000896class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000897public:
898 typedef const char * key_type;
899 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000901 typedef std::pair<int, struct stat> data_type;
902 typedef const data_type& data_type_ref;
903
904 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000905 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000906 }
Mike Stump1eb44332009-09-09 15:08:12 +0000907
908 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000909 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
910 data_type_ref Data) {
911 unsigned StrLen = strlen(path);
912 clang::io::Emit16(Out, StrLen);
913 unsigned DataLen = 1; // result value
914 if (Data.first == 0)
915 DataLen += 4 + 4 + 2 + 8 + 8;
916 clang::io::Emit8(Out, DataLen);
917 return std::make_pair(StrLen + 1, DataLen);
918 }
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000920 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
921 Out.write(path, KeyLen);
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000924 void EmitData(llvm::raw_ostream& Out, key_type_ref,
925 data_type_ref Data, unsigned DataLen) {
926 using namespace clang::io;
927 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000929 // Result of stat()
930 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000932 if (Data.first == 0) {
933 Emit32(Out, (uint32_t) Data.second.st_ino);
934 Emit32(Out, (uint32_t) Data.second.st_dev);
935 Emit16(Out, (uint16_t) Data.second.st_mode);
936 Emit64(Out, (uint64_t) Data.second.st_mtime);
937 Emit64(Out, (uint64_t) Data.second.st_size);
938 }
939
940 assert(Out.tell() - Start == DataLen && "Wrong data length");
941 }
942};
943} // end anonymous namespace
944
945/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregordd41ed52010-07-12 23:48:14 +0000946void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000947 // Build the on-disk hash table containing information about every
948 // stat() call.
949 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
950 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000951 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000952 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000953 Stat != StatEnd; ++Stat, ++NumStatEntries) {
954 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955 Generator.insert(Filename, Stat->second);
956 }
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000958 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000959 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000960 uint32_t BucketOffset;
961 {
962 llvm::raw_svector_ostream Out(StatCacheData);
963 // Make sure that no bucket is at offset 0
964 clang::io::Emit32(Out, 0);
965 BucketOffset = Generator.Emit(Out);
966 }
967
968 // Create a blob abbreviation
969 using namespace llvm;
970 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
971 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
975 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
976
977 // Write the stat cache
978 RecordData Record;
979 Record.push_back(pch::STAT_CACHE);
980 Record.push_back(BucketOffset);
981 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000982 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000983}
984
985//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000986// Source Manager Serialization
987//===----------------------------------------------------------------------===//
988
989/// \brief Create an abbreviation for the SLocEntry that refers to a
990/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000991static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000992 using namespace llvm;
993 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
994 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000999 // FileEntry fields.
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001002 // HeaderFileInfo fields.
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001008 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001009}
1010
1011/// \brief Create an abbreviation for the SLocEntry that refers to a
1012/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001013static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001014 using namespace llvm;
1015 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1016 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001022 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001023}
1024
1025/// \brief Create an abbreviation for the SLocEntry that refers to a
1026/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001027static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001028 using namespace llvm;
1029 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1030 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001032 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001033}
1034
1035/// \brief Create an abbreviation for the SLocEntry that refers to an
1036/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001037static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001038 using namespace llvm;
1039 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1040 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001046 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001047}
1048
1049/// \brief Writes the block containing the serialized form of the
1050/// source manager.
1051///
1052/// TODO: We should probably use an on-disk hash table (stored in a
1053/// blob), indexed based on the file name, so that we only create
1054/// entries for files that we actually need. In the common case (no
1055/// errors), we probably won't have to create file entries for any of
1056/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001057void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001058 const Preprocessor &PP,
1059 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001060 RecordData Record;
1061
Chris Lattnerf04ad692009-04-10 17:16:57 +00001062 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001063 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001064
1065 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001066 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1067 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1068 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1069 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001070
Douglas Gregorbd945002009-04-13 16:31:14 +00001071 // Write the line table.
1072 if (SourceMgr.hasLineTable()) {
1073 LineTableInfo &LineTable = SourceMgr.getLineTable();
1074
1075 // Emit the file names
1076 Record.push_back(LineTable.getNumFilenames());
1077 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1078 // Emit the file name
1079 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001080 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001081 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1082 Record.push_back(FilenameLen);
1083 if (FilenameLen)
1084 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1085 }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Douglas Gregorbd945002009-04-13 16:31:14 +00001087 // Emit the line entries
1088 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1089 L != LEnd; ++L) {
1090 // Emit the file ID
1091 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Douglas Gregorbd945002009-04-13 16:31:14 +00001093 // Emit the line entries
1094 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001095 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001096 LEEnd = L->second.end();
1097 LE != LEEnd; ++LE) {
1098 Record.push_back(LE->FileOffset);
1099 Record.push_back(LE->LineNo);
1100 Record.push_back(LE->FilenameID);
1101 Record.push_back((unsigned)LE->FileKind);
1102 Record.push_back(LE->IncludeOffset);
1103 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001104 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001105 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001106 }
1107
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001108 // Write out the source location entry table. We skip the first
1109 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001110 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001111 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001112 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1113 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1114 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1115 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001116 // Get this source location entry.
1117 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001118
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001119 // Record the offset of this source-location entry.
1120 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1121
1122 // Figure out which record code to use.
1123 unsigned Code;
1124 if (SLoc->isFile()) {
1125 if (SLoc->getFile().getContentCache()->Entry)
1126 Code = pch::SM_SLOC_FILE_ENTRY;
1127 else
1128 Code = pch::SM_SLOC_BUFFER_ENTRY;
1129 } else
1130 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1131 Record.clear();
1132 Record.push_back(Code);
1133
1134 Record.push_back(SLoc->getOffset());
1135 if (SLoc->isFile()) {
1136 const SrcMgr::FileInfo &File = SLoc->getFile();
1137 Record.push_back(File.getIncludeLoc().getRawEncoding());
1138 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1139 Record.push_back(File.hasLineDirectives());
1140
1141 const SrcMgr::ContentCache *Content = File.getContentCache();
1142 if (Content->Entry) {
1143 // The source location entry is a file. The blob associated
1144 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Douglas Gregor2d52be52010-03-21 22:49:54 +00001146 // Emit size/modification time for this file.
1147 Record.push_back(Content->Entry->getSize());
1148 Record.push_back(Content->Entry->getModificationTime());
1149
Douglas Gregor12fab312010-03-16 16:35:32 +00001150 // Emit header-search information associated with this file.
1151 HeaderFileInfo HFI;
1152 HeaderSearch &HS = PP.getHeaderSearchInfo();
1153 if (Content->Entry->getUID() < HS.header_file_size())
1154 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1155 Record.push_back(HFI.isImport);
1156 Record.push_back(HFI.DirInfo);
1157 Record.push_back(HFI.NumIncludes);
1158 AddIdentifierRef(HFI.ControllingMacro, Record);
1159
Douglas Gregore650c8c2009-07-07 00:12:59 +00001160 // Turn the file name into an absolute path, if it isn't already.
1161 const char *Filename = Content->Entry->getName();
1162 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001163 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001164 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Douglas Gregore650c8c2009-07-07 00:12:59 +00001166 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001167 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001168
1169 // FIXME: For now, preload all file source locations, so that
1170 // we get the appropriate File entries in the reader. This is
1171 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001172 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001173 } else {
1174 // The source location entry is a buffer. The blob associated
1175 // with this entry contains the contents of the buffer.
1176
1177 // We add one to the size so that we capture the trailing NULL
1178 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1179 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001180 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001181 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001182 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001183 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1184 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001185 Record.clear();
1186 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1187 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001188 llvm::StringRef(Buffer->getBufferStart(),
1189 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001190
1191 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001192 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001193 }
1194 } else {
1195 // The source location entry is an instantiation.
1196 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1197 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1198 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1199 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1200
1201 // Compute the token length for this macro expansion.
1202 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001203 if (I + 1 != N)
1204 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001205 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1206 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1207 }
1208 }
1209
Douglas Gregorc9490c02009-04-16 22:23:12 +00001210 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001211
1212 if (SLocEntryOffsets.empty())
1213 return;
1214
1215 // Write the source-location offsets table into the PCH block. This
1216 // table is used for lazily loading source-location information.
1217 using namespace llvm;
1218 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1219 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1223 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001225 Record.clear();
1226 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1227 Record.push_back(SLocEntryOffsets.size());
1228 Record.push_back(SourceMgr.getNextOffset());
1229 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001230 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001231 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001232
1233 // Write the source location entry preloads array, telling the PCH
1234 // reader which source locations entries it should load eagerly.
1235 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001236}
1237
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001238//===----------------------------------------------------------------------===//
1239// Preprocessor Serialization
1240//===----------------------------------------------------------------------===//
1241
Chris Lattner0b1fb982009-04-10 17:15:23 +00001242/// \brief Writes the block containing the serialized form of the
1243/// preprocessor.
1244///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001245void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001246 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001247
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001248 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1249 if (PP.getCounterValue() != 0) {
1250 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001251 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001252 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001253 }
1254
1255 // Enter the preprocessor block.
1256 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001258 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1259 // FIXME: use diagnostics subsystem for localization etc.
1260 if (PP.SawDateOrTime())
1261 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001263 // Loop over all the macro definitions that are live at the end of the file,
1264 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001265 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001266 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1267 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001268 // FIXME: This emits macros in hash table order, we should do it in a stable
1269 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001270 MacroInfo *MI = I->second;
1271
1272 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1273 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl083abdf2010-07-27 23:01:28 +00001274 // Also skip macros from a PCH file if we're chaining.
1275 if (MI->isBuiltinMacro() || (Chain && MI->isFromPCH()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001276 continue;
1277
Chris Lattner7356a312009-04-11 21:15:38 +00001278 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001279 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001280 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1281 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001283 unsigned Code;
1284 if (MI->isObjectLike()) {
1285 Code = pch::PP_MACRO_OBJECT_LIKE;
1286 } else {
1287 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001289 Record.push_back(MI->isC99Varargs());
1290 Record.push_back(MI->isGNUVarargs());
1291 Record.push_back(MI->getNumArgs());
1292 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1293 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001294 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001295 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001296
1297 // If we have a detailed preprocessing record, record the macro definition
1298 // ID that corresponds to this macro.
1299 if (PPRec)
1300 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1301
Douglas Gregorc9490c02009-04-16 22:23:12 +00001302 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001303 Record.clear();
1304
Chris Lattnerdf961c22009-04-10 18:08:30 +00001305 // Emit the tokens array.
1306 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1307 // Note that we know that the preprocessor does not have any annotation
1308 // tokens in it because they are created by the parser, and thus can't be
1309 // in a macro definition.
1310 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Chris Lattnerdf961c22009-04-10 18:08:30 +00001312 Record.push_back(Tok.getLocation().getRawEncoding());
1313 Record.push_back(Tok.getLength());
1314
Chris Lattnerdf961c22009-04-10 18:08:30 +00001315 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1316 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001317 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Chris Lattnerdf961c22009-04-10 18:08:30 +00001319 // FIXME: Should translate token kind to a stable encoding.
1320 Record.push_back(Tok.getKind());
1321 // FIXME: Should translate token flags to a stable encoding.
1322 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Douglas Gregorc9490c02009-04-16 22:23:12 +00001324 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001325 Record.clear();
1326 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001327 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001328 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001329
1330 // If the preprocessor has a preprocessing record, emit it.
1331 unsigned NumPreprocessingRecords = 0;
1332 if (PPRec) {
1333 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1334 E != EEnd; ++E) {
1335 Record.clear();
1336
1337 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1338 Record.push_back(NumPreprocessingRecords++);
1339 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1340 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1341 AddIdentifierRef(MI->getName(), Record);
1342 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1343 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1344 continue;
1345 }
1346
1347 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1348 // Record this macro definition's location.
1349 pch::IdentID ID = getMacroDefinitionID(MD);
1350 if (ID != MacroDefinitionOffsets.size()) {
1351 if (ID > MacroDefinitionOffsets.size())
1352 MacroDefinitionOffsets.resize(ID + 1);
1353
1354 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1355 } else
1356 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1357
1358 Record.push_back(NumPreprocessingRecords++);
1359 Record.push_back(ID);
1360 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1361 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1362 AddIdentifierRef(MD->getName(), Record);
1363 AddSourceLocation(MD->getLocation(), Record);
1364 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1365 continue;
1366 }
1367 }
1368 }
1369
Douglas Gregorc9490c02009-04-16 22:23:12 +00001370 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001371
1372 // Write the offsets table for the preprocessing record.
1373 if (NumPreprocessingRecords > 0) {
1374 // Write the offsets table for identifier IDs.
1375 using namespace llvm;
1376 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1377 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1381 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1382
1383 Record.clear();
1384 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1385 Record.push_back(NumPreprocessingRecords);
1386 Record.push_back(MacroDefinitionOffsets.size());
1387 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001388 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001389 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1390 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001391}
1392
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001393//===----------------------------------------------------------------------===//
1394// Type Serialization
1395//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001396
Douglas Gregor2cf26342009-04-09 22:27:44 +00001397/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001398void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001399 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001400 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001401 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Douglas Gregor2cf26342009-04-09 22:27:44 +00001403 // Record the offset for this type.
Sebastian Redl681d7232010-07-27 00:17:23 +00001404 unsigned Index = ID - FirstTypeID;
1405 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001406 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001407 else if (TypeOffsets.size() < Index) {
1408 TypeOffsets.resize(Index + 1);
1409 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001410 }
1411
1412 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Douglas Gregor2cf26342009-04-09 22:27:44 +00001414 // Emit the type's representation.
1415 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001416
Douglas Gregora4923eb2009-11-16 21:35:15 +00001417 if (T.hasLocalNonFastQualifiers()) {
1418 Qualifiers Qs = T.getLocalQualifiers();
1419 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001420 Record.push_back(Qs.getAsOpaqueValue());
1421 W.Code = pch::TYPE_EXT_QUAL;
1422 } else {
1423 switch (T->getTypeClass()) {
1424 // For all of the concrete, non-dependent types, call the
1425 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001426#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001427 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001428#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001429#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001430 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001431 }
1432
1433 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001434 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001435
1436 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001437 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001438}
1439
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001440//===----------------------------------------------------------------------===//
1441// Declaration Serialization
1442//===----------------------------------------------------------------------===//
1443
Douglas Gregor2cf26342009-04-09 22:27:44 +00001444/// \brief Write the block containing all of the declaration IDs
1445/// lexically declared within the given DeclContext.
1446///
1447/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1448/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001449uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001450 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001451 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001452 return 0;
1453
Douglas Gregorc9490c02009-04-16 22:23:12 +00001454 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001455 RecordData Record;
Sebastian Redl681d7232010-07-27 00:17:23 +00001456 Record.push_back(pch::DECL_CONTEXT_LEXICAL);
1457 llvm::SmallVector<pch::DeclID, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001458 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1459 D != DEnd; ++D)
Sebastian Redl681d7232010-07-27 00:17:23 +00001460 Decls.push_back(GetDeclRef(*D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001461
Douglas Gregor25123082009-04-22 22:34:57 +00001462 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001463 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
1464 reinterpret_cast<char*>(Decls.data()), Decls.size() * sizeof(pch::DeclID));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001465 return Offset;
1466}
1467
1468/// \brief Write the block containing all of the declaration IDs
1469/// visible from the given DeclContext.
1470///
1471/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1472/// bistream, or 0 if no block was written.
1473uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1474 DeclContext *DC) {
1475 if (DC->getPrimaryContext() != DC)
1476 return 0;
1477
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +00001478 // Since there is no name lookup into functions or methods, don't bother to
1479 // build a visible-declarations table for these entities.
1480 if (DC->isFunctionOrMethod())
1481 return 0;
1482
1483 // If not in C++, we perform name lookup for the translation unit via the
1484 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1485 // FIXME: In C++ we need the visible declarations in order to "see" the
1486 // friend declarations, is there a way to do this without writing the table ?
1487 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor58f06992009-04-18 15:49:20 +00001488 return 0;
1489
Douglas Gregor2cf26342009-04-09 22:27:44 +00001490 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001491 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001492
1493 // Serialize the contents of the mapping used for lookup. Note that,
1494 // although we have two very different code paths, the serialized
1495 // representation is the same for both cases: a declaration name,
1496 // followed by a size, followed by references to the visible
1497 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001498 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001499 RecordData Record;
1500 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001501 if (!Map)
1502 return 0;
1503
Douglas Gregor2cf26342009-04-09 22:27:44 +00001504 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1505 D != DEnd; ++D) {
1506 AddDeclarationName(D->first, Record);
1507 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1508 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001509 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001510 AddDeclRef(*Result.first, Record);
1511 }
1512
1513 if (Record.size() == 0)
1514 return 0;
1515
Douglas Gregorc9490c02009-04-16 22:23:12 +00001516 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001517 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001518 return Offset;
1519}
1520
Sebastian Redl1476ed42010-07-16 16:36:56 +00001521void PCHWriter::WriteTypeDeclOffsets() {
1522 using namespace llvm;
1523 RecordData Record;
1524
1525 // Write the type offsets array
1526 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1527 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1530 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1531 Record.clear();
1532 Record.push_back(pch::TYPE_OFFSET);
1533 Record.push_back(TypeOffsets.size());
1534 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001535 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001536 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1537
1538 // Write the declaration offsets array
1539 Abbrev = new BitCodeAbbrev();
1540 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1543 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1544 Record.clear();
1545 Record.push_back(pch::DECL_OFFSET);
1546 Record.push_back(DeclOffsets.size());
1547 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001548 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001549 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1550}
1551
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001552//===----------------------------------------------------------------------===//
1553// Global Method Pool and Selector Serialization
1554//===----------------------------------------------------------------------===//
1555
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001556namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001557// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001558class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001559 PCHWriter &Writer;
1560
1561public:
1562 typedef Selector key_type;
1563 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Sebastian Redl5d050072010-08-04 17:20:04 +00001565 struct data_type {
1566 pch::SelectorID ID;
1567 ObjCMethodList Instance, Factory;
1568 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001569 typedef const data_type& data_type_ref;
1570
1571 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001573 static unsigned ComputeHash(Selector Sel) {
1574 unsigned N = Sel.getNumArgs();
1575 if (N == 0)
1576 ++N;
1577 unsigned R = 5381;
1578 for (unsigned I = 0; I != N; ++I)
1579 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001580 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001581 return R;
1582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
1584 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001585 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1586 data_type_ref Methods) {
1587 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1588 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001589 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1590 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001591 Method = Method->Next)
1592 if (Method->Method)
1593 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001594 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001595 Method = Method->Next)
1596 if (Method->Method)
1597 DataLen += 4;
1598 clang::io::Emit16(Out, DataLen);
1599 return std::make_pair(KeyLen, DataLen);
1600 }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Douglas Gregor83941df2009-04-25 17:48:32 +00001602 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001603 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001604 assert((Start >> 32) == 0 && "Selector key offset too large");
1605 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001606 unsigned N = Sel.getNumArgs();
1607 clang::io::Emit16(Out, N);
1608 if (N == 0)
1609 N = 1;
1610 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001611 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001612 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1613 }
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001615 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001616 data_type_ref Methods, unsigned DataLen) {
1617 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001618 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001619 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001620 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001621 Method = Method->Next)
1622 if (Method->Method)
1623 ++NumInstanceMethods;
1624
1625 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001626 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001627 Method = Method->Next)
1628 if (Method->Method)
1629 ++NumFactoryMethods;
1630
1631 clang::io::Emit16(Out, NumInstanceMethods);
1632 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001633 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001634 Method = Method->Next)
1635 if (Method->Method)
1636 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001637 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001638 Method = Method->Next)
1639 if (Method->Method)
1640 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001641
1642 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001643 }
1644};
1645} // end anonymous namespace
1646
Sebastian Redl059612d2010-08-03 21:58:15 +00001647/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001648///
1649/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001650/// in an on-disk hash table indexed by the selector. The hash table also
1651/// contains an empty entry for every other selector known to Sema.
1652void PCHWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001653 using namespace llvm;
1654
Sebastian Redl059612d2010-08-03 21:58:15 +00001655 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001656 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001657 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001658 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001659 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001660 {
1661 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Sebastian Redl059612d2010-08-03 21:58:15 +00001663 // Create the on-disk hash table representation. We walk through every
1664 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001665 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl5d050072010-08-04 17:20:04 +00001666 for (llvm::DenseMap<Selector, pch::SelectorID>::iterator
1667 I = SelectorIDs.begin(), E = SelectorIDs.end();
1668 I != E; ++I) {
1669 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001670 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl5d050072010-08-04 17:20:04 +00001671 PCHMethodPoolTrait::data_type Data = {
1672 I->second,
1673 ObjCMethodList(),
1674 ObjCMethodList()
1675 };
1676 if (F != SemaRef.MethodPool.end()) {
1677 Data.Instance = F->second.first;
1678 Data.Factory = F->second.second;
1679 }
Sebastian Redle58aa892010-08-04 18:21:41 +00001680 // Only write this selector if it's not in an existing PCH or something
1681 // changed.
1682 if (Chain && I->second < FirstSelectorID) {
1683 // Selector already exists. Did it change?
1684 bool changed = false;
1685 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1686 M = M->Next) {
1687 if (M->Method->getPCHLevel() == 0)
1688 changed = true;
1689 }
1690 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1691 M = M->Next) {
1692 if (M->Method->getPCHLevel() == 0)
1693 changed = true;
1694 }
1695 if (!changed)
1696 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001697 } else if (Data.Instance.Method || Data.Factory.Method) {
1698 // A new method pool entry.
1699 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001700 }
Sebastian Redl5d050072010-08-04 17:20:04 +00001701 Generator.insert(S, Data);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001702 }
1703
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001704 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001705 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001706 uint32_t BucketOffset;
1707 {
1708 PCHMethodPoolTrait Trait(*this);
1709 llvm::raw_svector_ostream Out(MethodPool);
1710 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001711 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001712 BucketOffset = Generator.Emit(Out, Trait);
1713 }
1714
1715 // Create a blob abbreviation
1716 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1717 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001720 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1721 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1722
Douglas Gregor83941df2009-04-25 17:48:32 +00001723 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001724 RecordData Record;
1725 Record.push_back(pch::METHOD_POOL);
1726 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001727 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001728 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001729
1730 // Create a blob abbreviation for the selector table offsets.
1731 Abbrev = new BitCodeAbbrev();
1732 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1735 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1736
1737 // Write the selector offsets table.
1738 Record.clear();
1739 Record.push_back(pch::SELECTOR_OFFSETS);
1740 Record.push_back(SelectorOffsets.size());
1741 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001742 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001743 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001744 }
1745}
1746
Fariborz Jahanian32019832010-07-23 19:11:11 +00001747/// \brief Write the selectors referenced in @selector expression into PCH file.
1748void PCHWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
1749 using namespace llvm;
1750 if (SemaRef.ReferencedSelectors.empty())
1751 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001752
Fariborz Jahanian32019832010-07-23 19:11:11 +00001753 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001754
Sebastian Redla68340f2010-08-04 22:21:29 +00001755 // Note: this writes out all references even for a dependent PCH. But it is
1756 // very tricky to fix, and given that @selector shouldn't really appear in
1757 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001758 for (DenseMap<Selector, SourceLocation>::iterator S =
1759 SemaRef.ReferencedSelectors.begin(),
1760 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1761 Selector Sel = (*S).first;
1762 SourceLocation Loc = (*S).second;
1763 AddSelectorRef(Sel, Record);
1764 AddSourceLocation(Loc, Record);
1765 }
1766 Stream.EmitRecord(pch::REFERENCED_SELECTOR_POOL, Record);
1767}
1768
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001769//===----------------------------------------------------------------------===//
1770// Identifier Table Serialization
1771//===----------------------------------------------------------------------===//
1772
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001773namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001774class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001775 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001776 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001777
Douglas Gregora92193e2009-04-28 21:18:29 +00001778 /// \brief Determines whether this is an "interesting" identifier
1779 /// that needs a full IdentifierInfo structure written into the hash
1780 /// table.
1781 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1782 return II->isPoisoned() ||
1783 II->isExtensionToken() ||
1784 II->hasMacroDefinition() ||
1785 II->getObjCOrBuiltinID() ||
1786 II->getFETokenInfo<void>();
1787 }
1788
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001789public:
1790 typedef const IdentifierInfo* key_type;
1791 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001793 typedef pch::IdentID data_type;
1794 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001795
1796 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001797 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001798
1799 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001800 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001801 }
Mike Stump1eb44332009-09-09 15:08:12 +00001802
1803 std::pair<unsigned,unsigned>
1804 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001805 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001806 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001807 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1808 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001809 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001810 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001811 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001812 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001813 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1814 DEnd = IdentifierResolver::end();
1815 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001816 DataLen += sizeof(pch::DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00001817 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001818 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001819 // We emit the key length after the data length so that every
1820 // string is preceded by a 16-bit length. This matches the PTH
1821 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001822 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001823 return std::make_pair(KeyLen, DataLen);
1824 }
Mike Stump1eb44332009-09-09 15:08:12 +00001825
1826 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001827 unsigned KeyLen) {
1828 // Record the location of the key data. This is used when generating
1829 // the mapping from persistent IDs to strings.
1830 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001831 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001832 }
Mike Stump1eb44332009-09-09 15:08:12 +00001833
1834 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001835 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001836 if (!isInterestingIdentifier(II)) {
1837 clang::io::Emit32(Out, ID << 1);
1838 return;
1839 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001840
Douglas Gregora92193e2009-04-28 21:18:29 +00001841 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001842 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001843 bool hasMacroDefinition =
1844 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001845 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001846 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001847 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1848 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1849 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1850 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001851 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001852
Douglas Gregor37e26842009-04-21 23:56:24 +00001853 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001854 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001855
Douglas Gregor668c1a42009-04-21 22:25:48 +00001856 // Emit the declaration IDs in reverse order, because the
1857 // IdentifierResolver provides the declarations as they would be
1858 // visible (e.g., the function "stat" would come before the struct
1859 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1860 // adds declarations to the end of the list (so we need to see the
1861 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001862 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00001863 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001864 IdentifierResolver::end());
1865 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1866 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001867 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001868 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001869 }
1870};
1871} // end anonymous namespace
1872
Douglas Gregorafaf3082009-04-11 00:14:32 +00001873/// \brief Write the identifier table into the PCH file.
1874///
1875/// The identifier table consists of a blob containing string data
1876/// (the actual identifiers themselves) and a separate "offsets" index
1877/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001878void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001879 using namespace llvm;
1880
1881 // Create and write out the blob that contains the identifier
1882 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001883 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001884 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Douglas Gregor92b059e2009-04-28 20:33:11 +00001886 // Look for any identifiers that were named while processing the
1887 // headers, but are otherwise not needed. We add these to the hash
1888 // table to enable checking of the predefines buffer in the case
1889 // where the user adds new macro definitions when building the PCH
1890 // file.
1891 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1892 IDEnd = PP.getIdentifierTable().end();
1893 ID != IDEnd; ++ID)
1894 getIdentifierRef(ID->second);
1895
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001896 // Create the on-disk hash table representation. We only store offsets
1897 // for identifiers that appear here for the first time.
1898 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001899 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1900 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1901 ID != IDEnd; ++ID) {
1902 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00001903 if (!Chain || !ID->first->isFromPCH())
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001904 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001905 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001906
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001907 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001908 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001909 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001910 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001911 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001912 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001913 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001914 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001915 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001916 }
1917
1918 // Create a blob abbreviation
1919 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1920 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001923 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001924
1925 // Write the identifier table
1926 RecordData Record;
1927 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001928 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001929 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001930 }
1931
1932 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001933 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1934 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1937 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1938
1939 RecordData Record;
1940 Record.push_back(pch::IDENTIFIER_OFFSET);
1941 Record.push_back(IdentifierOffsets.size());
1942 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001943 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001944 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001945}
1946
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001947//===----------------------------------------------------------------------===//
1948// General Serialization Routines
1949//===----------------------------------------------------------------------===//
1950
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001951/// \brief Write a record containing the given attributes.
1952void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1953 RecordData Record;
1954 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001955 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001956 Record.push_back(Attr->isInherited());
1957 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001958 default:
1959 assert(0 && "Does not support PCH writing for this attribute yet!");
1960 break;
Sean Hunt387475d2010-06-16 23:43:53 +00001961 case attr::Alias:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001962 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1963 break;
1964
Sean Hunt387475d2010-06-16 23:43:53 +00001965 case attr::AlignMac68k:
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001966 break;
1967
Sean Hunt387475d2010-06-16 23:43:53 +00001968 case attr::Aligned:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001969 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1970 break;
1971
Sean Hunt387475d2010-06-16 23:43:53 +00001972 case attr::AlwaysInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001973 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Sean Hunt387475d2010-06-16 23:43:53 +00001975 case attr::AnalyzerNoReturn:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001976 break;
1977
Sean Hunt387475d2010-06-16 23:43:53 +00001978 case attr::Annotate:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001979 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1980 break;
1981
Sean Hunt387475d2010-06-16 23:43:53 +00001982 case attr::AsmLabel:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001983 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1984 break;
1985
Sean Hunt387475d2010-06-16 23:43:53 +00001986 case attr::BaseCheck:
Sean Hunt7725e672009-11-25 04:20:27 +00001987 break;
1988
Sean Hunt387475d2010-06-16 23:43:53 +00001989 case attr::Blocks:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001990 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1991 break;
1992
Sean Hunt387475d2010-06-16 23:43:53 +00001993 case attr::CDecl:
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001994 break;
1995
Sean Hunt387475d2010-06-16 23:43:53 +00001996 case attr::Cleanup:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001997 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1998 break;
1999
Sean Hunt387475d2010-06-16 23:43:53 +00002000 case attr::Const:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002001 break;
2002
Sean Hunt387475d2010-06-16 23:43:53 +00002003 case attr::Constructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002004 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2005 break;
2006
Sean Hunt387475d2010-06-16 23:43:53 +00002007 case attr::DLLExport:
2008 case attr::DLLImport:
2009 case attr::Deprecated:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002010 break;
2011
Sean Hunt387475d2010-06-16 23:43:53 +00002012 case attr::Destructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002013 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2014 break;
2015
Sean Hunt387475d2010-06-16 23:43:53 +00002016 case attr::FastCall:
2017 case attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002018 break;
2019
Sean Hunt387475d2010-06-16 23:43:53 +00002020 case attr::Format: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002021 const FormatAttr *Format = cast<FormatAttr>(Attr);
2022 AddString(Format->getType(), Record);
2023 Record.push_back(Format->getFormatIdx());
2024 Record.push_back(Format->getFirstArg());
2025 break;
2026 }
2027
Sean Hunt387475d2010-06-16 23:43:53 +00002028 case attr::FormatArg: {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002029 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
2030 Record.push_back(Format->getFormatIdx());
2031 break;
2032 }
2033
Sean Hunt387475d2010-06-16 23:43:53 +00002034 case attr::Sentinel : {
Fariborz Jahanian5b530052009-05-13 18:09:35 +00002035 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
2036 Record.push_back(Sentinel->getSentinel());
2037 Record.push_back(Sentinel->getNullPos());
2038 break;
2039 }
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Sean Hunt387475d2010-06-16 23:43:53 +00002041 case attr::GNUInline:
2042 case attr::Hiding:
2043 case attr::IBAction:
2044 case attr::IBOutlet:
2045 case attr::Malloc:
2046 case attr::NoDebug:
2047 case attr::NoInline:
2048 case attr::NoReturn:
2049 case attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002050 break;
2051
Sean Hunt387475d2010-06-16 23:43:53 +00002052 case attr::IBOutletCollection: {
Ted Kremenek857e9182010-05-19 17:38:06 +00002053 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
2054 AddDeclRef(ICA->getClass(), Record);
2055 break;
2056 }
2057
Sean Hunt387475d2010-06-16 23:43:53 +00002058 case attr::NonNull: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002059 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2060 Record.push_back(NonNull->size());
2061 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2062 break;
2063 }
2064
Sean Hunt387475d2010-06-16 23:43:53 +00002065 case attr::CFReturnsNotRetained:
2066 case attr::CFReturnsRetained:
2067 case attr::NSReturnsNotRetained:
2068 case attr::NSReturnsRetained:
2069 case attr::ObjCException:
2070 case attr::ObjCNSObject:
2071 case attr::Overloadable:
2072 case attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002073 break;
2074
Sean Hunt387475d2010-06-16 23:43:53 +00002075 case attr::MaxFieldAlignment:
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00002076 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002077 break;
2078
Sean Hunt387475d2010-06-16 23:43:53 +00002079 case attr::Packed:
Anders Carlssona860e752009-08-08 18:23:56 +00002080 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Sean Hunt387475d2010-06-16 23:43:53 +00002082 case attr::Pure:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002083 break;
2084
Sean Hunt387475d2010-06-16 23:43:53 +00002085 case attr::Regparm:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002086 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2087 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002088
Sean Hunt387475d2010-06-16 23:43:53 +00002089 case attr::ReqdWorkGroupSize:
Nate Begeman6f3d8382009-06-26 06:32:41 +00002090 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2091 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2092 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2093 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002094
Sean Hunt387475d2010-06-16 23:43:53 +00002095 case attr::Section:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002096 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2097 break;
2098
Sean Hunt387475d2010-06-16 23:43:53 +00002099 case attr::StdCall:
2100 case attr::TransparentUnion:
2101 case attr::Unavailable:
2102 case attr::Unused:
2103 case attr::Used:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002104 break;
2105
Sean Hunt387475d2010-06-16 23:43:53 +00002106 case attr::Visibility:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002107 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00002108 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Eli Friedmanaa8b0d12010-08-05 06:57:20 +00002109 Record.push_back(cast<VisibilityAttr>(Attr)->isFromPragma());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002110 break;
2111
Sean Hunt387475d2010-06-16 23:43:53 +00002112 case attr::WarnUnusedResult:
2113 case attr::Weak:
2114 case attr::WeakRef:
2115 case attr::WeakImport:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002116 break;
2117 }
2118 }
2119
Douglas Gregorc9490c02009-04-16 22:23:12 +00002120 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002121}
2122
2123void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2124 Record.push_back(Str.size());
2125 Record.insert(Record.end(), Str.begin(), Str.end());
2126}
2127
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002128/// \brief Note that the identifier II occurs at the given offset
2129/// within the identifier table.
2130void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002131 pch::IdentID ID = IdentifierIDs[II];
2132 // Only store offsets new to this PCH file. Other identifier names are looked
2133 // up earlier in the chain and thus don't need an offset.
2134 if (ID >= FirstIdentID)
2135 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002136}
2137
Douglas Gregor83941df2009-04-25 17:48:32 +00002138/// \brief Note that the selector Sel occurs at the given offset
2139/// within the method pool/selector table.
2140void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2141 unsigned ID = SelectorIDs[Sel];
2142 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002143 // Don't record offsets for selectors that are also available in a different
2144 // file.
2145 if (ID < FirstSelectorID)
2146 return;
2147 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002148}
2149
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002150PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Sebastian Redle58aa892010-08-04 18:21:41 +00002151 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
2152 FirstTypeID(pch::NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
2153 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
2154 NextSelectorID(FirstSelectorID), CollectedStmts(&StmtsToEmit),
2155 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2156 NumVisibleDeclContexts(0) {
Sebastian Redl30c514c2010-07-14 23:45:08 +00002157}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002158
Douglas Gregore650c8c2009-07-07 00:12:59 +00002159void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002160 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002161 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002162 Stream.Emit((unsigned)'C', 8);
2163 Stream.Emit((unsigned)'P', 8);
2164 Stream.Emit((unsigned)'C', 8);
2165 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002166
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002167 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002168
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002169 if (Chain)
Sebastian Redl30c514c2010-07-14 23:45:08 +00002170 WritePCHChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002171 else
2172 WritePCHCore(SemaRef, StatCalls, isysroot);
2173}
2174
2175void PCHWriter::WritePCHCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2176 const char *isysroot) {
2177 using namespace llvm;
2178
2179 ASTContext &Context = SemaRef.Context;
2180 Preprocessor &PP = SemaRef.PP;
2181
Douglas Gregor2cf26342009-04-09 22:27:44 +00002182 // The translation unit is the first declaration we'll emit.
2183 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002184 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002185 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002186
Douglas Gregor2deaea32009-04-22 18:49:13 +00002187 // Make sure that we emit IdentifierInfos (and any attached
2188 // declarations) for builtins.
2189 {
2190 IdentifierTable &Table = PP.getIdentifierTable();
2191 llvm::SmallVector<const char *, 32> BuiltinNames;
2192 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2193 Context.getLangOptions().NoBuiltin);
2194 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2195 getIdentifierRef(&Table.get(BuiltinNames[I]));
2196 }
2197
Chris Lattner63d65f82009-09-08 18:19:27 +00002198 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002199 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002200 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002201 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002202 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2203 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002204 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002205
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002206 // Build a record containing all of the static unused functions in this file.
2207 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002208 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002209 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Sebastian Redl40566802010-08-05 18:21:25 +00002210
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002211 RecordData WeakUndeclaredIdentifiers;
2212 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2213 WeakUndeclaredIdentifiers.push_back(
2214 SemaRef.WeakUndeclaredIdentifiers.size());
2215 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2216 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2217 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2218 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2219 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2220 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2221 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2222 }
2223 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002224
Douglas Gregor14c22f22009-04-22 22:18:58 +00002225 // Build a record containing all of the locally-scoped external
2226 // declarations in this header file. Generally, this record will be
2227 // empty.
2228 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002229 // FIXME: This is filling in the PCH file in densemap order which is
2230 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002231 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002232 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2233 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2234 TD != TDEnd; ++TD)
2235 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2236
Douglas Gregorb81c1702009-04-27 20:06:05 +00002237 // Build a record containing all of the ext_vector declarations.
2238 RecordData ExtVectorDecls;
2239 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2240 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2241
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002242 // Build a record containing all of the VTable uses information.
2243 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002244 if (!SemaRef.VTableUses.empty()) {
2245 VTableUses.push_back(SemaRef.VTableUses.size());
2246 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2247 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2248 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2249 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2250 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002251 }
2252
2253 // Build a record containing all of dynamic classes declarations.
2254 RecordData DynamicClasses;
2255 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2256 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2257
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002258 // Build a record containing all of pending implicit instantiations.
2259 RecordData PendingImplicitInstantiations;
2260 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2261 I = SemaRef.PendingImplicitInstantiations.begin(),
2262 N = SemaRef.PendingImplicitInstantiations.end(); I != N; ++I) {
2263 AddDeclRef(I->first, PendingImplicitInstantiations);
2264 AddSourceLocation(I->second, PendingImplicitInstantiations);
2265 }
2266 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2267 "There are local ones at end of translation unit!");
2268
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002269 // Build a record containing some declaration references.
2270 RecordData SemaDeclRefs;
2271 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2272 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2273 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2274 }
2275
Douglas Gregor2cf26342009-04-09 22:27:44 +00002276 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002277 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002278 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002279 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002280 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002281 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002282 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002283 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002284 // Write the record of special types.
2285 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002287 AddTypeRef(Context.getBuiltinVaListType(), Record);
2288 AddTypeRef(Context.getObjCIdType(), Record);
2289 AddTypeRef(Context.getObjCSelType(), Record);
2290 AddTypeRef(Context.getObjCProtoType(), Record);
2291 AddTypeRef(Context.getObjCClassType(), Record);
2292 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2293 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2294 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002295 AddTypeRef(Context.getjmp_bufType(), Record);
2296 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002297 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2298 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002299 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002300 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002301 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2302 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002303 Record.push_back(Context.isInt128Installed());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002304 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002305
Douglas Gregor366809a2009-04-26 03:49:13 +00002306 // Keep writing types and declarations until all types and
2307 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002308 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2309 WriteDeclsBlockAbbrevs();
2310 while (!DeclTypesToEmit.empty()) {
2311 DeclOrType DOT = DeclTypesToEmit.front();
2312 DeclTypesToEmit.pop();
2313 if (DOT.isType())
2314 WriteType(DOT.getType());
2315 else
2316 WriteDecl(Context, DOT.getDecl());
2317 }
2318 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002319
Douglas Gregor813a97b2009-10-17 17:25:45 +00002320 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002321 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002322 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002323 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002324
Sebastian Redl1476ed42010-07-16 16:36:56 +00002325 WriteTypeDeclOffsets();
Douglas Gregorad1de002009-04-18 05:55:16 +00002326
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002327 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002328 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002329 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002330
2331 // Write the record containing tentative definitions.
2332 if (!TentativeDefinitions.empty())
2333 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002334
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002335 // Write the record containing unused static functions.
2336 if (!UnusedStaticFuncs.empty())
2337 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002338
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002339 // Write the record containing weak undeclared identifiers.
2340 if (!WeakUndeclaredIdentifiers.empty())
2341 Stream.EmitRecord(pch::WEAK_UNDECLARED_IDENTIFIERS,
2342 WeakUndeclaredIdentifiers);
2343
Douglas Gregor14c22f22009-04-22 22:18:58 +00002344 // Write the record containing locally-scoped external definitions.
2345 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002346 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002347 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002348
2349 // Write the record containing ext_vector type names.
2350 if (!ExtVectorDecls.empty())
2351 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002353 // Write the record containing VTable uses information.
2354 if (!VTableUses.empty())
2355 Stream.EmitRecord(pch::VTABLE_USES, VTableUses);
2356
2357 // Write the record containing dynamic classes declarations.
2358 if (!DynamicClasses.empty())
2359 Stream.EmitRecord(pch::DYNAMIC_CLASSES, DynamicClasses);
2360
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002361 // Write the record containing pending implicit instantiations.
2362 if (!PendingImplicitInstantiations.empty())
2363 Stream.EmitRecord(pch::PENDING_IMPLICIT_INSTANTIATIONS,
2364 PendingImplicitInstantiations);
2365
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002366 // Write the record containing declaration references of Sema.
2367 if (!SemaDeclRefs.empty())
2368 Stream.EmitRecord(pch::SEMA_DECL_REFS, SemaDeclRefs);
2369
Douglas Gregor3e1af842009-04-17 22:13:46 +00002370 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002371 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002372 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002373 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002374 Record.push_back(NumLexicalDeclContexts);
2375 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002376 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002377 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002378}
2379
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002380void PCHWriter::WritePCHChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002381 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002382 using namespace llvm;
2383
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002384 FirstDeclID += Chain->getTotalNumDecls();
2385 FirstTypeID += Chain->getTotalNumTypes();
2386 FirstIdentID += Chain->getTotalNumIdentifiers();
Sebastian Redle58aa892010-08-04 18:21:41 +00002387 FirstSelectorID += Chain->getTotalNumSelectors();
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002388 NextDeclID = FirstDeclID;
2389 NextTypeID = FirstTypeID;
2390 NextIdentID = FirstIdentID;
Sebastian Redle58aa892010-08-04 18:21:41 +00002391 NextSelectorID = FirstSelectorID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002392
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002393 ASTContext &Context = SemaRef.Context;
2394 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002395
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002396 RecordData Record;
2397 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002398 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002399 if (StatCalls && !isysroot)
2400 WriteStatCache(*StatCalls);
2401 // FIXME: Source manager block should only write new stuff, which could be
2402 // done by tracking the largest ID in the chain
2403 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002404
2405 // The special types are in the chained PCH.
2406
2407 // We don't start with the translation unit, but with its decls that
2408 // don't come from the other PCH.
2409 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Sebastian Redld692af72010-07-27 18:24:41 +00002410 llvm::SmallVector<pch::DeclID, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002411 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2412 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002413 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002414 if ((*I)->getPCHLevel() == 0)
2415 NewGlobalDecls.push_back(GetDeclRef(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002416 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002417 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002418 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
2419 Abv->Add(llvm::BitCodeAbbrevOp(pch::TU_UPDATE_LEXICAL));
2420 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2421 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2422 Record.clear();
2423 Record.push_back(pch::TU_UPDATE_LEXICAL);
2424 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2425 reinterpret_cast<const char*>(NewGlobalDecls.data()),
2426 NewGlobalDecls.size() * sizeof(pch::DeclID));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002427
Sebastian Redl083abdf2010-07-27 23:01:28 +00002428 // Build a record containing all of the new tentative definitions in this
2429 // file, in TentativeDefinitions order.
2430 RecordData TentativeDefinitions;
2431 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2432 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2433 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2434 }
2435
2436 // Build a record containing all of the static unused functions in this file.
2437 RecordData UnusedStaticFuncs;
2438 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i) {
2439 if (SemaRef.UnusedStaticFuncs[i]->getPCHLevel() == 0)
2440 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
2441 }
2442
Sebastian Redl40566802010-08-05 18:21:25 +00002443 // We write the entire table, overwriting the tables from the chain.
2444 RecordData WeakUndeclaredIdentifiers;
2445 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2446 WeakUndeclaredIdentifiers.push_back(
2447 SemaRef.WeakUndeclaredIdentifiers.size());
2448 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2449 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2450 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2451 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2452 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2453 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2454 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2455 }
2456 }
2457
Sebastian Redl083abdf2010-07-27 23:01:28 +00002458 // Build a record containing all of the locally-scoped external
2459 // declarations in this header file. Generally, this record will be
2460 // empty.
2461 RecordData LocallyScopedExternalDecls;
2462 // FIXME: This is filling in the PCH file in densemap order which is
2463 // nondeterminstic!
2464 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2465 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2466 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2467 TD != TDEnd; ++TD) {
2468 if (TD->second->getPCHLevel() == 0)
2469 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2470 }
2471
2472 // Build a record containing all of the ext_vector declarations.
2473 RecordData ExtVectorDecls;
2474 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2475 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2476 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2477 }
2478
Sebastian Redl40566802010-08-05 18:21:25 +00002479 // Build a record containing all of the VTable uses information.
2480 // We write everything here, because it's too hard to determine whether
2481 // a use is new to this part.
2482 RecordData VTableUses;
2483 if (!SemaRef.VTableUses.empty()) {
2484 VTableUses.push_back(SemaRef.VTableUses.size());
2485 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2486 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2487 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2488 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2489 }
2490 }
2491
2492 // Build a record containing all of dynamic classes declarations.
2493 RecordData DynamicClasses;
2494 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2495 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2496 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2497
2498 // Build a record containing all of pending implicit instantiations.
2499 RecordData PendingImplicitInstantiations;
2500 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2501 I = SemaRef.PendingImplicitInstantiations.begin(),
2502 N = SemaRef.PendingImplicitInstantiations.end(); I != N; ++I) {
2503 if (I->first->getPCHLevel() == 0) {
2504 AddDeclRef(I->first, PendingImplicitInstantiations);
2505 AddSourceLocation(I->second, PendingImplicitInstantiations);
2506 }
2507 }
2508 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2509 "There are local ones at end of translation unit!");
2510
2511 // Build a record containing some declaration references.
2512 // It's not worth the effort to avoid duplication here.
2513 RecordData SemaDeclRefs;
2514 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2515 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2516 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2517 }
2518
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002519 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2520 WriteDeclsBlockAbbrevs();
2521 while (!DeclTypesToEmit.empty()) {
2522 DeclOrType DOT = DeclTypesToEmit.front();
2523 DeclTypesToEmit.pop();
2524 if (DOT.isType())
2525 WriteType(DOT.getType());
2526 else
2527 WriteDecl(Context, DOT.getDecl());
2528 }
2529 Stream.ExitBlock();
2530
Sebastian Redl083abdf2010-07-27 23:01:28 +00002531 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002532 WriteSelectors(SemaRef);
2533 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002534 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002535 WriteTypeDeclOffsets();
Sebastian Redl083abdf2010-07-27 23:01:28 +00002536
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002537 /// Build a record containing first declarations from a chained PCH and the
2538 /// most recent declarations in this PCH that they point to.
2539 RecordData FirstLatestDeclIDs;
2540 for (FirstLatestDeclMap::iterator
2541 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2542 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2543 "Expected first & second to be in different PCHs");
2544 AddDeclRef(I->first, FirstLatestDeclIDs);
2545 AddDeclRef(I->second, FirstLatestDeclIDs);
2546 }
2547 if (!FirstLatestDeclIDs.empty())
2548 Stream.EmitRecord(pch::REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
2549
Sebastian Redl083abdf2010-07-27 23:01:28 +00002550 // Write the record containing external, unnamed definitions.
2551 if (!ExternalDefinitions.empty())
2552 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
2553
2554 // Write the record containing tentative definitions.
2555 if (!TentativeDefinitions.empty())
2556 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
2557
2558 // Write the record containing unused static functions.
2559 if (!UnusedStaticFuncs.empty())
2560 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
2561
Sebastian Redl40566802010-08-05 18:21:25 +00002562 // Write the record containing weak undeclared identifiers.
2563 if (!WeakUndeclaredIdentifiers.empty())
2564 Stream.EmitRecord(pch::WEAK_UNDECLARED_IDENTIFIERS,
2565 WeakUndeclaredIdentifiers);
2566
Sebastian Redl083abdf2010-07-27 23:01:28 +00002567 // Write the record containing locally-scoped external definitions.
2568 if (!LocallyScopedExternalDecls.empty())
2569 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2570 LocallyScopedExternalDecls);
2571
2572 // Write the record containing ext_vector type names.
2573 if (!ExtVectorDecls.empty())
2574 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
2575
Sebastian Redl40566802010-08-05 18:21:25 +00002576 // Write the record containing VTable uses information.
2577 if (!VTableUses.empty())
2578 Stream.EmitRecord(pch::VTABLE_USES, VTableUses);
2579
2580 // Write the record containing dynamic classes declarations.
2581 if (!DynamicClasses.empty())
2582 Stream.EmitRecord(pch::DYNAMIC_CLASSES, DynamicClasses);
2583
2584 // Write the record containing pending implicit instantiations.
2585 if (!PendingImplicitInstantiations.empty())
2586 Stream.EmitRecord(pch::PENDING_IMPLICIT_INSTANTIATIONS,
2587 PendingImplicitInstantiations);
2588
2589 // Write the record containing declaration references of Sema.
2590 if (!SemaDeclRefs.empty())
2591 Stream.EmitRecord(pch::SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002592
2593 Record.clear();
2594 Record.push_back(NumStatements);
2595 Record.push_back(NumMacros);
2596 Record.push_back(NumLexicalDeclContexts);
2597 Record.push_back(NumVisibleDeclContexts);
2598 Stream.EmitRecord(pch::STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002599 Stream.ExitBlock();
2600}
2601
Douglas Gregor2cf26342009-04-09 22:27:44 +00002602void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2603 Record.push_back(Loc.getRawEncoding());
2604}
2605
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002606void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2607 AddSourceLocation(Range.getBegin(), Record);
2608 AddSourceLocation(Range.getEnd(), Record);
2609}
2610
Douglas Gregor2cf26342009-04-09 22:27:44 +00002611void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2612 Record.push_back(Value.getBitWidth());
2613 unsigned N = Value.getNumWords();
2614 const uint64_t* Words = Value.getRawData();
2615 for (unsigned I = 0; I != N; ++I)
2616 Record.push_back(Words[I]);
2617}
2618
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002619void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2620 Record.push_back(Value.isUnsigned());
2621 AddAPInt(Value, Record);
2622}
2623
Douglas Gregor17fc2232009-04-14 21:55:33 +00002624void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2625 AddAPInt(Value.bitcastToAPInt(), Record);
2626}
2627
Douglas Gregor2cf26342009-04-09 22:27:44 +00002628void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002629 Record.push_back(getIdentifierRef(II));
2630}
2631
2632pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2633 if (II == 0)
2634 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002635
2636 pch::IdentID &ID = IdentifierIDs[II];
2637 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002638 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002639 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002640}
2641
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002642pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2643 if (MD == 0)
2644 return 0;
2645
2646 pch::IdentID &ID = MacroDefinitions[MD];
2647 if (ID == 0)
2648 ID = MacroDefinitions.size();
2649 return ID;
2650}
2651
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002652void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002653 Record.push_back(getSelectorRef(SelRef));
2654}
2655
2656pch::SelectorID PCHWriter::getSelectorRef(Selector Sel) {
2657 if (Sel.getAsOpaquePtr() == 0) {
2658 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002659 }
2660
Sebastian Redl5d050072010-08-04 17:20:04 +00002661 pch::SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00002662 if (SID == 0 && Chain) {
2663 // This might trigger a ReadSelector callback, which will set the ID for
2664 // this selector.
2665 Chain->LoadSelector(Sel);
2666 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002667 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00002668 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002669 }
Sebastian Redl5d050072010-08-04 17:20:04 +00002670 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002671}
2672
Chris Lattnerd2598362010-05-10 00:25:06 +00002673void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2674 AddDeclRef(Temp->getDestructor(), Record);
2675}
2676
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002677void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2678 const TemplateArgumentLocInfo &Arg,
2679 RecordData &Record) {
2680 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002681 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002682 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002683 break;
2684 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002685 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002686 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002687 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002688 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2689 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002690 break;
John McCall833ca992009-10-29 08:12:44 +00002691 case TemplateArgument::Null:
2692 case TemplateArgument::Integral:
2693 case TemplateArgument::Declaration:
2694 case TemplateArgument::Pack:
2695 break;
2696 }
2697}
2698
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002699void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2700 RecordData &Record) {
2701 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002702
2703 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2704 bool InfoHasSameExpr
2705 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2706 Record.push_back(InfoHasSameExpr);
2707 if (InfoHasSameExpr)
2708 return; // Avoid storing the same expr twice.
2709 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002710 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2711 Record);
2712}
2713
John McCalla93c9342009-12-07 02:54:59 +00002714void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2715 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002716 AddTypeRef(QualType(), Record);
2717 return;
2718 }
2719
John McCalla93c9342009-12-07 02:54:59 +00002720 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002721 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002722 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002723 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002724}
2725
Douglas Gregor2cf26342009-04-09 22:27:44 +00002726void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2727 if (T.isNull()) {
2728 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2729 return;
2730 }
2731
Douglas Gregora4923eb2009-11-16 21:35:15 +00002732 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002733 T.removeFastQualifiers();
2734
Douglas Gregora4923eb2009-11-16 21:35:15 +00002735 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002736 pch::TypeID &ID = TypeIDs[T];
2737 if (ID == 0) {
2738 // We haven't seen these qualifiers applied to this type before.
2739 // Assign it a new ID. This is the only time we enqueue a
2740 // qualified type, and it has no CV qualifiers.
2741 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002742 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002743 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002744
John McCall0953e762009-09-24 19:53:00 +00002745 // Encode the type qualifiers in the type reference.
2746 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2747 return;
2748 }
2749
Douglas Gregora4923eb2009-11-16 21:35:15 +00002750 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002751
Douglas Gregor2cf26342009-04-09 22:27:44 +00002752 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002753 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002754 switch (BT->getKind()) {
2755 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2756 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2757 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2758 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2759 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2760 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2761 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2762 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002763 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002764 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2765 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2766 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2767 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2768 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2769 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2770 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002771 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002772 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2773 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2774 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002775 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002776 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2777 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002778 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2779 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002780 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2781 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002782 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002783 case BuiltinType::UndeducedAuto:
2784 assert(0 && "Should not see undeduced auto here");
2785 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002786 }
2787
John McCall0953e762009-09-24 19:53:00 +00002788 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002789 return;
2790 }
2791
John McCall0953e762009-09-24 19:53:00 +00002792 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002793 if (ID == 0) {
2794 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002795 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002796 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002797 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002798 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002799
2800 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002801 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002802}
2803
2804void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002805 Record.push_back(GetDeclRef(D));
2806}
2807
2808pch::DeclID PCHWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002809 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002810 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002811 }
2812
Douglas Gregor8038d512009-04-10 17:25:41 +00002813 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002814 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002815 // We haven't seen this declaration before. Give it a new ID and
2816 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002817 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002818 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002819 }
2820
Sebastian Redl681d7232010-07-27 00:17:23 +00002821 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002822}
2823
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002824pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2825 if (D == 0)
2826 return 0;
2827
2828 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2829 return DeclIDs[D];
2830}
2831
Douglas Gregor2cf26342009-04-09 22:27:44 +00002832void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002833 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002834 Record.push_back(Name.getNameKind());
2835 switch (Name.getNameKind()) {
2836 case DeclarationName::Identifier:
2837 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2838 break;
2839
2840 case DeclarationName::ObjCZeroArgSelector:
2841 case DeclarationName::ObjCOneArgSelector:
2842 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002843 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002844 break;
2845
2846 case DeclarationName::CXXConstructorName:
2847 case DeclarationName::CXXDestructorName:
2848 case DeclarationName::CXXConversionFunctionName:
2849 AddTypeRef(Name.getCXXNameType(), Record);
2850 break;
2851
2852 case DeclarationName::CXXOperatorName:
2853 Record.push_back(Name.getCXXOverloadedOperator());
2854 break;
2855
Sean Hunt3e518bd2009-11-29 07:34:05 +00002856 case DeclarationName::CXXLiteralOperatorName:
2857 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2858 break;
2859
Douglas Gregor2cf26342009-04-09 22:27:44 +00002860 case DeclarationName::CXXUsingDirective:
2861 // No extra data to emit
2862 break;
2863 }
2864}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002865
2866void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2867 RecordData &Record) {
2868 // Nested name specifiers usually aren't too long. I think that 8 would
2869 // typically accomodate the vast majority.
2870 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2871
2872 // Push each of the NNS's onto a stack for serialization in reverse order.
2873 while (NNS) {
2874 NestedNames.push_back(NNS);
2875 NNS = NNS->getPrefix();
2876 }
2877
2878 Record.push_back(NestedNames.size());
2879 while(!NestedNames.empty()) {
2880 NNS = NestedNames.pop_back_val();
2881 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2882 Record.push_back(Kind);
2883 switch (Kind) {
2884 case NestedNameSpecifier::Identifier:
2885 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2886 break;
2887
2888 case NestedNameSpecifier::Namespace:
2889 AddDeclRef(NNS->getAsNamespace(), Record);
2890 break;
2891
2892 case NestedNameSpecifier::TypeSpec:
2893 case NestedNameSpecifier::TypeSpecWithTemplate:
2894 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2895 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2896 break;
2897
2898 case NestedNameSpecifier::Global:
2899 // Don't need to write an associated value.
2900 break;
2901 }
2902 }
2903}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002904
2905void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2906 TemplateName::NameKind Kind = Name.getKind();
2907 Record.push_back(Kind);
2908 switch (Kind) {
2909 case TemplateName::Template:
2910 AddDeclRef(Name.getAsTemplateDecl(), Record);
2911 break;
2912
2913 case TemplateName::OverloadedTemplate: {
2914 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2915 Record.push_back(OvT->size());
2916 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2917 I != E; ++I)
2918 AddDeclRef(*I, Record);
2919 break;
2920 }
2921
2922 case TemplateName::QualifiedTemplate: {
2923 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2924 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2925 Record.push_back(QualT->hasTemplateKeyword());
2926 AddDeclRef(QualT->getTemplateDecl(), Record);
2927 break;
2928 }
2929
2930 case TemplateName::DependentTemplate: {
2931 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2932 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2933 Record.push_back(DepT->isIdentifier());
2934 if (DepT->isIdentifier())
2935 AddIdentifierRef(DepT->getIdentifier(), Record);
2936 else
2937 Record.push_back(DepT->getOperator());
2938 break;
2939 }
2940 }
2941}
2942
2943void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2944 RecordData &Record) {
2945 Record.push_back(Arg.getKind());
2946 switch (Arg.getKind()) {
2947 case TemplateArgument::Null:
2948 break;
2949 case TemplateArgument::Type:
2950 AddTypeRef(Arg.getAsType(), Record);
2951 break;
2952 case TemplateArgument::Declaration:
2953 AddDeclRef(Arg.getAsDecl(), Record);
2954 break;
2955 case TemplateArgument::Integral:
2956 AddAPSInt(*Arg.getAsIntegral(), Record);
2957 AddTypeRef(Arg.getIntegralType(), Record);
2958 break;
2959 case TemplateArgument::Template:
2960 AddTemplateName(Arg.getAsTemplate(), Record);
2961 break;
2962 case TemplateArgument::Expression:
2963 AddStmt(Arg.getAsExpr());
2964 break;
2965 case TemplateArgument::Pack:
2966 Record.push_back(Arg.pack_size());
2967 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2968 I != E; ++I)
2969 AddTemplateArgument(*I, Record);
2970 break;
2971 }
2972}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002973
2974void
2975PCHWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
2976 RecordData &Record) {
2977 assert(TemplateParams && "No TemplateParams!");
2978 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2979 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2980 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2981 Record.push_back(TemplateParams->size());
2982 for (TemplateParameterList::const_iterator
2983 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2984 P != PEnd; ++P)
2985 AddDeclRef(*P, Record);
2986}
2987
2988/// \brief Emit a template argument list.
2989void
2990PCHWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
2991 RecordData &Record) {
2992 assert(TemplateArgs && "No TemplateArgs!");
2993 Record.push_back(TemplateArgs->flat_size());
2994 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2995 AddTemplateArgument(TemplateArgs->get(i), Record);
2996}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00002997
2998
2999void
3000PCHWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
3001 Record.push_back(Set.size());
3002 for (UnresolvedSetImpl::const_iterator
3003 I = Set.begin(), E = Set.end(); I != E; ++I) {
3004 AddDeclRef(I.getDecl(), Record);
3005 Record.push_back(I.getAccess());
3006 }
3007}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003008
3009void PCHWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
3010 RecordData &Record) {
3011 Record.push_back(Base.isVirtual());
3012 Record.push_back(Base.isBaseOfClass());
3013 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky56062202010-07-26 16:56:01 +00003014 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003015 AddSourceRange(Base.getSourceRange(), Record);
3016}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003017
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003018void PCHWriter::SetReader(PCHReader *Reader) {
3019 assert(Reader && "Cannot remove chain");
3020 assert(FirstDeclID == NextDeclID &&
3021 FirstTypeID == NextTypeID &&
3022 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003023 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003024 "Setting chain after writing has started.");
3025 Chain = Reader;
3026}
3027
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003028void PCHWriter::IdentifierRead(pch::IdentID ID, IdentifierInfo *II) {
3029 IdentifierIDs[II] = ID;
3030}
3031
Sebastian Redl30c514c2010-07-14 23:45:08 +00003032void PCHWriter::TypeRead(pch::TypeID ID, QualType T) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003033 TypeIDs[T] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003034}
3035
3036void PCHWriter::DeclRead(pch::DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003037 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003038}
Sebastian Redl5d050072010-08-04 17:20:04 +00003039
3040void PCHWriter::SelectorRead(pch::SelectorID ID, Selector S) {
3041 SelectorIDs[S] = ID;
3042}