blob: ba406c83afe60e75930dbc52cafc95a78c7afbf9 [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);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +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());
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000714 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000715
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000716 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000717 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000718 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000719 RecordData Record;
720 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000721 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000722 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000723
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000724 // Repository branch/version information.
725 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
726 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
727 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
728 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000729 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000730 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000731 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
732 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000733}
734
735/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000736void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
737 RecordData Record;
738 Record.push_back(LangOpts.Trigraphs);
739 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
740 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
741 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
742 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
743 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
744 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
745 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
746 Record.push_back(LangOpts.C99); // C99 Support
747 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
748 Record.push_back(LangOpts.CPlusPlus); // C++ Support
749 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000750 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000752 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
753 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000754 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000755 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000756 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000757 // modern abi enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000759 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000760 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
761 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000762 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000763 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000764 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000765
766 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
767 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
768 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
769
Chris Lattnerea5ce472009-04-27 07:35:58 +0000770 // Whether static initializers are protected by locks.
771 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000772 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000773 Record.push_back(LangOpts.Blocks); // block extension to C
774 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
775 // they are unused.
776 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
777 // (modulo the platform support).
778
779 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
780 // signed integer arithmetic overflows.
781
782 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
783 // may be ripped out at any time.
784
785 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000786 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000787 // defined.
788 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
789 // opposed to __DYNAMIC__).
790 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
791
792 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
793 // used (instead of C99 semantics).
794 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000795 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
796 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000797 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
798 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000799 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000800 Record.push_back(LangOpts.getGCMode());
801 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000802 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000803 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000804 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000805 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000806 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000807 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000808}
809
Douglas Gregor14f79002009-04-10 03:52:48 +0000810//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000811// stat cache Serialization
812//===----------------------------------------------------------------------===//
813
814namespace {
815// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000816class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000817public:
818 typedef const char * key_type;
819 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000821 typedef std::pair<int, struct stat> data_type;
822 typedef const data_type& data_type_ref;
823
824 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000825 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
828 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000829 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
830 data_type_ref Data) {
831 unsigned StrLen = strlen(path);
832 clang::io::Emit16(Out, StrLen);
833 unsigned DataLen = 1; // result value
834 if (Data.first == 0)
835 DataLen += 4 + 4 + 2 + 8 + 8;
836 clang::io::Emit8(Out, DataLen);
837 return std::make_pair(StrLen + 1, DataLen);
838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000840 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
841 Out.write(path, KeyLen);
842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000844 void EmitData(llvm::raw_ostream& Out, key_type_ref,
845 data_type_ref Data, unsigned DataLen) {
846 using namespace clang::io;
847 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000849 // Result of stat()
850 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000852 if (Data.first == 0) {
853 Emit32(Out, (uint32_t) Data.second.st_ino);
854 Emit32(Out, (uint32_t) Data.second.st_dev);
855 Emit16(Out, (uint16_t) Data.second.st_mode);
856 Emit64(Out, (uint64_t) Data.second.st_mtime);
857 Emit64(Out, (uint64_t) Data.second.st_size);
858 }
859
860 assert(Out.tell() - Start == DataLen && "Wrong data length");
861 }
862};
863} // end anonymous namespace
864
865/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000866void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
867 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000868 // Build the on-disk hash table containing information about every
869 // stat() call.
870 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
871 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000872 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000873 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000874 Stat != StatEnd; ++Stat, ++NumStatEntries) {
875 const char *Filename = Stat->first();
876 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
877 Generator.insert(Filename, Stat->second);
878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000880 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000881 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000882 uint32_t BucketOffset;
883 {
884 llvm::raw_svector_ostream Out(StatCacheData);
885 // Make sure that no bucket is at offset 0
886 clang::io::Emit32(Out, 0);
887 BucketOffset = Generator.Emit(Out);
888 }
889
890 // Create a blob abbreviation
891 using namespace llvm;
892 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
893 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
897 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
898
899 // Write the stat cache
900 RecordData Record;
901 Record.push_back(pch::STAT_CACHE);
902 Record.push_back(BucketOffset);
903 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000904 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000905}
906
907//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000908// Source Manager Serialization
909//===----------------------------------------------------------------------===//
910
911/// \brief Create an abbreviation for the SLocEntry that refers to a
912/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000913static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000914 using namespace llvm;
915 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
916 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000922 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000923}
924
925/// \brief Create an abbreviation for the SLocEntry that refers to a
926/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000927static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000928 using namespace llvm;
929 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
930 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000936 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000937}
938
939/// \brief Create an abbreviation for the SLocEntry that refers to a
940/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000941static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000942 using namespace llvm;
943 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
944 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000946 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000947}
948
949/// \brief Create an abbreviation for the SLocEntry that refers to an
950/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000951static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000952 using namespace llvm;
953 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
954 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000960 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000961}
962
963/// \brief Writes the block containing the serialized form of the
964/// source manager.
965///
966/// TODO: We should probably use an on-disk hash table (stored in a
967/// blob), indexed based on the file name, so that we only create
968/// entries for files that we actually need. In the common case (no
969/// errors), we probably won't have to create file entries for any of
970/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000971void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 const Preprocessor &PP,
973 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000974 RecordData Record;
975
Chris Lattnerf04ad692009-04-10 17:16:57 +0000976 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000977 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000978
979 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000980 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
981 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
982 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
983 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000984
Douglas Gregorbd945002009-04-13 16:31:14 +0000985 // Write the line table.
986 if (SourceMgr.hasLineTable()) {
987 LineTableInfo &LineTable = SourceMgr.getLineTable();
988
989 // Emit the file names
990 Record.push_back(LineTable.getNumFilenames());
991 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
992 // Emit the file name
993 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000994 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +0000995 unsigned FilenameLen = Filename? strlen(Filename) : 0;
996 Record.push_back(FilenameLen);
997 if (FilenameLen)
998 Record.insert(Record.end(), Filename, Filename + FilenameLen);
999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Douglas Gregorbd945002009-04-13 16:31:14 +00001001 // Emit the line entries
1002 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1003 L != LEnd; ++L) {
1004 // Emit the file ID
1005 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Douglas Gregorbd945002009-04-13 16:31:14 +00001007 // Emit the line entries
1008 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001009 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001010 LEEnd = L->second.end();
1011 LE != LEEnd; ++LE) {
1012 Record.push_back(LE->FileOffset);
1013 Record.push_back(LE->LineNo);
1014 Record.push_back(LE->FilenameID);
1015 Record.push_back((unsigned)LE->FileKind);
1016 Record.push_back(LE->IncludeOffset);
1017 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001018 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001019 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001020 }
1021
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001022 // Write out entries for all of the header files we know about.
Mike Stump1eb44332009-09-09 15:08:12 +00001023 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001024 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001025 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001026 E = HS.header_file_end();
1027 I != E; ++I) {
1028 Record.push_back(I->isImport);
1029 Record.push_back(I->DirInfo);
1030 Record.push_back(I->NumIncludes);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001031 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001032 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1033 Record.clear();
1034 }
1035
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001036 // Write out the source location entry table. We skip the first
1037 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001038 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001039 RecordData PreloadSLocs;
1040 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001041 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1042 // Get this source location entry.
1043 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001044
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001045 // Record the offset of this source-location entry.
1046 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1047
1048 // Figure out which record code to use.
1049 unsigned Code;
1050 if (SLoc->isFile()) {
1051 if (SLoc->getFile().getContentCache()->Entry)
1052 Code = pch::SM_SLOC_FILE_ENTRY;
1053 else
1054 Code = pch::SM_SLOC_BUFFER_ENTRY;
1055 } else
1056 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1057 Record.clear();
1058 Record.push_back(Code);
1059
1060 Record.push_back(SLoc->getOffset());
1061 if (SLoc->isFile()) {
1062 const SrcMgr::FileInfo &File = SLoc->getFile();
1063 Record.push_back(File.getIncludeLoc().getRawEncoding());
1064 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1065 Record.push_back(File.hasLineDirectives());
1066
1067 const SrcMgr::ContentCache *Content = File.getContentCache();
1068 if (Content->Entry) {
1069 // The source location entry is a file. The blob associated
1070 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Douglas Gregore650c8c2009-07-07 00:12:59 +00001072 // Turn the file name into an absolute path, if it isn't already.
1073 const char *Filename = Content->Entry->getName();
1074 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001075 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001076 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Douglas Gregore650c8c2009-07-07 00:12:59 +00001078 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001079 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001080
1081 // FIXME: For now, preload all file source locations, so that
1082 // we get the appropriate File entries in the reader. This is
1083 // a temporary measure.
1084 PreloadSLocs.push_back(SLocEntryOffsets.size());
1085 } else {
1086 // The source location entry is a buffer. The blob associated
1087 // with this entry contains the contents of the buffer.
1088
1089 // We add one to the size so that we capture the trailing NULL
1090 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1091 // the reader side).
1092 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1093 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001094 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1095 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001096 Record.clear();
1097 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1098 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001099 llvm::StringRef(Buffer->getBufferStart(),
1100 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001101
1102 if (strcmp(Name, "<built-in>") == 0)
1103 PreloadSLocs.push_back(SLocEntryOffsets.size());
1104 }
1105 } else {
1106 // The source location entry is an instantiation.
1107 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1108 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1109 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1110 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1111
1112 // Compute the token length for this macro expansion.
1113 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001114 if (I + 1 != N)
1115 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001116 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1117 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1118 }
1119 }
1120
Douglas Gregorc9490c02009-04-16 22:23:12 +00001121 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001122
1123 if (SLocEntryOffsets.empty())
1124 return;
1125
1126 // Write the source-location offsets table into the PCH block. This
1127 // table is used for lazily loading source-location information.
1128 using namespace llvm;
1129 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1130 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1131 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1134 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001136 Record.clear();
1137 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1138 Record.push_back(SLocEntryOffsets.size());
1139 Record.push_back(SourceMgr.getNextOffset());
1140 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001141 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001142 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001143
1144 // Write the source location entry preloads array, telling the PCH
1145 // reader which source locations entries it should load eagerly.
1146 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001147}
1148
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001149//===----------------------------------------------------------------------===//
1150// Preprocessor Serialization
1151//===----------------------------------------------------------------------===//
1152
Chris Lattner0b1fb982009-04-10 17:15:23 +00001153/// \brief Writes the block containing the serialized form of the
1154/// preprocessor.
1155///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001156void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001157 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001158
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001159 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1160 if (PP.getCounterValue() != 0) {
1161 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001162 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001163 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001164 }
1165
1166 // Enter the preprocessor block.
1167 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001169 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1170 // FIXME: use diagnostics subsystem for localization etc.
1171 if (PP.SawDateOrTime())
1172 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001174 // Loop over all the macro definitions that are live at the end of the file,
1175 // emitting each to the PP section.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001176 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1177 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001178 // FIXME: This emits macros in hash table order, we should do it in a stable
1179 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001180 MacroInfo *MI = I->second;
1181
1182 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1183 // been redefined by the header (in which case they are not isBuiltinMacro).
1184 if (MI->isBuiltinMacro())
1185 continue;
1186
Chris Lattner7356a312009-04-11 21:15:38 +00001187 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001188 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001189 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1190 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001192 unsigned Code;
1193 if (MI->isObjectLike()) {
1194 Code = pch::PP_MACRO_OBJECT_LIKE;
1195 } else {
1196 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001198 Record.push_back(MI->isC99Varargs());
1199 Record.push_back(MI->isGNUVarargs());
1200 Record.push_back(MI->getNumArgs());
1201 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1202 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001203 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001204 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001205 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001206 Record.clear();
1207
Chris Lattnerdf961c22009-04-10 18:08:30 +00001208 // Emit the tokens array.
1209 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1210 // Note that we know that the preprocessor does not have any annotation
1211 // tokens in it because they are created by the parser, and thus can't be
1212 // in a macro definition.
1213 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Chris Lattnerdf961c22009-04-10 18:08:30 +00001215 Record.push_back(Tok.getLocation().getRawEncoding());
1216 Record.push_back(Tok.getLength());
1217
Chris Lattnerdf961c22009-04-10 18:08:30 +00001218 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1219 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001220 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Chris Lattnerdf961c22009-04-10 18:08:30 +00001222 // FIXME: Should translate token kind to a stable encoding.
1223 Record.push_back(Tok.getKind());
1224 // FIXME: Should translate token flags to a stable encoding.
1225 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregorc9490c02009-04-16 22:23:12 +00001227 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001228 Record.clear();
1229 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001230 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001231 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001232 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001233}
1234
Douglas Gregor2e222532009-07-02 17:08:52 +00001235void PCHWriter::WriteComments(ASTContext &Context) {
1236 using namespace llvm;
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Douglas Gregor2e222532009-07-02 17:08:52 +00001238 if (Context.Comments.empty())
1239 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Douglas Gregor2e222532009-07-02 17:08:52 +00001241 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1242 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1243 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1244 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Douglas Gregor2e222532009-07-02 17:08:52 +00001246 RecordData Record;
1247 Record.push_back(pch::COMMENT_RANGES);
Mike Stump1eb44332009-09-09 15:08:12 +00001248 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregor2e222532009-07-02 17:08:52 +00001249 (const char*)&Context.Comments[0],
1250 Context.Comments.size() * sizeof(SourceRange));
1251}
1252
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001253//===----------------------------------------------------------------------===//
1254// Type Serialization
1255//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001256
Douglas Gregor2cf26342009-04-09 22:27:44 +00001257/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001258void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001259 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001260 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001261 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Douglas Gregor2cf26342009-04-09 22:27:44 +00001263 // Record the offset for this type.
1264 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001265 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001266 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1267 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001268 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001269 }
1270
1271 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregor2cf26342009-04-09 22:27:44 +00001273 // Emit the type's representation.
1274 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001275
Douglas Gregora4923eb2009-11-16 21:35:15 +00001276 if (T.hasLocalNonFastQualifiers()) {
1277 Qualifiers Qs = T.getLocalQualifiers();
1278 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001279 Record.push_back(Qs.getAsOpaqueValue());
1280 W.Code = pch::TYPE_EXT_QUAL;
1281 } else {
1282 switch (T->getTypeClass()) {
1283 // For all of the concrete, non-dependent types, call the
1284 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001285#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001286 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001287#define ABSTRACT_TYPE(Class, Base)
1288#define DEPENDENT_TYPE(Class, Base)
1289#include "clang/AST/TypeNodes.def"
1290
John McCall0953e762009-09-24 19:53:00 +00001291 // For all of the dependent type nodes (which only occur in C++
1292 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001293#define TYPE(Class, Base)
1294#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1295#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001296 assert(false && "Cannot serialize dependent type nodes");
1297 break;
1298 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001299 }
1300
1301 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001302 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001303
1304 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001305 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001306}
1307
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001308//===----------------------------------------------------------------------===//
1309// Declaration Serialization
1310//===----------------------------------------------------------------------===//
1311
Douglas Gregor2cf26342009-04-09 22:27:44 +00001312/// \brief Write the block containing all of the declaration IDs
1313/// lexically declared within the given DeclContext.
1314///
1315/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1316/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001317uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001318 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001319 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001320 return 0;
1321
Douglas Gregorc9490c02009-04-16 22:23:12 +00001322 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001323 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001324 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1325 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001326 AddDeclRef(*D, Record);
1327
Douglas Gregor25123082009-04-22 22:34:57 +00001328 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001329 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001330 return Offset;
1331}
1332
1333/// \brief Write the block containing all of the declaration IDs
1334/// visible from the given DeclContext.
1335///
1336/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1337/// bistream, or 0 if no block was written.
1338uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1339 DeclContext *DC) {
1340 if (DC->getPrimaryContext() != DC)
1341 return 0;
1342
Douglas Gregoraff22df2009-04-21 22:32:33 +00001343 // Since there is no name lookup into functions or methods, and we
1344 // perform name lookup for the translation unit via the
1345 // IdentifierInfo chains, don't bother to build a
1346 // visible-declarations table for these entities.
1347 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001348 return 0;
1349
Douglas Gregor2cf26342009-04-09 22:27:44 +00001350 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001351 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001352
1353 // Serialize the contents of the mapping used for lookup. Note that,
1354 // although we have two very different code paths, the serialized
1355 // representation is the same for both cases: a declaration name,
1356 // followed by a size, followed by references to the visible
1357 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001358 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001359 RecordData Record;
1360 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001361 if (!Map)
1362 return 0;
1363
Douglas Gregor2cf26342009-04-09 22:27:44 +00001364 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1365 D != DEnd; ++D) {
1366 AddDeclarationName(D->first, Record);
1367 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1368 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001369 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001370 AddDeclRef(*Result.first, Record);
1371 }
1372
1373 if (Record.size() == 0)
1374 return 0;
1375
Douglas Gregorc9490c02009-04-16 22:23:12 +00001376 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001377 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001378 return Offset;
1379}
1380
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001381//===----------------------------------------------------------------------===//
1382// Global Method Pool and Selector Serialization
1383//===----------------------------------------------------------------------===//
1384
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001385namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001386// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001387class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001388 PCHWriter &Writer;
1389
1390public:
1391 typedef Selector key_type;
1392 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001394 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1395 typedef const data_type& data_type_ref;
1396
1397 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001399 static unsigned ComputeHash(Selector Sel) {
1400 unsigned N = Sel.getNumArgs();
1401 if (N == 0)
1402 ++N;
1403 unsigned R = 5381;
1404 for (unsigned I = 0; I != N; ++I)
1405 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001406 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001407 return R;
1408 }
Mike Stump1eb44332009-09-09 15:08:12 +00001409
1410 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001411 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1412 data_type_ref Methods) {
1413 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1414 clang::io::Emit16(Out, KeyLen);
1415 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001416 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001417 Method = Method->Next)
1418 if (Method->Method)
1419 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001420 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001421 Method = Method->Next)
1422 if (Method->Method)
1423 DataLen += 4;
1424 clang::io::Emit16(Out, DataLen);
1425 return std::make_pair(KeyLen, DataLen);
1426 }
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Douglas Gregor83941df2009-04-25 17:48:32 +00001428 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001429 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001430 assert((Start >> 32) == 0 && "Selector key offset too large");
1431 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001432 unsigned N = Sel.getNumArgs();
1433 clang::io::Emit16(Out, N);
1434 if (N == 0)
1435 N = 1;
1436 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001437 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001438 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1439 }
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001441 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001442 data_type_ref Methods, unsigned DataLen) {
1443 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001444 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001445 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001446 Method = Method->Next)
1447 if (Method->Method)
1448 ++NumInstanceMethods;
1449
1450 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001451 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001452 Method = Method->Next)
1453 if (Method->Method)
1454 ++NumFactoryMethods;
1455
1456 clang::io::Emit16(Out, NumInstanceMethods);
1457 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001458 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001459 Method = Method->Next)
1460 if (Method->Method)
1461 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001462 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001463 Method = Method->Next)
1464 if (Method->Method)
1465 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001466
1467 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001468 }
1469};
1470} // end anonymous namespace
1471
1472/// \brief Write the method pool into the PCH file.
1473///
1474/// The method pool contains both instance and factory methods, stored
1475/// in an on-disk hash table indexed by the selector.
1476void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1477 using namespace llvm;
1478
1479 // Create and write out the blob that contains the instance and
1480 // factor method pools.
1481 bool Empty = true;
1482 {
1483 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001485 // Create the on-disk hash table representation. Start by
1486 // iterating through the instance method pool.
1487 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001488 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001489 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001490 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001491 InstanceEnd = SemaRef.InstanceMethodPool.end();
1492 Instance != InstanceEnd; ++Instance) {
1493 // Check whether there is a factory method with the same
1494 // selector.
1495 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1496 = SemaRef.FactoryMethodPool.find(Instance->first);
1497
1498 if (Factory == SemaRef.FactoryMethodPool.end())
1499 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001500 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001501 ObjCMethodList()));
1502 else
1503 Generator.insert(Instance->first,
1504 std::make_pair(Instance->second, Factory->second));
1505
Douglas Gregor83941df2009-04-25 17:48:32 +00001506 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001507 Empty = false;
1508 }
1509
1510 // Now iterate through the factory method pool, to pick up any
1511 // selectors that weren't already in the instance method pool.
1512 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001513 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001514 FactoryEnd = SemaRef.FactoryMethodPool.end();
1515 Factory != FactoryEnd; ++Factory) {
1516 // Check whether there is an instance method with the same
1517 // selector. If so, there is no work to do here.
1518 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1519 = SemaRef.InstanceMethodPool.find(Factory->first);
1520
Douglas Gregor83941df2009-04-25 17:48:32 +00001521 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001522 Generator.insert(Factory->first,
1523 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001524 ++NumSelectorsInMethodPool;
1525 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001526
1527 Empty = false;
1528 }
1529
Douglas Gregor83941df2009-04-25 17:48:32 +00001530 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001531 return;
1532
1533 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001534 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001535 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001536 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001537 {
1538 PCHMethodPoolTrait Trait(*this);
1539 llvm::raw_svector_ostream Out(MethodPool);
1540 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001541 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001542 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001543
1544 // For every selector that we have seen but which was not
1545 // written into the hash table, write the selector itself and
1546 // record it's offset.
1547 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1548 if (SelectorOffsets[I] == 0)
1549 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001550 }
1551
1552 // Create a blob abbreviation
1553 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1554 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1558 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1559
Douglas Gregor83941df2009-04-25 17:48:32 +00001560 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001561 RecordData Record;
1562 Record.push_back(pch::METHOD_POOL);
1563 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001564 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001565 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001566
1567 // Create a blob abbreviation for the selector table offsets.
1568 Abbrev = new BitCodeAbbrev();
1569 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1572 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1573
1574 // Write the selector offsets table.
1575 Record.clear();
1576 Record.push_back(pch::SELECTOR_OFFSETS);
1577 Record.push_back(SelectorOffsets.size());
1578 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1579 (const char *)&SelectorOffsets.front(),
1580 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001581 }
1582}
1583
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001584//===----------------------------------------------------------------------===//
1585// Identifier Table Serialization
1586//===----------------------------------------------------------------------===//
1587
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001588namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001589class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001590 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001591 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001592
Douglas Gregora92193e2009-04-28 21:18:29 +00001593 /// \brief Determines whether this is an "interesting" identifier
1594 /// that needs a full IdentifierInfo structure written into the hash
1595 /// table.
1596 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1597 return II->isPoisoned() ||
1598 II->isExtensionToken() ||
1599 II->hasMacroDefinition() ||
1600 II->getObjCOrBuiltinID() ||
1601 II->getFETokenInfo<void>();
1602 }
1603
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001604public:
1605 typedef const IdentifierInfo* key_type;
1606 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001608 typedef pch::IdentID data_type;
1609 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001610
1611 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001612 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001613
1614 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001615 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617
1618 std::pair<unsigned,unsigned>
1619 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001620 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001621 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001622 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1623 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001624 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001625 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001626 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001627 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001628 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1629 DEnd = IdentifierResolver::end();
1630 D != DEnd; ++D)
1631 DataLen += sizeof(pch::DeclID);
1632 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001633 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001634 // We emit the key length after the data length so that every
1635 // string is preceded by a 16-bit length. This matches the PTH
1636 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001637 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001638 return std::make_pair(KeyLen, DataLen);
1639 }
Mike Stump1eb44332009-09-09 15:08:12 +00001640
1641 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001642 unsigned KeyLen) {
1643 // Record the location of the key data. This is used when generating
1644 // the mapping from persistent IDs to strings.
1645 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001646 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001647 }
Mike Stump1eb44332009-09-09 15:08:12 +00001648
1649 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001650 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001651 if (!isInterestingIdentifier(II)) {
1652 clang::io::Emit32(Out, ID << 1);
1653 return;
1654 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001655
Douglas Gregora92193e2009-04-28 21:18:29 +00001656 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001657 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001658 bool hasMacroDefinition =
1659 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001660 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001661 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001662 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1663 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1664 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1665 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001666 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001667
Douglas Gregor37e26842009-04-21 23:56:24 +00001668 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001669 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001670
Douglas Gregor668c1a42009-04-21 22:25:48 +00001671 // Emit the declaration IDs in reverse order, because the
1672 // IdentifierResolver provides the declarations as they would be
1673 // visible (e.g., the function "stat" would come before the struct
1674 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1675 // adds declarations to the end of the list (so we need to see the
1676 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001677 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001678 IdentifierResolver::end());
1679 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1680 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001681 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001682 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001683 }
1684};
1685} // end anonymous namespace
1686
Douglas Gregorafaf3082009-04-11 00:14:32 +00001687/// \brief Write the identifier table into the PCH file.
1688///
1689/// The identifier table consists of a blob containing string data
1690/// (the actual identifiers themselves) and a separate "offsets" index
1691/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001692void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001693 using namespace llvm;
1694
1695 // Create and write out the blob that contains the identifier
1696 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001697 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001698 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Douglas Gregor92b059e2009-04-28 20:33:11 +00001700 // Look for any identifiers that were named while processing the
1701 // headers, but are otherwise not needed. We add these to the hash
1702 // table to enable checking of the predefines buffer in the case
1703 // where the user adds new macro definitions when building the PCH
1704 // file.
1705 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1706 IDEnd = PP.getIdentifierTable().end();
1707 ID != IDEnd; ++ID)
1708 getIdentifierRef(ID->second);
1709
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001710 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001711 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001712 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1713 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1714 ID != IDEnd; ++ID) {
1715 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001716 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001717 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001718
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001719 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001720 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001721 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001722 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001723 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001724 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001725 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001726 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001727 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001728 }
1729
1730 // Create a blob abbreviation
1731 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1732 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001735 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001736
1737 // Write the identifier table
1738 RecordData Record;
1739 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001740 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001741 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001742 }
1743
1744 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001745 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1746 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1749 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1750
1751 RecordData Record;
1752 Record.push_back(pch::IDENTIFIER_OFFSET);
1753 Record.push_back(IdentifierOffsets.size());
1754 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1755 (const char *)&IdentifierOffsets.front(),
1756 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001757}
1758
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001759//===----------------------------------------------------------------------===//
1760// General Serialization Routines
1761//===----------------------------------------------------------------------===//
1762
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001763/// \brief Write a record containing the given attributes.
1764void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1765 RecordData Record;
1766 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001767 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001768 Record.push_back(Attr->isInherited());
1769 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001770 default:
1771 assert(0 && "Does not support PCH writing for this attribute yet!");
1772 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001773 case Attr::Alias:
1774 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1775 break;
1776
1777 case Attr::Aligned:
1778 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1779 break;
1780
1781 case Attr::AlwaysInline:
1782 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001784 case Attr::AnalyzerNoReturn:
1785 break;
1786
1787 case Attr::Annotate:
1788 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1789 break;
1790
1791 case Attr::AsmLabel:
1792 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1793 break;
1794
Sean Hunt7725e672009-11-25 04:20:27 +00001795 case Attr::BaseCheck:
1796 break;
1797
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001798 case Attr::Blocks:
1799 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1800 break;
1801
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001802 case Attr::CDecl:
1803 break;
1804
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001805 case Attr::Cleanup:
1806 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1807 break;
1808
1809 case Attr::Const:
1810 break;
1811
1812 case Attr::Constructor:
1813 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1814 break;
1815
1816 case Attr::DLLExport:
1817 case Attr::DLLImport:
1818 case Attr::Deprecated:
1819 break;
1820
1821 case Attr::Destructor:
1822 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1823 break;
1824
1825 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001826 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001827 break;
1828
1829 case Attr::Format: {
1830 const FormatAttr *Format = cast<FormatAttr>(Attr);
1831 AddString(Format->getType(), Record);
1832 Record.push_back(Format->getFormatIdx());
1833 Record.push_back(Format->getFirstArg());
1834 break;
1835 }
1836
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001837 case Attr::FormatArg: {
1838 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1839 Record.push_back(Format->getFormatIdx());
1840 break;
1841 }
1842
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001843 case Attr::Sentinel : {
1844 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1845 Record.push_back(Sentinel->getSentinel());
1846 Record.push_back(Sentinel->getNullPos());
1847 break;
1848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Chris Lattnercf2a7212009-04-20 19:12:28 +00001850 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001851 case Attr::Hiding:
Ted Kremenekefbddd22010-02-17 02:37:45 +00001852 case Attr::IBActionKind:
Ted Kremenek47e69902010-02-18 00:05:52 +00001853 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001854 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001855 case Attr::NoDebug:
Ted Kremenek47e69902010-02-18 00:05:52 +00001856 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001857 case Attr::NoReturn:
1858 case Attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001859 break;
1860
1861 case Attr::NonNull: {
1862 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1863 Record.push_back(NonNull->size());
1864 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1865 break;
1866 }
1867
Ted Kremenek31c780d2010-02-18 00:05:45 +00001868 case Attr::CFReturnsNotRetained:
1869 case Attr::CFReturnsRetained:
1870 case Attr::NSReturnsNotRetained:
1871 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001872 case Attr::ObjCException:
1873 case Attr::ObjCNSObject:
1874 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001875 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001876 break;
1877
Anders Carlssona860e752009-08-08 18:23:56 +00001878 case Attr::PragmaPack:
1879 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001880 break;
1881
Anders Carlssona860e752009-08-08 18:23:56 +00001882 case Attr::Packed:
1883 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001885 case Attr::Pure:
1886 break;
1887
1888 case Attr::Regparm:
1889 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1890 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Nate Begeman6f3d8382009-06-26 06:32:41 +00001892 case Attr::ReqdWorkGroupSize:
1893 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1894 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1895 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1896 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001897
1898 case Attr::Section:
1899 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1900 break;
1901
1902 case Attr::StdCall:
1903 case Attr::TransparentUnion:
1904 case Attr::Unavailable:
1905 case Attr::Unused:
1906 case Attr::Used:
1907 break;
1908
1909 case Attr::Visibility:
1910 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001911 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001912 break;
1913
1914 case Attr::WarnUnusedResult:
1915 case Attr::Weak:
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001916 case Attr::WeakRef:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001917 case Attr::WeakImport:
1918 break;
1919 }
1920 }
1921
Douglas Gregorc9490c02009-04-16 22:23:12 +00001922 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001923}
1924
1925void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1926 Record.push_back(Str.size());
1927 Record.insert(Record.end(), Str.begin(), Str.end());
1928}
1929
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001930/// \brief Note that the identifier II occurs at the given offset
1931/// within the identifier table.
1932void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001933 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001934}
1935
Douglas Gregor83941df2009-04-25 17:48:32 +00001936/// \brief Note that the selector Sel occurs at the given offset
1937/// within the method pool/selector table.
1938void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1939 unsigned ID = SelectorIDs[Sel];
1940 assert(ID && "Unknown selector");
1941 SelectorOffsets[ID - 1] = Offset;
1942}
1943
Mike Stump1eb44332009-09-09 15:08:12 +00001944PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1945 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00001946 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1947 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001948
Douglas Gregore650c8c2009-07-07 00:12:59 +00001949void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1950 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001951 using namespace llvm;
1952
Douglas Gregore7785042009-04-20 15:53:59 +00001953 ASTContext &Context = SemaRef.Context;
1954 Preprocessor &PP = SemaRef.PP;
1955
Douglas Gregor2cf26342009-04-09 22:27:44 +00001956 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001957 Stream.Emit((unsigned)'C', 8);
1958 Stream.Emit((unsigned)'P', 8);
1959 Stream.Emit((unsigned)'C', 8);
1960 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001962 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001963
1964 // The translation unit is the first declaration we'll emit.
1965 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001966 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001967
Douglas Gregor2deaea32009-04-22 18:49:13 +00001968 // Make sure that we emit IdentifierInfos (and any attached
1969 // declarations) for builtins.
1970 {
1971 IdentifierTable &Table = PP.getIdentifierTable();
1972 llvm::SmallVector<const char *, 32> BuiltinNames;
1973 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1974 Context.getLangOptions().NoBuiltin);
1975 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1976 getIdentifierRef(&Table.get(BuiltinNames[I]));
1977 }
1978
Chris Lattner63d65f82009-09-08 18:19:27 +00001979 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00001980 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00001981 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001982 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001983 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
1984 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00001985 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001986
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001987 // Build a record containing all of the static unused functions in this file.
1988 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001989 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001990 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001991
Douglas Gregor14c22f22009-04-22 22:18:58 +00001992 // Build a record containing all of the locally-scoped external
1993 // declarations in this header file. Generally, this record will be
1994 // empty.
1995 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00001996 // FIXME: This is filling in the PCH file in densemap order which is
1997 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00001998 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00001999 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2000 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2001 TD != TDEnd; ++TD)
2002 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2003
Douglas Gregorb81c1702009-04-27 20:06:05 +00002004 // Build a record containing all of the ext_vector declarations.
2005 RecordData ExtVectorDecls;
2006 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2007 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2008
Douglas Gregor2cf26342009-04-09 22:27:44 +00002009 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002010 RecordData Record;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002011 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002012 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002013 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002014 if (StatCalls && !isysroot)
2015 WriteStatCache(*StatCalls, isysroot);
2016 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump1eb44332009-09-09 15:08:12 +00002017 WriteComments(Context);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002018 // Write the record of special types.
2019 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002021 AddTypeRef(Context.getBuiltinVaListType(), Record);
2022 AddTypeRef(Context.getObjCIdType(), Record);
2023 AddTypeRef(Context.getObjCSelType(), Record);
2024 AddTypeRef(Context.getObjCProtoType(), Record);
2025 AddTypeRef(Context.getObjCClassType(), Record);
2026 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2027 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2028 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002029 AddTypeRef(Context.getjmp_bufType(), Record);
2030 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002031 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2032 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002033#if 0
2034 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00002035 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002036#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00002037 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002038 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002039 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Douglas Gregor366809a2009-04-26 03:49:13 +00002041 // Keep writing types and declarations until all types and
2042 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002043 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2044 WriteDeclsBlockAbbrevs();
2045 while (!DeclTypesToEmit.empty()) {
2046 DeclOrType DOT = DeclTypesToEmit.front();
2047 DeclTypesToEmit.pop();
2048 if (DOT.isType())
2049 WriteType(DOT.getType());
2050 else
2051 WriteDecl(Context, DOT.getDecl());
2052 }
2053 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002054
Douglas Gregor813a97b2009-10-17 17:25:45 +00002055 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002056 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002057 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002058
2059 // Write the type offsets array
2060 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2061 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2064 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2065 Record.clear();
2066 Record.push_back(pch::TYPE_OFFSET);
2067 Record.push_back(TypeOffsets.size());
2068 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002069 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002070 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002072 // Write the declaration offsets array
2073 Abbrev = new BitCodeAbbrev();
2074 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2077 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2078 Record.clear();
2079 Record.push_back(pch::DECL_OFFSET);
2080 Record.push_back(DeclOffsets.size());
2081 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002082 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002083 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002084
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002085 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002086 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002087 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002088
2089 // Write the record containing tentative definitions.
2090 if (!TentativeDefinitions.empty())
2091 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002092
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002093 // Write the record containing unused static functions.
2094 if (!UnusedStaticFuncs.empty())
2095 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002096
Douglas Gregor14c22f22009-04-22 22:18:58 +00002097 // Write the record containing locally-scoped external definitions.
2098 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002099 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002100 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002101
2102 // Write the record containing ext_vector type names.
2103 if (!ExtVectorDecls.empty())
2104 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Douglas Gregor3e1af842009-04-17 22:13:46 +00002106 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002107 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002108 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002109 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002110 Record.push_back(NumLexicalDeclContexts);
2111 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002112 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002113 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002114}
2115
2116void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2117 Record.push_back(Loc.getRawEncoding());
2118}
2119
2120void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2121 Record.push_back(Value.getBitWidth());
2122 unsigned N = Value.getNumWords();
2123 const uint64_t* Words = Value.getRawData();
2124 for (unsigned I = 0; I != N; ++I)
2125 Record.push_back(Words[I]);
2126}
2127
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002128void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2129 Record.push_back(Value.isUnsigned());
2130 AddAPInt(Value, Record);
2131}
2132
Douglas Gregor17fc2232009-04-14 21:55:33 +00002133void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2134 AddAPInt(Value.bitcastToAPInt(), Record);
2135}
2136
Douglas Gregor2cf26342009-04-09 22:27:44 +00002137void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002138 Record.push_back(getIdentifierRef(II));
2139}
2140
2141pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2142 if (II == 0)
2143 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002144
2145 pch::IdentID &ID = IdentifierIDs[II];
2146 if (ID == 0)
2147 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002148 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002149}
2150
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002151void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2152 if (SelRef.getAsOpaquePtr() == 0) {
2153 Record.push_back(0);
2154 return;
2155 }
2156
2157 pch::SelectorID &SID = SelectorIDs[SelRef];
2158 if (SID == 0) {
2159 SID = SelectorIDs.size();
2160 SelVector.push_back(SelRef);
2161 }
2162 Record.push_back(SID);
2163}
2164
John McCall833ca992009-10-29 08:12:44 +00002165void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2166 RecordData &Record) {
2167 switch (Arg.getArgument().getKind()) {
2168 case TemplateArgument::Expression:
2169 AddStmt(Arg.getLocInfo().getAsExpr());
2170 break;
2171 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002172 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002173 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002174 case TemplateArgument::Template:
2175 Record.push_back(
2176 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2177 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2178 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2179 break;
John McCall833ca992009-10-29 08:12:44 +00002180 case TemplateArgument::Null:
2181 case TemplateArgument::Integral:
2182 case TemplateArgument::Declaration:
2183 case TemplateArgument::Pack:
2184 break;
2185 }
2186}
2187
John McCalla93c9342009-12-07 02:54:59 +00002188void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2189 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002190 AddTypeRef(QualType(), Record);
2191 return;
2192 }
2193
John McCalla93c9342009-12-07 02:54:59 +00002194 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002195 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002196 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002197 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002198}
2199
Douglas Gregor2cf26342009-04-09 22:27:44 +00002200void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2201 if (T.isNull()) {
2202 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2203 return;
2204 }
2205
Douglas Gregora4923eb2009-11-16 21:35:15 +00002206 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002207 T.removeFastQualifiers();
2208
Douglas Gregora4923eb2009-11-16 21:35:15 +00002209 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002210 pch::TypeID &ID = TypeIDs[T];
2211 if (ID == 0) {
2212 // We haven't seen these qualifiers applied to this type before.
2213 // Assign it a new ID. This is the only time we enqueue a
2214 // qualified type, and it has no CV qualifiers.
2215 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002216 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002217 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002218
John McCall0953e762009-09-24 19:53:00 +00002219 // Encode the type qualifiers in the type reference.
2220 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2221 return;
2222 }
2223
Douglas Gregora4923eb2009-11-16 21:35:15 +00002224 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002225
Douglas Gregor2cf26342009-04-09 22:27:44 +00002226 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002227 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002228 switch (BT->getKind()) {
2229 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2230 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2231 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2232 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2233 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2234 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2235 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2236 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002237 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002238 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2239 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2240 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2241 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2242 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2243 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2244 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002245 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002246 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2247 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2248 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002249 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002250 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2251 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002252 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2253 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002254 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2255 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002256 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002257 case BuiltinType::UndeducedAuto:
2258 assert(0 && "Should not see undeduced auto here");
2259 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002260 }
2261
John McCall0953e762009-09-24 19:53:00 +00002262 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002263 return;
2264 }
2265
John McCall0953e762009-09-24 19:53:00 +00002266 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002267 if (ID == 0) {
2268 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002269 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002270 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002271 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002272 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002273
2274 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002275 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002276}
2277
2278void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2279 if (D == 0) {
2280 Record.push_back(0);
2281 return;
2282 }
2283
Douglas Gregor8038d512009-04-10 17:25:41 +00002284 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002285 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002286 // We haven't seen this declaration before. Give it a new ID and
2287 // enqueue it in the list of declarations to emit.
2288 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002289 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002290 }
2291
2292 Record.push_back(ID);
2293}
2294
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002295pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2296 if (D == 0)
2297 return 0;
2298
2299 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2300 return DeclIDs[D];
2301}
2302
Douglas Gregor2cf26342009-04-09 22:27:44 +00002303void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002304 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002305 Record.push_back(Name.getNameKind());
2306 switch (Name.getNameKind()) {
2307 case DeclarationName::Identifier:
2308 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2309 break;
2310
2311 case DeclarationName::ObjCZeroArgSelector:
2312 case DeclarationName::ObjCOneArgSelector:
2313 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002314 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002315 break;
2316
2317 case DeclarationName::CXXConstructorName:
2318 case DeclarationName::CXXDestructorName:
2319 case DeclarationName::CXXConversionFunctionName:
2320 AddTypeRef(Name.getCXXNameType(), Record);
2321 break;
2322
2323 case DeclarationName::CXXOperatorName:
2324 Record.push_back(Name.getCXXOverloadedOperator());
2325 break;
2326
Sean Hunt3e518bd2009-11-29 07:34:05 +00002327 case DeclarationName::CXXLiteralOperatorName:
2328 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2329 break;
2330
Douglas Gregor2cf26342009-04-09 22:27:44 +00002331 case DeclarationName::CXXUsingDirective:
2332 // No extra data to emit
2333 break;
2334 }
2335}