blob: eed3cc1a5b3d9fd61512603f6d46cf88487c0f2c [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"
24#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000031#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000037#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000038#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42// Type serialization
43//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000044
Douglas Gregor2cf26342009-04-09 22:27:44 +000045namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000046 class PCHTypeWriter {
Douglas Gregor2cf26342009-04-09 22:27:44 +000047 PCHWriter &Writer;
48 PCHWriter::RecordData &Record;
49
50 public:
51 /// \brief Type code that corresponds to the record generated.
52 pch::TypeCode Code;
53
Mike Stump1eb44332009-09-09 15:08:12 +000054 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000055 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000056
57 void VisitArrayType(const ArrayType *T);
58 void VisitFunctionType(const FunctionType *T);
59 void VisitTagType(const TagType *T);
60
61#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
62#define ABSTRACT_TYPE(Class, Base)
63#define DEPENDENT_TYPE(Class, Base)
64#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
Douglas Gregor2cf26342009-04-09 22:27:44 +000072void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
73 Writer.AddTypeRef(T->getElementType(), Record);
74 Code = pch::TYPE_COMPLEX;
75}
76
77void PCHTypeWriter::VisitPointerType(const PointerType *T) {
78 Writer.AddTypeRef(T->getPointeeType(), Record);
79 Code = pch::TYPE_POINTER;
80}
81
82void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000084 Code = pch::TYPE_BLOCK_POINTER;
85}
86
87void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_LVALUE_REFERENCE;
90}
91
92void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_RVALUE_REFERENCE;
95}
96
97void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100 Code = pch::TYPE_MEMBER_POINTER;
101}
102
103void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
104 Writer.AddTypeRef(T->getElementType(), Record);
105 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000106 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
109void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
110 VisitArrayType(T);
111 Writer.AddAPInt(T->getSize(), Record);
112 Code = pch::TYPE_CONSTANT_ARRAY;
113}
114
115void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
116 VisitArrayType(T);
117 Code = pch::TYPE_INCOMPLETE_ARRAY;
118}
119
120void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
121 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000122 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
123 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000124 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125 Code = pch::TYPE_VARIABLE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVectorType(const VectorType *T) {
129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getNumElements());
John Thompson82287d12010-02-05 00:12:22 +0000131 Record.push_back(T->isAltiVec());
132 Record.push_back(T->isPixel());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133 Code = pch::TYPE_VECTOR;
134}
135
136void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
137 VisitVectorType(T);
138 Code = pch::TYPE_EXT_VECTOR;
139}
140
141void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
142 Writer.AddTypeRef(T->getResultType(), Record);
Douglas Gregor91236662009-12-22 18:11:50 +0000143 Record.push_back(T->getNoReturnAttr());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000144 // FIXME: need to stabilize encoding of calling convention...
145 Record.push_back(T->getCallConv());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146}
147
148void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
149 VisitFunctionType(T);
150 Code = pch::TYPE_FUNCTION_NO_PROTO;
151}
152
153void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
154 VisitFunctionType(T);
155 Record.push_back(T->getNumArgs());
156 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
157 Writer.AddTypeRef(T->getArgType(I), Record);
158 Record.push_back(T->isVariadic());
159 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000160 Record.push_back(T->hasExceptionSpec());
161 Record.push_back(T->hasAnyExceptionSpec());
162 Record.push_back(T->getNumExceptions());
163 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
164 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165 Code = pch::TYPE_FUNCTION_PROTO;
166}
167
John McCalled976492009-12-04 22:46:56 +0000168#if 0
169// For when we want it....
170void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_UNRESOLVED_USING;
173}
174#endif
175
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
177 Writer.AddDeclRef(T->getDecl(), Record);
178 Code = pch::TYPE_TYPEDEF;
179}
180
181void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000182 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 Code = pch::TYPE_TYPEOF_EXPR;
184}
185
186void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
187 Writer.AddTypeRef(T->getUnderlyingType(), Record);
188 Code = pch::TYPE_TYPEOF;
189}
190
Anders Carlsson395b4752009-06-24 19:06:50 +0000191void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
192 Writer.AddStmt(T->getUnderlyingExpr());
193 Code = pch::TYPE_DECLTYPE;
194}
195
Douglas Gregor2cf26342009-04-09 22:27:44 +0000196void PCHTypeWriter::VisitTagType(const TagType *T) {
197 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000198 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199 "Cannot serialize in the middle of a type definition");
200}
201
202void PCHTypeWriter::VisitRecordType(const RecordType *T) {
203 VisitTagType(T);
204 Code = pch::TYPE_RECORD;
205}
206
207void PCHTypeWriter::VisitEnumType(const EnumType *T) {
208 VisitTagType(T);
209 Code = pch::TYPE_ENUM;
210}
211
John McCall7da24312009-09-05 00:15:47 +0000212void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
213 Writer.AddTypeRef(T->getUnderlyingType(), Record);
214 Record.push_back(T->getTagKind());
215 Code = pch::TYPE_ELABORATED;
216}
217
Mike Stump1eb44332009-09-09 15:08:12 +0000218void
John McCall49a832b2009-10-18 09:09:24 +0000219PCHTypeWriter::VisitSubstTemplateTypeParmType(
220 const SubstTemplateTypeParmType *T) {
221 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
222 Writer.AddTypeRef(T->getReplacementType(), Record);
223 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
224}
225
226void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000227PCHTypeWriter::VisitTemplateSpecializationType(
228 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000229 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000230 assert(false && "Cannot serialize template specialization types");
231}
232
233void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000234 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000235 assert(false && "Cannot serialize qualified name types");
236}
237
John McCall3cb0ebd2010-03-10 03:28:59 +0000238void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
239 Writer.AddDeclRef(T->getDecl(), Record);
240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
241 Code = pch::TYPE_INJECTED_CLASS_NAME;
242}
243
Douglas Gregor2cf26342009-04-09 22:27:44 +0000244void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
245 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000246 Record.push_back(T->getNumProtocols());
Steve Naroff446ee4e2009-05-27 16:21:00 +0000247 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
248 E = T->qual_end(); I != E; ++I)
249 Writer.AddDeclRef(*I, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000250 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000251}
252
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000253void
254PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000255 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256 Record.push_back(T->getNumProtocols());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000257 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000258 E = T->qual_end(); I != E; ++I)
259 Writer.AddDeclRef(*I, Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000260 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000261}
262
John McCalla1ee0c52009-10-16 21:56:05 +0000263namespace {
264
265class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
266 PCHWriter &Writer;
267 PCHWriter::RecordData &Record;
268
269public:
270 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
271 : Writer(Writer), Record(Record) { }
272
John McCall51bd8032009-10-18 01:05:36 +0000273#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000274#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000275 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000276#include "clang/AST/TypeLocNodes.def"
277
John McCall51bd8032009-10-18 01:05:36 +0000278 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
279 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000280};
281
282}
283
John McCall51bd8032009-10-18 01:05:36 +0000284void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
285 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000286}
John McCall51bd8032009-10-18 01:05:36 +0000287void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000288 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
289 if (TL.needsExtraLocalData()) {
290 Record.push_back(TL.getWrittenTypeSpec());
291 Record.push_back(TL.getWrittenSignSpec());
292 Record.push_back(TL.getWrittenWidthSpec());
293 Record.push_back(TL.hasModeAttr());
294 }
John McCalla1ee0c52009-10-16 21:56:05 +0000295}
John McCall51bd8032009-10-18 01:05:36 +0000296void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
297 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000298}
John McCall51bd8032009-10-18 01:05:36 +0000299void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
300 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000301}
John McCall51bd8032009-10-18 01:05:36 +0000302void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
303 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000304}
John McCall51bd8032009-10-18 01:05:36 +0000305void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
306 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000307}
John McCall51bd8032009-10-18 01:05:36 +0000308void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
309 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000310}
John McCall51bd8032009-10-18 01:05:36 +0000311void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
312 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000313}
John McCall51bd8032009-10-18 01:05:36 +0000314void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
315 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
316 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
317 Record.push_back(TL.getSizeExpr() ? 1 : 0);
318 if (TL.getSizeExpr())
319 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000320}
John McCall51bd8032009-10-18 01:05:36 +0000321void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
322 VisitArrayTypeLoc(TL);
323}
324void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
325 VisitArrayTypeLoc(TL);
326}
327void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
328 VisitArrayTypeLoc(TL);
329}
330void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
331 DependentSizedArrayTypeLoc TL) {
332 VisitArrayTypeLoc(TL);
333}
334void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
335 DependentSizedExtVectorTypeLoc TL) {
336 Writer.AddSourceLocation(TL.getNameLoc(), Record);
337}
338void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getNameLoc(), Record);
340}
341void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
342 Writer.AddSourceLocation(TL.getNameLoc(), Record);
343}
344void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
345 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
346 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
347 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
348 Writer.AddDeclRef(TL.getArg(i), Record);
349}
350void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
351 VisitFunctionTypeLoc(TL);
352}
353void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
354 VisitFunctionTypeLoc(TL);
355}
John McCalled976492009-12-04 22:46:56 +0000356void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
357 Writer.AddSourceLocation(TL.getNameLoc(), Record);
358}
John McCall51bd8032009-10-18 01:05:36 +0000359void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
360 Writer.AddSourceLocation(TL.getNameLoc(), Record);
361}
362void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000363 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
364 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
365 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000366}
367void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000368 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
369 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
370 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
371 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000372}
373void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getNameLoc(), Record);
375}
376void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
382void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getNameLoc(), Record);
384}
385void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getNameLoc(), Record);
387}
John McCall49a832b2009-10-18 09:09:24 +0000388void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
389 SubstTemplateTypeParmTypeLoc TL) {
390 Writer.AddSourceLocation(TL.getNameLoc(), Record);
391}
John McCall51bd8032009-10-18 01:05:36 +0000392void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
393 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000394 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
395 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
396 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
397 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
398 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000399}
400void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getNameLoc(), Record);
402}
John McCall3cb0ebd2010-03-10 03:28:59 +0000403void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getNameLoc(), Record);
405}
John McCall51bd8032009-10-18 01:05:36 +0000406void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getNameLoc(), Record);
408}
409void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000411 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
412 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
413 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
414 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000415}
John McCall54e14c42009-10-22 22:37:11 +0000416void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
417 Writer.AddSourceLocation(TL.getStarLoc(), Record);
418 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
419 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
420 Record.push_back(TL.hasBaseTypeAsWritten());
421 Record.push_back(TL.hasProtocolsAsWritten());
422 if (TL.hasProtocolsAsWritten())
423 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
424 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
425}
John McCalla1ee0c52009-10-16 21:56:05 +0000426
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000427//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000428// PCHWriter Implementation
429//===----------------------------------------------------------------------===//
430
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000431static void EmitBlockID(unsigned ID, const char *Name,
432 llvm::BitstreamWriter &Stream,
433 PCHWriter::RecordData &Record) {
434 Record.clear();
435 Record.push_back(ID);
436 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
437
438 // Emit the block name if present.
439 if (Name == 0 || Name[0] == 0) return;
440 Record.clear();
441 while (*Name)
442 Record.push_back(*Name++);
443 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
444}
445
446static void EmitRecordID(unsigned ID, const char *Name,
447 llvm::BitstreamWriter &Stream,
448 PCHWriter::RecordData &Record) {
449 Record.clear();
450 Record.push_back(ID);
451 while (*Name)
452 Record.push_back(*Name++);
453 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000454}
455
456static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
457 PCHWriter::RecordData &Record) {
458#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
459 RECORD(STMT_STOP);
460 RECORD(STMT_NULL_PTR);
461 RECORD(STMT_NULL);
462 RECORD(STMT_COMPOUND);
463 RECORD(STMT_CASE);
464 RECORD(STMT_DEFAULT);
465 RECORD(STMT_LABEL);
466 RECORD(STMT_IF);
467 RECORD(STMT_SWITCH);
468 RECORD(STMT_WHILE);
469 RECORD(STMT_DO);
470 RECORD(STMT_FOR);
471 RECORD(STMT_GOTO);
472 RECORD(STMT_INDIRECT_GOTO);
473 RECORD(STMT_CONTINUE);
474 RECORD(STMT_BREAK);
475 RECORD(STMT_RETURN);
476 RECORD(STMT_DECL);
477 RECORD(STMT_ASM);
478 RECORD(EXPR_PREDEFINED);
479 RECORD(EXPR_DECL_REF);
480 RECORD(EXPR_INTEGER_LITERAL);
481 RECORD(EXPR_FLOATING_LITERAL);
482 RECORD(EXPR_IMAGINARY_LITERAL);
483 RECORD(EXPR_STRING_LITERAL);
484 RECORD(EXPR_CHARACTER_LITERAL);
485 RECORD(EXPR_PAREN);
486 RECORD(EXPR_UNARY_OPERATOR);
487 RECORD(EXPR_SIZEOF_ALIGN_OF);
488 RECORD(EXPR_ARRAY_SUBSCRIPT);
489 RECORD(EXPR_CALL);
490 RECORD(EXPR_MEMBER);
491 RECORD(EXPR_BINARY_OPERATOR);
492 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
493 RECORD(EXPR_CONDITIONAL_OPERATOR);
494 RECORD(EXPR_IMPLICIT_CAST);
495 RECORD(EXPR_CSTYLE_CAST);
496 RECORD(EXPR_COMPOUND_LITERAL);
497 RECORD(EXPR_EXT_VECTOR_ELEMENT);
498 RECORD(EXPR_INIT_LIST);
499 RECORD(EXPR_DESIGNATED_INIT);
500 RECORD(EXPR_IMPLICIT_VALUE_INIT);
501 RECORD(EXPR_VA_ARG);
502 RECORD(EXPR_ADDR_LABEL);
503 RECORD(EXPR_STMT);
504 RECORD(EXPR_TYPES_COMPATIBLE);
505 RECORD(EXPR_CHOOSE);
506 RECORD(EXPR_GNU_NULL);
507 RECORD(EXPR_SHUFFLE_VECTOR);
508 RECORD(EXPR_BLOCK);
509 RECORD(EXPR_BLOCK_DECL_REF);
510 RECORD(EXPR_OBJC_STRING_LITERAL);
511 RECORD(EXPR_OBJC_ENCODE);
512 RECORD(EXPR_OBJC_SELECTOR_EXPR);
513 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
514 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
515 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
516 RECORD(EXPR_OBJC_KVC_REF_EXPR);
517 RECORD(EXPR_OBJC_MESSAGE_EXPR);
518 RECORD(EXPR_OBJC_SUPER_EXPR);
519 RECORD(STMT_OBJC_FOR_COLLECTION);
520 RECORD(STMT_OBJC_CATCH);
521 RECORD(STMT_OBJC_FINALLY);
522 RECORD(STMT_OBJC_AT_TRY);
523 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
524 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000525 RECORD(EXPR_CXX_OPERATOR_CALL);
526 RECORD(EXPR_CXX_CONSTRUCT);
527 RECORD(EXPR_CXX_STATIC_CAST);
528 RECORD(EXPR_CXX_DYNAMIC_CAST);
529 RECORD(EXPR_CXX_REINTERPRET_CAST);
530 RECORD(EXPR_CXX_CONST_CAST);
531 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
532 RECORD(EXPR_CXX_BOOL_LITERAL);
533 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000534#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000535}
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000537void PCHWriter::WriteBlockInfoBlock() {
538 RecordData Record;
539 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattner2f4efd12009-04-27 00:40:25 +0000541#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000542#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000544 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000545 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000546 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000547 RECORD(TYPE_OFFSET);
548 RECORD(DECL_OFFSET);
549 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000550 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000551 RECORD(IDENTIFIER_OFFSET);
552 RECORD(IDENTIFIER_TABLE);
553 RECORD(EXTERNAL_DEFINITIONS);
554 RECORD(SPECIAL_TYPES);
555 RECORD(STATISTICS);
556 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000557 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000558 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
559 RECORD(SELECTOR_OFFSETS);
560 RECORD(METHOD_POOL);
561 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000562 RECORD(SOURCE_LOCATION_OFFSETS);
563 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000564 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000565 RECORD(EXT_VECTOR_DECLS);
Douglas Gregor2e222532009-07-02 17:08:52 +0000566 RECORD(COMMENT_RANGES);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000567 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000568
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000569 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000570 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000571 RECORD(SM_SLOC_FILE_ENTRY);
572 RECORD(SM_SLOC_BUFFER_ENTRY);
573 RECORD(SM_SLOC_BUFFER_BLOB);
574 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
575 RECORD(SM_LINE_TABLE);
576 RECORD(SM_HEADER_FILE_INFO);
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000578 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000579 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000580 RECORD(PP_MACRO_OBJECT_LIKE);
581 RECORD(PP_MACRO_FUNCTION_LIKE);
582 RECORD(PP_TOKEN);
583
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000584 // Decls and Types block.
585 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000586 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000587 RECORD(TYPE_COMPLEX);
588 RECORD(TYPE_POINTER);
589 RECORD(TYPE_BLOCK_POINTER);
590 RECORD(TYPE_LVALUE_REFERENCE);
591 RECORD(TYPE_RVALUE_REFERENCE);
592 RECORD(TYPE_MEMBER_POINTER);
593 RECORD(TYPE_CONSTANT_ARRAY);
594 RECORD(TYPE_INCOMPLETE_ARRAY);
595 RECORD(TYPE_VARIABLE_ARRAY);
596 RECORD(TYPE_VECTOR);
597 RECORD(TYPE_EXT_VECTOR);
598 RECORD(TYPE_FUNCTION_PROTO);
599 RECORD(TYPE_FUNCTION_NO_PROTO);
600 RECORD(TYPE_TYPEDEF);
601 RECORD(TYPE_TYPEOF_EXPR);
602 RECORD(TYPE_TYPEOF);
603 RECORD(TYPE_RECORD);
604 RECORD(TYPE_ENUM);
605 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000606 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000607 RECORD(DECL_ATTR);
608 RECORD(DECL_TRANSLATION_UNIT);
609 RECORD(DECL_TYPEDEF);
610 RECORD(DECL_ENUM);
611 RECORD(DECL_RECORD);
612 RECORD(DECL_ENUM_CONSTANT);
613 RECORD(DECL_FUNCTION);
614 RECORD(DECL_OBJC_METHOD);
615 RECORD(DECL_OBJC_INTERFACE);
616 RECORD(DECL_OBJC_PROTOCOL);
617 RECORD(DECL_OBJC_IVAR);
618 RECORD(DECL_OBJC_AT_DEFS_FIELD);
619 RECORD(DECL_OBJC_CLASS);
620 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
621 RECORD(DECL_OBJC_CATEGORY);
622 RECORD(DECL_OBJC_CATEGORY_IMPL);
623 RECORD(DECL_OBJC_IMPLEMENTATION);
624 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
625 RECORD(DECL_OBJC_PROPERTY);
626 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000627 RECORD(DECL_FIELD);
628 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000629 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000630 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000631 RECORD(DECL_FILE_SCOPE_ASM);
632 RECORD(DECL_BLOCK);
633 RECORD(DECL_CONTEXT_LEXICAL);
634 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000635 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000636 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000637#undef RECORD
638#undef BLOCK
639 Stream.ExitBlock();
640}
641
Douglas Gregore650c8c2009-07-07 00:12:59 +0000642/// \brief Adjusts the given filename to only write out the portion of the
643/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000644///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000645/// \param Filename the file name to adjust.
646///
647/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
648/// the returned filename will be adjusted by this system root.
649///
650/// \returns either the original filename (if it needs no adjustment) or the
651/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000652static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000653adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
654 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Douglas Gregore650c8c2009-07-07 00:12:59 +0000656 if (!isysroot)
657 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Douglas Gregore650c8c2009-07-07 00:12:59 +0000659 // Verify that the filename and the system root have the same prefix.
660 unsigned Pos = 0;
661 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
662 if (Filename[Pos] != isysroot[Pos])
663 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregore650c8c2009-07-07 00:12:59 +0000665 // We hit the end of the filename before we hit the end of the system root.
666 if (!Filename[Pos])
667 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregore650c8c2009-07-07 00:12:59 +0000669 // If the file name has a '/' at the current position, skip over the '/'.
670 // We distinguish sysroot-based includes from absolute includes by the
671 // absence of '/' at the beginning of sysroot-based includes.
672 if (Filename[Pos] == '/')
673 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Douglas Gregore650c8c2009-07-07 00:12:59 +0000675 return Filename + Pos;
676}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000677
Douglas Gregorab41e632009-04-27 22:23:34 +0000678/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000679void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000680 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000681
Douglas Gregore650c8c2009-07-07 00:12:59 +0000682 // Metadata
683 const TargetInfo &Target = Context.Target;
684 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
685 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
686 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
687 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
688 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
689 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
690 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
691 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
692 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Douglas Gregore650c8c2009-07-07 00:12:59 +0000694 RecordData Record;
695 Record.push_back(pch::METADATA);
696 Record.push_back(pch::VERSION_MAJOR);
697 Record.push_back(pch::VERSION_MINOR);
698 Record.push_back(CLANG_VERSION_MAJOR);
699 Record.push_back(CLANG_VERSION_MINOR);
700 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000701 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000702 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Douglas Gregorb64c1932009-05-12 01:31:05 +0000704 // Original file name
705 SourceManager &SM = Context.getSourceManager();
706 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
707 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
708 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
709 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
710 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
711
712 llvm::sys::Path MainFilePath(MainFile->getName());
713 std::string MainFileName;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Douglas Gregorb64c1932009-05-12 01:31:05 +0000715 if (!MainFilePath.isAbsolute()) {
716 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000717 P.appendComponent(MainFilePath.str());
718 MainFileName = P.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000719 } else {
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000720 MainFileName = MainFilePath.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000721 }
722
Douglas Gregore650c8c2009-07-07 00:12:59 +0000723 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000724 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000725 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000726 RecordData Record;
727 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000728 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000729 }
Douglas Gregor445e23e2009-10-05 21:07:28 +0000730
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000731 // Repository branch/version information.
732 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
733 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
734 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
735 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000736 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000737 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000738 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
739 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000740}
741
742/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000743void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
744 RecordData Record;
745 Record.push_back(LangOpts.Trigraphs);
746 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
747 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
748 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
749 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
750 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
751 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
752 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
753 Record.push_back(LangOpts.C99); // C99 Support
754 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
755 Record.push_back(LangOpts.CPlusPlus); // C++ Support
756 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000757 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000759 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
760 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000761 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
762 // modern abi enabled.
763 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
764 // modern abi enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000766 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000767 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
768 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000769 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000770 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000771 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000772
773 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
774 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
775 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
776
Chris Lattnerea5ce472009-04-27 07:35:58 +0000777 // Whether static initializers are protected by locks.
778 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000779 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000780 Record.push_back(LangOpts.Blocks); // block extension to C
781 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
782 // they are unused.
783 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
784 // (modulo the platform support).
785
786 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
787 // signed integer arithmetic overflows.
788
789 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
790 // may be ripped out at any time.
791
792 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000793 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000794 // defined.
795 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
796 // opposed to __DYNAMIC__).
797 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
798
799 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
800 // used (instead of C99 semantics).
801 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000802 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
803 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000804 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
805 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000806 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000807 Record.push_back(LangOpts.getGCMode());
808 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000809 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000810 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000811 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000812 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000813 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000814 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000815}
816
Douglas Gregor14f79002009-04-10 03:52:48 +0000817//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000818// stat cache Serialization
819//===----------------------------------------------------------------------===//
820
821namespace {
822// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000823class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000824public:
825 typedef const char * key_type;
826 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000828 typedef std::pair<int, struct stat> data_type;
829 typedef const data_type& data_type_ref;
830
831 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000832 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
835 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000836 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
837 data_type_ref Data) {
838 unsigned StrLen = strlen(path);
839 clang::io::Emit16(Out, StrLen);
840 unsigned DataLen = 1; // result value
841 if (Data.first == 0)
842 DataLen += 4 + 4 + 2 + 8 + 8;
843 clang::io::Emit8(Out, DataLen);
844 return std::make_pair(StrLen + 1, DataLen);
845 }
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000847 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
848 Out.write(path, KeyLen);
849 }
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000851 void EmitData(llvm::raw_ostream& Out, key_type_ref,
852 data_type_ref Data, unsigned DataLen) {
853 using namespace clang::io;
854 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856 // Result of stat()
857 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000859 if (Data.first == 0) {
860 Emit32(Out, (uint32_t) Data.second.st_ino);
861 Emit32(Out, (uint32_t) Data.second.st_dev);
862 Emit16(Out, (uint16_t) Data.second.st_mode);
863 Emit64(Out, (uint64_t) Data.second.st_mtime);
864 Emit64(Out, (uint64_t) Data.second.st_size);
865 }
866
867 assert(Out.tell() - Start == DataLen && "Wrong data length");
868 }
869};
870} // end anonymous namespace
871
872/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000873void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
874 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000875 // Build the on-disk hash table containing information about every
876 // stat() call.
877 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
878 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000879 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000880 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000881 Stat != StatEnd; ++Stat, ++NumStatEntries) {
882 const char *Filename = Stat->first();
883 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
884 Generator.insert(Filename, Stat->second);
885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000887 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000888 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000889 uint32_t BucketOffset;
890 {
891 llvm::raw_svector_ostream Out(StatCacheData);
892 // Make sure that no bucket is at offset 0
893 clang::io::Emit32(Out, 0);
894 BucketOffset = Generator.Emit(Out);
895 }
896
897 // Create a blob abbreviation
898 using namespace llvm;
899 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
900 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
904 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
905
906 // Write the stat cache
907 RecordData Record;
908 Record.push_back(pch::STAT_CACHE);
909 Record.push_back(BucketOffset);
910 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000911 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000912}
913
914//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000915// Source Manager Serialization
916//===----------------------------------------------------------------------===//
917
918/// \brief Create an abbreviation for the SLocEntry that refers to a
919/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000920static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000921 using namespace llvm;
922 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
923 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000929 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000930}
931
932/// \brief Create an abbreviation for the SLocEntry that refers to a
933/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000934static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000935 using namespace llvm;
936 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
937 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000943 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000944}
945
946/// \brief Create an abbreviation for the SLocEntry that refers to a
947/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000948static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000949 using namespace llvm;
950 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
951 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000953 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000954}
955
956/// \brief Create an abbreviation for the SLocEntry that refers to an
957/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000958static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000959 using namespace llvm;
960 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
961 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000967 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000968}
969
970/// \brief Writes the block containing the serialized form of the
971/// source manager.
972///
973/// TODO: We should probably use an on-disk hash table (stored in a
974/// blob), indexed based on the file name, so that we only create
975/// entries for files that we actually need. In the common case (no
976/// errors), we probably won't have to create file entries for any of
977/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000978void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 const Preprocessor &PP,
980 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000981 RecordData Record;
982
Chris Lattnerf04ad692009-04-10 17:16:57 +0000983 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000984 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000985
986 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000987 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
988 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
989 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
990 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000991
Douglas Gregorbd945002009-04-13 16:31:14 +0000992 // Write the line table.
993 if (SourceMgr.hasLineTable()) {
994 LineTableInfo &LineTable = SourceMgr.getLineTable();
995
996 // Emit the file names
997 Record.push_back(LineTable.getNumFilenames());
998 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
999 // Emit the file name
1000 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001001 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001002 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1003 Record.push_back(FilenameLen);
1004 if (FilenameLen)
1005 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1006 }
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Douglas Gregorbd945002009-04-13 16:31:14 +00001008 // Emit the line entries
1009 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1010 L != LEnd; ++L) {
1011 // Emit the file ID
1012 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Douglas Gregorbd945002009-04-13 16:31:14 +00001014 // Emit the line entries
1015 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001016 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001017 LEEnd = L->second.end();
1018 LE != LEEnd; ++LE) {
1019 Record.push_back(LE->FileOffset);
1020 Record.push_back(LE->LineNo);
1021 Record.push_back(LE->FilenameID);
1022 Record.push_back((unsigned)LE->FileKind);
1023 Record.push_back(LE->IncludeOffset);
1024 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001025 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001026 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001027 }
1028
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001029 // Write out entries for all of the header files we know about.
Mike Stump1eb44332009-09-09 15:08:12 +00001030 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001031 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001032 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001033 E = HS.header_file_end();
1034 I != E; ++I) {
1035 Record.push_back(I->isImport);
1036 Record.push_back(I->DirInfo);
1037 Record.push_back(I->NumIncludes);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001038 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001039 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1040 Record.clear();
1041 }
1042
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001043 // Write out the source location entry table. We skip the first
1044 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001045 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001046 RecordData PreloadSLocs;
1047 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001048 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1049 // Get this source location entry.
1050 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1051
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001052 // Record the offset of this source-location entry.
1053 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1054
1055 // Figure out which record code to use.
1056 unsigned Code;
1057 if (SLoc->isFile()) {
1058 if (SLoc->getFile().getContentCache()->Entry)
1059 Code = pch::SM_SLOC_FILE_ENTRY;
1060 else
1061 Code = pch::SM_SLOC_BUFFER_ENTRY;
1062 } else
1063 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1064 Record.clear();
1065 Record.push_back(Code);
1066
1067 Record.push_back(SLoc->getOffset());
1068 if (SLoc->isFile()) {
1069 const SrcMgr::FileInfo &File = SLoc->getFile();
1070 Record.push_back(File.getIncludeLoc().getRawEncoding());
1071 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1072 Record.push_back(File.hasLineDirectives());
1073
1074 const SrcMgr::ContentCache *Content = File.getContentCache();
1075 if (Content->Entry) {
1076 // The source location entry is a file. The blob associated
1077 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Douglas Gregore650c8c2009-07-07 00:12:59 +00001079 // Turn the file name into an absolute path, if it isn't already.
1080 const char *Filename = Content->Entry->getName();
1081 llvm::sys::Path FilePath(Filename, strlen(Filename));
1082 std::string FilenameStr;
1083 if (!FilePath.isAbsolute()) {
1084 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +00001085 P.appendComponent(FilePath.str());
1086 FilenameStr = P.str();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001087 Filename = FilenameStr.c_str();
1088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Douglas Gregore650c8c2009-07-07 00:12:59 +00001090 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001091 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001092
1093 // FIXME: For now, preload all file source locations, so that
1094 // we get the appropriate File entries in the reader. This is
1095 // a temporary measure.
1096 PreloadSLocs.push_back(SLocEntryOffsets.size());
1097 } else {
1098 // The source location entry is a buffer. The blob associated
1099 // with this entry contains the contents of the buffer.
1100
1101 // We add one to the size so that we capture the trailing NULL
1102 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1103 // the reader side).
1104 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1105 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001106 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1107 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001108 Record.clear();
1109 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1110 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001111 llvm::StringRef(Buffer->getBufferStart(),
1112 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001113
1114 if (strcmp(Name, "<built-in>") == 0)
1115 PreloadSLocs.push_back(SLocEntryOffsets.size());
1116 }
1117 } else {
1118 // The source location entry is an instantiation.
1119 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1120 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1121 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1122 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1123
1124 // Compute the token length for this macro expansion.
1125 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001126 if (I + 1 != N)
1127 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1129 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1130 }
1131 }
1132
Douglas Gregorc9490c02009-04-16 22:23:12 +00001133 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001134
1135 if (SLocEntryOffsets.empty())
1136 return;
1137
1138 // Write the source-location offsets table into the PCH block. This
1139 // table is used for lazily loading source-location information.
1140 using namespace llvm;
1141 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1142 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1146 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001148 Record.clear();
1149 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1150 Record.push_back(SLocEntryOffsets.size());
1151 Record.push_back(SourceMgr.getNextOffset());
1152 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001153 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001154 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001155
1156 // Write the source location entry preloads array, telling the PCH
1157 // reader which source locations entries it should load eagerly.
1158 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001159}
1160
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001161//===----------------------------------------------------------------------===//
1162// Preprocessor Serialization
1163//===----------------------------------------------------------------------===//
1164
Chris Lattner0b1fb982009-04-10 17:15:23 +00001165/// \brief Writes the block containing the serialized form of the
1166/// preprocessor.
1167///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001168void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001169 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001170
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001171 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1172 if (PP.getCounterValue() != 0) {
1173 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001174 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001175 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001176 }
1177
1178 // Enter the preprocessor block.
1179 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001181 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1182 // FIXME: use diagnostics subsystem for localization etc.
1183 if (PP.SawDateOrTime())
1184 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001186 // Loop over all the macro definitions that are live at the end of the file,
1187 // emitting each to the PP section.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001188 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1189 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001190 // FIXME: This emits macros in hash table order, we should do it in a stable
1191 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001192 MacroInfo *MI = I->second;
1193
1194 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1195 // been redefined by the header (in which case they are not isBuiltinMacro).
1196 if (MI->isBuiltinMacro())
1197 continue;
1198
Chris Lattner7356a312009-04-11 21:15:38 +00001199 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001200 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001201 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1202 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001204 unsigned Code;
1205 if (MI->isObjectLike()) {
1206 Code = pch::PP_MACRO_OBJECT_LIKE;
1207 } else {
1208 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001210 Record.push_back(MI->isC99Varargs());
1211 Record.push_back(MI->isGNUVarargs());
1212 Record.push_back(MI->getNumArgs());
1213 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1214 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001215 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001216 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001217 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001218 Record.clear();
1219
Chris Lattnerdf961c22009-04-10 18:08:30 +00001220 // Emit the tokens array.
1221 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1222 // Note that we know that the preprocessor does not have any annotation
1223 // tokens in it because they are created by the parser, and thus can't be
1224 // in a macro definition.
1225 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Chris Lattnerdf961c22009-04-10 18:08:30 +00001227 Record.push_back(Tok.getLocation().getRawEncoding());
1228 Record.push_back(Tok.getLength());
1229
Chris Lattnerdf961c22009-04-10 18:08:30 +00001230 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1231 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001232 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Chris Lattnerdf961c22009-04-10 18:08:30 +00001234 // FIXME: Should translate token kind to a stable encoding.
1235 Record.push_back(Tok.getKind());
1236 // FIXME: Should translate token flags to a stable encoding.
1237 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Douglas Gregorc9490c02009-04-16 22:23:12 +00001239 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001240 Record.clear();
1241 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001242 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001243 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001244 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001245}
1246
Douglas Gregor2e222532009-07-02 17:08:52 +00001247void PCHWriter::WriteComments(ASTContext &Context) {
1248 using namespace llvm;
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Douglas Gregor2e222532009-07-02 17:08:52 +00001250 if (Context.Comments.empty())
1251 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Douglas Gregor2e222532009-07-02 17:08:52 +00001253 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1254 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1255 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1256 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Douglas Gregor2e222532009-07-02 17:08:52 +00001258 RecordData Record;
1259 Record.push_back(pch::COMMENT_RANGES);
Mike Stump1eb44332009-09-09 15:08:12 +00001260 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregor2e222532009-07-02 17:08:52 +00001261 (const char*)&Context.Comments[0],
1262 Context.Comments.size() * sizeof(SourceRange));
1263}
1264
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001265//===----------------------------------------------------------------------===//
1266// Type Serialization
1267//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001268
Douglas Gregor2cf26342009-04-09 22:27:44 +00001269/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001270void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001271 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001272 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001273 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Douglas Gregor2cf26342009-04-09 22:27:44 +00001275 // Record the offset for this type.
1276 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001277 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001278 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1279 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001280 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001281 }
1282
1283 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Douglas Gregor2cf26342009-04-09 22:27:44 +00001285 // Emit the type's representation.
1286 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001287
Douglas Gregora4923eb2009-11-16 21:35:15 +00001288 if (T.hasLocalNonFastQualifiers()) {
1289 Qualifiers Qs = T.getLocalQualifiers();
1290 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001291 Record.push_back(Qs.getAsOpaqueValue());
1292 W.Code = pch::TYPE_EXT_QUAL;
1293 } else {
1294 switch (T->getTypeClass()) {
1295 // For all of the concrete, non-dependent types, call the
1296 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001297#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001298 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001299#define ABSTRACT_TYPE(Class, Base)
1300#define DEPENDENT_TYPE(Class, Base)
1301#include "clang/AST/TypeNodes.def"
1302
John McCall0953e762009-09-24 19:53:00 +00001303 // For all of the dependent type nodes (which only occur in C++
1304 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001305#define TYPE(Class, Base)
1306#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1307#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001308 assert(false && "Cannot serialize dependent type nodes");
1309 break;
1310 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001311 }
1312
1313 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001314 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001315
1316 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001317 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001318}
1319
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001320//===----------------------------------------------------------------------===//
1321// Declaration Serialization
1322//===----------------------------------------------------------------------===//
1323
Douglas Gregor2cf26342009-04-09 22:27:44 +00001324/// \brief Write the block containing all of the declaration IDs
1325/// lexically declared within the given DeclContext.
1326///
1327/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1328/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001329uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001330 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001331 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001332 return 0;
1333
Douglas Gregorc9490c02009-04-16 22:23:12 +00001334 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001335 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001336 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1337 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001338 AddDeclRef(*D, Record);
1339
Douglas Gregor25123082009-04-22 22:34:57 +00001340 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001341 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001342 return Offset;
1343}
1344
1345/// \brief Write the block containing all of the declaration IDs
1346/// visible from the given DeclContext.
1347///
1348/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1349/// bistream, or 0 if no block was written.
1350uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1351 DeclContext *DC) {
1352 if (DC->getPrimaryContext() != DC)
1353 return 0;
1354
Douglas Gregoraff22df2009-04-21 22:32:33 +00001355 // Since there is no name lookup into functions or methods, and we
1356 // perform name lookup for the translation unit via the
1357 // IdentifierInfo chains, don't bother to build a
1358 // visible-declarations table for these entities.
1359 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001360 return 0;
1361
Douglas Gregor2cf26342009-04-09 22:27:44 +00001362 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001363 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001364
1365 // Serialize the contents of the mapping used for lookup. Note that,
1366 // although we have two very different code paths, the serialized
1367 // representation is the same for both cases: a declaration name,
1368 // followed by a size, followed by references to the visible
1369 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001370 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001371 RecordData Record;
1372 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001373 if (!Map)
1374 return 0;
1375
Douglas Gregor2cf26342009-04-09 22:27:44 +00001376 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1377 D != DEnd; ++D) {
1378 AddDeclarationName(D->first, Record);
1379 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1380 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001381 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001382 AddDeclRef(*Result.first, Record);
1383 }
1384
1385 if (Record.size() == 0)
1386 return 0;
1387
Douglas Gregorc9490c02009-04-16 22:23:12 +00001388 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001389 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001390 return Offset;
1391}
1392
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001393//===----------------------------------------------------------------------===//
1394// Global Method Pool and Selector Serialization
1395//===----------------------------------------------------------------------===//
1396
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001397namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001398// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001399class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001400 PCHWriter &Writer;
1401
1402public:
1403 typedef Selector key_type;
1404 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001406 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1407 typedef const data_type& data_type_ref;
1408
1409 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001411 static unsigned ComputeHash(Selector Sel) {
1412 unsigned N = Sel.getNumArgs();
1413 if (N == 0)
1414 ++N;
1415 unsigned R = 5381;
1416 for (unsigned I = 0; I != N; ++I)
1417 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001418 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001419 return R;
1420 }
Mike Stump1eb44332009-09-09 15:08:12 +00001421
1422 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001423 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1424 data_type_ref Methods) {
1425 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1426 clang::io::Emit16(Out, KeyLen);
1427 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001428 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001429 Method = Method->Next)
1430 if (Method->Method)
1431 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001432 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001433 Method = Method->Next)
1434 if (Method->Method)
1435 DataLen += 4;
1436 clang::io::Emit16(Out, DataLen);
1437 return std::make_pair(KeyLen, DataLen);
1438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Douglas Gregor83941df2009-04-25 17:48:32 +00001440 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001441 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001442 assert((Start >> 32) == 0 && "Selector key offset too large");
1443 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001444 unsigned N = Sel.getNumArgs();
1445 clang::io::Emit16(Out, N);
1446 if (N == 0)
1447 N = 1;
1448 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001449 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001450 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1451 }
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001453 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001454 data_type_ref Methods, unsigned DataLen) {
1455 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001456 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001457 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001458 Method = Method->Next)
1459 if (Method->Method)
1460 ++NumInstanceMethods;
1461
1462 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001463 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001464 Method = Method->Next)
1465 if (Method->Method)
1466 ++NumFactoryMethods;
1467
1468 clang::io::Emit16(Out, NumInstanceMethods);
1469 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001470 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001471 Method = Method->Next)
1472 if (Method->Method)
1473 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001474 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001475 Method = Method->Next)
1476 if (Method->Method)
1477 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001478
1479 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001480 }
1481};
1482} // end anonymous namespace
1483
1484/// \brief Write the method pool into the PCH file.
1485///
1486/// The method pool contains both instance and factory methods, stored
1487/// in an on-disk hash table indexed by the selector.
1488void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1489 using namespace llvm;
1490
1491 // Create and write out the blob that contains the instance and
1492 // factor method pools.
1493 bool Empty = true;
1494 {
1495 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001497 // Create the on-disk hash table representation. Start by
1498 // iterating through the instance method pool.
1499 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001500 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001501 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001502 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001503 InstanceEnd = SemaRef.InstanceMethodPool.end();
1504 Instance != InstanceEnd; ++Instance) {
1505 // Check whether there is a factory method with the same
1506 // selector.
1507 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1508 = SemaRef.FactoryMethodPool.find(Instance->first);
1509
1510 if (Factory == SemaRef.FactoryMethodPool.end())
1511 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001512 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001513 ObjCMethodList()));
1514 else
1515 Generator.insert(Instance->first,
1516 std::make_pair(Instance->second, Factory->second));
1517
Douglas Gregor83941df2009-04-25 17:48:32 +00001518 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001519 Empty = false;
1520 }
1521
1522 // Now iterate through the factory method pool, to pick up any
1523 // selectors that weren't already in the instance method pool.
1524 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001525 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001526 FactoryEnd = SemaRef.FactoryMethodPool.end();
1527 Factory != FactoryEnd; ++Factory) {
1528 // Check whether there is an instance method with the same
1529 // selector. If so, there is no work to do here.
1530 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1531 = SemaRef.InstanceMethodPool.find(Factory->first);
1532
Douglas Gregor83941df2009-04-25 17:48:32 +00001533 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001534 Generator.insert(Factory->first,
1535 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001536 ++NumSelectorsInMethodPool;
1537 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001538
1539 Empty = false;
1540 }
1541
Douglas Gregor83941df2009-04-25 17:48:32 +00001542 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001543 return;
1544
1545 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001546 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001547 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001548 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001549 {
1550 PCHMethodPoolTrait Trait(*this);
1551 llvm::raw_svector_ostream Out(MethodPool);
1552 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001553 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001554 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001555
1556 // For every selector that we have seen but which was not
1557 // written into the hash table, write the selector itself and
1558 // record it's offset.
1559 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1560 if (SelectorOffsets[I] == 0)
1561 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001562 }
1563
1564 // Create a blob abbreviation
1565 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1566 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1570 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1571
Douglas Gregor83941df2009-04-25 17:48:32 +00001572 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001573 RecordData Record;
1574 Record.push_back(pch::METHOD_POOL);
1575 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001576 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001577 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001578
1579 // Create a blob abbreviation for the selector table offsets.
1580 Abbrev = new BitCodeAbbrev();
1581 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1584 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1585
1586 // Write the selector offsets table.
1587 Record.clear();
1588 Record.push_back(pch::SELECTOR_OFFSETS);
1589 Record.push_back(SelectorOffsets.size());
1590 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1591 (const char *)&SelectorOffsets.front(),
1592 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001593 }
1594}
1595
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001596//===----------------------------------------------------------------------===//
1597// Identifier Table Serialization
1598//===----------------------------------------------------------------------===//
1599
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001601class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001602 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001603 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001604
Douglas Gregora92193e2009-04-28 21:18:29 +00001605 /// \brief Determines whether this is an "interesting" identifier
1606 /// that needs a full IdentifierInfo structure written into the hash
1607 /// table.
1608 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1609 return II->isPoisoned() ||
1610 II->isExtensionToken() ||
1611 II->hasMacroDefinition() ||
1612 II->getObjCOrBuiltinID() ||
1613 II->getFETokenInfo<void>();
1614 }
1615
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001616public:
1617 typedef const IdentifierInfo* key_type;
1618 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001620 typedef pch::IdentID data_type;
1621 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001622
1623 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001624 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001625
1626 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001627 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001628 }
Mike Stump1eb44332009-09-09 15:08:12 +00001629
1630 std::pair<unsigned,unsigned>
1631 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001632 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001633 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001634 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1635 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001636 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001637 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001638 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001639 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001640 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1641 DEnd = IdentifierResolver::end();
1642 D != DEnd; ++D)
1643 DataLen += sizeof(pch::DeclID);
1644 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001645 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001646 // We emit the key length after the data length so that every
1647 // string is preceded by a 16-bit length. This matches the PTH
1648 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001649 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001650 return std::make_pair(KeyLen, DataLen);
1651 }
Mike Stump1eb44332009-09-09 15:08:12 +00001652
1653 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001654 unsigned KeyLen) {
1655 // Record the location of the key data. This is used when generating
1656 // the mapping from persistent IDs to strings.
1657 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001658 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001659 }
Mike Stump1eb44332009-09-09 15:08:12 +00001660
1661 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001662 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001663 if (!isInterestingIdentifier(II)) {
1664 clang::io::Emit32(Out, ID << 1);
1665 return;
1666 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001667
Douglas Gregora92193e2009-04-28 21:18:29 +00001668 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001669 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001670 bool hasMacroDefinition =
1671 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001672 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001673 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001674 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1675 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1676 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1677 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001678 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001679
Douglas Gregor37e26842009-04-21 23:56:24 +00001680 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001681 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001682
Douglas Gregor668c1a42009-04-21 22:25:48 +00001683 // Emit the declaration IDs in reverse order, because the
1684 // IdentifierResolver provides the declarations as they would be
1685 // visible (e.g., the function "stat" would come before the struct
1686 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1687 // adds declarations to the end of the list (so we need to see the
1688 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001689 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001690 IdentifierResolver::end());
1691 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1692 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001693 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001694 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001695 }
1696};
1697} // end anonymous namespace
1698
Douglas Gregorafaf3082009-04-11 00:14:32 +00001699/// \brief Write the identifier table into the PCH file.
1700///
1701/// The identifier table consists of a blob containing string data
1702/// (the actual identifiers themselves) and a separate "offsets" index
1703/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001704void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001705 using namespace llvm;
1706
1707 // Create and write out the blob that contains the identifier
1708 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001709 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001710 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Douglas Gregor92b059e2009-04-28 20:33:11 +00001712 // Look for any identifiers that were named while processing the
1713 // headers, but are otherwise not needed. We add these to the hash
1714 // table to enable checking of the predefines buffer in the case
1715 // where the user adds new macro definitions when building the PCH
1716 // file.
1717 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1718 IDEnd = PP.getIdentifierTable().end();
1719 ID != IDEnd; ++ID)
1720 getIdentifierRef(ID->second);
1721
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001722 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001723 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001724 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1725 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1726 ID != IDEnd; ++ID) {
1727 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001728 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001729 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001730
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001731 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001732 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001733 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001734 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001735 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001736 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001737 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001738 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001739 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001740 }
1741
1742 // Create a blob abbreviation
1743 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1744 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001747 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001748
1749 // Write the identifier table
1750 RecordData Record;
1751 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001752 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001753 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001754 }
1755
1756 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001757 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1758 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1761 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1762
1763 RecordData Record;
1764 Record.push_back(pch::IDENTIFIER_OFFSET);
1765 Record.push_back(IdentifierOffsets.size());
1766 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1767 (const char *)&IdentifierOffsets.front(),
1768 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001769}
1770
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001771//===----------------------------------------------------------------------===//
1772// General Serialization Routines
1773//===----------------------------------------------------------------------===//
1774
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001775/// \brief Write a record containing the given attributes.
1776void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1777 RecordData Record;
1778 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001779 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001780 Record.push_back(Attr->isInherited());
1781 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001782 default:
1783 assert(0 && "Does not support PCH writing for this attribute yet!");
1784 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001785 case Attr::Alias:
1786 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1787 break;
1788
1789 case Attr::Aligned:
1790 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1791 break;
1792
1793 case Attr::AlwaysInline:
1794 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001796 case Attr::AnalyzerNoReturn:
1797 break;
1798
1799 case Attr::Annotate:
1800 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1801 break;
1802
1803 case Attr::AsmLabel:
1804 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1805 break;
1806
Sean Hunt7725e672009-11-25 04:20:27 +00001807 case Attr::BaseCheck:
1808 break;
1809
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001810 case Attr::Blocks:
1811 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1812 break;
1813
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001814 case Attr::CDecl:
1815 break;
1816
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001817 case Attr::Cleanup:
1818 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1819 break;
1820
1821 case Attr::Const:
1822 break;
1823
1824 case Attr::Constructor:
1825 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1826 break;
1827
1828 case Attr::DLLExport:
1829 case Attr::DLLImport:
1830 case Attr::Deprecated:
1831 break;
1832
1833 case Attr::Destructor:
1834 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1835 break;
1836
1837 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001838 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001839 break;
1840
1841 case Attr::Format: {
1842 const FormatAttr *Format = cast<FormatAttr>(Attr);
1843 AddString(Format->getType(), Record);
1844 Record.push_back(Format->getFormatIdx());
1845 Record.push_back(Format->getFirstArg());
1846 break;
1847 }
1848
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001849 case Attr::FormatArg: {
1850 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1851 Record.push_back(Format->getFormatIdx());
1852 break;
1853 }
1854
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001855 case Attr::Sentinel : {
1856 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1857 Record.push_back(Sentinel->getSentinel());
1858 Record.push_back(Sentinel->getNullPos());
1859 break;
1860 }
Mike Stump1eb44332009-09-09 15:08:12 +00001861
Chris Lattnercf2a7212009-04-20 19:12:28 +00001862 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001863 case Attr::Hiding:
Ted Kremenekefbddd22010-02-17 02:37:45 +00001864 case Attr::IBActionKind:
Ted Kremenek47e69902010-02-18 00:05:52 +00001865 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001866 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001867 case Attr::NoDebug:
Ted Kremenek47e69902010-02-18 00:05:52 +00001868 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001869 case Attr::NoReturn:
1870 case Attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001871 break;
1872
1873 case Attr::NonNull: {
1874 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1875 Record.push_back(NonNull->size());
1876 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1877 break;
1878 }
1879
Ted Kremenek31c780d2010-02-18 00:05:45 +00001880 case Attr::CFReturnsNotRetained:
1881 case Attr::CFReturnsRetained:
1882 case Attr::NSReturnsNotRetained:
1883 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001884 case Attr::ObjCException:
1885 case Attr::ObjCNSObject:
1886 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001887 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001888 break;
1889
Anders Carlssona860e752009-08-08 18:23:56 +00001890 case Attr::PragmaPack:
1891 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001892 break;
1893
Anders Carlssona860e752009-08-08 18:23:56 +00001894 case Attr::Packed:
1895 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001897 case Attr::Pure:
1898 break;
1899
1900 case Attr::Regparm:
1901 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1902 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Nate Begeman6f3d8382009-06-26 06:32:41 +00001904 case Attr::ReqdWorkGroupSize:
1905 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1906 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1907 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1908 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001909
1910 case Attr::Section:
1911 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1912 break;
1913
1914 case Attr::StdCall:
1915 case Attr::TransparentUnion:
1916 case Attr::Unavailable:
1917 case Attr::Unused:
1918 case Attr::Used:
1919 break;
1920
1921 case Attr::Visibility:
1922 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001923 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001924 break;
1925
1926 case Attr::WarnUnusedResult:
1927 case Attr::Weak:
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001928 case Attr::WeakRef:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001929 case Attr::WeakImport:
1930 break;
1931 }
1932 }
1933
Douglas Gregorc9490c02009-04-16 22:23:12 +00001934 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001935}
1936
1937void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1938 Record.push_back(Str.size());
1939 Record.insert(Record.end(), Str.begin(), Str.end());
1940}
1941
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001942/// \brief Note that the identifier II occurs at the given offset
1943/// within the identifier table.
1944void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001945 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001946}
1947
Douglas Gregor83941df2009-04-25 17:48:32 +00001948/// \brief Note that the selector Sel occurs at the given offset
1949/// within the method pool/selector table.
1950void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1951 unsigned ID = SelectorIDs[Sel];
1952 assert(ID && "Unknown selector");
1953 SelectorOffsets[ID - 1] = Offset;
1954}
1955
Mike Stump1eb44332009-09-09 15:08:12 +00001956PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1957 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00001958 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1959 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001960
Douglas Gregore650c8c2009-07-07 00:12:59 +00001961void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1962 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001963 using namespace llvm;
1964
Douglas Gregore7785042009-04-20 15:53:59 +00001965 ASTContext &Context = SemaRef.Context;
1966 Preprocessor &PP = SemaRef.PP;
1967
Douglas Gregor2cf26342009-04-09 22:27:44 +00001968 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001969 Stream.Emit((unsigned)'C', 8);
1970 Stream.Emit((unsigned)'P', 8);
1971 Stream.Emit((unsigned)'C', 8);
1972 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001974 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001975
1976 // The translation unit is the first declaration we'll emit.
1977 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001978 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001979
Douglas Gregor2deaea32009-04-22 18:49:13 +00001980 // Make sure that we emit IdentifierInfos (and any attached
1981 // declarations) for builtins.
1982 {
1983 IdentifierTable &Table = PP.getIdentifierTable();
1984 llvm::SmallVector<const char *, 32> BuiltinNames;
1985 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1986 Context.getLangOptions().NoBuiltin);
1987 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1988 getIdentifierRef(&Table.get(BuiltinNames[I]));
1989 }
1990
Chris Lattner63d65f82009-09-08 18:19:27 +00001991 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00001992 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00001993 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001994 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001995 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
1996 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00001997 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001998
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001999 // Build a record containing all of the static unused functions in this file.
2000 RecordData UnusedStaticFuncs;
2001 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
2002 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
2003
Douglas Gregor14c22f22009-04-22 22:18:58 +00002004 // Build a record containing all of the locally-scoped external
2005 // declarations in this header file. Generally, this record will be
2006 // empty.
2007 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002008 // FIXME: This is filling in the PCH file in densemap order which is
2009 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002010 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002011 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2012 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2013 TD != TDEnd; ++TD)
2014 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2015
Douglas Gregorb81c1702009-04-27 20:06:05 +00002016 // Build a record containing all of the ext_vector declarations.
2017 RecordData ExtVectorDecls;
2018 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2019 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2020
Douglas Gregor2cf26342009-04-09 22:27:44 +00002021 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002022 RecordData Record;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002023 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002024 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002025 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002026 if (StatCalls && !isysroot)
2027 WriteStatCache(*StatCalls, isysroot);
2028 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump1eb44332009-09-09 15:08:12 +00002029 WriteComments(Context);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002030 // Write the record of special types.
2031 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002032
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002033 AddTypeRef(Context.getBuiltinVaListType(), Record);
2034 AddTypeRef(Context.getObjCIdType(), Record);
2035 AddTypeRef(Context.getObjCSelType(), Record);
2036 AddTypeRef(Context.getObjCProtoType(), Record);
2037 AddTypeRef(Context.getObjCClassType(), Record);
2038 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2039 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2040 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002041 AddTypeRef(Context.getjmp_bufType(), Record);
2042 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002043 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2044 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002045#if 0
2046 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00002047 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002048#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00002049 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002050 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002051 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Douglas Gregor366809a2009-04-26 03:49:13 +00002053 // Keep writing types and declarations until all types and
2054 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002055 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2056 WriteDeclsBlockAbbrevs();
2057 while (!DeclTypesToEmit.empty()) {
2058 DeclOrType DOT = DeclTypesToEmit.front();
2059 DeclTypesToEmit.pop();
2060 if (DOT.isType())
2061 WriteType(DOT.getType());
2062 else
2063 WriteDecl(Context, DOT.getDecl());
2064 }
2065 Stream.ExitBlock();
2066
Douglas Gregor813a97b2009-10-17 17:25:45 +00002067 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002068 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002069 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002070
2071 // Write the type offsets array
2072 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2073 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2076 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2077 Record.clear();
2078 Record.push_back(pch::TYPE_OFFSET);
2079 Record.push_back(TypeOffsets.size());
2080 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002081 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002082 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002083
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002084 // Write the declaration offsets array
2085 Abbrev = new BitCodeAbbrev();
2086 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2089 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2090 Record.clear();
2091 Record.push_back(pch::DECL_OFFSET);
2092 Record.push_back(DeclOffsets.size());
2093 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002094 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002095 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002096
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002097 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002098 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002099 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002100
2101 // Write the record containing tentative definitions.
2102 if (!TentativeDefinitions.empty())
2103 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002104
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002105 // Write the record containing unused static functions.
2106 if (!UnusedStaticFuncs.empty())
2107 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
2108
Douglas Gregor14c22f22009-04-22 22:18:58 +00002109 // Write the record containing locally-scoped external definitions.
2110 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002111 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002112 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002113
2114 // Write the record containing ext_vector type names.
2115 if (!ExtVectorDecls.empty())
2116 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Douglas Gregor3e1af842009-04-17 22:13:46 +00002118 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002119 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002120 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002121 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002122 Record.push_back(NumLexicalDeclContexts);
2123 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002124 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002125 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002126}
2127
2128void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2129 Record.push_back(Loc.getRawEncoding());
2130}
2131
2132void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2133 Record.push_back(Value.getBitWidth());
2134 unsigned N = Value.getNumWords();
2135 const uint64_t* Words = Value.getRawData();
2136 for (unsigned I = 0; I != N; ++I)
2137 Record.push_back(Words[I]);
2138}
2139
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002140void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2141 Record.push_back(Value.isUnsigned());
2142 AddAPInt(Value, Record);
2143}
2144
Douglas Gregor17fc2232009-04-14 21:55:33 +00002145void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2146 AddAPInt(Value.bitcastToAPInt(), Record);
2147}
2148
Douglas Gregor2cf26342009-04-09 22:27:44 +00002149void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002150 Record.push_back(getIdentifierRef(II));
2151}
2152
2153pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2154 if (II == 0)
2155 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002156
2157 pch::IdentID &ID = IdentifierIDs[II];
2158 if (ID == 0)
2159 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002160 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002161}
2162
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002163void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2164 if (SelRef.getAsOpaquePtr() == 0) {
2165 Record.push_back(0);
2166 return;
2167 }
2168
2169 pch::SelectorID &SID = SelectorIDs[SelRef];
2170 if (SID == 0) {
2171 SID = SelectorIDs.size();
2172 SelVector.push_back(SelRef);
2173 }
2174 Record.push_back(SID);
2175}
2176
John McCall833ca992009-10-29 08:12:44 +00002177void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2178 RecordData &Record) {
2179 switch (Arg.getArgument().getKind()) {
2180 case TemplateArgument::Expression:
2181 AddStmt(Arg.getLocInfo().getAsExpr());
2182 break;
2183 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002184 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002185 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002186 case TemplateArgument::Template:
2187 Record.push_back(
2188 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2189 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2190 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2191 break;
John McCall833ca992009-10-29 08:12:44 +00002192 case TemplateArgument::Null:
2193 case TemplateArgument::Integral:
2194 case TemplateArgument::Declaration:
2195 case TemplateArgument::Pack:
2196 break;
2197 }
2198}
2199
John McCalla93c9342009-12-07 02:54:59 +00002200void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2201 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002202 AddTypeRef(QualType(), Record);
2203 return;
2204 }
2205
John McCalla93c9342009-12-07 02:54:59 +00002206 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002207 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002208 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002209 TLW.Visit(TL);
2210}
2211
Douglas Gregor2cf26342009-04-09 22:27:44 +00002212void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2213 if (T.isNull()) {
2214 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2215 return;
2216 }
2217
Douglas Gregora4923eb2009-11-16 21:35:15 +00002218 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002219 T.removeFastQualifiers();
2220
Douglas Gregora4923eb2009-11-16 21:35:15 +00002221 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002222 pch::TypeID &ID = TypeIDs[T];
2223 if (ID == 0) {
2224 // We haven't seen these qualifiers applied to this type before.
2225 // Assign it a new ID. This is the only time we enqueue a
2226 // qualified type, and it has no CV qualifiers.
2227 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002228 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002229 }
2230
2231 // Encode the type qualifiers in the type reference.
2232 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2233 return;
2234 }
2235
Douglas Gregora4923eb2009-11-16 21:35:15 +00002236 assert(!T.hasLocalQualifiers());
John McCall0953e762009-09-24 19:53:00 +00002237
Douglas Gregor2cf26342009-04-09 22:27:44 +00002238 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002239 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002240 switch (BT->getKind()) {
2241 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2242 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2243 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2244 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2245 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2246 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2247 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2248 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002249 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002250 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2251 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2252 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2253 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2254 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2255 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2256 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002257 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002258 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2259 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2260 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002261 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002262 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2263 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002264 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2265 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002266 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2267 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002268 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002269 case BuiltinType::UndeducedAuto:
2270 assert(0 && "Should not see undeduced auto here");
2271 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002272 }
2273
John McCall0953e762009-09-24 19:53:00 +00002274 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002275 return;
2276 }
2277
John McCall0953e762009-09-24 19:53:00 +00002278 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002279 if (ID == 0) {
2280 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002281 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002282 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002283 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002284 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002285
2286 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002287 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002288}
2289
2290void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2291 if (D == 0) {
2292 Record.push_back(0);
2293 return;
2294 }
2295
Douglas Gregor8038d512009-04-10 17:25:41 +00002296 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002297 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002298 // We haven't seen this declaration before. Give it a new ID and
2299 // enqueue it in the list of declarations to emit.
2300 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002301 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002302 }
2303
2304 Record.push_back(ID);
2305}
2306
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002307pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2308 if (D == 0)
2309 return 0;
2310
2311 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2312 return DeclIDs[D];
2313}
2314
Douglas Gregor2cf26342009-04-09 22:27:44 +00002315void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002316 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002317 Record.push_back(Name.getNameKind());
2318 switch (Name.getNameKind()) {
2319 case DeclarationName::Identifier:
2320 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2321 break;
2322
2323 case DeclarationName::ObjCZeroArgSelector:
2324 case DeclarationName::ObjCOneArgSelector:
2325 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002326 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327 break;
2328
2329 case DeclarationName::CXXConstructorName:
2330 case DeclarationName::CXXDestructorName:
2331 case DeclarationName::CXXConversionFunctionName:
2332 AddTypeRef(Name.getCXXNameType(), Record);
2333 break;
2334
2335 case DeclarationName::CXXOperatorName:
2336 Record.push_back(Name.getCXXOverloadedOperator());
2337 break;
2338
Sean Hunt3e518bd2009-11-29 07:34:05 +00002339 case DeclarationName::CXXLiteralOperatorName:
2340 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2341 break;
2342
Douglas Gregor2cf26342009-04-09 22:27:44 +00002343 case DeclarationName::CXXUsingDirective:
2344 // No extra data to emit
2345 break;
2346 }
2347}