blob: caf1ce47a18c685f5b92e1fddca530b738ac8c61 [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());
131 Code = pch::TYPE_VECTOR;
132}
133
134void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
135 VisitVectorType(T);
136 Code = pch::TYPE_EXT_VECTOR;
137}
138
139void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
140 Writer.AddTypeRef(T->getResultType(), Record);
Douglas Gregor91236662009-12-22 18:11:50 +0000141 Record.push_back(T->getNoReturnAttr());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000142 // FIXME: need to stabilize encoding of calling convention...
143 Record.push_back(T->getCallConv());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
146void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
147 VisitFunctionType(T);
148 Code = pch::TYPE_FUNCTION_NO_PROTO;
149}
150
151void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
152 VisitFunctionType(T);
153 Record.push_back(T->getNumArgs());
154 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
155 Writer.AddTypeRef(T->getArgType(I), Record);
156 Record.push_back(T->isVariadic());
157 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000158 Record.push_back(T->hasExceptionSpec());
159 Record.push_back(T->hasAnyExceptionSpec());
160 Record.push_back(T->getNumExceptions());
161 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
162 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163 Code = pch::TYPE_FUNCTION_PROTO;
164}
165
John McCalled976492009-12-04 22:46:56 +0000166#if 0
167// For when we want it....
168void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
169 Writer.AddDeclRef(T->getDecl(), Record);
170 Code = pch::TYPE_UNRESOLVED_USING;
171}
172#endif
173
Douglas Gregor2cf26342009-04-09 22:27:44 +0000174void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
175 Writer.AddDeclRef(T->getDecl(), Record);
176 Code = pch::TYPE_TYPEDEF;
177}
178
179void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000180 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181 Code = pch::TYPE_TYPEOF_EXPR;
182}
183
184void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
185 Writer.AddTypeRef(T->getUnderlyingType(), Record);
186 Code = pch::TYPE_TYPEOF;
187}
188
Anders Carlsson395b4752009-06-24 19:06:50 +0000189void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
190 Writer.AddStmt(T->getUnderlyingExpr());
191 Code = pch::TYPE_DECLTYPE;
192}
193
Douglas Gregor2cf26342009-04-09 22:27:44 +0000194void PCHTypeWriter::VisitTagType(const TagType *T) {
195 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000196 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197 "Cannot serialize in the middle of a type definition");
198}
199
200void PCHTypeWriter::VisitRecordType(const RecordType *T) {
201 VisitTagType(T);
202 Code = pch::TYPE_RECORD;
203}
204
205void PCHTypeWriter::VisitEnumType(const EnumType *T) {
206 VisitTagType(T);
207 Code = pch::TYPE_ENUM;
208}
209
John McCall7da24312009-09-05 00:15:47 +0000210void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
211 Writer.AddTypeRef(T->getUnderlyingType(), Record);
212 Record.push_back(T->getTagKind());
213 Code = pch::TYPE_ELABORATED;
214}
215
Mike Stump1eb44332009-09-09 15:08:12 +0000216void
John McCall49a832b2009-10-18 09:09:24 +0000217PCHTypeWriter::VisitSubstTemplateTypeParmType(
218 const SubstTemplateTypeParmType *T) {
219 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
220 Writer.AddTypeRef(T->getReplacementType(), Record);
221 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
222}
223
224void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225PCHTypeWriter::VisitTemplateSpecializationType(
226 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000227 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228 assert(false && "Cannot serialize template specialization types");
229}
230
231void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000232 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000233 assert(false && "Cannot serialize qualified name types");
234}
235
236void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
237 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000238 Record.push_back(T->getNumProtocols());
Steve Naroff446ee4e2009-05-27 16:21:00 +0000239 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
240 E = T->qual_end(); I != E; ++I)
241 Writer.AddDeclRef(*I, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000242 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000243}
244
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000245void
246PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000247 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 Record.push_back(T->getNumProtocols());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000249 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000250 E = T->qual_end(); I != E; ++I)
251 Writer.AddDeclRef(*I, Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000252 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253}
254
John McCalla1ee0c52009-10-16 21:56:05 +0000255namespace {
256
257class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
258 PCHWriter &Writer;
259 PCHWriter::RecordData &Record;
260
261public:
262 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
263 : Writer(Writer), Record(Record) { }
264
John McCall51bd8032009-10-18 01:05:36 +0000265#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000266#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000267 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000268#include "clang/AST/TypeLocNodes.def"
269
John McCall51bd8032009-10-18 01:05:36 +0000270 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
271 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000272};
273
274}
275
John McCall51bd8032009-10-18 01:05:36 +0000276void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
277 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000278}
John McCall51bd8032009-10-18 01:05:36 +0000279void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000280 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
281 if (TL.needsExtraLocalData()) {
282 Record.push_back(TL.getWrittenTypeSpec());
283 Record.push_back(TL.getWrittenSignSpec());
284 Record.push_back(TL.getWrittenWidthSpec());
285 Record.push_back(TL.hasModeAttr());
286 }
John McCalla1ee0c52009-10-16 21:56:05 +0000287}
John McCall51bd8032009-10-18 01:05:36 +0000288void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
289 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000290}
John McCall51bd8032009-10-18 01:05:36 +0000291void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
292 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000293}
John McCall51bd8032009-10-18 01:05:36 +0000294void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
295 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000296}
John McCall51bd8032009-10-18 01:05:36 +0000297void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
298 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000299}
John McCall51bd8032009-10-18 01:05:36 +0000300void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
301 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000302}
John McCall51bd8032009-10-18 01:05:36 +0000303void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
304 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000305}
John McCall51bd8032009-10-18 01:05:36 +0000306void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
307 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
308 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
309 Record.push_back(TL.getSizeExpr() ? 1 : 0);
310 if (TL.getSizeExpr())
311 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000312}
John McCall51bd8032009-10-18 01:05:36 +0000313void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
314 VisitArrayTypeLoc(TL);
315}
316void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
317 VisitArrayTypeLoc(TL);
318}
319void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
320 VisitArrayTypeLoc(TL);
321}
322void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
323 DependentSizedArrayTypeLoc TL) {
324 VisitArrayTypeLoc(TL);
325}
326void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
327 DependentSizedExtVectorTypeLoc TL) {
328 Writer.AddSourceLocation(TL.getNameLoc(), Record);
329}
330void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
331 Writer.AddSourceLocation(TL.getNameLoc(), Record);
332}
333void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
334 Writer.AddSourceLocation(TL.getNameLoc(), Record);
335}
336void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
337 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
338 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
339 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
340 Writer.AddDeclRef(TL.getArg(i), Record);
341}
342void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
343 VisitFunctionTypeLoc(TL);
344}
345void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
346 VisitFunctionTypeLoc(TL);
347}
John McCalled976492009-12-04 22:46:56 +0000348void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
349 Writer.AddSourceLocation(TL.getNameLoc(), Record);
350}
John McCall51bd8032009-10-18 01:05:36 +0000351void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
352 Writer.AddSourceLocation(TL.getNameLoc(), Record);
353}
354void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000355 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
356 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
357 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000358}
359void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000360 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
361 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
362 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
363 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000364}
365void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
366 Writer.AddSourceLocation(TL.getNameLoc(), Record);
367}
368void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
369 Writer.AddSourceLocation(TL.getNameLoc(), Record);
370}
371void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
372 Writer.AddSourceLocation(TL.getNameLoc(), Record);
373}
374void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
375 Writer.AddSourceLocation(TL.getNameLoc(), Record);
376}
377void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
378 Writer.AddSourceLocation(TL.getNameLoc(), Record);
379}
John McCall49a832b2009-10-18 09:09:24 +0000380void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
381 SubstTemplateTypeParmTypeLoc TL) {
382 Writer.AddSourceLocation(TL.getNameLoc(), Record);
383}
John McCall51bd8032009-10-18 01:05:36 +0000384void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
385 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000386 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
387 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
388 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
389 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
390 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000391}
392void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
393 Writer.AddSourceLocation(TL.getNameLoc(), Record);
394}
395void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
396 Writer.AddSourceLocation(TL.getNameLoc(), Record);
397}
398void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
399 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000400 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
401 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
402 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
403 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000404}
John McCall54e14c42009-10-22 22:37:11 +0000405void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getStarLoc(), Record);
407 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
408 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
409 Record.push_back(TL.hasBaseTypeAsWritten());
410 Record.push_back(TL.hasProtocolsAsWritten());
411 if (TL.hasProtocolsAsWritten())
412 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
413 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
414}
John McCalla1ee0c52009-10-16 21:56:05 +0000415
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000416//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000417// PCHWriter Implementation
418//===----------------------------------------------------------------------===//
419
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000420static void EmitBlockID(unsigned ID, const char *Name,
421 llvm::BitstreamWriter &Stream,
422 PCHWriter::RecordData &Record) {
423 Record.clear();
424 Record.push_back(ID);
425 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
426
427 // Emit the block name if present.
428 if (Name == 0 || Name[0] == 0) return;
429 Record.clear();
430 while (*Name)
431 Record.push_back(*Name++);
432 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
433}
434
435static void EmitRecordID(unsigned ID, const char *Name,
436 llvm::BitstreamWriter &Stream,
437 PCHWriter::RecordData &Record) {
438 Record.clear();
439 Record.push_back(ID);
440 while (*Name)
441 Record.push_back(*Name++);
442 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000443}
444
445static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
446 PCHWriter::RecordData &Record) {
447#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
448 RECORD(STMT_STOP);
449 RECORD(STMT_NULL_PTR);
450 RECORD(STMT_NULL);
451 RECORD(STMT_COMPOUND);
452 RECORD(STMT_CASE);
453 RECORD(STMT_DEFAULT);
454 RECORD(STMT_LABEL);
455 RECORD(STMT_IF);
456 RECORD(STMT_SWITCH);
457 RECORD(STMT_WHILE);
458 RECORD(STMT_DO);
459 RECORD(STMT_FOR);
460 RECORD(STMT_GOTO);
461 RECORD(STMT_INDIRECT_GOTO);
462 RECORD(STMT_CONTINUE);
463 RECORD(STMT_BREAK);
464 RECORD(STMT_RETURN);
465 RECORD(STMT_DECL);
466 RECORD(STMT_ASM);
467 RECORD(EXPR_PREDEFINED);
468 RECORD(EXPR_DECL_REF);
469 RECORD(EXPR_INTEGER_LITERAL);
470 RECORD(EXPR_FLOATING_LITERAL);
471 RECORD(EXPR_IMAGINARY_LITERAL);
472 RECORD(EXPR_STRING_LITERAL);
473 RECORD(EXPR_CHARACTER_LITERAL);
474 RECORD(EXPR_PAREN);
475 RECORD(EXPR_UNARY_OPERATOR);
476 RECORD(EXPR_SIZEOF_ALIGN_OF);
477 RECORD(EXPR_ARRAY_SUBSCRIPT);
478 RECORD(EXPR_CALL);
479 RECORD(EXPR_MEMBER);
480 RECORD(EXPR_BINARY_OPERATOR);
481 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
482 RECORD(EXPR_CONDITIONAL_OPERATOR);
483 RECORD(EXPR_IMPLICIT_CAST);
484 RECORD(EXPR_CSTYLE_CAST);
485 RECORD(EXPR_COMPOUND_LITERAL);
486 RECORD(EXPR_EXT_VECTOR_ELEMENT);
487 RECORD(EXPR_INIT_LIST);
488 RECORD(EXPR_DESIGNATED_INIT);
489 RECORD(EXPR_IMPLICIT_VALUE_INIT);
490 RECORD(EXPR_VA_ARG);
491 RECORD(EXPR_ADDR_LABEL);
492 RECORD(EXPR_STMT);
493 RECORD(EXPR_TYPES_COMPATIBLE);
494 RECORD(EXPR_CHOOSE);
495 RECORD(EXPR_GNU_NULL);
496 RECORD(EXPR_SHUFFLE_VECTOR);
497 RECORD(EXPR_BLOCK);
498 RECORD(EXPR_BLOCK_DECL_REF);
499 RECORD(EXPR_OBJC_STRING_LITERAL);
500 RECORD(EXPR_OBJC_ENCODE);
501 RECORD(EXPR_OBJC_SELECTOR_EXPR);
502 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
503 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
504 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
505 RECORD(EXPR_OBJC_KVC_REF_EXPR);
506 RECORD(EXPR_OBJC_MESSAGE_EXPR);
507 RECORD(EXPR_OBJC_SUPER_EXPR);
508 RECORD(STMT_OBJC_FOR_COLLECTION);
509 RECORD(STMT_OBJC_CATCH);
510 RECORD(STMT_OBJC_FINALLY);
511 RECORD(STMT_OBJC_AT_TRY);
512 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
513 RECORD(STMT_OBJC_AT_THROW);
514#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000515}
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000517void PCHWriter::WriteBlockInfoBlock() {
518 RecordData Record;
519 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner2f4efd12009-04-27 00:40:25 +0000521#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000522#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000524 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000525 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000526 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000527 RECORD(TYPE_OFFSET);
528 RECORD(DECL_OFFSET);
529 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000530 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000531 RECORD(IDENTIFIER_OFFSET);
532 RECORD(IDENTIFIER_TABLE);
533 RECORD(EXTERNAL_DEFINITIONS);
534 RECORD(SPECIAL_TYPES);
535 RECORD(STATISTICS);
536 RECORD(TENTATIVE_DEFINITIONS);
537 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
538 RECORD(SELECTOR_OFFSETS);
539 RECORD(METHOD_POOL);
540 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000541 RECORD(SOURCE_LOCATION_OFFSETS);
542 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000543 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000544 RECORD(EXT_VECTOR_DECLS);
Douglas Gregor2e222532009-07-02 17:08:52 +0000545 RECORD(COMMENT_RANGES);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000546 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000547
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000548 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000549 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000550 RECORD(SM_SLOC_FILE_ENTRY);
551 RECORD(SM_SLOC_BUFFER_ENTRY);
552 RECORD(SM_SLOC_BUFFER_BLOB);
553 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
554 RECORD(SM_LINE_TABLE);
555 RECORD(SM_HEADER_FILE_INFO);
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000557 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000558 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000559 RECORD(PP_MACRO_OBJECT_LIKE);
560 RECORD(PP_MACRO_FUNCTION_LIKE);
561 RECORD(PP_TOKEN);
562
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000563 // Decls and Types block.
564 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000565 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000566 RECORD(TYPE_COMPLEX);
567 RECORD(TYPE_POINTER);
568 RECORD(TYPE_BLOCK_POINTER);
569 RECORD(TYPE_LVALUE_REFERENCE);
570 RECORD(TYPE_RVALUE_REFERENCE);
571 RECORD(TYPE_MEMBER_POINTER);
572 RECORD(TYPE_CONSTANT_ARRAY);
573 RECORD(TYPE_INCOMPLETE_ARRAY);
574 RECORD(TYPE_VARIABLE_ARRAY);
575 RECORD(TYPE_VECTOR);
576 RECORD(TYPE_EXT_VECTOR);
577 RECORD(TYPE_FUNCTION_PROTO);
578 RECORD(TYPE_FUNCTION_NO_PROTO);
579 RECORD(TYPE_TYPEDEF);
580 RECORD(TYPE_TYPEOF_EXPR);
581 RECORD(TYPE_TYPEOF);
582 RECORD(TYPE_RECORD);
583 RECORD(TYPE_ENUM);
584 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000585 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000586 RECORD(DECL_ATTR);
587 RECORD(DECL_TRANSLATION_UNIT);
588 RECORD(DECL_TYPEDEF);
589 RECORD(DECL_ENUM);
590 RECORD(DECL_RECORD);
591 RECORD(DECL_ENUM_CONSTANT);
592 RECORD(DECL_FUNCTION);
593 RECORD(DECL_OBJC_METHOD);
594 RECORD(DECL_OBJC_INTERFACE);
595 RECORD(DECL_OBJC_PROTOCOL);
596 RECORD(DECL_OBJC_IVAR);
597 RECORD(DECL_OBJC_AT_DEFS_FIELD);
598 RECORD(DECL_OBJC_CLASS);
599 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
600 RECORD(DECL_OBJC_CATEGORY);
601 RECORD(DECL_OBJC_CATEGORY_IMPL);
602 RECORD(DECL_OBJC_IMPLEMENTATION);
603 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
604 RECORD(DECL_OBJC_PROPERTY);
605 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000606 RECORD(DECL_FIELD);
607 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000608 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000609 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000610 RECORD(DECL_FILE_SCOPE_ASM);
611 RECORD(DECL_BLOCK);
612 RECORD(DECL_CONTEXT_LEXICAL);
613 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000614 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000615 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000616#undef RECORD
617#undef BLOCK
618 Stream.ExitBlock();
619}
620
Douglas Gregore650c8c2009-07-07 00:12:59 +0000621/// \brief Adjusts the given filename to only write out the portion of the
622/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000623///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000624/// \param Filename the file name to adjust.
625///
626/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
627/// the returned filename will be adjusted by this system root.
628///
629/// \returns either the original filename (if it needs no adjustment) or the
630/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000631static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000632adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
633 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Douglas Gregore650c8c2009-07-07 00:12:59 +0000635 if (!isysroot)
636 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Douglas Gregore650c8c2009-07-07 00:12:59 +0000638 // Verify that the filename and the system root have the same prefix.
639 unsigned Pos = 0;
640 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
641 if (Filename[Pos] != isysroot[Pos])
642 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Douglas Gregore650c8c2009-07-07 00:12:59 +0000644 // We hit the end of the filename before we hit the end of the system root.
645 if (!Filename[Pos])
646 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Douglas Gregore650c8c2009-07-07 00:12:59 +0000648 // If the file name has a '/' at the current position, skip over the '/'.
649 // We distinguish sysroot-based includes from absolute includes by the
650 // absence of '/' at the beginning of sysroot-based includes.
651 if (Filename[Pos] == '/')
652 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Douglas Gregore650c8c2009-07-07 00:12:59 +0000654 return Filename + Pos;
655}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000656
Douglas Gregorab41e632009-04-27 22:23:34 +0000657/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000658void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000659 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000660
Douglas Gregore650c8c2009-07-07 00:12:59 +0000661 // Metadata
662 const TargetInfo &Target = Context.Target;
663 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
664 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
665 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
666 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
667 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
668 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
669 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
670 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
671 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Douglas Gregore650c8c2009-07-07 00:12:59 +0000673 RecordData Record;
674 Record.push_back(pch::METADATA);
675 Record.push_back(pch::VERSION_MAJOR);
676 Record.push_back(pch::VERSION_MINOR);
677 Record.push_back(CLANG_VERSION_MAJOR);
678 Record.push_back(CLANG_VERSION_MINOR);
679 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000680 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000681 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Douglas Gregorb64c1932009-05-12 01:31:05 +0000683 // Original file name
684 SourceManager &SM = Context.getSourceManager();
685 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
686 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
687 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
688 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
689 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
690
691 llvm::sys::Path MainFilePath(MainFile->getName());
692 std::string MainFileName;
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Douglas Gregorb64c1932009-05-12 01:31:05 +0000694 if (!MainFilePath.isAbsolute()) {
695 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000696 P.appendComponent(MainFilePath.str());
697 MainFileName = P.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000698 } else {
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000699 MainFileName = MainFilePath.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000700 }
701
Douglas Gregore650c8c2009-07-07 00:12:59 +0000702 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000703 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000704 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000705 RecordData Record;
706 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000707 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000708 }
Douglas Gregor445e23e2009-10-05 21:07:28 +0000709
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000710 // Repository branch/version information.
711 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
712 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
713 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
714 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000715 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000716 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000717 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
718 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000719}
720
721/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000722void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
723 RecordData Record;
724 Record.push_back(LangOpts.Trigraphs);
725 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
726 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
727 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
728 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
729 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
730 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
731 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
732 Record.push_back(LangOpts.C99); // C99 Support
733 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
734 Record.push_back(LangOpts.CPlusPlus); // C++ Support
735 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000736 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000738 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
739 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
740 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000742 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000743 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
744 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000745 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000746 Record.push_back(LangOpts.Exceptions); // Support exception handling.
747
748 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
749 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
750 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
751
Chris Lattnerea5ce472009-04-27 07:35:58 +0000752 // Whether static initializers are protected by locks.
753 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000754 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000755 Record.push_back(LangOpts.Blocks); // block extension to C
756 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
757 // they are unused.
758 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
759 // (modulo the platform support).
760
761 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
762 // signed integer arithmetic overflows.
763
764 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
765 // may be ripped out at any time.
766
767 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000768 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000769 // defined.
770 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
771 // opposed to __DYNAMIC__).
772 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
773
774 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
775 // used (instead of C99 semantics).
776 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000777 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
778 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000779 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
780 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000781 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000782 Record.push_back(LangOpts.getGCMode());
783 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000784 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000785 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000786 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000787 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000788 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000789 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000790}
791
Douglas Gregor14f79002009-04-10 03:52:48 +0000792//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000793// stat cache Serialization
794//===----------------------------------------------------------------------===//
795
796namespace {
797// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000798class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000799public:
800 typedef const char * key_type;
801 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000803 typedef std::pair<int, struct stat> data_type;
804 typedef const data_type& data_type_ref;
805
806 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000807 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000808 }
Mike Stump1eb44332009-09-09 15:08:12 +0000809
810 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000811 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
812 data_type_ref Data) {
813 unsigned StrLen = strlen(path);
814 clang::io::Emit16(Out, StrLen);
815 unsigned DataLen = 1; // result value
816 if (Data.first == 0)
817 DataLen += 4 + 4 + 2 + 8 + 8;
818 clang::io::Emit8(Out, DataLen);
819 return std::make_pair(StrLen + 1, DataLen);
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000822 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
823 Out.write(path, KeyLen);
824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000826 void EmitData(llvm::raw_ostream& Out, key_type_ref,
827 data_type_ref Data, unsigned DataLen) {
828 using namespace clang::io;
829 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000831 // Result of stat()
832 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000834 if (Data.first == 0) {
835 Emit32(Out, (uint32_t) Data.second.st_ino);
836 Emit32(Out, (uint32_t) Data.second.st_dev);
837 Emit16(Out, (uint16_t) Data.second.st_mode);
838 Emit64(Out, (uint64_t) Data.second.st_mtime);
839 Emit64(Out, (uint64_t) Data.second.st_size);
840 }
841
842 assert(Out.tell() - Start == DataLen && "Wrong data length");
843 }
844};
845} // end anonymous namespace
846
847/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000848void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
849 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000850 // Build the on-disk hash table containing information about every
851 // stat() call.
852 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
853 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000854 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000855 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000856 Stat != StatEnd; ++Stat, ++NumStatEntries) {
857 const char *Filename = Stat->first();
858 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
859 Generator.insert(Filename, Stat->second);
860 }
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000862 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000863 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000864 uint32_t BucketOffset;
865 {
866 llvm::raw_svector_ostream Out(StatCacheData);
867 // Make sure that no bucket is at offset 0
868 clang::io::Emit32(Out, 0);
869 BucketOffset = Generator.Emit(Out);
870 }
871
872 // Create a blob abbreviation
873 using namespace llvm;
874 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
875 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
879 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
880
881 // Write the stat cache
882 RecordData Record;
883 Record.push_back(pch::STAT_CACHE);
884 Record.push_back(BucketOffset);
885 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000886 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000887}
888
889//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000890// Source Manager Serialization
891//===----------------------------------------------------------------------===//
892
893/// \brief Create an abbreviation for the SLocEntry that refers to a
894/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000895static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000896 using namespace llvm;
897 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
898 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000904 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000905}
906
907/// \brief Create an abbreviation for the SLocEntry that refers to a
908/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000909static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000910 using namespace llvm;
911 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
912 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000918 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000919}
920
921/// \brief Create an abbreviation for the SLocEntry that refers to a
922/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000923static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000924 using namespace llvm;
925 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
926 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000928 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000929}
930
931/// \brief Create an abbreviation for the SLocEntry that refers to an
932/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000933static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000934 using namespace llvm;
935 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
936 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000942 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000943}
944
945/// \brief Writes the block containing the serialized form of the
946/// source manager.
947///
948/// TODO: We should probably use an on-disk hash table (stored in a
949/// blob), indexed based on the file name, so that we only create
950/// entries for files that we actually need. In the common case (no
951/// errors), we probably won't have to create file entries for any of
952/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000953void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000954 const Preprocessor &PP,
955 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000956 RecordData Record;
957
Chris Lattnerf04ad692009-04-10 17:16:57 +0000958 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000959 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000960
961 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000962 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
963 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
964 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
965 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000966
Douglas Gregorbd945002009-04-13 16:31:14 +0000967 // Write the line table.
968 if (SourceMgr.hasLineTable()) {
969 LineTableInfo &LineTable = SourceMgr.getLineTable();
970
971 // Emit the file names
972 Record.push_back(LineTable.getNumFilenames());
973 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
974 // Emit the file name
975 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000976 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +0000977 unsigned FilenameLen = Filename? strlen(Filename) : 0;
978 Record.push_back(FilenameLen);
979 if (FilenameLen)
980 Record.insert(Record.end(), Filename, Filename + FilenameLen);
981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Douglas Gregorbd945002009-04-13 16:31:14 +0000983 // Emit the line entries
984 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
985 L != LEnd; ++L) {
986 // Emit the file ID
987 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregorbd945002009-04-13 16:31:14 +0000989 // Emit the line entries
990 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000991 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +0000992 LEEnd = L->second.end();
993 LE != LEEnd; ++LE) {
994 Record.push_back(LE->FileOffset);
995 Record.push_back(LE->LineNo);
996 Record.push_back(LE->FilenameID);
997 Record.push_back((unsigned)LE->FileKind);
998 Record.push_back(LE->IncludeOffset);
999 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001000 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001001 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001002 }
1003
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001004 // Write out entries for all of the header files we know about.
Mike Stump1eb44332009-09-09 15:08:12 +00001005 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001006 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001007 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001008 E = HS.header_file_end();
1009 I != E; ++I) {
1010 Record.push_back(I->isImport);
1011 Record.push_back(I->DirInfo);
1012 Record.push_back(I->NumIncludes);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001013 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001014 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1015 Record.clear();
1016 }
1017
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001018 // Write out the source location entry table. We skip the first
1019 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001020 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001021 RecordData PreloadSLocs;
1022 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001023 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1024 // Get this source location entry.
1025 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1026
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001027 // Record the offset of this source-location entry.
1028 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1029
1030 // Figure out which record code to use.
1031 unsigned Code;
1032 if (SLoc->isFile()) {
1033 if (SLoc->getFile().getContentCache()->Entry)
1034 Code = pch::SM_SLOC_FILE_ENTRY;
1035 else
1036 Code = pch::SM_SLOC_BUFFER_ENTRY;
1037 } else
1038 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1039 Record.clear();
1040 Record.push_back(Code);
1041
1042 Record.push_back(SLoc->getOffset());
1043 if (SLoc->isFile()) {
1044 const SrcMgr::FileInfo &File = SLoc->getFile();
1045 Record.push_back(File.getIncludeLoc().getRawEncoding());
1046 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1047 Record.push_back(File.hasLineDirectives());
1048
1049 const SrcMgr::ContentCache *Content = File.getContentCache();
1050 if (Content->Entry) {
1051 // The source location entry is a file. The blob associated
1052 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Douglas Gregore650c8c2009-07-07 00:12:59 +00001054 // Turn the file name into an absolute path, if it isn't already.
1055 const char *Filename = Content->Entry->getName();
1056 llvm::sys::Path FilePath(Filename, strlen(Filename));
1057 std::string FilenameStr;
1058 if (!FilePath.isAbsolute()) {
1059 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +00001060 P.appendComponent(FilePath.str());
1061 FilenameStr = P.str();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001062 Filename = FilenameStr.c_str();
1063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Douglas Gregore650c8c2009-07-07 00:12:59 +00001065 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001066 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001067
1068 // FIXME: For now, preload all file source locations, so that
1069 // we get the appropriate File entries in the reader. This is
1070 // a temporary measure.
1071 PreloadSLocs.push_back(SLocEntryOffsets.size());
1072 } else {
1073 // The source location entry is a buffer. The blob associated
1074 // with this entry contains the contents of the buffer.
1075
1076 // We add one to the size so that we capture the trailing NULL
1077 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1078 // the reader side).
1079 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1080 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001081 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1082 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001083 Record.clear();
1084 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1085 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001086 llvm::StringRef(Buffer->getBufferStart(),
1087 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001088
1089 if (strcmp(Name, "<built-in>") == 0)
1090 PreloadSLocs.push_back(SLocEntryOffsets.size());
1091 }
1092 } else {
1093 // The source location entry is an instantiation.
1094 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1095 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1096 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1097 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1098
1099 // Compute the token length for this macro expansion.
1100 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001101 if (I + 1 != N)
1102 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001103 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1104 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1105 }
1106 }
1107
Douglas Gregorc9490c02009-04-16 22:23:12 +00001108 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001109
1110 if (SLocEntryOffsets.empty())
1111 return;
1112
1113 // Write the source-location offsets table into the PCH block. This
1114 // table is used for lazily loading source-location information.
1115 using namespace llvm;
1116 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1117 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1118 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1119 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1120 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1121 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001123 Record.clear();
1124 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1125 Record.push_back(SLocEntryOffsets.size());
1126 Record.push_back(SourceMgr.getNextOffset());
1127 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001128 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001129 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001130
1131 // Write the source location entry preloads array, telling the PCH
1132 // reader which source locations entries it should load eagerly.
1133 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001134}
1135
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001136//===----------------------------------------------------------------------===//
1137// Preprocessor Serialization
1138//===----------------------------------------------------------------------===//
1139
Chris Lattner0b1fb982009-04-10 17:15:23 +00001140/// \brief Writes the block containing the serialized form of the
1141/// preprocessor.
1142///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001143void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001144 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001145
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001146 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1147 if (PP.getCounterValue() != 0) {
1148 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001149 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001150 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001151 }
1152
1153 // Enter the preprocessor block.
1154 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001156 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1157 // FIXME: use diagnostics subsystem for localization etc.
1158 if (PP.SawDateOrTime())
1159 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001161 // Loop over all the macro definitions that are live at the end of the file,
1162 // emitting each to the PP section.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001163 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1164 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001165 // FIXME: This emits macros in hash table order, we should do it in a stable
1166 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001167 MacroInfo *MI = I->second;
1168
1169 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1170 // been redefined by the header (in which case they are not isBuiltinMacro).
1171 if (MI->isBuiltinMacro())
1172 continue;
1173
Chris Lattner7356a312009-04-11 21:15:38 +00001174 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001175 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001176 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1177 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001179 unsigned Code;
1180 if (MI->isObjectLike()) {
1181 Code = pch::PP_MACRO_OBJECT_LIKE;
1182 } else {
1183 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001185 Record.push_back(MI->isC99Varargs());
1186 Record.push_back(MI->isGNUVarargs());
1187 Record.push_back(MI->getNumArgs());
1188 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1189 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001190 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001191 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001192 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001193 Record.clear();
1194
Chris Lattnerdf961c22009-04-10 18:08:30 +00001195 // Emit the tokens array.
1196 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1197 // Note that we know that the preprocessor does not have any annotation
1198 // tokens in it because they are created by the parser, and thus can't be
1199 // in a macro definition.
1200 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Chris Lattnerdf961c22009-04-10 18:08:30 +00001202 Record.push_back(Tok.getLocation().getRawEncoding());
1203 Record.push_back(Tok.getLength());
1204
Chris Lattnerdf961c22009-04-10 18:08:30 +00001205 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1206 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001207 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Chris Lattnerdf961c22009-04-10 18:08:30 +00001209 // FIXME: Should translate token kind to a stable encoding.
1210 Record.push_back(Tok.getKind());
1211 // FIXME: Should translate token flags to a stable encoding.
1212 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Douglas Gregorc9490c02009-04-16 22:23:12 +00001214 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001215 Record.clear();
1216 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001217 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001218 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001219 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001220}
1221
Douglas Gregor2e222532009-07-02 17:08:52 +00001222void PCHWriter::WriteComments(ASTContext &Context) {
1223 using namespace llvm;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Douglas Gregor2e222532009-07-02 17:08:52 +00001225 if (Context.Comments.empty())
1226 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregor2e222532009-07-02 17:08:52 +00001228 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1229 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1230 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1231 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Douglas Gregor2e222532009-07-02 17:08:52 +00001233 RecordData Record;
1234 Record.push_back(pch::COMMENT_RANGES);
Mike Stump1eb44332009-09-09 15:08:12 +00001235 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregor2e222532009-07-02 17:08:52 +00001236 (const char*)&Context.Comments[0],
1237 Context.Comments.size() * sizeof(SourceRange));
1238}
1239
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001240//===----------------------------------------------------------------------===//
1241// Type Serialization
1242//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001243
Douglas Gregor2cf26342009-04-09 22:27:44 +00001244/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001245void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001246 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001247 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001248 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Douglas Gregor2cf26342009-04-09 22:27:44 +00001250 // Record the offset for this type.
1251 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001252 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001253 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1254 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001255 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001256 }
1257
1258 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Douglas Gregor2cf26342009-04-09 22:27:44 +00001260 // Emit the type's representation.
1261 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001262
Douglas Gregora4923eb2009-11-16 21:35:15 +00001263 if (T.hasLocalNonFastQualifiers()) {
1264 Qualifiers Qs = T.getLocalQualifiers();
1265 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001266 Record.push_back(Qs.getAsOpaqueValue());
1267 W.Code = pch::TYPE_EXT_QUAL;
1268 } else {
1269 switch (T->getTypeClass()) {
1270 // For all of the concrete, non-dependent types, call the
1271 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001272#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001273 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001274#define ABSTRACT_TYPE(Class, Base)
1275#define DEPENDENT_TYPE(Class, Base)
1276#include "clang/AST/TypeNodes.def"
1277
John McCall0953e762009-09-24 19:53:00 +00001278 // For all of the dependent type nodes (which only occur in C++
1279 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001280#define TYPE(Class, Base)
1281#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1282#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001283 assert(false && "Cannot serialize dependent type nodes");
1284 break;
1285 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001286 }
1287
1288 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001289 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001290
1291 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001292 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001293}
1294
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001295//===----------------------------------------------------------------------===//
1296// Declaration Serialization
1297//===----------------------------------------------------------------------===//
1298
Douglas Gregor2cf26342009-04-09 22:27:44 +00001299/// \brief Write the block containing all of the declaration IDs
1300/// lexically declared within the given DeclContext.
1301///
1302/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1303/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001304uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001305 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001306 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001307 return 0;
1308
Douglas Gregorc9490c02009-04-16 22:23:12 +00001309 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001310 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001311 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1312 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001313 AddDeclRef(*D, Record);
1314
Douglas Gregor25123082009-04-22 22:34:57 +00001315 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001316 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001317 return Offset;
1318}
1319
1320/// \brief Write the block containing all of the declaration IDs
1321/// visible from the given DeclContext.
1322///
1323/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1324/// bistream, or 0 if no block was written.
1325uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1326 DeclContext *DC) {
1327 if (DC->getPrimaryContext() != DC)
1328 return 0;
1329
Douglas Gregoraff22df2009-04-21 22:32:33 +00001330 // Since there is no name lookup into functions or methods, and we
1331 // perform name lookup for the translation unit via the
1332 // IdentifierInfo chains, don't bother to build a
1333 // visible-declarations table for these entities.
1334 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001335 return 0;
1336
Douglas Gregor2cf26342009-04-09 22:27:44 +00001337 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001338 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001339
1340 // Serialize the contents of the mapping used for lookup. Note that,
1341 // although we have two very different code paths, the serialized
1342 // representation is the same for both cases: a declaration name,
1343 // followed by a size, followed by references to the visible
1344 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001345 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001346 RecordData Record;
1347 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001348 if (!Map)
1349 return 0;
1350
Douglas Gregor2cf26342009-04-09 22:27:44 +00001351 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1352 D != DEnd; ++D) {
1353 AddDeclarationName(D->first, Record);
1354 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1355 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001356 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001357 AddDeclRef(*Result.first, Record);
1358 }
1359
1360 if (Record.size() == 0)
1361 return 0;
1362
Douglas Gregorc9490c02009-04-16 22:23:12 +00001363 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001364 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001365 return Offset;
1366}
1367
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001368//===----------------------------------------------------------------------===//
1369// Global Method Pool and Selector Serialization
1370//===----------------------------------------------------------------------===//
1371
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001372namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001373// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001374class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001375 PCHWriter &Writer;
1376
1377public:
1378 typedef Selector key_type;
1379 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001381 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1382 typedef const data_type& data_type_ref;
1383
1384 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001386 static unsigned ComputeHash(Selector Sel) {
1387 unsigned N = Sel.getNumArgs();
1388 if (N == 0)
1389 ++N;
1390 unsigned R = 5381;
1391 for (unsigned I = 0; I != N; ++I)
1392 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001393 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001394 return R;
1395 }
Mike Stump1eb44332009-09-09 15:08:12 +00001396
1397 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001398 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1399 data_type_ref Methods) {
1400 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1401 clang::io::Emit16(Out, KeyLen);
1402 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001403 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001404 Method = Method->Next)
1405 if (Method->Method)
1406 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001407 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001408 Method = Method->Next)
1409 if (Method->Method)
1410 DataLen += 4;
1411 clang::io::Emit16(Out, DataLen);
1412 return std::make_pair(KeyLen, DataLen);
1413 }
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Douglas Gregor83941df2009-04-25 17:48:32 +00001415 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001416 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001417 assert((Start >> 32) == 0 && "Selector key offset too large");
1418 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001419 unsigned N = Sel.getNumArgs();
1420 clang::io::Emit16(Out, N);
1421 if (N == 0)
1422 N = 1;
1423 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001424 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001425 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1426 }
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001428 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001429 data_type_ref Methods, unsigned DataLen) {
1430 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001431 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001432 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001433 Method = Method->Next)
1434 if (Method->Method)
1435 ++NumInstanceMethods;
1436
1437 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001438 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001439 Method = Method->Next)
1440 if (Method->Method)
1441 ++NumFactoryMethods;
1442
1443 clang::io::Emit16(Out, NumInstanceMethods);
1444 clang::io::Emit16(Out, NumFactoryMethods);
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 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001449 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001450 Method = Method->Next)
1451 if (Method->Method)
1452 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001453
1454 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001455 }
1456};
1457} // end anonymous namespace
1458
1459/// \brief Write the method pool into the PCH file.
1460///
1461/// The method pool contains both instance and factory methods, stored
1462/// in an on-disk hash table indexed by the selector.
1463void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1464 using namespace llvm;
1465
1466 // Create and write out the blob that contains the instance and
1467 // factor method pools.
1468 bool Empty = true;
1469 {
1470 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001472 // Create the on-disk hash table representation. Start by
1473 // iterating through the instance method pool.
1474 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001475 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001476 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001477 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001478 InstanceEnd = SemaRef.InstanceMethodPool.end();
1479 Instance != InstanceEnd; ++Instance) {
1480 // Check whether there is a factory method with the same
1481 // selector.
1482 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1483 = SemaRef.FactoryMethodPool.find(Instance->first);
1484
1485 if (Factory == SemaRef.FactoryMethodPool.end())
1486 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001487 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001488 ObjCMethodList()));
1489 else
1490 Generator.insert(Instance->first,
1491 std::make_pair(Instance->second, Factory->second));
1492
Douglas Gregor83941df2009-04-25 17:48:32 +00001493 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001494 Empty = false;
1495 }
1496
1497 // Now iterate through the factory method pool, to pick up any
1498 // selectors that weren't already in the instance method pool.
1499 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001500 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001501 FactoryEnd = SemaRef.FactoryMethodPool.end();
1502 Factory != FactoryEnd; ++Factory) {
1503 // Check whether there is an instance method with the same
1504 // selector. If so, there is no work to do here.
1505 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1506 = SemaRef.InstanceMethodPool.find(Factory->first);
1507
Douglas Gregor83941df2009-04-25 17:48:32 +00001508 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001509 Generator.insert(Factory->first,
1510 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001511 ++NumSelectorsInMethodPool;
1512 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001513
1514 Empty = false;
1515 }
1516
Douglas Gregor83941df2009-04-25 17:48:32 +00001517 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001518 return;
1519
1520 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001521 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001522 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001523 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001524 {
1525 PCHMethodPoolTrait Trait(*this);
1526 llvm::raw_svector_ostream Out(MethodPool);
1527 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001528 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001529 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001530
1531 // For every selector that we have seen but which was not
1532 // written into the hash table, write the selector itself and
1533 // record it's offset.
1534 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1535 if (SelectorOffsets[I] == 0)
1536 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001537 }
1538
1539 // Create a blob abbreviation
1540 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1541 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1545 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1546
Douglas Gregor83941df2009-04-25 17:48:32 +00001547 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001548 RecordData Record;
1549 Record.push_back(pch::METHOD_POOL);
1550 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001551 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001552 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001553
1554 // Create a blob abbreviation for the selector table offsets.
1555 Abbrev = new BitCodeAbbrev();
1556 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1559 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1560
1561 // Write the selector offsets table.
1562 Record.clear();
1563 Record.push_back(pch::SELECTOR_OFFSETS);
1564 Record.push_back(SelectorOffsets.size());
1565 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1566 (const char *)&SelectorOffsets.front(),
1567 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001568 }
1569}
1570
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001571//===----------------------------------------------------------------------===//
1572// Identifier Table Serialization
1573//===----------------------------------------------------------------------===//
1574
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001575namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001576class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001577 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001578 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001579
Douglas Gregora92193e2009-04-28 21:18:29 +00001580 /// \brief Determines whether this is an "interesting" identifier
1581 /// that needs a full IdentifierInfo structure written into the hash
1582 /// table.
1583 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1584 return II->isPoisoned() ||
1585 II->isExtensionToken() ||
1586 II->hasMacroDefinition() ||
1587 II->getObjCOrBuiltinID() ||
1588 II->getFETokenInfo<void>();
1589 }
1590
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001591public:
1592 typedef const IdentifierInfo* key_type;
1593 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001595 typedef pch::IdentID data_type;
1596 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001597
1598 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001599 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001600
1601 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001602 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001603 }
Mike Stump1eb44332009-09-09 15:08:12 +00001604
1605 std::pair<unsigned,unsigned>
1606 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001607 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001608 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001609 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1610 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001611 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001612 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001613 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001614 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001615 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1616 DEnd = IdentifierResolver::end();
1617 D != DEnd; ++D)
1618 DataLen += sizeof(pch::DeclID);
1619 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001620 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001621 // We emit the key length after the data length so that every
1622 // string is preceded by a 16-bit length. This matches the PTH
1623 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001624 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001625 return std::make_pair(KeyLen, DataLen);
1626 }
Mike Stump1eb44332009-09-09 15:08:12 +00001627
1628 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001629 unsigned KeyLen) {
1630 // Record the location of the key data. This is used when generating
1631 // the mapping from persistent IDs to strings.
1632 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001633 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001634 }
Mike Stump1eb44332009-09-09 15:08:12 +00001635
1636 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001637 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001638 if (!isInterestingIdentifier(II)) {
1639 clang::io::Emit32(Out, ID << 1);
1640 return;
1641 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001642
Douglas Gregora92193e2009-04-28 21:18:29 +00001643 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001644 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001645 bool hasMacroDefinition =
1646 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001647 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001648 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001649 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1650 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1651 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1652 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001653 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001654
Douglas Gregor37e26842009-04-21 23:56:24 +00001655 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001656 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001657
Douglas Gregor668c1a42009-04-21 22:25:48 +00001658 // Emit the declaration IDs in reverse order, because the
1659 // IdentifierResolver provides the declarations as they would be
1660 // visible (e.g., the function "stat" would come before the struct
1661 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1662 // adds declarations to the end of the list (so we need to see the
1663 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001664 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001665 IdentifierResolver::end());
1666 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1667 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001668 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001669 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001670 }
1671};
1672} // end anonymous namespace
1673
Douglas Gregorafaf3082009-04-11 00:14:32 +00001674/// \brief Write the identifier table into the PCH file.
1675///
1676/// The identifier table consists of a blob containing string data
1677/// (the actual identifiers themselves) and a separate "offsets" index
1678/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001679void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001680 using namespace llvm;
1681
1682 // Create and write out the blob that contains the identifier
1683 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001684 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001685 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Douglas Gregor92b059e2009-04-28 20:33:11 +00001687 // Look for any identifiers that were named while processing the
1688 // headers, but are otherwise not needed. We add these to the hash
1689 // table to enable checking of the predefines buffer in the case
1690 // where the user adds new macro definitions when building the PCH
1691 // file.
1692 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1693 IDEnd = PP.getIdentifierTable().end();
1694 ID != IDEnd; ++ID)
1695 getIdentifierRef(ID->second);
1696
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001697 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001698 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001699 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1700 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1701 ID != IDEnd; ++ID) {
1702 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001703 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001704 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001705
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001706 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001707 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001708 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001709 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001710 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001711 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001712 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001713 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001714 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001715 }
1716
1717 // Create a blob abbreviation
1718 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1719 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001720 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001721 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001722 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001723
1724 // Write the identifier table
1725 RecordData Record;
1726 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001727 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001728 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001729 }
1730
1731 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001732 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1733 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1736 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1737
1738 RecordData Record;
1739 Record.push_back(pch::IDENTIFIER_OFFSET);
1740 Record.push_back(IdentifierOffsets.size());
1741 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1742 (const char *)&IdentifierOffsets.front(),
1743 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001744}
1745
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001746//===----------------------------------------------------------------------===//
1747// General Serialization Routines
1748//===----------------------------------------------------------------------===//
1749
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001750/// \brief Write a record containing the given attributes.
1751void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1752 RecordData Record;
1753 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001754 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001755 Record.push_back(Attr->isInherited());
1756 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001757 default:
1758 assert(0 && "Does not support PCH writing for this attribute yet!");
1759 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001760 case Attr::Alias:
1761 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1762 break;
1763
1764 case Attr::Aligned:
1765 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1766 break;
1767
1768 case Attr::AlwaysInline:
1769 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001771 case Attr::AnalyzerNoReturn:
1772 break;
1773
1774 case Attr::Annotate:
1775 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1776 break;
1777
1778 case Attr::AsmLabel:
1779 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1780 break;
1781
Sean Hunt7725e672009-11-25 04:20:27 +00001782 case Attr::BaseCheck:
1783 break;
1784
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001785 case Attr::Blocks:
1786 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1787 break;
1788
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001789 case Attr::CDecl:
1790 break;
1791
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001792 case Attr::Cleanup:
1793 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1794 break;
1795
1796 case Attr::Const:
1797 break;
1798
1799 case Attr::Constructor:
1800 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1801 break;
1802
1803 case Attr::DLLExport:
1804 case Attr::DLLImport:
1805 case Attr::Deprecated:
1806 break;
1807
1808 case Attr::Destructor:
1809 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1810 break;
1811
1812 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001813 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001814 break;
1815
1816 case Attr::Format: {
1817 const FormatAttr *Format = cast<FormatAttr>(Attr);
1818 AddString(Format->getType(), Record);
1819 Record.push_back(Format->getFormatIdx());
1820 Record.push_back(Format->getFirstArg());
1821 break;
1822 }
1823
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001824 case Attr::FormatArg: {
1825 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1826 Record.push_back(Format->getFormatIdx());
1827 break;
1828 }
1829
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001830 case Attr::Sentinel : {
1831 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1832 Record.push_back(Sentinel->getSentinel());
1833 Record.push_back(Sentinel->getNullPos());
1834 break;
1835 }
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Chris Lattnercf2a7212009-04-20 19:12:28 +00001837 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001838 case Attr::Hiding:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001839 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001840 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001841 case Attr::NoDebug:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001842 case Attr::NoReturn:
1843 case Attr::NoThrow:
Mike Stump1feade82009-08-26 22:31:08 +00001844 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001845 break;
1846
1847 case Attr::NonNull: {
1848 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1849 Record.push_back(NonNull->size());
1850 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1851 break;
1852 }
1853
1854 case Attr::ObjCException:
1855 case Attr::ObjCNSObject:
Ted Kremenekb71368d2009-05-09 02:44:38 +00001856 case Attr::CFReturnsRetained:
1857 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001858 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001859 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001860 break;
1861
Anders Carlssona860e752009-08-08 18:23:56 +00001862 case Attr::PragmaPack:
1863 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001864 break;
1865
Anders Carlssona860e752009-08-08 18:23:56 +00001866 case Attr::Packed:
1867 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001869 case Attr::Pure:
1870 break;
1871
1872 case Attr::Regparm:
1873 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1874 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Nate Begeman6f3d8382009-06-26 06:32:41 +00001876 case Attr::ReqdWorkGroupSize:
1877 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1878 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1879 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1880 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001881
1882 case Attr::Section:
1883 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1884 break;
1885
1886 case Attr::StdCall:
1887 case Attr::TransparentUnion:
1888 case Attr::Unavailable:
1889 case Attr::Unused:
1890 case Attr::Used:
1891 break;
1892
1893 case Attr::Visibility:
1894 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001895 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001896 break;
1897
1898 case Attr::WarnUnusedResult:
1899 case Attr::Weak:
1900 case Attr::WeakImport:
1901 break;
1902 }
1903 }
1904
Douglas Gregorc9490c02009-04-16 22:23:12 +00001905 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001906}
1907
1908void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1909 Record.push_back(Str.size());
1910 Record.insert(Record.end(), Str.begin(), Str.end());
1911}
1912
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001913/// \brief Note that the identifier II occurs at the given offset
1914/// within the identifier table.
1915void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001916 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001917}
1918
Douglas Gregor83941df2009-04-25 17:48:32 +00001919/// \brief Note that the selector Sel occurs at the given offset
1920/// within the method pool/selector table.
1921void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1922 unsigned ID = SelectorIDs[Sel];
1923 assert(ID && "Unknown selector");
1924 SelectorOffsets[ID - 1] = Offset;
1925}
1926
Mike Stump1eb44332009-09-09 15:08:12 +00001927PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1928 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00001929 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1930 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001931
Douglas Gregore650c8c2009-07-07 00:12:59 +00001932void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1933 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001934 using namespace llvm;
1935
Douglas Gregore7785042009-04-20 15:53:59 +00001936 ASTContext &Context = SemaRef.Context;
1937 Preprocessor &PP = SemaRef.PP;
1938
Douglas Gregor2cf26342009-04-09 22:27:44 +00001939 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001940 Stream.Emit((unsigned)'C', 8);
1941 Stream.Emit((unsigned)'P', 8);
1942 Stream.Emit((unsigned)'C', 8);
1943 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001945 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001946
1947 // The translation unit is the first declaration we'll emit.
1948 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001949 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001950
Douglas Gregor2deaea32009-04-22 18:49:13 +00001951 // Make sure that we emit IdentifierInfos (and any attached
1952 // declarations) for builtins.
1953 {
1954 IdentifierTable &Table = PP.getIdentifierTable();
1955 llvm::SmallVector<const char *, 32> BuiltinNames;
1956 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1957 Context.getLangOptions().NoBuiltin);
1958 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1959 getIdentifierRef(&Table.get(BuiltinNames[I]));
1960 }
1961
Chris Lattner63d65f82009-09-08 18:19:27 +00001962 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00001963 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00001964 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001965 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001966 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
1967 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00001968 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001969
Douglas Gregor14c22f22009-04-22 22:18:58 +00001970 // Build a record containing all of the locally-scoped external
1971 // declarations in this header file. Generally, this record will be
1972 // empty.
1973 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00001974 // FIXME: This is filling in the PCH file in densemap order which is
1975 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00001976 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00001977 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1978 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1979 TD != TDEnd; ++TD)
1980 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1981
Douglas Gregorb81c1702009-04-27 20:06:05 +00001982 // Build a record containing all of the ext_vector declarations.
1983 RecordData ExtVectorDecls;
1984 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1985 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1986
Douglas Gregor2cf26342009-04-09 22:27:44 +00001987 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001988 RecordData Record;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001989 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001990 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001991 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001992 if (StatCalls && !isysroot)
1993 WriteStatCache(*StatCalls, isysroot);
1994 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump1eb44332009-09-09 15:08:12 +00001995 WriteComments(Context);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001996 // Write the record of special types.
1997 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001999 AddTypeRef(Context.getBuiltinVaListType(), Record);
2000 AddTypeRef(Context.getObjCIdType(), Record);
2001 AddTypeRef(Context.getObjCSelType(), Record);
2002 AddTypeRef(Context.getObjCProtoType(), Record);
2003 AddTypeRef(Context.getObjCClassType(), Record);
2004 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2005 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2006 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002007 AddTypeRef(Context.getjmp_bufType(), Record);
2008 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002009 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2010 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002011#if 0
2012 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00002013 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002014#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00002015 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002016 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002017 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Douglas Gregor366809a2009-04-26 03:49:13 +00002019 // Keep writing types and declarations until all types and
2020 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002021 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2022 WriteDeclsBlockAbbrevs();
2023 while (!DeclTypesToEmit.empty()) {
2024 DeclOrType DOT = DeclTypesToEmit.front();
2025 DeclTypesToEmit.pop();
2026 if (DOT.isType())
2027 WriteType(DOT.getType());
2028 else
2029 WriteDecl(Context, DOT.getDecl());
2030 }
2031 Stream.ExitBlock();
2032
Douglas Gregor813a97b2009-10-17 17:25:45 +00002033 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002034 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002035 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002036
2037 // Write the type offsets array
2038 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2039 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2042 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2043 Record.clear();
2044 Record.push_back(pch::TYPE_OFFSET);
2045 Record.push_back(TypeOffsets.size());
2046 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002047 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002048 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002050 // Write the declaration offsets array
2051 Abbrev = new BitCodeAbbrev();
2052 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2055 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2056 Record.clear();
2057 Record.push_back(pch::DECL_OFFSET);
2058 Record.push_back(DeclOffsets.size());
2059 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002060 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002061 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002062
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002063 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002064 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002065 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002066
2067 // Write the record containing tentative definitions.
2068 if (!TentativeDefinitions.empty())
2069 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002070
2071 // Write the record containing locally-scoped external definitions.
2072 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002073 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002074 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002075
2076 // Write the record containing ext_vector type names.
2077 if (!ExtVectorDecls.empty())
2078 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002079
Douglas Gregor3e1af842009-04-17 22:13:46 +00002080 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002081 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002082 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002083 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002084 Record.push_back(NumLexicalDeclContexts);
2085 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002086 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002087 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002088}
2089
2090void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2091 Record.push_back(Loc.getRawEncoding());
2092}
2093
2094void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2095 Record.push_back(Value.getBitWidth());
2096 unsigned N = Value.getNumWords();
2097 const uint64_t* Words = Value.getRawData();
2098 for (unsigned I = 0; I != N; ++I)
2099 Record.push_back(Words[I]);
2100}
2101
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002102void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2103 Record.push_back(Value.isUnsigned());
2104 AddAPInt(Value, Record);
2105}
2106
Douglas Gregor17fc2232009-04-14 21:55:33 +00002107void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2108 AddAPInt(Value.bitcastToAPInt(), Record);
2109}
2110
Douglas Gregor2cf26342009-04-09 22:27:44 +00002111void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002112 Record.push_back(getIdentifierRef(II));
2113}
2114
2115pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2116 if (II == 0)
2117 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002118
2119 pch::IdentID &ID = IdentifierIDs[II];
2120 if (ID == 0)
2121 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002122 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002123}
2124
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002125void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2126 if (SelRef.getAsOpaquePtr() == 0) {
2127 Record.push_back(0);
2128 return;
2129 }
2130
2131 pch::SelectorID &SID = SelectorIDs[SelRef];
2132 if (SID == 0) {
2133 SID = SelectorIDs.size();
2134 SelVector.push_back(SelRef);
2135 }
2136 Record.push_back(SID);
2137}
2138
John McCall833ca992009-10-29 08:12:44 +00002139void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2140 RecordData &Record) {
2141 switch (Arg.getArgument().getKind()) {
2142 case TemplateArgument::Expression:
2143 AddStmt(Arg.getLocInfo().getAsExpr());
2144 break;
2145 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002146 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002147 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002148 case TemplateArgument::Template:
2149 Record.push_back(
2150 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2151 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2152 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2153 break;
John McCall833ca992009-10-29 08:12:44 +00002154 case TemplateArgument::Null:
2155 case TemplateArgument::Integral:
2156 case TemplateArgument::Declaration:
2157 case TemplateArgument::Pack:
2158 break;
2159 }
2160}
2161
John McCalla93c9342009-12-07 02:54:59 +00002162void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2163 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002164 AddTypeRef(QualType(), Record);
2165 return;
2166 }
2167
John McCalla93c9342009-12-07 02:54:59 +00002168 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002169 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002170 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002171 TLW.Visit(TL);
2172}
2173
Douglas Gregor2cf26342009-04-09 22:27:44 +00002174void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2175 if (T.isNull()) {
2176 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2177 return;
2178 }
2179
Douglas Gregora4923eb2009-11-16 21:35:15 +00002180 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002181 T.removeFastQualifiers();
2182
Douglas Gregora4923eb2009-11-16 21:35:15 +00002183 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002184 pch::TypeID &ID = TypeIDs[T];
2185 if (ID == 0) {
2186 // We haven't seen these qualifiers applied to this type before.
2187 // Assign it a new ID. This is the only time we enqueue a
2188 // qualified type, and it has no CV qualifiers.
2189 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002190 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002191 }
2192
2193 // Encode the type qualifiers in the type reference.
2194 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2195 return;
2196 }
2197
Douglas Gregora4923eb2009-11-16 21:35:15 +00002198 assert(!T.hasLocalQualifiers());
John McCall0953e762009-09-24 19:53:00 +00002199
Douglas Gregor2cf26342009-04-09 22:27:44 +00002200 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002201 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002202 switch (BT->getKind()) {
2203 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2204 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2205 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2206 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2207 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2208 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2209 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2210 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002211 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002212 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2213 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2214 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2215 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2216 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2217 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2218 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002219 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002220 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2221 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2222 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002223 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002224 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2225 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002226 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2227 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002228 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2229 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002230 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002231 case BuiltinType::UndeducedAuto:
2232 assert(0 && "Should not see undeduced auto here");
2233 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002234 }
2235
John McCall0953e762009-09-24 19:53:00 +00002236 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002237 return;
2238 }
2239
John McCall0953e762009-09-24 19:53:00 +00002240 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002241 if (ID == 0) {
2242 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002243 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002244 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002245 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002246 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002247
2248 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002249 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002250}
2251
2252void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2253 if (D == 0) {
2254 Record.push_back(0);
2255 return;
2256 }
2257
Douglas Gregor8038d512009-04-10 17:25:41 +00002258 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002259 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002260 // We haven't seen this declaration before. Give it a new ID and
2261 // enqueue it in the list of declarations to emit.
2262 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002263 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002264 }
2265
2266 Record.push_back(ID);
2267}
2268
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002269pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2270 if (D == 0)
2271 return 0;
2272
2273 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2274 return DeclIDs[D];
2275}
2276
Douglas Gregor2cf26342009-04-09 22:27:44 +00002277void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002278 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002279 Record.push_back(Name.getNameKind());
2280 switch (Name.getNameKind()) {
2281 case DeclarationName::Identifier:
2282 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2283 break;
2284
2285 case DeclarationName::ObjCZeroArgSelector:
2286 case DeclarationName::ObjCOneArgSelector:
2287 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002288 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002289 break;
2290
2291 case DeclarationName::CXXConstructorName:
2292 case DeclarationName::CXXDestructorName:
2293 case DeclarationName::CXXConversionFunctionName:
2294 AddTypeRef(Name.getCXXNameType(), Record);
2295 break;
2296
2297 case DeclarationName::CXXOperatorName:
2298 Record.push_back(Name.getCXXOverloadedOperator());
2299 break;
2300
Sean Hunt3e518bd2009-11-29 07:34:05 +00002301 case DeclarationName::CXXLiteralOperatorName:
2302 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2303 break;
2304
Douglas Gregor2cf26342009-04-09 22:27:44 +00002305 case DeclarationName::CXXUsingDirective:
2306 // No extra data to emit
2307 break;
2308 }
2309}
Douglas Gregor0b748912009-04-14 21:18:50 +00002310