blob: 97cd84f44713da2f400b0e8fd29e1c5667969f7c [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);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000546 RECORD(SVN_BRANCH_REVISION);
547
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
710 // Subversion branch/version information.
711 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
712 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
713 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
714 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
715 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
716 Record.clear();
717 Record.push_back(pch::SVN_BRANCH_REVISION);
718 Record.push_back(getClangSubversionRevision());
719 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000720}
721
722/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000723void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
724 RecordData Record;
725 Record.push_back(LangOpts.Trigraphs);
726 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
727 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
728 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
729 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
730 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
731 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
732 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
733 Record.push_back(LangOpts.C99); // C99 Support
734 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
735 Record.push_back(LangOpts.CPlusPlus); // C++ Support
736 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000737 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000739 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
740 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
741 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000743 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000744 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
745 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000746 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000747 Record.push_back(LangOpts.Exceptions); // Support exception handling.
748
749 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
750 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
751 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
752
Chris Lattnerea5ce472009-04-27 07:35:58 +0000753 // Whether static initializers are protected by locks.
754 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000755 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000756 Record.push_back(LangOpts.Blocks); // block extension to C
757 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
758 // they are unused.
759 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
760 // (modulo the platform support).
761
762 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
763 // signed integer arithmetic overflows.
764
765 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
766 // may be ripped out at any time.
767
768 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000769 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000770 // defined.
771 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
772 // opposed to __DYNAMIC__).
773 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
774
775 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
776 // used (instead of C99 semantics).
777 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000778 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
779 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000780 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
781 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000782 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000783 Record.push_back(LangOpts.getGCMode());
784 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000785 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000786 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000787 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000788 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000789 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000790 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000791}
792
Douglas Gregor14f79002009-04-10 03:52:48 +0000793//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000794// stat cache Serialization
795//===----------------------------------------------------------------------===//
796
797namespace {
798// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000799class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000800public:
801 typedef const char * key_type;
802 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000804 typedef std::pair<int, struct stat> data_type;
805 typedef const data_type& data_type_ref;
806
807 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000808 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000809 }
Mike Stump1eb44332009-09-09 15:08:12 +0000810
811 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000812 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
813 data_type_ref Data) {
814 unsigned StrLen = strlen(path);
815 clang::io::Emit16(Out, StrLen);
816 unsigned DataLen = 1; // result value
817 if (Data.first == 0)
818 DataLen += 4 + 4 + 2 + 8 + 8;
819 clang::io::Emit8(Out, DataLen);
820 return std::make_pair(StrLen + 1, DataLen);
821 }
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000823 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
824 Out.write(path, KeyLen);
825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000827 void EmitData(llvm::raw_ostream& Out, key_type_ref,
828 data_type_ref Data, unsigned DataLen) {
829 using namespace clang::io;
830 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000832 // Result of stat()
833 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000835 if (Data.first == 0) {
836 Emit32(Out, (uint32_t) Data.second.st_ino);
837 Emit32(Out, (uint32_t) Data.second.st_dev);
838 Emit16(Out, (uint16_t) Data.second.st_mode);
839 Emit64(Out, (uint64_t) Data.second.st_mtime);
840 Emit64(Out, (uint64_t) Data.second.st_size);
841 }
842
843 assert(Out.tell() - Start == DataLen && "Wrong data length");
844 }
845};
846} // end anonymous namespace
847
848/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000849void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
850 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000851 // Build the on-disk hash table containing information about every
852 // stat() call.
853 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
854 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000855 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000857 Stat != StatEnd; ++Stat, ++NumStatEntries) {
858 const char *Filename = Stat->first();
859 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
860 Generator.insert(Filename, Stat->second);
861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000863 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000864 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000865 uint32_t BucketOffset;
866 {
867 llvm::raw_svector_ostream Out(StatCacheData);
868 // Make sure that no bucket is at offset 0
869 clang::io::Emit32(Out, 0);
870 BucketOffset = Generator.Emit(Out);
871 }
872
873 // Create a blob abbreviation
874 using namespace llvm;
875 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
876 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
880 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
881
882 // Write the stat cache
883 RecordData Record;
884 Record.push_back(pch::STAT_CACHE);
885 Record.push_back(BucketOffset);
886 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000887 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000888}
889
890//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000891// Source Manager Serialization
892//===----------------------------------------------------------------------===//
893
894/// \brief Create an abbreviation for the SLocEntry that refers to a
895/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000896static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000897 using namespace llvm;
898 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
899 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000905 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000906}
907
908/// \brief Create an abbreviation for the SLocEntry that refers to a
909/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000910static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000911 using namespace llvm;
912 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
913 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000919 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000920}
921
922/// \brief Create an abbreviation for the SLocEntry that refers to a
923/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000924static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000925 using namespace llvm;
926 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
927 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000929 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000930}
931
932/// \brief Create an abbreviation for the SLocEntry that refers to an
933/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000934static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000935 using namespace llvm;
936 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
937 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000943 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000944}
945
946/// \brief Writes the block containing the serialized form of the
947/// source manager.
948///
949/// TODO: We should probably use an on-disk hash table (stored in a
950/// blob), indexed based on the file name, so that we only create
951/// entries for files that we actually need. In the common case (no
952/// errors), we probably won't have to create file entries for any of
953/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000954void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955 const Preprocessor &PP,
956 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000957 RecordData Record;
958
Chris Lattnerf04ad692009-04-10 17:16:57 +0000959 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000960 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000961
962 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000963 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
964 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
965 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
966 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000967
Douglas Gregorbd945002009-04-13 16:31:14 +0000968 // Write the line table.
969 if (SourceMgr.hasLineTable()) {
970 LineTableInfo &LineTable = SourceMgr.getLineTable();
971
972 // Emit the file names
973 Record.push_back(LineTable.getNumFilenames());
974 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
975 // Emit the file name
976 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +0000978 unsigned FilenameLen = Filename? strlen(Filename) : 0;
979 Record.push_back(FilenameLen);
980 if (FilenameLen)
981 Record.insert(Record.end(), Filename, Filename + FilenameLen);
982 }
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Douglas Gregorbd945002009-04-13 16:31:14 +0000984 // Emit the line entries
985 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
986 L != LEnd; ++L) {
987 // Emit the file ID
988 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregorbd945002009-04-13 16:31:14 +0000990 // Emit the line entries
991 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000992 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +0000993 LEEnd = L->second.end();
994 LE != LEEnd; ++LE) {
995 Record.push_back(LE->FileOffset);
996 Record.push_back(LE->LineNo);
997 Record.push_back(LE->FilenameID);
998 Record.push_back((unsigned)LE->FileKind);
999 Record.push_back(LE->IncludeOffset);
1000 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001001 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001002 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001003 }
1004
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001005 // Write out entries for all of the header files we know about.
Mike Stump1eb44332009-09-09 15:08:12 +00001006 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001007 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001008 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001009 E = HS.header_file_end();
1010 I != E; ++I) {
1011 Record.push_back(I->isImport);
1012 Record.push_back(I->DirInfo);
1013 Record.push_back(I->NumIncludes);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001014 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001015 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1016 Record.clear();
1017 }
1018
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001019 // Write out the source location entry table. We skip the first
1020 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001021 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001022 RecordData PreloadSLocs;
1023 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001024 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1025 // Get this source location entry.
1026 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1027
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001028 // Record the offset of this source-location entry.
1029 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1030
1031 // Figure out which record code to use.
1032 unsigned Code;
1033 if (SLoc->isFile()) {
1034 if (SLoc->getFile().getContentCache()->Entry)
1035 Code = pch::SM_SLOC_FILE_ENTRY;
1036 else
1037 Code = pch::SM_SLOC_BUFFER_ENTRY;
1038 } else
1039 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1040 Record.clear();
1041 Record.push_back(Code);
1042
1043 Record.push_back(SLoc->getOffset());
1044 if (SLoc->isFile()) {
1045 const SrcMgr::FileInfo &File = SLoc->getFile();
1046 Record.push_back(File.getIncludeLoc().getRawEncoding());
1047 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1048 Record.push_back(File.hasLineDirectives());
1049
1050 const SrcMgr::ContentCache *Content = File.getContentCache();
1051 if (Content->Entry) {
1052 // The source location entry is a file. The blob associated
1053 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Douglas Gregore650c8c2009-07-07 00:12:59 +00001055 // Turn the file name into an absolute path, if it isn't already.
1056 const char *Filename = Content->Entry->getName();
1057 llvm::sys::Path FilePath(Filename, strlen(Filename));
1058 std::string FilenameStr;
1059 if (!FilePath.isAbsolute()) {
1060 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +00001061 P.appendComponent(FilePath.str());
1062 FilenameStr = P.str();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001063 Filename = FilenameStr.c_str();
1064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Douglas Gregore650c8c2009-07-07 00:12:59 +00001066 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001067 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001068
1069 // FIXME: For now, preload all file source locations, so that
1070 // we get the appropriate File entries in the reader. This is
1071 // a temporary measure.
1072 PreloadSLocs.push_back(SLocEntryOffsets.size());
1073 } else {
1074 // The source location entry is a buffer. The blob associated
1075 // with this entry contains the contents of the buffer.
1076
1077 // We add one to the size so that we capture the trailing NULL
1078 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1079 // the reader side).
1080 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1081 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001082 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1083 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001084 Record.clear();
1085 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1086 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001087 llvm::StringRef(Buffer->getBufferStart(),
1088 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001089
1090 if (strcmp(Name, "<built-in>") == 0)
1091 PreloadSLocs.push_back(SLocEntryOffsets.size());
1092 }
1093 } else {
1094 // The source location entry is an instantiation.
1095 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1096 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1097 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1098 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1099
1100 // Compute the token length for this macro expansion.
1101 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001102 if (I + 1 != N)
1103 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001104 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1105 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1106 }
1107 }
1108
Douglas Gregorc9490c02009-04-16 22:23:12 +00001109 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001110
1111 if (SLocEntryOffsets.empty())
1112 return;
1113
1114 // Write the source-location offsets table into the PCH block. This
1115 // table is used for lazily loading source-location information.
1116 using namespace llvm;
1117 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1118 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1119 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1120 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1122 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001124 Record.clear();
1125 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1126 Record.push_back(SLocEntryOffsets.size());
1127 Record.push_back(SourceMgr.getNextOffset());
1128 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001129 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001130 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131
1132 // Write the source location entry preloads array, telling the PCH
1133 // reader which source locations entries it should load eagerly.
1134 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001135}
1136
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001137//===----------------------------------------------------------------------===//
1138// Preprocessor Serialization
1139//===----------------------------------------------------------------------===//
1140
Chris Lattner0b1fb982009-04-10 17:15:23 +00001141/// \brief Writes the block containing the serialized form of the
1142/// preprocessor.
1143///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001144void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001145 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001146
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001147 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1148 if (PP.getCounterValue() != 0) {
1149 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001150 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001151 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001152 }
1153
1154 // Enter the preprocessor block.
1155 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001157 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1158 // FIXME: use diagnostics subsystem for localization etc.
1159 if (PP.SawDateOrTime())
1160 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001162 // Loop over all the macro definitions that are live at the end of the file,
1163 // emitting each to the PP section.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001164 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1165 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001166 // FIXME: This emits macros in hash table order, we should do it in a stable
1167 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001168 MacroInfo *MI = I->second;
1169
1170 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1171 // been redefined by the header (in which case they are not isBuiltinMacro).
1172 if (MI->isBuiltinMacro())
1173 continue;
1174
Chris Lattner7356a312009-04-11 21:15:38 +00001175 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001176 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001177 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1178 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001180 unsigned Code;
1181 if (MI->isObjectLike()) {
1182 Code = pch::PP_MACRO_OBJECT_LIKE;
1183 } else {
1184 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001186 Record.push_back(MI->isC99Varargs());
1187 Record.push_back(MI->isGNUVarargs());
1188 Record.push_back(MI->getNumArgs());
1189 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1190 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001191 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001192 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001193 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001194 Record.clear();
1195
Chris Lattnerdf961c22009-04-10 18:08:30 +00001196 // Emit the tokens array.
1197 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1198 // Note that we know that the preprocessor does not have any annotation
1199 // tokens in it because they are created by the parser, and thus can't be
1200 // in a macro definition.
1201 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Chris Lattnerdf961c22009-04-10 18:08:30 +00001203 Record.push_back(Tok.getLocation().getRawEncoding());
1204 Record.push_back(Tok.getLength());
1205
Chris Lattnerdf961c22009-04-10 18:08:30 +00001206 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1207 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001208 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattnerdf961c22009-04-10 18:08:30 +00001210 // FIXME: Should translate token kind to a stable encoding.
1211 Record.push_back(Tok.getKind());
1212 // FIXME: Should translate token flags to a stable encoding.
1213 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Douglas Gregorc9490c02009-04-16 22:23:12 +00001215 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001216 Record.clear();
1217 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001218 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001219 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001220 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001221}
1222
Douglas Gregor2e222532009-07-02 17:08:52 +00001223void PCHWriter::WriteComments(ASTContext &Context) {
1224 using namespace llvm;
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Douglas Gregor2e222532009-07-02 17:08:52 +00001226 if (Context.Comments.empty())
1227 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Douglas Gregor2e222532009-07-02 17:08:52 +00001229 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1230 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1231 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1232 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Douglas Gregor2e222532009-07-02 17:08:52 +00001234 RecordData Record;
1235 Record.push_back(pch::COMMENT_RANGES);
Mike Stump1eb44332009-09-09 15:08:12 +00001236 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregor2e222532009-07-02 17:08:52 +00001237 (const char*)&Context.Comments[0],
1238 Context.Comments.size() * sizeof(SourceRange));
1239}
1240
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001241//===----------------------------------------------------------------------===//
1242// Type Serialization
1243//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001244
Douglas Gregor2cf26342009-04-09 22:27:44 +00001245/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001246void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001247 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001248 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001249 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Douglas Gregor2cf26342009-04-09 22:27:44 +00001251 // Record the offset for this type.
1252 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001253 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001254 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1255 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001256 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001257 }
1258
1259 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Douglas Gregor2cf26342009-04-09 22:27:44 +00001261 // Emit the type's representation.
1262 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001263
Douglas Gregora4923eb2009-11-16 21:35:15 +00001264 if (T.hasLocalNonFastQualifiers()) {
1265 Qualifiers Qs = T.getLocalQualifiers();
1266 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001267 Record.push_back(Qs.getAsOpaqueValue());
1268 W.Code = pch::TYPE_EXT_QUAL;
1269 } else {
1270 switch (T->getTypeClass()) {
1271 // For all of the concrete, non-dependent types, call the
1272 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001273#define TYPE(Class, Base) \
John McCall0953e762009-09-24 19:53:00 +00001274 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001275#define ABSTRACT_TYPE(Class, Base)
1276#define DEPENDENT_TYPE(Class, Base)
1277#include "clang/AST/TypeNodes.def"
1278
John McCall0953e762009-09-24 19:53:00 +00001279 // For all of the dependent type nodes (which only occur in C++
1280 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001281#define TYPE(Class, Base)
1282#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1283#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001284 assert(false && "Cannot serialize dependent type nodes");
1285 break;
1286 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001287 }
1288
1289 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001290 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001291
1292 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001293 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001294}
1295
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001296//===----------------------------------------------------------------------===//
1297// Declaration Serialization
1298//===----------------------------------------------------------------------===//
1299
Douglas Gregor2cf26342009-04-09 22:27:44 +00001300/// \brief Write the block containing all of the declaration IDs
1301/// lexically declared within the given DeclContext.
1302///
1303/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1304/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001305uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001306 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001307 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001308 return 0;
1309
Douglas Gregorc9490c02009-04-16 22:23:12 +00001310 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001311 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001312 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1313 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001314 AddDeclRef(*D, Record);
1315
Douglas Gregor25123082009-04-22 22:34:57 +00001316 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001317 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001318 return Offset;
1319}
1320
1321/// \brief Write the block containing all of the declaration IDs
1322/// visible from the given DeclContext.
1323///
1324/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1325/// bistream, or 0 if no block was written.
1326uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1327 DeclContext *DC) {
1328 if (DC->getPrimaryContext() != DC)
1329 return 0;
1330
Douglas Gregoraff22df2009-04-21 22:32:33 +00001331 // Since there is no name lookup into functions or methods, and we
1332 // perform name lookup for the translation unit via the
1333 // IdentifierInfo chains, don't bother to build a
1334 // visible-declarations table for these entities.
1335 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001336 return 0;
1337
Douglas Gregor2cf26342009-04-09 22:27:44 +00001338 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001339 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001340
1341 // Serialize the contents of the mapping used for lookup. Note that,
1342 // although we have two very different code paths, the serialized
1343 // representation is the same for both cases: a declaration name,
1344 // followed by a size, followed by references to the visible
1345 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001346 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001347 RecordData Record;
1348 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001349 if (!Map)
1350 return 0;
1351
Douglas Gregor2cf26342009-04-09 22:27:44 +00001352 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1353 D != DEnd; ++D) {
1354 AddDeclarationName(D->first, Record);
1355 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1356 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001357 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001358 AddDeclRef(*Result.first, Record);
1359 }
1360
1361 if (Record.size() == 0)
1362 return 0;
1363
Douglas Gregorc9490c02009-04-16 22:23:12 +00001364 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001365 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001366 return Offset;
1367}
1368
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001369//===----------------------------------------------------------------------===//
1370// Global Method Pool and Selector Serialization
1371//===----------------------------------------------------------------------===//
1372
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001373namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001374// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001375class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001376 PCHWriter &Writer;
1377
1378public:
1379 typedef Selector key_type;
1380 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001382 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1383 typedef const data_type& data_type_ref;
1384
1385 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001387 static unsigned ComputeHash(Selector Sel) {
1388 unsigned N = Sel.getNumArgs();
1389 if (N == 0)
1390 ++N;
1391 unsigned R = 5381;
1392 for (unsigned I = 0; I != N; ++I)
1393 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001394 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001395 return R;
1396 }
Mike Stump1eb44332009-09-09 15:08:12 +00001397
1398 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001399 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1400 data_type_ref Methods) {
1401 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1402 clang::io::Emit16(Out, KeyLen);
1403 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001404 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001405 Method = Method->Next)
1406 if (Method->Method)
1407 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001408 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001409 Method = Method->Next)
1410 if (Method->Method)
1411 DataLen += 4;
1412 clang::io::Emit16(Out, DataLen);
1413 return std::make_pair(KeyLen, DataLen);
1414 }
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Douglas Gregor83941df2009-04-25 17:48:32 +00001416 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001417 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001418 assert((Start >> 32) == 0 && "Selector key offset too large");
1419 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001420 unsigned N = Sel.getNumArgs();
1421 clang::io::Emit16(Out, N);
1422 if (N == 0)
1423 N = 1;
1424 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001425 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001426 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001429 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001430 data_type_ref Methods, unsigned DataLen) {
1431 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001432 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001433 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001434 Method = Method->Next)
1435 if (Method->Method)
1436 ++NumInstanceMethods;
1437
1438 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001439 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001440 Method = Method->Next)
1441 if (Method->Method)
1442 ++NumFactoryMethods;
1443
1444 clang::io::Emit16(Out, NumInstanceMethods);
1445 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001446 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001447 Method = Method->Next)
1448 if (Method->Method)
1449 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001450 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001451 Method = Method->Next)
1452 if (Method->Method)
1453 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001454
1455 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001456 }
1457};
1458} // end anonymous namespace
1459
1460/// \brief Write the method pool into the PCH file.
1461///
1462/// The method pool contains both instance and factory methods, stored
1463/// in an on-disk hash table indexed by the selector.
1464void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1465 using namespace llvm;
1466
1467 // Create and write out the blob that contains the instance and
1468 // factor method pools.
1469 bool Empty = true;
1470 {
1471 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001473 // Create the on-disk hash table representation. Start by
1474 // iterating through the instance method pool.
1475 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001476 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001477 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001478 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001479 InstanceEnd = SemaRef.InstanceMethodPool.end();
1480 Instance != InstanceEnd; ++Instance) {
1481 // Check whether there is a factory method with the same
1482 // selector.
1483 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1484 = SemaRef.FactoryMethodPool.find(Instance->first);
1485
1486 if (Factory == SemaRef.FactoryMethodPool.end())
1487 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001488 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001489 ObjCMethodList()));
1490 else
1491 Generator.insert(Instance->first,
1492 std::make_pair(Instance->second, Factory->second));
1493
Douglas Gregor83941df2009-04-25 17:48:32 +00001494 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001495 Empty = false;
1496 }
1497
1498 // Now iterate through the factory method pool, to pick up any
1499 // selectors that weren't already in the instance method pool.
1500 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001501 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001502 FactoryEnd = SemaRef.FactoryMethodPool.end();
1503 Factory != FactoryEnd; ++Factory) {
1504 // Check whether there is an instance method with the same
1505 // selector. If so, there is no work to do here.
1506 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1507 = SemaRef.InstanceMethodPool.find(Factory->first);
1508
Douglas Gregor83941df2009-04-25 17:48:32 +00001509 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001510 Generator.insert(Factory->first,
1511 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001512 ++NumSelectorsInMethodPool;
1513 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001514
1515 Empty = false;
1516 }
1517
Douglas Gregor83941df2009-04-25 17:48:32 +00001518 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001519 return;
1520
1521 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001522 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001523 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001524 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001525 {
1526 PCHMethodPoolTrait Trait(*this);
1527 llvm::raw_svector_ostream Out(MethodPool);
1528 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001529 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001530 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001531
1532 // For every selector that we have seen but which was not
1533 // written into the hash table, write the selector itself and
1534 // record it's offset.
1535 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1536 if (SelectorOffsets[I] == 0)
1537 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001538 }
1539
1540 // Create a blob abbreviation
1541 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1542 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1546 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1547
Douglas Gregor83941df2009-04-25 17:48:32 +00001548 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001549 RecordData Record;
1550 Record.push_back(pch::METHOD_POOL);
1551 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001552 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001553 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001554
1555 // Create a blob abbreviation for the selector table offsets.
1556 Abbrev = new BitCodeAbbrev();
1557 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1560 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1561
1562 // Write the selector offsets table.
1563 Record.clear();
1564 Record.push_back(pch::SELECTOR_OFFSETS);
1565 Record.push_back(SelectorOffsets.size());
1566 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1567 (const char *)&SelectorOffsets.front(),
1568 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001569 }
1570}
1571
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001572//===----------------------------------------------------------------------===//
1573// Identifier Table Serialization
1574//===----------------------------------------------------------------------===//
1575
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001576namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001577class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001578 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001579 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001580
Douglas Gregora92193e2009-04-28 21:18:29 +00001581 /// \brief Determines whether this is an "interesting" identifier
1582 /// that needs a full IdentifierInfo structure written into the hash
1583 /// table.
1584 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1585 return II->isPoisoned() ||
1586 II->isExtensionToken() ||
1587 II->hasMacroDefinition() ||
1588 II->getObjCOrBuiltinID() ||
1589 II->getFETokenInfo<void>();
1590 }
1591
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001592public:
1593 typedef const IdentifierInfo* key_type;
1594 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001596 typedef pch::IdentID data_type;
1597 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001598
1599 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001600 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001601
1602 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001603 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
1606 std::pair<unsigned,unsigned>
1607 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001608 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001609 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001610 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1611 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001612 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001613 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001614 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001615 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001616 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1617 DEnd = IdentifierResolver::end();
1618 D != DEnd; ++D)
1619 DataLen += sizeof(pch::DeclID);
1620 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001621 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001622 // We emit the key length after the data length so that every
1623 // string is preceded by a 16-bit length. This matches the PTH
1624 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001625 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001626 return std::make_pair(KeyLen, DataLen);
1627 }
Mike Stump1eb44332009-09-09 15:08:12 +00001628
1629 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001630 unsigned KeyLen) {
1631 // Record the location of the key data. This is used when generating
1632 // the mapping from persistent IDs to strings.
1633 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001634 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
1637 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001638 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001639 if (!isInterestingIdentifier(II)) {
1640 clang::io::Emit32(Out, ID << 1);
1641 return;
1642 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001643
Douglas Gregora92193e2009-04-28 21:18:29 +00001644 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001645 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001646 bool hasMacroDefinition =
1647 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001648 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001649 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001650 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1651 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1652 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1653 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001654 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001655
Douglas Gregor37e26842009-04-21 23:56:24 +00001656 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001657 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001658
Douglas Gregor668c1a42009-04-21 22:25:48 +00001659 // Emit the declaration IDs in reverse order, because the
1660 // IdentifierResolver provides the declarations as they would be
1661 // visible (e.g., the function "stat" would come before the struct
1662 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1663 // adds declarations to the end of the list (so we need to see the
1664 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001665 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001666 IdentifierResolver::end());
1667 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1668 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001669 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001670 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001671 }
1672};
1673} // end anonymous namespace
1674
Douglas Gregorafaf3082009-04-11 00:14:32 +00001675/// \brief Write the identifier table into the PCH file.
1676///
1677/// The identifier table consists of a blob containing string data
1678/// (the actual identifiers themselves) and a separate "offsets" index
1679/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001680void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001681 using namespace llvm;
1682
1683 // Create and write out the blob that contains the identifier
1684 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001685 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001686 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Douglas Gregor92b059e2009-04-28 20:33:11 +00001688 // Look for any identifiers that were named while processing the
1689 // headers, but are otherwise not needed. We add these to the hash
1690 // table to enable checking of the predefines buffer in the case
1691 // where the user adds new macro definitions when building the PCH
1692 // file.
1693 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1694 IDEnd = PP.getIdentifierTable().end();
1695 ID != IDEnd; ++ID)
1696 getIdentifierRef(ID->second);
1697
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001698 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001699 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001700 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1701 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1702 ID != IDEnd; ++ID) {
1703 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001704 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001705 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001706
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001707 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001708 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001709 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001710 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001711 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001712 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001713 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001714 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001715 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001716 }
1717
1718 // Create a blob abbreviation
1719 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1720 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001721 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001722 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001723 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001724
1725 // Write the identifier table
1726 RecordData Record;
1727 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001728 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001729 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001730 }
1731
1732 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001733 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1734 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1737 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1738
1739 RecordData Record;
1740 Record.push_back(pch::IDENTIFIER_OFFSET);
1741 Record.push_back(IdentifierOffsets.size());
1742 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1743 (const char *)&IdentifierOffsets.front(),
1744 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001745}
1746
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001747//===----------------------------------------------------------------------===//
1748// General Serialization Routines
1749//===----------------------------------------------------------------------===//
1750
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001751/// \brief Write a record containing the given attributes.
1752void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1753 RecordData Record;
1754 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001755 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001756 Record.push_back(Attr->isInherited());
1757 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001758 default:
1759 assert(0 && "Does not support PCH writing for this attribute yet!");
1760 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001761 case Attr::Alias:
1762 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1763 break;
1764
1765 case Attr::Aligned:
1766 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1767 break;
1768
1769 case Attr::AlwaysInline:
1770 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001772 case Attr::AnalyzerNoReturn:
1773 break;
1774
1775 case Attr::Annotate:
1776 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1777 break;
1778
1779 case Attr::AsmLabel:
1780 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1781 break;
1782
Sean Hunt7725e672009-11-25 04:20:27 +00001783 case Attr::BaseCheck:
1784 break;
1785
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001786 case Attr::Blocks:
1787 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1788 break;
1789
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001790 case Attr::CDecl:
1791 break;
1792
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001793 case Attr::Cleanup:
1794 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1795 break;
1796
1797 case Attr::Const:
1798 break;
1799
1800 case Attr::Constructor:
1801 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1802 break;
1803
1804 case Attr::DLLExport:
1805 case Attr::DLLImport:
1806 case Attr::Deprecated:
1807 break;
1808
1809 case Attr::Destructor:
1810 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1811 break;
1812
1813 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001814 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001815 break;
1816
1817 case Attr::Format: {
1818 const FormatAttr *Format = cast<FormatAttr>(Attr);
1819 AddString(Format->getType(), Record);
1820 Record.push_back(Format->getFormatIdx());
1821 Record.push_back(Format->getFirstArg());
1822 break;
1823 }
1824
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001825 case Attr::FormatArg: {
1826 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1827 Record.push_back(Format->getFormatIdx());
1828 break;
1829 }
1830
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001831 case Attr::Sentinel : {
1832 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1833 Record.push_back(Sentinel->getSentinel());
1834 Record.push_back(Sentinel->getNullPos());
1835 break;
1836 }
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Chris Lattnercf2a7212009-04-20 19:12:28 +00001838 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001839 case Attr::Hiding:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001840 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001841 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001842 case Attr::NoDebug:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001843 case Attr::NoReturn:
1844 case Attr::NoThrow:
Mike Stump1feade82009-08-26 22:31:08 +00001845 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001846 break;
1847
1848 case Attr::NonNull: {
1849 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1850 Record.push_back(NonNull->size());
1851 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1852 break;
1853 }
1854
1855 case Attr::ObjCException:
1856 case Attr::ObjCNSObject:
Ted Kremenekb71368d2009-05-09 02:44:38 +00001857 case Attr::CFReturnsRetained:
1858 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001859 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001860 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001861 break;
1862
Anders Carlssona860e752009-08-08 18:23:56 +00001863 case Attr::PragmaPack:
1864 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001865 break;
1866
Anders Carlssona860e752009-08-08 18:23:56 +00001867 case Attr::Packed:
1868 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001870 case Attr::Pure:
1871 break;
1872
1873 case Attr::Regparm:
1874 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1875 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Nate Begeman6f3d8382009-06-26 06:32:41 +00001877 case Attr::ReqdWorkGroupSize:
1878 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1879 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1880 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1881 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001882
1883 case Attr::Section:
1884 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1885 break;
1886
1887 case Attr::StdCall:
1888 case Attr::TransparentUnion:
1889 case Attr::Unavailable:
1890 case Attr::Unused:
1891 case Attr::Used:
1892 break;
1893
1894 case Attr::Visibility:
1895 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001896 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001897 break;
1898
1899 case Attr::WarnUnusedResult:
1900 case Attr::Weak:
1901 case Attr::WeakImport:
1902 break;
1903 }
1904 }
1905
Douglas Gregorc9490c02009-04-16 22:23:12 +00001906 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001907}
1908
1909void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1910 Record.push_back(Str.size());
1911 Record.insert(Record.end(), Str.begin(), Str.end());
1912}
1913
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001914/// \brief Note that the identifier II occurs at the given offset
1915/// within the identifier table.
1916void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001917 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001918}
1919
Douglas Gregor83941df2009-04-25 17:48:32 +00001920/// \brief Note that the selector Sel occurs at the given offset
1921/// within the method pool/selector table.
1922void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1923 unsigned ID = SelectorIDs[Sel];
1924 assert(ID && "Unknown selector");
1925 SelectorOffsets[ID - 1] = Offset;
1926}
1927
Mike Stump1eb44332009-09-09 15:08:12 +00001928PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1929 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00001930 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1931 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001932
Douglas Gregore650c8c2009-07-07 00:12:59 +00001933void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1934 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001935 using namespace llvm;
1936
Douglas Gregore7785042009-04-20 15:53:59 +00001937 ASTContext &Context = SemaRef.Context;
1938 Preprocessor &PP = SemaRef.PP;
1939
Douglas Gregor2cf26342009-04-09 22:27:44 +00001940 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001941 Stream.Emit((unsigned)'C', 8);
1942 Stream.Emit((unsigned)'P', 8);
1943 Stream.Emit((unsigned)'C', 8);
1944 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001946 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001947
1948 // The translation unit is the first declaration we'll emit.
1949 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001950 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001951
Douglas Gregor2deaea32009-04-22 18:49:13 +00001952 // Make sure that we emit IdentifierInfos (and any attached
1953 // declarations) for builtins.
1954 {
1955 IdentifierTable &Table = PP.getIdentifierTable();
1956 llvm::SmallVector<const char *, 32> BuiltinNames;
1957 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1958 Context.getLangOptions().NoBuiltin);
1959 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1960 getIdentifierRef(&Table.get(BuiltinNames[I]));
1961 }
1962
Chris Lattner63d65f82009-09-08 18:19:27 +00001963 // Build a record containing all of the tentative definitions in this file, in
1964 // TentativeDefinitionList order. Generally, this record will be empty for
1965 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001966 RecordData TentativeDefinitions;
Chris Lattner63d65f82009-09-08 18:19:27 +00001967 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1968 VarDecl *VD =
1969 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1970 if (VD) AddDeclRef(VD, TentativeDefinitions);
1971 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001972
Douglas Gregor14c22f22009-04-22 22:18:58 +00001973 // Build a record containing all of the locally-scoped external
1974 // declarations in this header file. Generally, this record will be
1975 // empty.
1976 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00001977 // FIXME: This is filling in the PCH file in densemap order which is
1978 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00001979 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00001980 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1981 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1982 TD != TDEnd; ++TD)
1983 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1984
Douglas Gregorb81c1702009-04-27 20:06:05 +00001985 // Build a record containing all of the ext_vector declarations.
1986 RecordData ExtVectorDecls;
1987 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1988 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1989
Douglas Gregor2cf26342009-04-09 22:27:44 +00001990 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001991 RecordData Record;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001992 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001993 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001994 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001995 if (StatCalls && !isysroot)
1996 WriteStatCache(*StatCalls, isysroot);
1997 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump1eb44332009-09-09 15:08:12 +00001998 WriteComments(Context);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001999 // Write the record of special types.
2000 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002002 AddTypeRef(Context.getBuiltinVaListType(), Record);
2003 AddTypeRef(Context.getObjCIdType(), Record);
2004 AddTypeRef(Context.getObjCSelType(), Record);
2005 AddTypeRef(Context.getObjCProtoType(), Record);
2006 AddTypeRef(Context.getObjCClassType(), Record);
2007 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2008 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2009 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002010 AddTypeRef(Context.getjmp_bufType(), Record);
2011 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002012 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2013 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002014#if 0
2015 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00002016 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002017#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00002018 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002019 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002020 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Douglas Gregor366809a2009-04-26 03:49:13 +00002022 // Keep writing types and declarations until all types and
2023 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002024 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2025 WriteDeclsBlockAbbrevs();
2026 while (!DeclTypesToEmit.empty()) {
2027 DeclOrType DOT = DeclTypesToEmit.front();
2028 DeclTypesToEmit.pop();
2029 if (DOT.isType())
2030 WriteType(DOT.getType());
2031 else
2032 WriteDecl(Context, DOT.getDecl());
2033 }
2034 Stream.ExitBlock();
2035
Douglas Gregor813a97b2009-10-17 17:25:45 +00002036 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002037 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002038 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002039
2040 // Write the type offsets array
2041 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2042 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2045 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2046 Record.clear();
2047 Record.push_back(pch::TYPE_OFFSET);
2048 Record.push_back(TypeOffsets.size());
2049 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002050 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002051 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002053 // Write the declaration offsets array
2054 Abbrev = new BitCodeAbbrev();
2055 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2058 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2059 Record.clear();
2060 Record.push_back(pch::DECL_OFFSET);
2061 Record.push_back(DeclOffsets.size());
2062 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002063 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002064 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002065
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002066 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002067 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002068 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002069
2070 // Write the record containing tentative definitions.
2071 if (!TentativeDefinitions.empty())
2072 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002073
2074 // Write the record containing locally-scoped external definitions.
2075 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002076 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002077 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002078
2079 // Write the record containing ext_vector type names.
2080 if (!ExtVectorDecls.empty())
2081 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Douglas Gregor3e1af842009-04-17 22:13:46 +00002083 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002084 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002085 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002086 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002087 Record.push_back(NumLexicalDeclContexts);
2088 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002089 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002090 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002091}
2092
2093void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2094 Record.push_back(Loc.getRawEncoding());
2095}
2096
2097void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2098 Record.push_back(Value.getBitWidth());
2099 unsigned N = Value.getNumWords();
2100 const uint64_t* Words = Value.getRawData();
2101 for (unsigned I = 0; I != N; ++I)
2102 Record.push_back(Words[I]);
2103}
2104
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002105void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2106 Record.push_back(Value.isUnsigned());
2107 AddAPInt(Value, Record);
2108}
2109
Douglas Gregor17fc2232009-04-14 21:55:33 +00002110void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2111 AddAPInt(Value.bitcastToAPInt(), Record);
2112}
2113
Douglas Gregor2cf26342009-04-09 22:27:44 +00002114void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002115 Record.push_back(getIdentifierRef(II));
2116}
2117
2118pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2119 if (II == 0)
2120 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002121
2122 pch::IdentID &ID = IdentifierIDs[II];
2123 if (ID == 0)
2124 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002125 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002126}
2127
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002128void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2129 if (SelRef.getAsOpaquePtr() == 0) {
2130 Record.push_back(0);
2131 return;
2132 }
2133
2134 pch::SelectorID &SID = SelectorIDs[SelRef];
2135 if (SID == 0) {
2136 SID = SelectorIDs.size();
2137 SelVector.push_back(SelRef);
2138 }
2139 Record.push_back(SID);
2140}
2141
John McCall833ca992009-10-29 08:12:44 +00002142void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2143 RecordData &Record) {
2144 switch (Arg.getArgument().getKind()) {
2145 case TemplateArgument::Expression:
2146 AddStmt(Arg.getLocInfo().getAsExpr());
2147 break;
2148 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002149 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002150 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002151 case TemplateArgument::Template:
2152 Record.push_back(
2153 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2154 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2155 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2156 break;
John McCall833ca992009-10-29 08:12:44 +00002157 case TemplateArgument::Null:
2158 case TemplateArgument::Integral:
2159 case TemplateArgument::Declaration:
2160 case TemplateArgument::Pack:
2161 break;
2162 }
2163}
2164
John McCalla93c9342009-12-07 02:54:59 +00002165void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2166 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002167 AddTypeRef(QualType(), Record);
2168 return;
2169 }
2170
John McCalla93c9342009-12-07 02:54:59 +00002171 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002172 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002173 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002174 TLW.Visit(TL);
2175}
2176
Douglas Gregor2cf26342009-04-09 22:27:44 +00002177void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2178 if (T.isNull()) {
2179 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2180 return;
2181 }
2182
Douglas Gregora4923eb2009-11-16 21:35:15 +00002183 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002184 T.removeFastQualifiers();
2185
Douglas Gregora4923eb2009-11-16 21:35:15 +00002186 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002187 pch::TypeID &ID = TypeIDs[T];
2188 if (ID == 0) {
2189 // We haven't seen these qualifiers applied to this type before.
2190 // Assign it a new ID. This is the only time we enqueue a
2191 // qualified type, and it has no CV qualifiers.
2192 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002193 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002194 }
2195
2196 // Encode the type qualifiers in the type reference.
2197 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2198 return;
2199 }
2200
Douglas Gregora4923eb2009-11-16 21:35:15 +00002201 assert(!T.hasLocalQualifiers());
John McCall0953e762009-09-24 19:53:00 +00002202
Douglas Gregor2cf26342009-04-09 22:27:44 +00002203 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002204 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002205 switch (BT->getKind()) {
2206 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2207 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2208 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2209 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2210 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2211 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2212 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2213 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002214 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002215 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2216 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2217 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2218 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2219 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2220 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2221 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002222 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002223 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2224 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2225 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002226 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002227 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2228 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002229 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2230 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002231 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2232 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002233 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002234 case BuiltinType::UndeducedAuto:
2235 assert(0 && "Should not see undeduced auto here");
2236 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002237 }
2238
John McCall0953e762009-09-24 19:53:00 +00002239 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002240 return;
2241 }
2242
John McCall0953e762009-09-24 19:53:00 +00002243 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002244 if (ID == 0) {
2245 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002246 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002247 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002248 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002249 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002250
2251 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002252 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002253}
2254
2255void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2256 if (D == 0) {
2257 Record.push_back(0);
2258 return;
2259 }
2260
Douglas Gregor8038d512009-04-10 17:25:41 +00002261 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002262 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002263 // We haven't seen this declaration before. Give it a new ID and
2264 // enqueue it in the list of declarations to emit.
2265 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002266 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002267 }
2268
2269 Record.push_back(ID);
2270}
2271
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002272pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2273 if (D == 0)
2274 return 0;
2275
2276 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2277 return DeclIDs[D];
2278}
2279
Douglas Gregor2cf26342009-04-09 22:27:44 +00002280void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002281 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002282 Record.push_back(Name.getNameKind());
2283 switch (Name.getNameKind()) {
2284 case DeclarationName::Identifier:
2285 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2286 break;
2287
2288 case DeclarationName::ObjCZeroArgSelector:
2289 case DeclarationName::ObjCOneArgSelector:
2290 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002291 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002292 break;
2293
2294 case DeclarationName::CXXConstructorName:
2295 case DeclarationName::CXXDestructorName:
2296 case DeclarationName::CXXConversionFunctionName:
2297 AddTypeRef(Name.getCXXNameType(), Record);
2298 break;
2299
2300 case DeclarationName::CXXOperatorName:
2301 Record.push_back(Name.getCXXOverloadedOperator());
2302 break;
2303
Sean Hunt3e518bd2009-11-29 07:34:05 +00002304 case DeclarationName::CXXLiteralOperatorName:
2305 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2306 break;
2307
Douglas Gregor2cf26342009-04-09 22:27:44 +00002308 case DeclarationName::CXXUsingDirective:
2309 // No extra data to emit
2310 break;
2311 }
2312}
Douglas Gregor0b748912009-04-14 21:18:50 +00002313