blob: df41ca1e59fda03430f1bcb1ae682e3ff70403a1 [file] [log] [blame]
Douglas Gregor2cf26342009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregore7785042009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump1eb44332009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000033#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000045
Douglas Gregor2cf26342009-04-09 22:27:44 +000046namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000047 class PCHTypeWriter {
Douglas Gregor2cf26342009-04-09 22:27:44 +000048 PCHWriter &Writer;
49 PCHWriter::RecordData &Record;
50
51 public:
52 /// \brief Type code that corresponds to the record generated.
53 pch::TypeCode Code;
54
Mike Stump1eb44332009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
58 void VisitArrayType(const ArrayType *T);
59 void VisitFunctionType(const FunctionType *T);
60 void VisitTagType(const TagType *T);
61
62#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63#define ABSTRACT_TYPE(Class, Base)
64#define DEPENDENT_TYPE(Class, Base)
65#include "clang/AST/TypeNodes.def"
66 };
67}
68
Douglas Gregor2cf26342009-04-09 22:27:44 +000069void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
Douglas Gregor2cf26342009-04-09 22:27:44 +000073void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
74 Writer.AddTypeRef(T->getElementType(), Record);
75 Code = pch::TYPE_COMPLEX;
76}
77
78void PCHTypeWriter::VisitPointerType(const PointerType *T) {
79 Writer.AddTypeRef(T->getPointeeType(), Record);
80 Code = pch::TYPE_POINTER;
81}
82
83void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000084 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000085 Code = pch::TYPE_BLOCK_POINTER;
86}
87
88void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_LVALUE_REFERENCE;
91}
92
93void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_RVALUE_REFERENCE;
96}
97
98void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000099 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101 Code = pch::TYPE_MEMBER_POINTER;
102}
103
104void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
105 Writer.AddTypeRef(T->getElementType(), Record);
106 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000107 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
110void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
111 VisitArrayType(T);
112 Writer.AddAPInt(T->getSize(), Record);
113 Code = pch::TYPE_CONSTANT_ARRAY;
114}
115
116void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
117 VisitArrayType(T);
118 Code = pch::TYPE_INCOMPLETE_ARRAY;
119}
120
121void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
122 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000123 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
124 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000125 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126 Code = pch::TYPE_VARIABLE_ARRAY;
127}
128
129void PCHTypeWriter::VisitVectorType(const VectorType *T) {
130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getNumElements());
John Thompson82287d12010-02-05 00:12:22 +0000132 Record.push_back(T->isAltiVec());
133 Record.push_back(T->isPixel());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 Code = pch::TYPE_VECTOR;
135}
136
137void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
138 VisitVectorType(T);
139 Code = pch::TYPE_EXT_VECTOR;
140}
141
142void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
143 Writer.AddTypeRef(T->getResultType(), Record);
Douglas Gregor91236662009-12-22 18:11:50 +0000144 Record.push_back(T->getNoReturnAttr());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000145 // FIXME: need to stabilize encoding of calling convention...
146 Record.push_back(T->getCallConv());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147}
148
149void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
150 VisitFunctionType(T);
151 Code = pch::TYPE_FUNCTION_NO_PROTO;
152}
153
154void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
155 VisitFunctionType(T);
156 Record.push_back(T->getNumArgs());
157 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
158 Writer.AddTypeRef(T->getArgType(I), Record);
159 Record.push_back(T->isVariadic());
160 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000161 Record.push_back(T->hasExceptionSpec());
162 Record.push_back(T->hasAnyExceptionSpec());
163 Record.push_back(T->getNumExceptions());
164 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
165 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 Code = pch::TYPE_FUNCTION_PROTO;
167}
168
John McCalled976492009-12-04 22:46:56 +0000169#if 0
170// For when we want it....
171void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
172 Writer.AddDeclRef(T->getDecl(), Record);
173 Code = pch::TYPE_UNRESOLVED_USING;
174}
175#endif
176
Douglas Gregor2cf26342009-04-09 22:27:44 +0000177void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
178 Writer.AddDeclRef(T->getDecl(), Record);
179 Code = pch::TYPE_TYPEDEF;
180}
181
182void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000183 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184 Code = pch::TYPE_TYPEOF_EXPR;
185}
186
187void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
188 Writer.AddTypeRef(T->getUnderlyingType(), Record);
189 Code = pch::TYPE_TYPEOF;
190}
191
Anders Carlsson395b4752009-06-24 19:06:50 +0000192void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
193 Writer.AddStmt(T->getUnderlyingExpr());
194 Code = pch::TYPE_DECLTYPE;
195}
196
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197void PCHTypeWriter::VisitTagType(const TagType *T) {
198 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000199 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200 "Cannot serialize in the middle of a type definition");
201}
202
203void PCHTypeWriter::VisitRecordType(const RecordType *T) {
204 VisitTagType(T);
205 Code = pch::TYPE_RECORD;
206}
207
208void PCHTypeWriter::VisitEnumType(const EnumType *T) {
209 VisitTagType(T);
210 Code = pch::TYPE_ENUM;
211}
212
John McCall7da24312009-09-05 00:15:47 +0000213void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
214 Writer.AddTypeRef(T->getUnderlyingType(), Record);
215 Record.push_back(T->getTagKind());
216 Code = pch::TYPE_ELABORATED;
217}
218
Mike Stump1eb44332009-09-09 15:08:12 +0000219void
John McCall49a832b2009-10-18 09:09:24 +0000220PCHTypeWriter::VisitSubstTemplateTypeParmType(
221 const SubstTemplateTypeParmType *T) {
222 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
223 Writer.AddTypeRef(T->getReplacementType(), Record);
224 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
225}
226
227void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228PCHTypeWriter::VisitTemplateSpecializationType(
229 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000230 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231 assert(false && "Cannot serialize template specialization types");
232}
233
234void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000235 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000236 assert(false && "Cannot serialize qualified name types");
237}
238
John McCall3cb0ebd2010-03-10 03:28:59 +0000239void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
240 Writer.AddDeclRef(T->getDecl(), Record);
241 Writer.AddTypeRef(T->getUnderlyingType(), Record);
242 Code = pch::TYPE_INJECTED_CLASS_NAME;
243}
244
Douglas Gregor2cf26342009-04-09 22:27:44 +0000245void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
246 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000247 Record.push_back(T->getNumProtocols());
Steve Naroff446ee4e2009-05-27 16:21:00 +0000248 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
249 E = T->qual_end(); I != E; ++I)
250 Writer.AddDeclRef(*I, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000251 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000252}
253
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000254void
255PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000256 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257 Record.push_back(T->getNumProtocols());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000258 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000259 E = T->qual_end(); I != E; ++I)
260 Writer.AddDeclRef(*I, Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000261 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000262}
263
John McCalla1ee0c52009-10-16 21:56:05 +0000264namespace {
265
266class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
267 PCHWriter &Writer;
268 PCHWriter::RecordData &Record;
269
270public:
271 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
272 : Writer(Writer), Record(Record) { }
273
John McCall51bd8032009-10-18 01:05:36 +0000274#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000275#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000276 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000277#include "clang/AST/TypeLocNodes.def"
278
John McCall51bd8032009-10-18 01:05:36 +0000279 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
280 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000281};
282
283}
284
John McCall51bd8032009-10-18 01:05:36 +0000285void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
286 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000287}
John McCall51bd8032009-10-18 01:05:36 +0000288void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000289 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
290 if (TL.needsExtraLocalData()) {
291 Record.push_back(TL.getWrittenTypeSpec());
292 Record.push_back(TL.getWrittenSignSpec());
293 Record.push_back(TL.getWrittenWidthSpec());
294 Record.push_back(TL.hasModeAttr());
295 }
John McCalla1ee0c52009-10-16 21:56:05 +0000296}
John McCall51bd8032009-10-18 01:05:36 +0000297void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
298 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000299}
John McCall51bd8032009-10-18 01:05:36 +0000300void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
301 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000302}
John McCall51bd8032009-10-18 01:05:36 +0000303void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
304 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000305}
John McCall51bd8032009-10-18 01:05:36 +0000306void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
307 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000308}
John McCall51bd8032009-10-18 01:05:36 +0000309void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
310 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000311}
John McCall51bd8032009-10-18 01:05:36 +0000312void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
313 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000314}
John McCall51bd8032009-10-18 01:05:36 +0000315void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
316 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
317 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
318 Record.push_back(TL.getSizeExpr() ? 1 : 0);
319 if (TL.getSizeExpr())
320 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000321}
John McCall51bd8032009-10-18 01:05:36 +0000322void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
323 VisitArrayTypeLoc(TL);
324}
325void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
326 VisitArrayTypeLoc(TL);
327}
328void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
329 VisitArrayTypeLoc(TL);
330}
331void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
332 DependentSizedArrayTypeLoc TL) {
333 VisitArrayTypeLoc(TL);
334}
335void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
336 DependentSizedExtVectorTypeLoc TL) {
337 Writer.AddSourceLocation(TL.getNameLoc(), Record);
338}
339void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
340 Writer.AddSourceLocation(TL.getNameLoc(), Record);
341}
342void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
343 Writer.AddSourceLocation(TL.getNameLoc(), Record);
344}
345void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
346 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
347 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
348 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
349 Writer.AddDeclRef(TL.getArg(i), Record);
350}
351void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
352 VisitFunctionTypeLoc(TL);
353}
354void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
355 VisitFunctionTypeLoc(TL);
356}
John McCalled976492009-12-04 22:46:56 +0000357void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
358 Writer.AddSourceLocation(TL.getNameLoc(), Record);
359}
John McCall51bd8032009-10-18 01:05:36 +0000360void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getNameLoc(), Record);
362}
363void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000364 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
365 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
366 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000367}
368void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000369 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
370 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
371 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
372 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000373}
374void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
375 Writer.AddSourceLocation(TL.getNameLoc(), Record);
376}
377void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
378 Writer.AddSourceLocation(TL.getNameLoc(), Record);
379}
380void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
381 Writer.AddSourceLocation(TL.getNameLoc(), Record);
382}
383void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
384 Writer.AddSourceLocation(TL.getNameLoc(), Record);
385}
386void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
387 Writer.AddSourceLocation(TL.getNameLoc(), Record);
388}
John McCall49a832b2009-10-18 09:09:24 +0000389void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
390 SubstTemplateTypeParmTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getNameLoc(), Record);
392}
John McCall51bd8032009-10-18 01:05:36 +0000393void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
394 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000395 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
396 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
397 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
398 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
399 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000400}
401void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
402 Writer.AddSourceLocation(TL.getNameLoc(), Record);
403}
John McCall3cb0ebd2010-03-10 03:28:59 +0000404void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
405 Writer.AddSourceLocation(TL.getNameLoc(), Record);
406}
John McCall51bd8032009-10-18 01:05:36 +0000407void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getNameLoc(), Record);
409}
410void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000412 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
413 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
414 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
415 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000416}
John McCall54e14c42009-10-22 22:37:11 +0000417void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getStarLoc(), Record);
419 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
420 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
421 Record.push_back(TL.hasBaseTypeAsWritten());
422 Record.push_back(TL.hasProtocolsAsWritten());
423 if (TL.hasProtocolsAsWritten())
424 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
425 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
426}
John McCalla1ee0c52009-10-16 21:56:05 +0000427
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000428//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000429// PCHWriter Implementation
430//===----------------------------------------------------------------------===//
431
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000432static void EmitBlockID(unsigned ID, const char *Name,
433 llvm::BitstreamWriter &Stream,
434 PCHWriter::RecordData &Record) {
435 Record.clear();
436 Record.push_back(ID);
437 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
438
439 // Emit the block name if present.
440 if (Name == 0 || Name[0] == 0) return;
441 Record.clear();
442 while (*Name)
443 Record.push_back(*Name++);
444 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
445}
446
447static void EmitRecordID(unsigned ID, const char *Name,
448 llvm::BitstreamWriter &Stream,
449 PCHWriter::RecordData &Record) {
450 Record.clear();
451 Record.push_back(ID);
452 while (*Name)
453 Record.push_back(*Name++);
454 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000455}
456
457static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
458 PCHWriter::RecordData &Record) {
459#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
460 RECORD(STMT_STOP);
461 RECORD(STMT_NULL_PTR);
462 RECORD(STMT_NULL);
463 RECORD(STMT_COMPOUND);
464 RECORD(STMT_CASE);
465 RECORD(STMT_DEFAULT);
466 RECORD(STMT_LABEL);
467 RECORD(STMT_IF);
468 RECORD(STMT_SWITCH);
469 RECORD(STMT_WHILE);
470 RECORD(STMT_DO);
471 RECORD(STMT_FOR);
472 RECORD(STMT_GOTO);
473 RECORD(STMT_INDIRECT_GOTO);
474 RECORD(STMT_CONTINUE);
475 RECORD(STMT_BREAK);
476 RECORD(STMT_RETURN);
477 RECORD(STMT_DECL);
478 RECORD(STMT_ASM);
479 RECORD(EXPR_PREDEFINED);
480 RECORD(EXPR_DECL_REF);
481 RECORD(EXPR_INTEGER_LITERAL);
482 RECORD(EXPR_FLOATING_LITERAL);
483 RECORD(EXPR_IMAGINARY_LITERAL);
484 RECORD(EXPR_STRING_LITERAL);
485 RECORD(EXPR_CHARACTER_LITERAL);
486 RECORD(EXPR_PAREN);
487 RECORD(EXPR_UNARY_OPERATOR);
488 RECORD(EXPR_SIZEOF_ALIGN_OF);
489 RECORD(EXPR_ARRAY_SUBSCRIPT);
490 RECORD(EXPR_CALL);
491 RECORD(EXPR_MEMBER);
492 RECORD(EXPR_BINARY_OPERATOR);
493 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
494 RECORD(EXPR_CONDITIONAL_OPERATOR);
495 RECORD(EXPR_IMPLICIT_CAST);
496 RECORD(EXPR_CSTYLE_CAST);
497 RECORD(EXPR_COMPOUND_LITERAL);
498 RECORD(EXPR_EXT_VECTOR_ELEMENT);
499 RECORD(EXPR_INIT_LIST);
500 RECORD(EXPR_DESIGNATED_INIT);
501 RECORD(EXPR_IMPLICIT_VALUE_INIT);
502 RECORD(EXPR_VA_ARG);
503 RECORD(EXPR_ADDR_LABEL);
504 RECORD(EXPR_STMT);
505 RECORD(EXPR_TYPES_COMPATIBLE);
506 RECORD(EXPR_CHOOSE);
507 RECORD(EXPR_GNU_NULL);
508 RECORD(EXPR_SHUFFLE_VECTOR);
509 RECORD(EXPR_BLOCK);
510 RECORD(EXPR_BLOCK_DECL_REF);
511 RECORD(EXPR_OBJC_STRING_LITERAL);
512 RECORD(EXPR_OBJC_ENCODE);
513 RECORD(EXPR_OBJC_SELECTOR_EXPR);
514 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
515 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
516 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
517 RECORD(EXPR_OBJC_KVC_REF_EXPR);
518 RECORD(EXPR_OBJC_MESSAGE_EXPR);
519 RECORD(EXPR_OBJC_SUPER_EXPR);
520 RECORD(STMT_OBJC_FOR_COLLECTION);
521 RECORD(STMT_OBJC_CATCH);
522 RECORD(STMT_OBJC_FINALLY);
523 RECORD(STMT_OBJC_AT_TRY);
524 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
525 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000526 RECORD(EXPR_CXX_OPERATOR_CALL);
527 RECORD(EXPR_CXX_CONSTRUCT);
528 RECORD(EXPR_CXX_STATIC_CAST);
529 RECORD(EXPR_CXX_DYNAMIC_CAST);
530 RECORD(EXPR_CXX_REINTERPRET_CAST);
531 RECORD(EXPR_CXX_CONST_CAST);
532 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
533 RECORD(EXPR_CXX_BOOL_LITERAL);
534 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000535#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000536}
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000538void PCHWriter::WriteBlockInfoBlock() {
539 RecordData Record;
540 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Chris Lattner2f4efd12009-04-27 00:40:25 +0000542#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000543#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000545 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000546 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000547 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000548 RECORD(TYPE_OFFSET);
549 RECORD(DECL_OFFSET);
550 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000551 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000552 RECORD(IDENTIFIER_OFFSET);
553 RECORD(IDENTIFIER_TABLE);
554 RECORD(EXTERNAL_DEFINITIONS);
555 RECORD(SPECIAL_TYPES);
556 RECORD(STATISTICS);
557 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000558 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000559 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
560 RECORD(SELECTOR_OFFSETS);
561 RECORD(METHOD_POOL);
562 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000563 RECORD(SOURCE_LOCATION_OFFSETS);
564 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000565 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000566 RECORD(EXT_VECTOR_DECLS);
Douglas Gregor2e222532009-07-02 17:08:52 +0000567 RECORD(COMMENT_RANGES);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000568 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000569 RECORD(UNUSED_STATIC_FUNCS);
570 RECORD(MACRO_DEFINITION_OFFSETS);
571
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000572 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000573 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000574 RECORD(SM_SLOC_FILE_ENTRY);
575 RECORD(SM_SLOC_BUFFER_ENTRY);
576 RECORD(SM_SLOC_BUFFER_BLOB);
577 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
578 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000580 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000581 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000582 RECORD(PP_MACRO_OBJECT_LIKE);
583 RECORD(PP_MACRO_FUNCTION_LIKE);
584 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000585 RECORD(PP_MACRO_INSTANTIATION);
586 RECORD(PP_MACRO_DEFINITION);
587
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000588 // Decls and Types block.
589 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000590 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000591 RECORD(TYPE_COMPLEX);
592 RECORD(TYPE_POINTER);
593 RECORD(TYPE_BLOCK_POINTER);
594 RECORD(TYPE_LVALUE_REFERENCE);
595 RECORD(TYPE_RVALUE_REFERENCE);
596 RECORD(TYPE_MEMBER_POINTER);
597 RECORD(TYPE_CONSTANT_ARRAY);
598 RECORD(TYPE_INCOMPLETE_ARRAY);
599 RECORD(TYPE_VARIABLE_ARRAY);
600 RECORD(TYPE_VECTOR);
601 RECORD(TYPE_EXT_VECTOR);
602 RECORD(TYPE_FUNCTION_PROTO);
603 RECORD(TYPE_FUNCTION_NO_PROTO);
604 RECORD(TYPE_TYPEDEF);
605 RECORD(TYPE_TYPEOF_EXPR);
606 RECORD(TYPE_TYPEOF);
607 RECORD(TYPE_RECORD);
608 RECORD(TYPE_ENUM);
609 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000610 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000611 RECORD(DECL_ATTR);
612 RECORD(DECL_TRANSLATION_UNIT);
613 RECORD(DECL_TYPEDEF);
614 RECORD(DECL_ENUM);
615 RECORD(DECL_RECORD);
616 RECORD(DECL_ENUM_CONSTANT);
617 RECORD(DECL_FUNCTION);
618 RECORD(DECL_OBJC_METHOD);
619 RECORD(DECL_OBJC_INTERFACE);
620 RECORD(DECL_OBJC_PROTOCOL);
621 RECORD(DECL_OBJC_IVAR);
622 RECORD(DECL_OBJC_AT_DEFS_FIELD);
623 RECORD(DECL_OBJC_CLASS);
624 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
625 RECORD(DECL_OBJC_CATEGORY);
626 RECORD(DECL_OBJC_CATEGORY_IMPL);
627 RECORD(DECL_OBJC_IMPLEMENTATION);
628 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
629 RECORD(DECL_OBJC_PROPERTY);
630 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000631 RECORD(DECL_FIELD);
632 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000633 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000634 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000635 RECORD(DECL_FILE_SCOPE_ASM);
636 RECORD(DECL_BLOCK);
637 RECORD(DECL_CONTEXT_LEXICAL);
638 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000639 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000640 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000641#undef RECORD
642#undef BLOCK
643 Stream.ExitBlock();
644}
645
Douglas Gregore650c8c2009-07-07 00:12:59 +0000646/// \brief Adjusts the given filename to only write out the portion of the
647/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000648///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000649/// \param Filename the file name to adjust.
650///
651/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
652/// the returned filename will be adjusted by this system root.
653///
654/// \returns either the original filename (if it needs no adjustment) or the
655/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000656static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000657adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
658 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Douglas Gregore650c8c2009-07-07 00:12:59 +0000660 if (!isysroot)
661 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Douglas Gregore650c8c2009-07-07 00:12:59 +0000663 // Verify that the filename and the system root have the same prefix.
664 unsigned Pos = 0;
665 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
666 if (Filename[Pos] != isysroot[Pos])
667 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregore650c8c2009-07-07 00:12:59 +0000669 // We hit the end of the filename before we hit the end of the system root.
670 if (!Filename[Pos])
671 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Douglas Gregore650c8c2009-07-07 00:12:59 +0000673 // If the file name has a '/' at the current position, skip over the '/'.
674 // We distinguish sysroot-based includes from absolute includes by the
675 // absence of '/' at the beginning of sysroot-based includes.
676 if (Filename[Pos] == '/')
677 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Douglas Gregore650c8c2009-07-07 00:12:59 +0000679 return Filename + Pos;
680}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000681
Douglas Gregorab41e632009-04-27 22:23:34 +0000682/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000683void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000684 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000685
Douglas Gregore650c8c2009-07-07 00:12:59 +0000686 // Metadata
687 const TargetInfo &Target = Context.Target;
688 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
689 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
690 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
691 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
692 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
693 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
694 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
695 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
696 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Douglas Gregore650c8c2009-07-07 00:12:59 +0000698 RecordData Record;
699 Record.push_back(pch::METADATA);
700 Record.push_back(pch::VERSION_MAJOR);
701 Record.push_back(pch::VERSION_MINOR);
702 Record.push_back(CLANG_VERSION_MAJOR);
703 Record.push_back(CLANG_VERSION_MINOR);
704 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000705 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000706 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Douglas Gregorb64c1932009-05-12 01:31:05 +0000708 // Original file name
709 SourceManager &SM = Context.getSourceManager();
710 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
711 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
712 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
713 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
714 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
715
716 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000718 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000719
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000720 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000721 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000722 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000723 RecordData Record;
724 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000725 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000726 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000727
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000728 // Repository branch/version information.
729 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
730 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
731 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
732 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000733 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000734 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000735 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
736 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000737}
738
739/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000740void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
741 RecordData Record;
742 Record.push_back(LangOpts.Trigraphs);
743 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
744 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
745 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
746 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
747 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
748 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
749 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
750 Record.push_back(LangOpts.C99); // C99 Support
751 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
752 Record.push_back(LangOpts.CPlusPlus); // C++ Support
753 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000754 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000756 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
757 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000758 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000759 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000760 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000761 // modern abi enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000763 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000764 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
765 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000766 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000767 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000768 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000769
770 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
771 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
772 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
773
Chris Lattnerea5ce472009-04-27 07:35:58 +0000774 // Whether static initializers are protected by locks.
775 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000776 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000777 Record.push_back(LangOpts.Blocks); // block extension to C
778 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
779 // they are unused.
780 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
781 // (modulo the platform support).
782
783 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
784 // signed integer arithmetic overflows.
785
786 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
787 // may be ripped out at any time.
788
789 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000790 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000791 // defined.
792 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
793 // opposed to __DYNAMIC__).
794 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
795
796 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
797 // used (instead of C99 semantics).
798 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000799 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
800 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000801 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
802 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000803 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000804 Record.push_back(LangOpts.getGCMode());
805 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000806 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000807 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000808 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000809 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000810 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000811 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000812}
813
Douglas Gregor14f79002009-04-10 03:52:48 +0000814//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000815// stat cache Serialization
816//===----------------------------------------------------------------------===//
817
818namespace {
819// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000820class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000821public:
822 typedef const char * key_type;
823 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000825 typedef std::pair<int, struct stat> data_type;
826 typedef const data_type& data_type_ref;
827
828 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000829 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
832 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000833 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
834 data_type_ref Data) {
835 unsigned StrLen = strlen(path);
836 clang::io::Emit16(Out, StrLen);
837 unsigned DataLen = 1; // result value
838 if (Data.first == 0)
839 DataLen += 4 + 4 + 2 + 8 + 8;
840 clang::io::Emit8(Out, DataLen);
841 return std::make_pair(StrLen + 1, DataLen);
842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000844 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
845 Out.write(path, KeyLen);
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000848 void EmitData(llvm::raw_ostream& Out, key_type_ref,
849 data_type_ref Data, unsigned DataLen) {
850 using namespace clang::io;
851 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000853 // Result of stat()
854 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856 if (Data.first == 0) {
857 Emit32(Out, (uint32_t) Data.second.st_ino);
858 Emit32(Out, (uint32_t) Data.second.st_dev);
859 Emit16(Out, (uint16_t) Data.second.st_mode);
860 Emit64(Out, (uint64_t) Data.second.st_mtime);
861 Emit64(Out, (uint64_t) Data.second.st_size);
862 }
863
864 assert(Out.tell() - Start == DataLen && "Wrong data length");
865 }
866};
867} // end anonymous namespace
868
869/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000870void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
871 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000872 // Build the on-disk hash table containing information about every
873 // stat() call.
874 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
875 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000876 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000877 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000878 Stat != StatEnd; ++Stat, ++NumStatEntries) {
879 const char *Filename = Stat->first();
880 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
881 Generator.insert(Filename, Stat->second);
882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000884 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000885 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000886 uint32_t BucketOffset;
887 {
888 llvm::raw_svector_ostream Out(StatCacheData);
889 // Make sure that no bucket is at offset 0
890 clang::io::Emit32(Out, 0);
891 BucketOffset = Generator.Emit(Out);
892 }
893
894 // Create a blob abbreviation
895 using namespace llvm;
896 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
897 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
901 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
902
903 // Write the stat cache
904 RecordData Record;
905 Record.push_back(pch::STAT_CACHE);
906 Record.push_back(BucketOffset);
907 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000908 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000909}
910
911//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000912// Source Manager Serialization
913//===----------------------------------------------------------------------===//
914
915/// \brief Create an abbreviation for the SLocEntry that refers to a
916/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000917static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000918 using namespace llvm;
919 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
920 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor12fab312010-03-16 16:35:32 +0000925 // HeaderFileInfo fields.
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000931 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000932}
933
934/// \brief Create an abbreviation for the SLocEntry that refers to a
935/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000936static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000937 using namespace llvm;
938 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
939 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000945 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000946}
947
948/// \brief Create an abbreviation for the SLocEntry that refers to a
949/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000950static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000951 using namespace llvm;
952 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
953 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000955 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000956}
957
958/// \brief Create an abbreviation for the SLocEntry that refers to an
959/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000960static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000961 using namespace llvm;
962 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
963 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000969 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000970}
971
972/// \brief Writes the block containing the serialized form of the
973/// source manager.
974///
975/// TODO: We should probably use an on-disk hash table (stored in a
976/// blob), indexed based on the file name, so that we only create
977/// entries for files that we actually need. In the common case (no
978/// errors), we probably won't have to create file entries for any of
979/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000980void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 const Preprocessor &PP,
982 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000983 RecordData Record;
984
Chris Lattnerf04ad692009-04-10 17:16:57 +0000985 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000986 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000987
988 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000989 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
990 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
991 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
992 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000993
Douglas Gregorbd945002009-04-13 16:31:14 +0000994 // Write the line table.
995 if (SourceMgr.hasLineTable()) {
996 LineTableInfo &LineTable = SourceMgr.getLineTable();
997
998 // Emit the file names
999 Record.push_back(LineTable.getNumFilenames());
1000 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1001 // Emit the file name
1002 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001003 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001004 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1005 Record.push_back(FilenameLen);
1006 if (FilenameLen)
1007 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Douglas Gregorbd945002009-04-13 16:31:14 +00001010 // Emit the line entries
1011 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1012 L != LEnd; ++L) {
1013 // Emit the file ID
1014 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Douglas Gregorbd945002009-04-13 16:31:14 +00001016 // Emit the line entries
1017 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001018 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001019 LEEnd = L->second.end();
1020 LE != LEEnd; ++LE) {
1021 Record.push_back(LE->FileOffset);
1022 Record.push_back(LE->LineNo);
1023 Record.push_back(LE->FilenameID);
1024 Record.push_back((unsigned)LE->FileKind);
1025 Record.push_back(LE->IncludeOffset);
1026 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001027 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001028 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001029 }
1030
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001031 // Write out the source location entry table. We skip the first
1032 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001033 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001034 RecordData PreloadSLocs;
1035 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001036 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1037 // Get this source location entry.
1038 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001039
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001040 // Record the offset of this source-location entry.
1041 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1042
1043 // Figure out which record code to use.
1044 unsigned Code;
1045 if (SLoc->isFile()) {
1046 if (SLoc->getFile().getContentCache()->Entry)
1047 Code = pch::SM_SLOC_FILE_ENTRY;
1048 else
1049 Code = pch::SM_SLOC_BUFFER_ENTRY;
1050 } else
1051 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1052 Record.clear();
1053 Record.push_back(Code);
1054
1055 Record.push_back(SLoc->getOffset());
1056 if (SLoc->isFile()) {
1057 const SrcMgr::FileInfo &File = SLoc->getFile();
1058 Record.push_back(File.getIncludeLoc().getRawEncoding());
1059 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1060 Record.push_back(File.hasLineDirectives());
1061
1062 const SrcMgr::ContentCache *Content = File.getContentCache();
1063 if (Content->Entry) {
1064 // The source location entry is a file. The blob associated
1065 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Douglas Gregor12fab312010-03-16 16:35:32 +00001067 // Emit header-search information associated with this file.
1068 HeaderFileInfo HFI;
1069 HeaderSearch &HS = PP.getHeaderSearchInfo();
1070 if (Content->Entry->getUID() < HS.header_file_size())
1071 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1072 Record.push_back(HFI.isImport);
1073 Record.push_back(HFI.DirInfo);
1074 Record.push_back(HFI.NumIncludes);
1075 AddIdentifierRef(HFI.ControllingMacro, Record);
1076
Douglas Gregore650c8c2009-07-07 00:12:59 +00001077 // Turn the file name into an absolute path, if it isn't already.
1078 const char *Filename = Content->Entry->getName();
1079 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001080 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001081 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Douglas Gregore650c8c2009-07-07 00:12:59 +00001083 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001084 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001085
1086 // FIXME: For now, preload all file source locations, so that
1087 // we get the appropriate File entries in the reader. This is
1088 // a temporary measure.
1089 PreloadSLocs.push_back(SLocEntryOffsets.size());
1090 } else {
1091 // The source location entry is a buffer. The blob associated
1092 // with this entry contains the contents of the buffer.
1093
1094 // We add one to the size so that we capture the trailing NULL
1095 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1096 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001097 const llvm::MemoryBuffer *Buffer
1098 = Content->getBuffer(PP.getDiagnostics());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001099 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001100 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1101 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001102 Record.clear();
1103 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1104 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001105 llvm::StringRef(Buffer->getBufferStart(),
1106 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001107
1108 if (strcmp(Name, "<built-in>") == 0)
1109 PreloadSLocs.push_back(SLocEntryOffsets.size());
1110 }
1111 } else {
1112 // The source location entry is an instantiation.
1113 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1114 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1115 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1116 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1117
1118 // Compute the token length for this macro expansion.
1119 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001120 if (I + 1 != N)
1121 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001122 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1123 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1124 }
1125 }
1126
Douglas Gregorc9490c02009-04-16 22:23:12 +00001127 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128
1129 if (SLocEntryOffsets.empty())
1130 return;
1131
1132 // Write the source-location offsets table into the PCH block. This
1133 // table is used for lazily loading source-location information.
1134 using namespace llvm;
1135 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1136 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1139 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1140 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001142 Record.clear();
1143 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1144 Record.push_back(SLocEntryOffsets.size());
1145 Record.push_back(SourceMgr.getNextOffset());
1146 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001147 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001148 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001149
1150 // Write the source location entry preloads array, telling the PCH
1151 // reader which source locations entries it should load eagerly.
1152 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001153}
1154
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001155//===----------------------------------------------------------------------===//
1156// Preprocessor Serialization
1157//===----------------------------------------------------------------------===//
1158
Chris Lattner0b1fb982009-04-10 17:15:23 +00001159/// \brief Writes the block containing the serialized form of the
1160/// preprocessor.
1161///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001162void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001163 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001164
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001165 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1166 if (PP.getCounterValue() != 0) {
1167 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001168 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001169 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001170 }
1171
1172 // Enter the preprocessor block.
1173 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001175 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1176 // FIXME: use diagnostics subsystem for localization etc.
1177 if (PP.SawDateOrTime())
1178 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001180 // Loop over all the macro definitions that are live at the end of the file,
1181 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001182 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001183 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1184 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001185 // FIXME: This emits macros in hash table order, we should do it in a stable
1186 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001187 MacroInfo *MI = I->second;
1188
1189 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1190 // been redefined by the header (in which case they are not isBuiltinMacro).
1191 if (MI->isBuiltinMacro())
1192 continue;
1193
Chris Lattner7356a312009-04-11 21:15:38 +00001194 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001195 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001196 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1197 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001199 unsigned Code;
1200 if (MI->isObjectLike()) {
1201 Code = pch::PP_MACRO_OBJECT_LIKE;
1202 } else {
1203 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001205 Record.push_back(MI->isC99Varargs());
1206 Record.push_back(MI->isGNUVarargs());
1207 Record.push_back(MI->getNumArgs());
1208 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1209 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001210 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001211 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001212
1213 // If we have a detailed preprocessing record, record the macro definition
1214 // ID that corresponds to this macro.
1215 if (PPRec)
1216 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1217
Douglas Gregorc9490c02009-04-16 22:23:12 +00001218 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001219 Record.clear();
1220
Chris Lattnerdf961c22009-04-10 18:08:30 +00001221 // Emit the tokens array.
1222 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1223 // Note that we know that the preprocessor does not have any annotation
1224 // tokens in it because they are created by the parser, and thus can't be
1225 // in a macro definition.
1226 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Chris Lattnerdf961c22009-04-10 18:08:30 +00001228 Record.push_back(Tok.getLocation().getRawEncoding());
1229 Record.push_back(Tok.getLength());
1230
Chris Lattnerdf961c22009-04-10 18:08:30 +00001231 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1232 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001233 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Chris Lattnerdf961c22009-04-10 18:08:30 +00001235 // FIXME: Should translate token kind to a stable encoding.
1236 Record.push_back(Tok.getKind());
1237 // FIXME: Should translate token flags to a stable encoding.
1238 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregorc9490c02009-04-16 22:23:12 +00001240 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001241 Record.clear();
1242 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001243 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001244 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001245
1246 // If the preprocessor has a preprocessing record, emit it.
1247 unsigned NumPreprocessingRecords = 0;
1248 if (PPRec) {
1249 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1250 E != EEnd; ++E) {
1251 Record.clear();
1252
1253 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1254 Record.push_back(NumPreprocessingRecords++);
1255 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1256 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1257 AddIdentifierRef(MI->getName(), Record);
1258 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1259 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1260 continue;
1261 }
1262
1263 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1264 // Record this macro definition's location.
1265 pch::IdentID ID = getMacroDefinitionID(MD);
1266 if (ID != MacroDefinitionOffsets.size()) {
1267 if (ID > MacroDefinitionOffsets.size())
1268 MacroDefinitionOffsets.resize(ID + 1);
1269
1270 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1271 } else
1272 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1273
1274 Record.push_back(NumPreprocessingRecords++);
1275 Record.push_back(ID);
1276 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1277 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1278 AddIdentifierRef(MD->getName(), Record);
1279 AddSourceLocation(MD->getLocation(), Record);
1280 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1281 continue;
1282 }
1283 }
1284 }
1285
Douglas Gregorc9490c02009-04-16 22:23:12 +00001286 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001287
1288 // Write the offsets table for the preprocessing record.
1289 if (NumPreprocessingRecords > 0) {
1290 // Write the offsets table for identifier IDs.
1291 using namespace llvm;
1292 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1293 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1295 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1297 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1298
1299 Record.clear();
1300 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1301 Record.push_back(NumPreprocessingRecords);
1302 Record.push_back(MacroDefinitionOffsets.size());
1303 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1304 (const char *)&MacroDefinitionOffsets.front(),
1305 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1306 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001307}
1308
Douglas Gregor2e222532009-07-02 17:08:52 +00001309void PCHWriter::WriteComments(ASTContext &Context) {
1310 using namespace llvm;
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Douglas Gregor2e222532009-07-02 17:08:52 +00001312 if (Context.Comments.empty())
1313 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Douglas Gregor2e222532009-07-02 17:08:52 +00001315 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1316 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1317 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1318 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Douglas Gregor2e222532009-07-02 17:08:52 +00001320 RecordData Record;
1321 Record.push_back(pch::COMMENT_RANGES);
Mike Stump1eb44332009-09-09 15:08:12 +00001322 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregor2e222532009-07-02 17:08:52 +00001323 (const char*)&Context.Comments[0],
1324 Context.Comments.size() * sizeof(SourceRange));
1325}
1326
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001327//===----------------------------------------------------------------------===//
1328// Type Serialization
1329//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001330
Douglas Gregor2cf26342009-04-09 22:27:44 +00001331/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001332void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001333 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001334 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001335 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Douglas Gregor2cf26342009-04-09 22:27:44 +00001337 // Record the offset for this type.
1338 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001339 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001340 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1341 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001342 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001343 }
1344
1345 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001346
Douglas Gregor2cf26342009-04-09 22:27:44 +00001347 // Emit the type's representation.
1348 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001349
Douglas Gregora4923eb2009-11-16 21:35:15 +00001350 if (T.hasLocalNonFastQualifiers()) {
1351 Qualifiers Qs = T.getLocalQualifiers();
1352 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001353 Record.push_back(Qs.getAsOpaqueValue());
1354 W.Code = pch::TYPE_EXT_QUAL;
1355 } else {
1356 switch (T->getTypeClass()) {
1357 // For all of the concrete, non-dependent types, call the
1358 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001359#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001360 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001361#define ABSTRACT_TYPE(Class, Base)
1362#define DEPENDENT_TYPE(Class, Base)
1363#include "clang/AST/TypeNodes.def"
1364
John McCall0953e762009-09-24 19:53:00 +00001365 // For all of the dependent type nodes (which only occur in C++
1366 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001367#define TYPE(Class, Base)
1368#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1369#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001370 assert(false && "Cannot serialize dependent type nodes");
1371 break;
1372 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001373 }
1374
1375 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001376 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001377
1378 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001379 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001380}
1381
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001382//===----------------------------------------------------------------------===//
1383// Declaration Serialization
1384//===----------------------------------------------------------------------===//
1385
Douglas Gregor2cf26342009-04-09 22:27:44 +00001386/// \brief Write the block containing all of the declaration IDs
1387/// lexically declared within the given DeclContext.
1388///
1389/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1390/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001391uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001392 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001393 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001394 return 0;
1395
Douglas Gregorc9490c02009-04-16 22:23:12 +00001396 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001397 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001398 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1399 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001400 AddDeclRef(*D, Record);
1401
Douglas Gregor25123082009-04-22 22:34:57 +00001402 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001403 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001404 return Offset;
1405}
1406
1407/// \brief Write the block containing all of the declaration IDs
1408/// visible from the given DeclContext.
1409///
1410/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1411/// bistream, or 0 if no block was written.
1412uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1413 DeclContext *DC) {
1414 if (DC->getPrimaryContext() != DC)
1415 return 0;
1416
Douglas Gregoraff22df2009-04-21 22:32:33 +00001417 // Since there is no name lookup into functions or methods, and we
1418 // perform name lookup for the translation unit via the
1419 // IdentifierInfo chains, don't bother to build a
1420 // visible-declarations table for these entities.
1421 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001422 return 0;
1423
Douglas Gregor2cf26342009-04-09 22:27:44 +00001424 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001425 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001426
1427 // Serialize the contents of the mapping used for lookup. Note that,
1428 // although we have two very different code paths, the serialized
1429 // representation is the same for both cases: a declaration name,
1430 // followed by a size, followed by references to the visible
1431 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001432 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001433 RecordData Record;
1434 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001435 if (!Map)
1436 return 0;
1437
Douglas Gregor2cf26342009-04-09 22:27:44 +00001438 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1439 D != DEnd; ++D) {
1440 AddDeclarationName(D->first, Record);
1441 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1442 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001443 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001444 AddDeclRef(*Result.first, Record);
1445 }
1446
1447 if (Record.size() == 0)
1448 return 0;
1449
Douglas Gregorc9490c02009-04-16 22:23:12 +00001450 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001451 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001452 return Offset;
1453}
1454
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001455//===----------------------------------------------------------------------===//
1456// Global Method Pool and Selector Serialization
1457//===----------------------------------------------------------------------===//
1458
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001459namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001460// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001461class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001462 PCHWriter &Writer;
1463
1464public:
1465 typedef Selector key_type;
1466 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001468 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1469 typedef const data_type& data_type_ref;
1470
1471 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001473 static unsigned ComputeHash(Selector Sel) {
1474 unsigned N = Sel.getNumArgs();
1475 if (N == 0)
1476 ++N;
1477 unsigned R = 5381;
1478 for (unsigned I = 0; I != N; ++I)
1479 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001480 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001481 return R;
1482 }
Mike Stump1eb44332009-09-09 15:08:12 +00001483
1484 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001485 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1486 data_type_ref Methods) {
1487 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1488 clang::io::Emit16(Out, KeyLen);
1489 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001490 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001491 Method = Method->Next)
1492 if (Method->Method)
1493 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001494 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001495 Method = Method->Next)
1496 if (Method->Method)
1497 DataLen += 4;
1498 clang::io::Emit16(Out, DataLen);
1499 return std::make_pair(KeyLen, DataLen);
1500 }
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Douglas Gregor83941df2009-04-25 17:48:32 +00001502 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001503 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001504 assert((Start >> 32) == 0 && "Selector key offset too large");
1505 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001506 unsigned N = Sel.getNumArgs();
1507 clang::io::Emit16(Out, N);
1508 if (N == 0)
1509 N = 1;
1510 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001511 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001512 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1513 }
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001515 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001516 data_type_ref Methods, unsigned DataLen) {
1517 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001518 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001519 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001520 Method = Method->Next)
1521 if (Method->Method)
1522 ++NumInstanceMethods;
1523
1524 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001525 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001526 Method = Method->Next)
1527 if (Method->Method)
1528 ++NumFactoryMethods;
1529
1530 clang::io::Emit16(Out, NumInstanceMethods);
1531 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001532 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001533 Method = Method->Next)
1534 if (Method->Method)
1535 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001536 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001537 Method = Method->Next)
1538 if (Method->Method)
1539 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001540
1541 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001542 }
1543};
1544} // end anonymous namespace
1545
1546/// \brief Write the method pool into the PCH file.
1547///
1548/// The method pool contains both instance and factory methods, stored
1549/// in an on-disk hash table indexed by the selector.
1550void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1551 using namespace llvm;
1552
1553 // Create and write out the blob that contains the instance and
1554 // factor method pools.
1555 bool Empty = true;
1556 {
1557 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001559 // Create the on-disk hash table representation. Start by
1560 // iterating through the instance method pool.
1561 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001562 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001563 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001564 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001565 InstanceEnd = SemaRef.InstanceMethodPool.end();
1566 Instance != InstanceEnd; ++Instance) {
1567 // Check whether there is a factory method with the same
1568 // selector.
1569 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1570 = SemaRef.FactoryMethodPool.find(Instance->first);
1571
1572 if (Factory == SemaRef.FactoryMethodPool.end())
1573 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001574 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001575 ObjCMethodList()));
1576 else
1577 Generator.insert(Instance->first,
1578 std::make_pair(Instance->second, Factory->second));
1579
Douglas Gregor83941df2009-04-25 17:48:32 +00001580 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001581 Empty = false;
1582 }
1583
1584 // Now iterate through the factory method pool, to pick up any
1585 // selectors that weren't already in the instance method pool.
1586 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001587 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001588 FactoryEnd = SemaRef.FactoryMethodPool.end();
1589 Factory != FactoryEnd; ++Factory) {
1590 // Check whether there is an instance method with the same
1591 // selector. If so, there is no work to do here.
1592 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1593 = SemaRef.InstanceMethodPool.find(Factory->first);
1594
Douglas Gregor83941df2009-04-25 17:48:32 +00001595 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001596 Generator.insert(Factory->first,
1597 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001598 ++NumSelectorsInMethodPool;
1599 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600
1601 Empty = false;
1602 }
1603
Douglas Gregor83941df2009-04-25 17:48:32 +00001604 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001605 return;
1606
1607 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001608 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001609 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001610 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001611 {
1612 PCHMethodPoolTrait Trait(*this);
1613 llvm::raw_svector_ostream Out(MethodPool);
1614 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001615 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001616 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001617
1618 // For every selector that we have seen but which was not
1619 // written into the hash table, write the selector itself and
1620 // record it's offset.
1621 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1622 if (SelectorOffsets[I] == 0)
1623 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001624 }
1625
1626 // Create a blob abbreviation
1627 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1628 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1632 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1633
Douglas Gregor83941df2009-04-25 17:48:32 +00001634 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001635 RecordData Record;
1636 Record.push_back(pch::METHOD_POOL);
1637 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001638 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001639 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001640
1641 // Create a blob abbreviation for the selector table offsets.
1642 Abbrev = new BitCodeAbbrev();
1643 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1644 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1646 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1647
1648 // Write the selector offsets table.
1649 Record.clear();
1650 Record.push_back(pch::SELECTOR_OFFSETS);
1651 Record.push_back(SelectorOffsets.size());
1652 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1653 (const char *)&SelectorOffsets.front(),
1654 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001655 }
1656}
1657
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001658//===----------------------------------------------------------------------===//
1659// Identifier Table Serialization
1660//===----------------------------------------------------------------------===//
1661
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001662namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001663class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001664 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001665 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001666
Douglas Gregora92193e2009-04-28 21:18:29 +00001667 /// \brief Determines whether this is an "interesting" identifier
1668 /// that needs a full IdentifierInfo structure written into the hash
1669 /// table.
1670 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1671 return II->isPoisoned() ||
1672 II->isExtensionToken() ||
1673 II->hasMacroDefinition() ||
1674 II->getObjCOrBuiltinID() ||
1675 II->getFETokenInfo<void>();
1676 }
1677
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001678public:
1679 typedef const IdentifierInfo* key_type;
1680 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001682 typedef pch::IdentID data_type;
1683 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001684
1685 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001686 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001687
1688 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001689 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001690 }
Mike Stump1eb44332009-09-09 15:08:12 +00001691
1692 std::pair<unsigned,unsigned>
1693 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001694 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001695 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001696 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1697 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001698 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001699 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001700 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001701 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001702 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1703 DEnd = IdentifierResolver::end();
1704 D != DEnd; ++D)
1705 DataLen += sizeof(pch::DeclID);
1706 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001707 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001708 // We emit the key length after the data length so that every
1709 // string is preceded by a 16-bit length. This matches the PTH
1710 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001711 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001712 return std::make_pair(KeyLen, DataLen);
1713 }
Mike Stump1eb44332009-09-09 15:08:12 +00001714
1715 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001716 unsigned KeyLen) {
1717 // Record the location of the key data. This is used when generating
1718 // the mapping from persistent IDs to strings.
1719 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001720 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001721 }
Mike Stump1eb44332009-09-09 15:08:12 +00001722
1723 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001724 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001725 if (!isInterestingIdentifier(II)) {
1726 clang::io::Emit32(Out, ID << 1);
1727 return;
1728 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001729
Douglas Gregora92193e2009-04-28 21:18:29 +00001730 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001731 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001732 bool hasMacroDefinition =
1733 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001734 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001735 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001736 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1737 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1738 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1739 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001740 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001741
Douglas Gregor37e26842009-04-21 23:56:24 +00001742 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001743 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001744
Douglas Gregor668c1a42009-04-21 22:25:48 +00001745 // Emit the declaration IDs in reverse order, because the
1746 // IdentifierResolver provides the declarations as they would be
1747 // visible (e.g., the function "stat" would come before the struct
1748 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1749 // adds declarations to the end of the list (so we need to see the
1750 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001751 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001752 IdentifierResolver::end());
1753 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1754 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001755 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001756 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001757 }
1758};
1759} // end anonymous namespace
1760
Douglas Gregorafaf3082009-04-11 00:14:32 +00001761/// \brief Write the identifier table into the PCH file.
1762///
1763/// The identifier table consists of a blob containing string data
1764/// (the actual identifiers themselves) and a separate "offsets" index
1765/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001766void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001767 using namespace llvm;
1768
1769 // Create and write out the blob that contains the identifier
1770 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001771 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001772 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Douglas Gregor92b059e2009-04-28 20:33:11 +00001774 // Look for any identifiers that were named while processing the
1775 // headers, but are otherwise not needed. We add these to the hash
1776 // table to enable checking of the predefines buffer in the case
1777 // where the user adds new macro definitions when building the PCH
1778 // file.
1779 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1780 IDEnd = PP.getIdentifierTable().end();
1781 ID != IDEnd; ++ID)
1782 getIdentifierRef(ID->second);
1783
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001784 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001785 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001786 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1787 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1788 ID != IDEnd; ++ID) {
1789 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001790 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001791 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001792
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001793 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001794 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001795 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001796 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001797 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001798 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001799 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001800 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001801 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001802 }
1803
1804 // Create a blob abbreviation
1805 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1806 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001809 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001810
1811 // Write the identifier table
1812 RecordData Record;
1813 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001814 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001815 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001816 }
1817
1818 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001819 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1820 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1821 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1822 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1823 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1824
1825 RecordData Record;
1826 Record.push_back(pch::IDENTIFIER_OFFSET);
1827 Record.push_back(IdentifierOffsets.size());
1828 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1829 (const char *)&IdentifierOffsets.front(),
1830 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001831}
1832
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001833//===----------------------------------------------------------------------===//
1834// General Serialization Routines
1835//===----------------------------------------------------------------------===//
1836
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001837/// \brief Write a record containing the given attributes.
1838void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1839 RecordData Record;
1840 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001841 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001842 Record.push_back(Attr->isInherited());
1843 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001844 default:
1845 assert(0 && "Does not support PCH writing for this attribute yet!");
1846 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001847 case Attr::Alias:
1848 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1849 break;
1850
1851 case Attr::Aligned:
1852 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1853 break;
1854
1855 case Attr::AlwaysInline:
1856 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001858 case Attr::AnalyzerNoReturn:
1859 break;
1860
1861 case Attr::Annotate:
1862 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1863 break;
1864
1865 case Attr::AsmLabel:
1866 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1867 break;
1868
Sean Hunt7725e672009-11-25 04:20:27 +00001869 case Attr::BaseCheck:
1870 break;
1871
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001872 case Attr::Blocks:
1873 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1874 break;
1875
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001876 case Attr::CDecl:
1877 break;
1878
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001879 case Attr::Cleanup:
1880 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1881 break;
1882
1883 case Attr::Const:
1884 break;
1885
1886 case Attr::Constructor:
1887 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1888 break;
1889
1890 case Attr::DLLExport:
1891 case Attr::DLLImport:
1892 case Attr::Deprecated:
1893 break;
1894
1895 case Attr::Destructor:
1896 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1897 break;
1898
1899 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001900 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001901 break;
1902
1903 case Attr::Format: {
1904 const FormatAttr *Format = cast<FormatAttr>(Attr);
1905 AddString(Format->getType(), Record);
1906 Record.push_back(Format->getFormatIdx());
1907 Record.push_back(Format->getFirstArg());
1908 break;
1909 }
1910
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001911 case Attr::FormatArg: {
1912 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1913 Record.push_back(Format->getFormatIdx());
1914 break;
1915 }
1916
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001917 case Attr::Sentinel : {
1918 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1919 Record.push_back(Sentinel->getSentinel());
1920 Record.push_back(Sentinel->getNullPos());
1921 break;
1922 }
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Chris Lattnercf2a7212009-04-20 19:12:28 +00001924 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001925 case Attr::Hiding:
Ted Kremenekefbddd22010-02-17 02:37:45 +00001926 case Attr::IBActionKind:
Ted Kremenek47e69902010-02-18 00:05:52 +00001927 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001928 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001929 case Attr::NoDebug:
Ted Kremenek47e69902010-02-18 00:05:52 +00001930 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001931 case Attr::NoReturn:
1932 case Attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001933 break;
1934
1935 case Attr::NonNull: {
1936 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1937 Record.push_back(NonNull->size());
1938 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1939 break;
1940 }
1941
Ted Kremenek31c780d2010-02-18 00:05:45 +00001942 case Attr::CFReturnsNotRetained:
1943 case Attr::CFReturnsRetained:
1944 case Attr::NSReturnsNotRetained:
1945 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001946 case Attr::ObjCException:
1947 case Attr::ObjCNSObject:
1948 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001949 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001950 break;
1951
Anders Carlssona860e752009-08-08 18:23:56 +00001952 case Attr::PragmaPack:
1953 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001954 break;
1955
Anders Carlssona860e752009-08-08 18:23:56 +00001956 case Attr::Packed:
1957 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001959 case Attr::Pure:
1960 break;
1961
1962 case Attr::Regparm:
1963 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1964 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Nate Begeman6f3d8382009-06-26 06:32:41 +00001966 case Attr::ReqdWorkGroupSize:
1967 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1968 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1969 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1970 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001971
1972 case Attr::Section:
1973 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1974 break;
1975
1976 case Attr::StdCall:
1977 case Attr::TransparentUnion:
1978 case Attr::Unavailable:
1979 case Attr::Unused:
1980 case Attr::Used:
1981 break;
1982
1983 case Attr::Visibility:
1984 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001985 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001986 break;
1987
1988 case Attr::WarnUnusedResult:
1989 case Attr::Weak:
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001990 case Attr::WeakRef:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001991 case Attr::WeakImport:
1992 break;
1993 }
1994 }
1995
Douglas Gregorc9490c02009-04-16 22:23:12 +00001996 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001997}
1998
1999void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2000 Record.push_back(Str.size());
2001 Record.insert(Record.end(), Str.begin(), Str.end());
2002}
2003
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002004/// \brief Note that the identifier II occurs at the given offset
2005/// within the identifier table.
2006void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002007 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002008}
2009
Douglas Gregor83941df2009-04-25 17:48:32 +00002010/// \brief Note that the selector Sel occurs at the given offset
2011/// within the method pool/selector table.
2012void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2013 unsigned ID = SelectorIDs[Sel];
2014 assert(ID && "Unknown selector");
2015 SelectorOffsets[ID - 1] = Offset;
2016}
2017
Mike Stump1eb44332009-09-09 15:08:12 +00002018PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2019 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00002020 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2021 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002022
Douglas Gregore650c8c2009-07-07 00:12:59 +00002023void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2024 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002025 using namespace llvm;
2026
Douglas Gregore7785042009-04-20 15:53:59 +00002027 ASTContext &Context = SemaRef.Context;
2028 Preprocessor &PP = SemaRef.PP;
2029
Douglas Gregor2cf26342009-04-09 22:27:44 +00002030 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002031 Stream.Emit((unsigned)'C', 8);
2032 Stream.Emit((unsigned)'P', 8);
2033 Stream.Emit((unsigned)'C', 8);
2034 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002036 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002037
2038 // The translation unit is the first declaration we'll emit.
2039 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002040 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002041
Douglas Gregor2deaea32009-04-22 18:49:13 +00002042 // Make sure that we emit IdentifierInfos (and any attached
2043 // declarations) for builtins.
2044 {
2045 IdentifierTable &Table = PP.getIdentifierTable();
2046 llvm::SmallVector<const char *, 32> BuiltinNames;
2047 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2048 Context.getLangOptions().NoBuiltin);
2049 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2050 getIdentifierRef(&Table.get(BuiltinNames[I]));
2051 }
2052
Chris Lattner63d65f82009-09-08 18:19:27 +00002053 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002054 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002055 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002056 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002057 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2058 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002059 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002060
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002061 // Build a record containing all of the static unused functions in this file.
2062 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002063 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002064 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002065
Douglas Gregor14c22f22009-04-22 22:18:58 +00002066 // Build a record containing all of the locally-scoped external
2067 // declarations in this header file. Generally, this record will be
2068 // empty.
2069 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002070 // FIXME: This is filling in the PCH file in densemap order which is
2071 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002072 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002073 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2074 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2075 TD != TDEnd; ++TD)
2076 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2077
Douglas Gregorb81c1702009-04-27 20:06:05 +00002078 // Build a record containing all of the ext_vector declarations.
2079 RecordData ExtVectorDecls;
2080 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2081 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2082
Douglas Gregor2cf26342009-04-09 22:27:44 +00002083 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002084 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002085 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002086 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002087 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002088 if (StatCalls && !isysroot)
2089 WriteStatCache(*StatCalls, isysroot);
2090 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump1eb44332009-09-09 15:08:12 +00002091 WriteComments(Context);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002092 // Write the record of special types.
2093 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002095 AddTypeRef(Context.getBuiltinVaListType(), Record);
2096 AddTypeRef(Context.getObjCIdType(), Record);
2097 AddTypeRef(Context.getObjCSelType(), Record);
2098 AddTypeRef(Context.getObjCProtoType(), Record);
2099 AddTypeRef(Context.getObjCClassType(), Record);
2100 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2101 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2102 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002103 AddTypeRef(Context.getjmp_bufType(), Record);
2104 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002105 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2106 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002107#if 0
2108 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00002109 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002110#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00002111 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002112 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002113 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002114
Douglas Gregor366809a2009-04-26 03:49:13 +00002115 // Keep writing types and declarations until all types and
2116 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002117 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2118 WriteDeclsBlockAbbrevs();
2119 while (!DeclTypesToEmit.empty()) {
2120 DeclOrType DOT = DeclTypesToEmit.front();
2121 DeclTypesToEmit.pop();
2122 if (DOT.isType())
2123 WriteType(DOT.getType());
2124 else
2125 WriteDecl(Context, DOT.getDecl());
2126 }
2127 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002128
Douglas Gregor813a97b2009-10-17 17:25:45 +00002129 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002130 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002131 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002132
2133 // Write the type offsets array
2134 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2135 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2138 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2139 Record.clear();
2140 Record.push_back(pch::TYPE_OFFSET);
2141 Record.push_back(TypeOffsets.size());
2142 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002143 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002144 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002146 // Write the declaration offsets array
2147 Abbrev = new BitCodeAbbrev();
2148 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2151 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2152 Record.clear();
2153 Record.push_back(pch::DECL_OFFSET);
2154 Record.push_back(DeclOffsets.size());
2155 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002156 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002157 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002158
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002159 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002160 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002161 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002162
2163 // Write the record containing tentative definitions.
2164 if (!TentativeDefinitions.empty())
2165 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002166
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002167 // Write the record containing unused static functions.
2168 if (!UnusedStaticFuncs.empty())
2169 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002170
Douglas Gregor14c22f22009-04-22 22:18:58 +00002171 // Write the record containing locally-scoped external definitions.
2172 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002173 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002174 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002175
2176 // Write the record containing ext_vector type names.
2177 if (!ExtVectorDecls.empty())
2178 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002179
Douglas Gregor3e1af842009-04-17 22:13:46 +00002180 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002181 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002182 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002183 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002184 Record.push_back(NumLexicalDeclContexts);
2185 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002186 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002187 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002188}
2189
2190void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2191 Record.push_back(Loc.getRawEncoding());
2192}
2193
2194void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2195 Record.push_back(Value.getBitWidth());
2196 unsigned N = Value.getNumWords();
2197 const uint64_t* Words = Value.getRawData();
2198 for (unsigned I = 0; I != N; ++I)
2199 Record.push_back(Words[I]);
2200}
2201
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002202void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2203 Record.push_back(Value.isUnsigned());
2204 AddAPInt(Value, Record);
2205}
2206
Douglas Gregor17fc2232009-04-14 21:55:33 +00002207void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2208 AddAPInt(Value.bitcastToAPInt(), Record);
2209}
2210
Douglas Gregor2cf26342009-04-09 22:27:44 +00002211void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002212 Record.push_back(getIdentifierRef(II));
2213}
2214
2215pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2216 if (II == 0)
2217 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002218
2219 pch::IdentID &ID = IdentifierIDs[II];
2220 if (ID == 0)
2221 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002222 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002223}
2224
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002225pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2226 if (MD == 0)
2227 return 0;
2228
2229 pch::IdentID &ID = MacroDefinitions[MD];
2230 if (ID == 0)
2231 ID = MacroDefinitions.size();
2232 return ID;
2233}
2234
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002235void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2236 if (SelRef.getAsOpaquePtr() == 0) {
2237 Record.push_back(0);
2238 return;
2239 }
2240
2241 pch::SelectorID &SID = SelectorIDs[SelRef];
2242 if (SID == 0) {
2243 SID = SelectorIDs.size();
2244 SelVector.push_back(SelRef);
2245 }
2246 Record.push_back(SID);
2247}
2248
John McCall833ca992009-10-29 08:12:44 +00002249void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2250 RecordData &Record) {
2251 switch (Arg.getArgument().getKind()) {
2252 case TemplateArgument::Expression:
2253 AddStmt(Arg.getLocInfo().getAsExpr());
2254 break;
2255 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002256 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002257 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002258 case TemplateArgument::Template:
2259 Record.push_back(
2260 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2261 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2262 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2263 break;
John McCall833ca992009-10-29 08:12:44 +00002264 case TemplateArgument::Null:
2265 case TemplateArgument::Integral:
2266 case TemplateArgument::Declaration:
2267 case TemplateArgument::Pack:
2268 break;
2269 }
2270}
2271
John McCalla93c9342009-12-07 02:54:59 +00002272void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2273 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002274 AddTypeRef(QualType(), Record);
2275 return;
2276 }
2277
John McCalla93c9342009-12-07 02:54:59 +00002278 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002279 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002280 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002281 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002282}
2283
Douglas Gregor2cf26342009-04-09 22:27:44 +00002284void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2285 if (T.isNull()) {
2286 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2287 return;
2288 }
2289
Douglas Gregora4923eb2009-11-16 21:35:15 +00002290 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002291 T.removeFastQualifiers();
2292
Douglas Gregora4923eb2009-11-16 21:35:15 +00002293 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002294 pch::TypeID &ID = TypeIDs[T];
2295 if (ID == 0) {
2296 // We haven't seen these qualifiers applied to this type before.
2297 // Assign it a new ID. This is the only time we enqueue a
2298 // qualified type, and it has no CV qualifiers.
2299 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002300 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002301 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002302
John McCall0953e762009-09-24 19:53:00 +00002303 // Encode the type qualifiers in the type reference.
2304 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2305 return;
2306 }
2307
Douglas Gregora4923eb2009-11-16 21:35:15 +00002308 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002309
Douglas Gregor2cf26342009-04-09 22:27:44 +00002310 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002311 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002312 switch (BT->getKind()) {
2313 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2314 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2315 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2316 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2317 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2318 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2319 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2320 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002321 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002322 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2323 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2324 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2325 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2326 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2327 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2328 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002329 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002330 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2331 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2332 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002333 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002334 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2335 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002336 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2337 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002338 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2339 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002340 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002341 case BuiltinType::UndeducedAuto:
2342 assert(0 && "Should not see undeduced auto here");
2343 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002344 }
2345
John McCall0953e762009-09-24 19:53:00 +00002346 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002347 return;
2348 }
2349
John McCall0953e762009-09-24 19:53:00 +00002350 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002351 if (ID == 0) {
2352 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002353 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002354 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002355 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002356 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002357
2358 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002359 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002360}
2361
2362void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2363 if (D == 0) {
2364 Record.push_back(0);
2365 return;
2366 }
2367
Douglas Gregor8038d512009-04-10 17:25:41 +00002368 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002369 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002370 // We haven't seen this declaration before. Give it a new ID and
2371 // enqueue it in the list of declarations to emit.
2372 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002373 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002374 }
2375
2376 Record.push_back(ID);
2377}
2378
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002379pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2380 if (D == 0)
2381 return 0;
2382
2383 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2384 return DeclIDs[D];
2385}
2386
Douglas Gregor2cf26342009-04-09 22:27:44 +00002387void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002388 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002389 Record.push_back(Name.getNameKind());
2390 switch (Name.getNameKind()) {
2391 case DeclarationName::Identifier:
2392 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2393 break;
2394
2395 case DeclarationName::ObjCZeroArgSelector:
2396 case DeclarationName::ObjCOneArgSelector:
2397 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002398 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002399 break;
2400
2401 case DeclarationName::CXXConstructorName:
2402 case DeclarationName::CXXDestructorName:
2403 case DeclarationName::CXXConversionFunctionName:
2404 AddTypeRef(Name.getCXXNameType(), Record);
2405 break;
2406
2407 case DeclarationName::CXXOperatorName:
2408 Record.push_back(Name.getCXXOverloadedOperator());
2409 break;
2410
Sean Hunt3e518bd2009-11-29 07:34:05 +00002411 case DeclarationName::CXXLiteralOperatorName:
2412 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2413 break;
2414
Douglas Gregor2cf26342009-04-09 22:27:44 +00002415 case DeclarationName::CXXUsingDirective:
2416 // No extra data to emit
2417 break;
2418 }
2419}