blob: 681c1ff32cb0d0ed1bf735c7077fb987a232fd22 [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
72void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
73 Record.push_back(T->getWidth());
74 Record.push_back(T->isSigned());
75 Code = pch::TYPE_FIXED_WIDTH_INT;
76}
77
78void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
79 Writer.AddTypeRef(T->getElementType(), Record);
80 Code = pch::TYPE_COMPLEX;
81}
82
83void PCHTypeWriter::VisitPointerType(const PointerType *T) {
84 Writer.AddTypeRef(T->getPointeeType(), Record);
85 Code = pch::TYPE_POINTER;
86}
87
88void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000089 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000090 Code = pch::TYPE_BLOCK_POINTER;
91}
92
93void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_LVALUE_REFERENCE;
96}
97
98void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
99 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Code = pch::TYPE_RVALUE_REFERENCE;
101}
102
103void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
105 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106 Code = pch::TYPE_MEMBER_POINTER;
107}
108
109void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
110 Writer.AddTypeRef(T->getElementType(), Record);
111 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000112 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000113}
114
115void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
116 VisitArrayType(T);
117 Writer.AddAPInt(T->getSize(), Record);
118 Code = pch::TYPE_CONSTANT_ARRAY;
119}
120
121void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
122 VisitArrayType(T);
123 Code = pch::TYPE_INCOMPLETE_ARRAY;
124}
125
126void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
127 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000128 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
129 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000130 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131 Code = pch::TYPE_VARIABLE_ARRAY;
132}
133
134void PCHTypeWriter::VisitVectorType(const VectorType *T) {
135 Writer.AddTypeRef(T->getElementType(), Record);
136 Record.push_back(T->getNumElements());
137 Code = pch::TYPE_VECTOR;
138}
139
140void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
141 VisitVectorType(T);
142 Code = pch::TYPE_EXT_VECTOR;
143}
144
145void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
146 Writer.AddTypeRef(T->getResultType(), Record);
147}
148
149void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
150 VisitFunctionType(T);
151 Code = pch::TYPE_FUNCTION_NO_PROTO;
152}
153
154void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
155 VisitFunctionType(T);
156 Record.push_back(T->getNumArgs());
157 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
158 Writer.AddTypeRef(T->getArgType(I), Record);
159 Record.push_back(T->isVariadic());
160 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000161 Record.push_back(T->hasExceptionSpec());
162 Record.push_back(T->hasAnyExceptionSpec());
163 Record.push_back(T->getNumExceptions());
164 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
165 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 Code = pch::TYPE_FUNCTION_PROTO;
167}
168
John McCalled976492009-12-04 22:46:56 +0000169#if 0
170// For when we want it....
171void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
172 Writer.AddDeclRef(T->getDecl(), Record);
173 Code = pch::TYPE_UNRESOLVED_USING;
174}
175#endif
176
Douglas Gregor2cf26342009-04-09 22:27:44 +0000177void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
178 Writer.AddDeclRef(T->getDecl(), Record);
179 Code = pch::TYPE_TYPEDEF;
180}
181
182void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000183 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184 Code = pch::TYPE_TYPEOF_EXPR;
185}
186
187void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
188 Writer.AddTypeRef(T->getUnderlyingType(), Record);
189 Code = pch::TYPE_TYPEOF;
190}
191
Anders Carlsson395b4752009-06-24 19:06:50 +0000192void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
193 Writer.AddStmt(T->getUnderlyingExpr());
194 Code = pch::TYPE_DECLTYPE;
195}
196
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197void PCHTypeWriter::VisitTagType(const TagType *T) {
198 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000199 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200 "Cannot serialize in the middle of a type definition");
201}
202
203void PCHTypeWriter::VisitRecordType(const RecordType *T) {
204 VisitTagType(T);
205 Code = pch::TYPE_RECORD;
206}
207
208void PCHTypeWriter::VisitEnumType(const EnumType *T) {
209 VisitTagType(T);
210 Code = pch::TYPE_ENUM;
211}
212
John McCall7da24312009-09-05 00:15:47 +0000213void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
214 Writer.AddTypeRef(T->getUnderlyingType(), Record);
215 Record.push_back(T->getTagKind());
216 Code = pch::TYPE_ELABORATED;
217}
218
Mike Stump1eb44332009-09-09 15:08:12 +0000219void
John McCall49a832b2009-10-18 09:09:24 +0000220PCHTypeWriter::VisitSubstTemplateTypeParmType(
221 const SubstTemplateTypeParmType *T) {
222 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
223 Writer.AddTypeRef(T->getReplacementType(), Record);
224 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
225}
226
227void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228PCHTypeWriter::VisitTemplateSpecializationType(
229 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000230 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231 assert(false && "Cannot serialize template specialization types");
232}
233
234void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000235 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000236 assert(false && "Cannot serialize qualified name types");
237}
238
239void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
240 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000241 Record.push_back(T->getNumProtocols());
Steve Naroff446ee4e2009-05-27 16:21:00 +0000242 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
243 E = T->qual_end(); I != E; ++I)
244 Writer.AddDeclRef(*I, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000245 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000246}
247
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000248void
249PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000250 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000251 Record.push_back(T->getNumProtocols());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000252 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000253 E = T->qual_end(); I != E; ++I)
254 Writer.AddDeclRef(*I, Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000255 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256}
257
John McCalla1ee0c52009-10-16 21:56:05 +0000258namespace {
259
260class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
261 PCHWriter &Writer;
262 PCHWriter::RecordData &Record;
263
264public:
265 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
266 : Writer(Writer), Record(Record) { }
267
John McCall51bd8032009-10-18 01:05:36 +0000268#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000269#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000270 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000271#include "clang/AST/TypeLocNodes.def"
272
John McCall51bd8032009-10-18 01:05:36 +0000273 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
274 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000275};
276
277}
278
John McCall51bd8032009-10-18 01:05:36 +0000279void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
280 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000281}
John McCall51bd8032009-10-18 01:05:36 +0000282void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
283 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000284}
John McCall51bd8032009-10-18 01:05:36 +0000285void TypeLocWriter::VisitFixedWidthIntTypeLoc(FixedWidthIntTypeLoc TL) {
286 Writer.AddSourceLocation(TL.getNameLoc(), Record);
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) {
355 Writer.AddSourceLocation(TL.getNameLoc(), Record);
356}
357void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
358 Writer.AddSourceLocation(TL.getNameLoc(), Record);
359}
360void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getNameLoc(), Record);
362}
363void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getNameLoc(), Record);
365}
366void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getNameLoc(), Record);
368}
369void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
370 Writer.AddSourceLocation(TL.getNameLoc(), Record);
371}
372void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
373 Writer.AddSourceLocation(TL.getNameLoc(), Record);
374}
John McCall49a832b2009-10-18 09:09:24 +0000375void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
376 SubstTemplateTypeParmTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
John McCall51bd8032009-10-18 01:05:36 +0000379void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
380 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000381 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
382 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
383 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
384 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
385 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000386}
387void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
388 Writer.AddSourceLocation(TL.getNameLoc(), Record);
389}
390void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getNameLoc(), Record);
392}
393void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
394 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000395 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
396 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
397 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
398 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000399}
John McCall54e14c42009-10-22 22:37:11 +0000400void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getStarLoc(), Record);
402 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
403 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
404 Record.push_back(TL.hasBaseTypeAsWritten());
405 Record.push_back(TL.hasProtocolsAsWritten());
406 if (TL.hasProtocolsAsWritten())
407 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
408 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
409}
John McCalla1ee0c52009-10-16 21:56:05 +0000410
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000411//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000412// PCHWriter Implementation
413//===----------------------------------------------------------------------===//
414
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000415static void EmitBlockID(unsigned ID, const char *Name,
416 llvm::BitstreamWriter &Stream,
417 PCHWriter::RecordData &Record) {
418 Record.clear();
419 Record.push_back(ID);
420 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
421
422 // Emit the block name if present.
423 if (Name == 0 || Name[0] == 0) return;
424 Record.clear();
425 while (*Name)
426 Record.push_back(*Name++);
427 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
428}
429
430static void EmitRecordID(unsigned ID, const char *Name,
431 llvm::BitstreamWriter &Stream,
432 PCHWriter::RecordData &Record) {
433 Record.clear();
434 Record.push_back(ID);
435 while (*Name)
436 Record.push_back(*Name++);
437 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000438}
439
440static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
441 PCHWriter::RecordData &Record) {
442#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
443 RECORD(STMT_STOP);
444 RECORD(STMT_NULL_PTR);
445 RECORD(STMT_NULL);
446 RECORD(STMT_COMPOUND);
447 RECORD(STMT_CASE);
448 RECORD(STMT_DEFAULT);
449 RECORD(STMT_LABEL);
450 RECORD(STMT_IF);
451 RECORD(STMT_SWITCH);
452 RECORD(STMT_WHILE);
453 RECORD(STMT_DO);
454 RECORD(STMT_FOR);
455 RECORD(STMT_GOTO);
456 RECORD(STMT_INDIRECT_GOTO);
457 RECORD(STMT_CONTINUE);
458 RECORD(STMT_BREAK);
459 RECORD(STMT_RETURN);
460 RECORD(STMT_DECL);
461 RECORD(STMT_ASM);
462 RECORD(EXPR_PREDEFINED);
463 RECORD(EXPR_DECL_REF);
464 RECORD(EXPR_INTEGER_LITERAL);
465 RECORD(EXPR_FLOATING_LITERAL);
466 RECORD(EXPR_IMAGINARY_LITERAL);
467 RECORD(EXPR_STRING_LITERAL);
468 RECORD(EXPR_CHARACTER_LITERAL);
469 RECORD(EXPR_PAREN);
470 RECORD(EXPR_UNARY_OPERATOR);
471 RECORD(EXPR_SIZEOF_ALIGN_OF);
472 RECORD(EXPR_ARRAY_SUBSCRIPT);
473 RECORD(EXPR_CALL);
474 RECORD(EXPR_MEMBER);
475 RECORD(EXPR_BINARY_OPERATOR);
476 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
477 RECORD(EXPR_CONDITIONAL_OPERATOR);
478 RECORD(EXPR_IMPLICIT_CAST);
479 RECORD(EXPR_CSTYLE_CAST);
480 RECORD(EXPR_COMPOUND_LITERAL);
481 RECORD(EXPR_EXT_VECTOR_ELEMENT);
482 RECORD(EXPR_INIT_LIST);
483 RECORD(EXPR_DESIGNATED_INIT);
484 RECORD(EXPR_IMPLICIT_VALUE_INIT);
485 RECORD(EXPR_VA_ARG);
486 RECORD(EXPR_ADDR_LABEL);
487 RECORD(EXPR_STMT);
488 RECORD(EXPR_TYPES_COMPATIBLE);
489 RECORD(EXPR_CHOOSE);
490 RECORD(EXPR_GNU_NULL);
491 RECORD(EXPR_SHUFFLE_VECTOR);
492 RECORD(EXPR_BLOCK);
493 RECORD(EXPR_BLOCK_DECL_REF);
494 RECORD(EXPR_OBJC_STRING_LITERAL);
495 RECORD(EXPR_OBJC_ENCODE);
496 RECORD(EXPR_OBJC_SELECTOR_EXPR);
497 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
498 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
499 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
500 RECORD(EXPR_OBJC_KVC_REF_EXPR);
501 RECORD(EXPR_OBJC_MESSAGE_EXPR);
502 RECORD(EXPR_OBJC_SUPER_EXPR);
503 RECORD(STMT_OBJC_FOR_COLLECTION);
504 RECORD(STMT_OBJC_CATCH);
505 RECORD(STMT_OBJC_FINALLY);
506 RECORD(STMT_OBJC_AT_TRY);
507 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
508 RECORD(STMT_OBJC_AT_THROW);
509#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000510}
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000512void PCHWriter::WriteBlockInfoBlock() {
513 RecordData Record;
514 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattner2f4efd12009-04-27 00:40:25 +0000516#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000517#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000519 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000520 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000521 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000522 RECORD(TYPE_OFFSET);
523 RECORD(DECL_OFFSET);
524 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000525 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000526 RECORD(IDENTIFIER_OFFSET);
527 RECORD(IDENTIFIER_TABLE);
528 RECORD(EXTERNAL_DEFINITIONS);
529 RECORD(SPECIAL_TYPES);
530 RECORD(STATISTICS);
531 RECORD(TENTATIVE_DEFINITIONS);
532 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
533 RECORD(SELECTOR_OFFSETS);
534 RECORD(METHOD_POOL);
535 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000536 RECORD(SOURCE_LOCATION_OFFSETS);
537 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000538 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000539 RECORD(EXT_VECTOR_DECLS);
Douglas Gregor2e222532009-07-02 17:08:52 +0000540 RECORD(COMMENT_RANGES);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000541 RECORD(SVN_BRANCH_REVISION);
542
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000543 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000544 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000545 RECORD(SM_SLOC_FILE_ENTRY);
546 RECORD(SM_SLOC_BUFFER_ENTRY);
547 RECORD(SM_SLOC_BUFFER_BLOB);
548 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
549 RECORD(SM_LINE_TABLE);
550 RECORD(SM_HEADER_FILE_INFO);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000552 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000553 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000554 RECORD(PP_MACRO_OBJECT_LIKE);
555 RECORD(PP_MACRO_FUNCTION_LIKE);
556 RECORD(PP_TOKEN);
557
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000558 // Decls and Types block.
559 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000560 RECORD(TYPE_EXT_QUAL);
561 RECORD(TYPE_FIXED_WIDTH_INT);
562 RECORD(TYPE_COMPLEX);
563 RECORD(TYPE_POINTER);
564 RECORD(TYPE_BLOCK_POINTER);
565 RECORD(TYPE_LVALUE_REFERENCE);
566 RECORD(TYPE_RVALUE_REFERENCE);
567 RECORD(TYPE_MEMBER_POINTER);
568 RECORD(TYPE_CONSTANT_ARRAY);
569 RECORD(TYPE_INCOMPLETE_ARRAY);
570 RECORD(TYPE_VARIABLE_ARRAY);
571 RECORD(TYPE_VECTOR);
572 RECORD(TYPE_EXT_VECTOR);
573 RECORD(TYPE_FUNCTION_PROTO);
574 RECORD(TYPE_FUNCTION_NO_PROTO);
575 RECORD(TYPE_TYPEDEF);
576 RECORD(TYPE_TYPEOF_EXPR);
577 RECORD(TYPE_TYPEOF);
578 RECORD(TYPE_RECORD);
579 RECORD(TYPE_ENUM);
580 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000581 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000582 RECORD(DECL_ATTR);
583 RECORD(DECL_TRANSLATION_UNIT);
584 RECORD(DECL_TYPEDEF);
585 RECORD(DECL_ENUM);
586 RECORD(DECL_RECORD);
587 RECORD(DECL_ENUM_CONSTANT);
588 RECORD(DECL_FUNCTION);
589 RECORD(DECL_OBJC_METHOD);
590 RECORD(DECL_OBJC_INTERFACE);
591 RECORD(DECL_OBJC_PROTOCOL);
592 RECORD(DECL_OBJC_IVAR);
593 RECORD(DECL_OBJC_AT_DEFS_FIELD);
594 RECORD(DECL_OBJC_CLASS);
595 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
596 RECORD(DECL_OBJC_CATEGORY);
597 RECORD(DECL_OBJC_CATEGORY_IMPL);
598 RECORD(DECL_OBJC_IMPLEMENTATION);
599 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
600 RECORD(DECL_OBJC_PROPERTY);
601 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000602 RECORD(DECL_FIELD);
603 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000604 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000605 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000606 RECORD(DECL_FILE_SCOPE_ASM);
607 RECORD(DECL_BLOCK);
608 RECORD(DECL_CONTEXT_LEXICAL);
609 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000610 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000611 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000612#undef RECORD
613#undef BLOCK
614 Stream.ExitBlock();
615}
616
Douglas Gregore650c8c2009-07-07 00:12:59 +0000617/// \brief Adjusts the given filename to only write out the portion of the
618/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000619///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000620/// \param Filename the file name to adjust.
621///
622/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
623/// the returned filename will be adjusted by this system root.
624///
625/// \returns either the original filename (if it needs no adjustment) or the
626/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000627static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000628adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
629 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Douglas Gregore650c8c2009-07-07 00:12:59 +0000631 if (!isysroot)
632 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Douglas Gregore650c8c2009-07-07 00:12:59 +0000634 // Verify that the filename and the system root have the same prefix.
635 unsigned Pos = 0;
636 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
637 if (Filename[Pos] != isysroot[Pos])
638 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Douglas Gregore650c8c2009-07-07 00:12:59 +0000640 // We hit the end of the filename before we hit the end of the system root.
641 if (!Filename[Pos])
642 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Douglas Gregore650c8c2009-07-07 00:12:59 +0000644 // If the file name has a '/' at the current position, skip over the '/'.
645 // We distinguish sysroot-based includes from absolute includes by the
646 // absence of '/' at the beginning of sysroot-based includes.
647 if (Filename[Pos] == '/')
648 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Douglas Gregore650c8c2009-07-07 00:12:59 +0000650 return Filename + Pos;
651}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000652
Douglas Gregorab41e632009-04-27 22:23:34 +0000653/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000654void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000655 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000656
Douglas Gregore650c8c2009-07-07 00:12:59 +0000657 // Metadata
658 const TargetInfo &Target = Context.Target;
659 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
660 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
661 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
662 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
663 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
664 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
665 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
666 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
667 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregore650c8c2009-07-07 00:12:59 +0000669 RecordData Record;
670 Record.push_back(pch::METADATA);
671 Record.push_back(pch::VERSION_MAJOR);
672 Record.push_back(pch::VERSION_MINOR);
673 Record.push_back(CLANG_VERSION_MAJOR);
674 Record.push_back(CLANG_VERSION_MINOR);
675 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000676 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000677 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Douglas Gregorb64c1932009-05-12 01:31:05 +0000679 // Original file name
680 SourceManager &SM = Context.getSourceManager();
681 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
682 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
683 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
684 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
685 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
686
687 llvm::sys::Path MainFilePath(MainFile->getName());
688 std::string MainFileName;
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Douglas Gregorb64c1932009-05-12 01:31:05 +0000690 if (!MainFilePath.isAbsolute()) {
691 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000692 P.appendComponent(MainFilePath.str());
693 MainFileName = P.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000694 } else {
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000695 MainFileName = MainFilePath.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000696 }
697
Douglas Gregore650c8c2009-07-07 00:12:59 +0000698 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000699 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000700 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000701 RecordData Record;
702 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000703 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000704 }
Douglas Gregor445e23e2009-10-05 21:07:28 +0000705
706 // Subversion branch/version information.
707 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
708 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
709 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
710 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
711 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
712 Record.clear();
713 Record.push_back(pch::SVN_BRANCH_REVISION);
714 Record.push_back(getClangSubversionRevision());
715 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000716}
717
718/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000719void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
720 RecordData Record;
721 Record.push_back(LangOpts.Trigraphs);
722 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
723 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
724 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
725 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
726 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
727 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
728 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
729 Record.push_back(LangOpts.C99); // C99 Support
730 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
731 Record.push_back(LangOpts.CPlusPlus); // C++ Support
732 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000733 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000735 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
736 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
737 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000739 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000740 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
741 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000742 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000743 Record.push_back(LangOpts.Exceptions); // Support exception handling.
744
745 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
746 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
747 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
748
Chris Lattnerea5ce472009-04-27 07:35:58 +0000749 // Whether static initializers are protected by locks.
750 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000751 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000752 Record.push_back(LangOpts.Blocks); // block extension to C
753 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
754 // they are unused.
755 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
756 // (modulo the platform support).
757
758 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
759 // signed integer arithmetic overflows.
760
761 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
762 // may be ripped out at any time.
763
764 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000765 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000766 // defined.
767 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
768 // opposed to __DYNAMIC__).
769 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
770
771 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
772 // used (instead of C99 semantics).
773 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000774 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
775 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000776 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
777 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000778 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000779 Record.push_back(LangOpts.getGCMode());
780 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000781 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000782 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000783 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000784 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000785 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000786 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000787}
788
Douglas Gregor14f79002009-04-10 03:52:48 +0000789//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000790// stat cache Serialization
791//===----------------------------------------------------------------------===//
792
793namespace {
794// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000795class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000796public:
797 typedef const char * key_type;
798 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000800 typedef std::pair<int, struct stat> data_type;
801 typedef const data_type& data_type_ref;
802
803 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000804 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
807 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000808 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
809 data_type_ref Data) {
810 unsigned StrLen = strlen(path);
811 clang::io::Emit16(Out, StrLen);
812 unsigned DataLen = 1; // result value
813 if (Data.first == 0)
814 DataLen += 4 + 4 + 2 + 8 + 8;
815 clang::io::Emit8(Out, DataLen);
816 return std::make_pair(StrLen + 1, DataLen);
817 }
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000819 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
820 Out.write(path, KeyLen);
821 }
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000823 void EmitData(llvm::raw_ostream& Out, key_type_ref,
824 data_type_ref Data, unsigned DataLen) {
825 using namespace clang::io;
826 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000828 // Result of stat()
829 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000831 if (Data.first == 0) {
832 Emit32(Out, (uint32_t) Data.second.st_ino);
833 Emit32(Out, (uint32_t) Data.second.st_dev);
834 Emit16(Out, (uint16_t) Data.second.st_mode);
835 Emit64(Out, (uint64_t) Data.second.st_mtime);
836 Emit64(Out, (uint64_t) Data.second.st_size);
837 }
838
839 assert(Out.tell() - Start == DataLen && "Wrong data length");
840 }
841};
842} // end anonymous namespace
843
844/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000845void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
846 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000847 // Build the on-disk hash table containing information about every
848 // stat() call.
849 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
850 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000851 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000852 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000853 Stat != StatEnd; ++Stat, ++NumStatEntries) {
854 const char *Filename = Stat->first();
855 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
856 Generator.insert(Filename, Stat->second);
857 }
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000859 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000860 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000861 uint32_t BucketOffset;
862 {
863 llvm::raw_svector_ostream Out(StatCacheData);
864 // Make sure that no bucket is at offset 0
865 clang::io::Emit32(Out, 0);
866 BucketOffset = Generator.Emit(Out);
867 }
868
869 // Create a blob abbreviation
870 using namespace llvm;
871 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
872 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
873 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
876 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
877
878 // Write the stat cache
879 RecordData Record;
880 Record.push_back(pch::STAT_CACHE);
881 Record.push_back(BucketOffset);
882 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000883 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000884}
885
886//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000887// Source Manager Serialization
888//===----------------------------------------------------------------------===//
889
890/// \brief Create an abbreviation for the SLocEntry that refers to a
891/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000892static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000893 using namespace llvm;
894 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
895 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000901 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000902}
903
904/// \brief Create an abbreviation for the SLocEntry that refers to a
905/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000906static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000907 using namespace llvm;
908 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
909 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000915 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000916}
917
918/// \brief Create an abbreviation for the SLocEntry that refers to a
919/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000920static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000921 using namespace llvm;
922 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
923 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000925 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000926}
927
928/// \brief Create an abbreviation for the SLocEntry that refers to an
929/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000930static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000931 using namespace llvm;
932 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
933 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000939 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000940}
941
942/// \brief Writes the block containing the serialized form of the
943/// source manager.
944///
945/// TODO: We should probably use an on-disk hash table (stored in a
946/// blob), indexed based on the file name, so that we only create
947/// entries for files that we actually need. In the common case (no
948/// errors), we probably won't have to create file entries for any of
949/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000950void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000951 const Preprocessor &PP,
952 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000953 RecordData Record;
954
Chris Lattnerf04ad692009-04-10 17:16:57 +0000955 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000956 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000957
958 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000959 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
960 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
961 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
962 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000963
Douglas Gregorbd945002009-04-13 16:31:14 +0000964 // Write the line table.
965 if (SourceMgr.hasLineTable()) {
966 LineTableInfo &LineTable = SourceMgr.getLineTable();
967
968 // Emit the file names
969 Record.push_back(LineTable.getNumFilenames());
970 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
971 // Emit the file name
972 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +0000974 unsigned FilenameLen = Filename? strlen(Filename) : 0;
975 Record.push_back(FilenameLen);
976 if (FilenameLen)
977 Record.insert(Record.end(), Filename, Filename + FilenameLen);
978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregorbd945002009-04-13 16:31:14 +0000980 // Emit the line entries
981 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
982 L != LEnd; ++L) {
983 // Emit the file ID
984 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Douglas Gregorbd945002009-04-13 16:31:14 +0000986 // Emit the line entries
987 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000988 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +0000989 LEEnd = L->second.end();
990 LE != LEEnd; ++LE) {
991 Record.push_back(LE->FileOffset);
992 Record.push_back(LE->LineNo);
993 Record.push_back(LE->FilenameID);
994 Record.push_back((unsigned)LE->FileKind);
995 Record.push_back(LE->IncludeOffset);
996 }
Douglas Gregorbd945002009-04-13 16:31:14 +0000997 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +0000998 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +0000999 }
1000
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001001 // Write out entries for all of the header files we know about.
Mike Stump1eb44332009-09-09 15:08:12 +00001002 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001003 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001004 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001005 E = HS.header_file_end();
1006 I != E; ++I) {
1007 Record.push_back(I->isImport);
1008 Record.push_back(I->DirInfo);
1009 Record.push_back(I->NumIncludes);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001010 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001011 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1012 Record.clear();
1013 }
1014
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001015 // Write out the source location entry table. We skip the first
1016 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001017 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001018 RecordData PreloadSLocs;
1019 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001020 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1021 // Get this source location entry.
1022 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1023
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001024 // Record the offset of this source-location entry.
1025 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1026
1027 // Figure out which record code to use.
1028 unsigned Code;
1029 if (SLoc->isFile()) {
1030 if (SLoc->getFile().getContentCache()->Entry)
1031 Code = pch::SM_SLOC_FILE_ENTRY;
1032 else
1033 Code = pch::SM_SLOC_BUFFER_ENTRY;
1034 } else
1035 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1036 Record.clear();
1037 Record.push_back(Code);
1038
1039 Record.push_back(SLoc->getOffset());
1040 if (SLoc->isFile()) {
1041 const SrcMgr::FileInfo &File = SLoc->getFile();
1042 Record.push_back(File.getIncludeLoc().getRawEncoding());
1043 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1044 Record.push_back(File.hasLineDirectives());
1045
1046 const SrcMgr::ContentCache *Content = File.getContentCache();
1047 if (Content->Entry) {
1048 // The source location entry is a file. The blob associated
1049 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Douglas Gregore650c8c2009-07-07 00:12:59 +00001051 // Turn the file name into an absolute path, if it isn't already.
1052 const char *Filename = Content->Entry->getName();
1053 llvm::sys::Path FilePath(Filename, strlen(Filename));
1054 std::string FilenameStr;
1055 if (!FilePath.isAbsolute()) {
1056 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +00001057 P.appendComponent(FilePath.str());
1058 FilenameStr = P.str();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001059 Filename = FilenameStr.c_str();
1060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Douglas Gregore650c8c2009-07-07 00:12:59 +00001062 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001063 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001064
1065 // FIXME: For now, preload all file source locations, so that
1066 // we get the appropriate File entries in the reader. This is
1067 // a temporary measure.
1068 PreloadSLocs.push_back(SLocEntryOffsets.size());
1069 } else {
1070 // The source location entry is a buffer. The blob associated
1071 // with this entry contains the contents of the buffer.
1072
1073 // We add one to the size so that we capture the trailing NULL
1074 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1075 // the reader side).
1076 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1077 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001078 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1079 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001080 Record.clear();
1081 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1082 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001083 llvm::StringRef(Buffer->getBufferStart(),
1084 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001085
1086 if (strcmp(Name, "<built-in>") == 0)
1087 PreloadSLocs.push_back(SLocEntryOffsets.size());
1088 }
1089 } else {
1090 // The source location entry is an instantiation.
1091 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1092 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1093 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1094 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1095
1096 // Compute the token length for this macro expansion.
1097 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001098 if (I + 1 != N)
1099 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001100 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1101 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1102 }
1103 }
1104
Douglas Gregorc9490c02009-04-16 22:23:12 +00001105 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001106
1107 if (SLocEntryOffsets.empty())
1108 return;
1109
1110 // Write the source-location offsets table into the PCH block. This
1111 // table is used for lazily loading source-location information.
1112 using namespace llvm;
1113 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1114 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1115 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1117 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1118 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001120 Record.clear();
1121 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1122 Record.push_back(SLocEntryOffsets.size());
1123 Record.push_back(SourceMgr.getNextOffset());
1124 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001125 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001126 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001127
1128 // Write the source location entry preloads array, telling the PCH
1129 // reader which source locations entries it should load eagerly.
1130 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001131}
1132
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001133//===----------------------------------------------------------------------===//
1134// Preprocessor Serialization
1135//===----------------------------------------------------------------------===//
1136
Chris Lattner0b1fb982009-04-10 17:15:23 +00001137/// \brief Writes the block containing the serialized form of the
1138/// preprocessor.
1139///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001140void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001141 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001142
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001143 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1144 if (PP.getCounterValue() != 0) {
1145 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001146 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001147 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001148 }
1149
1150 // Enter the preprocessor block.
1151 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001153 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1154 // FIXME: use diagnostics subsystem for localization etc.
1155 if (PP.SawDateOrTime())
1156 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001158 // Loop over all the macro definitions that are live at the end of the file,
1159 // emitting each to the PP section.
Douglas Gregor813a97b2009-10-17 17:25:45 +00001160 // FIXME: Make sure that this sees macros defined in included PCH files.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001161 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1162 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001163 // FIXME: This emits macros in hash table order, we should do it in a stable
1164 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001165 MacroInfo *MI = I->second;
1166
1167 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1168 // been redefined by the header (in which case they are not isBuiltinMacro).
1169 if (MI->isBuiltinMacro())
1170 continue;
1171
Douglas Gregor37e26842009-04-21 23:56:24 +00001172 // FIXME: Remove this identifier reference?
Chris Lattner7356a312009-04-11 21:15:38 +00001173 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001174 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001175 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1176 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001178 unsigned Code;
1179 if (MI->isObjectLike()) {
1180 Code = pch::PP_MACRO_OBJECT_LIKE;
1181 } else {
1182 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001184 Record.push_back(MI->isC99Varargs());
1185 Record.push_back(MI->isGNUVarargs());
1186 Record.push_back(MI->getNumArgs());
1187 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1188 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001189 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001190 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001191 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001192 Record.clear();
1193
Chris Lattnerdf961c22009-04-10 18:08:30 +00001194 // Emit the tokens array.
1195 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1196 // Note that we know that the preprocessor does not have any annotation
1197 // tokens in it because they are created by the parser, and thus can't be
1198 // in a macro definition.
1199 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Chris Lattnerdf961c22009-04-10 18:08:30 +00001201 Record.push_back(Tok.getLocation().getRawEncoding());
1202 Record.push_back(Tok.getLength());
1203
Chris Lattnerdf961c22009-04-10 18:08:30 +00001204 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1205 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001206 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Chris Lattnerdf961c22009-04-10 18:08:30 +00001208 // FIXME: Should translate token kind to a stable encoding.
1209 Record.push_back(Tok.getKind());
1210 // FIXME: Should translate token flags to a stable encoding.
1211 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Douglas Gregorc9490c02009-04-16 22:23:12 +00001213 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001214 Record.clear();
1215 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001216 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001217 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001218 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001219}
1220
Douglas Gregor2e222532009-07-02 17:08:52 +00001221void PCHWriter::WriteComments(ASTContext &Context) {
1222 using namespace llvm;
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregor2e222532009-07-02 17:08:52 +00001224 if (Context.Comments.empty())
1225 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregor2e222532009-07-02 17:08:52 +00001227 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1228 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1229 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1230 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Douglas Gregor2e222532009-07-02 17:08:52 +00001232 RecordData Record;
1233 Record.push_back(pch::COMMENT_RANGES);
Mike Stump1eb44332009-09-09 15:08:12 +00001234 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregor2e222532009-07-02 17:08:52 +00001235 (const char*)&Context.Comments[0],
1236 Context.Comments.size() * sizeof(SourceRange));
1237}
1238
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001239//===----------------------------------------------------------------------===//
1240// Type Serialization
1241//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001242
Douglas Gregor2cf26342009-04-09 22:27:44 +00001243/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001244void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001245 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001246 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001247 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Douglas Gregor2cf26342009-04-09 22:27:44 +00001249 // Record the offset for this type.
1250 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001251 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001252 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1253 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001254 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001255 }
1256
1257 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Douglas Gregor2cf26342009-04-09 22:27:44 +00001259 // Emit the type's representation.
1260 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001261
Douglas Gregora4923eb2009-11-16 21:35:15 +00001262 if (T.hasLocalNonFastQualifiers()) {
1263 Qualifiers Qs = T.getLocalQualifiers();
1264 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001265 Record.push_back(Qs.getAsOpaqueValue());
1266 W.Code = pch::TYPE_EXT_QUAL;
1267 } else {
1268 switch (T->getTypeClass()) {
1269 // For all of the concrete, non-dependent types, call the
1270 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001271#define TYPE(Class, Base) \
John McCall0953e762009-09-24 19:53:00 +00001272 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001273#define ABSTRACT_TYPE(Class, Base)
1274#define DEPENDENT_TYPE(Class, Base)
1275#include "clang/AST/TypeNodes.def"
1276
John McCall0953e762009-09-24 19:53:00 +00001277 // For all of the dependent type nodes (which only occur in C++
1278 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001279#define TYPE(Class, Base)
1280#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1281#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001282 assert(false && "Cannot serialize dependent type nodes");
1283 break;
1284 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001285 }
1286
1287 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001288 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001289
1290 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001291 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001292}
1293
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001294//===----------------------------------------------------------------------===//
1295// Declaration Serialization
1296//===----------------------------------------------------------------------===//
1297
Douglas Gregor2cf26342009-04-09 22:27:44 +00001298/// \brief Write the block containing all of the declaration IDs
1299/// lexically declared within the given DeclContext.
1300///
1301/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1302/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001303uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001304 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001305 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001306 return 0;
1307
Douglas Gregorc9490c02009-04-16 22:23:12 +00001308 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001309 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001310 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1311 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001312 AddDeclRef(*D, Record);
1313
Douglas Gregor25123082009-04-22 22:34:57 +00001314 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001315 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001316 return Offset;
1317}
1318
1319/// \brief Write the block containing all of the declaration IDs
1320/// visible from the given DeclContext.
1321///
1322/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1323/// bistream, or 0 if no block was written.
1324uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1325 DeclContext *DC) {
1326 if (DC->getPrimaryContext() != DC)
1327 return 0;
1328
Douglas Gregoraff22df2009-04-21 22:32:33 +00001329 // Since there is no name lookup into functions or methods, and we
1330 // perform name lookup for the translation unit via the
1331 // IdentifierInfo chains, don't bother to build a
1332 // visible-declarations table for these entities.
1333 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001334 return 0;
1335
Douglas Gregor2cf26342009-04-09 22:27:44 +00001336 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001337 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001338
1339 // Serialize the contents of the mapping used for lookup. Note that,
1340 // although we have two very different code paths, the serialized
1341 // representation is the same for both cases: a declaration name,
1342 // followed by a size, followed by references to the visible
1343 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001344 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001345 RecordData Record;
1346 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001347 if (!Map)
1348 return 0;
1349
Douglas Gregor2cf26342009-04-09 22:27:44 +00001350 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1351 D != DEnd; ++D) {
1352 AddDeclarationName(D->first, Record);
1353 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1354 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001355 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001356 AddDeclRef(*Result.first, Record);
1357 }
1358
1359 if (Record.size() == 0)
1360 return 0;
1361
Douglas Gregorc9490c02009-04-16 22:23:12 +00001362 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001363 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001364 return Offset;
1365}
1366
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001367//===----------------------------------------------------------------------===//
1368// Global Method Pool and Selector Serialization
1369//===----------------------------------------------------------------------===//
1370
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001371namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001372// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001373class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001374 PCHWriter &Writer;
1375
1376public:
1377 typedef Selector key_type;
1378 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001380 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1381 typedef const data_type& data_type_ref;
1382
1383 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001385 static unsigned ComputeHash(Selector Sel) {
1386 unsigned N = Sel.getNumArgs();
1387 if (N == 0)
1388 ++N;
1389 unsigned R = 5381;
1390 for (unsigned I = 0; I != N; ++I)
1391 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001392 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001393 return R;
1394 }
Mike Stump1eb44332009-09-09 15:08:12 +00001395
1396 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001397 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1398 data_type_ref Methods) {
1399 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1400 clang::io::Emit16(Out, KeyLen);
1401 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001402 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001403 Method = Method->Next)
1404 if (Method->Method)
1405 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001406 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001407 Method = Method->Next)
1408 if (Method->Method)
1409 DataLen += 4;
1410 clang::io::Emit16(Out, DataLen);
1411 return std::make_pair(KeyLen, DataLen);
1412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Douglas Gregor83941df2009-04-25 17:48:32 +00001414 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001415 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001416 assert((Start >> 32) == 0 && "Selector key offset too large");
1417 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001418 unsigned N = Sel.getNumArgs();
1419 clang::io::Emit16(Out, N);
1420 if (N == 0)
1421 N = 1;
1422 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001423 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001424 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1425 }
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001427 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001428 data_type_ref Methods, unsigned DataLen) {
1429 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001430 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001431 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001432 Method = Method->Next)
1433 if (Method->Method)
1434 ++NumInstanceMethods;
1435
1436 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001437 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001438 Method = Method->Next)
1439 if (Method->Method)
1440 ++NumFactoryMethods;
1441
1442 clang::io::Emit16(Out, NumInstanceMethods);
1443 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001444 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001445 Method = Method->Next)
1446 if (Method->Method)
1447 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001448 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001449 Method = Method->Next)
1450 if (Method->Method)
1451 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001452
1453 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001454 }
1455};
1456} // end anonymous namespace
1457
1458/// \brief Write the method pool into the PCH file.
1459///
1460/// The method pool contains both instance and factory methods, stored
1461/// in an on-disk hash table indexed by the selector.
1462void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1463 using namespace llvm;
1464
1465 // Create and write out the blob that contains the instance and
1466 // factor method pools.
1467 bool Empty = true;
1468 {
1469 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001471 // Create the on-disk hash table representation. Start by
1472 // iterating through the instance method pool.
1473 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001474 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001475 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001476 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001477 InstanceEnd = SemaRef.InstanceMethodPool.end();
1478 Instance != InstanceEnd; ++Instance) {
1479 // Check whether there is a factory method with the same
1480 // selector.
1481 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1482 = SemaRef.FactoryMethodPool.find(Instance->first);
1483
1484 if (Factory == SemaRef.FactoryMethodPool.end())
1485 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001486 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001487 ObjCMethodList()));
1488 else
1489 Generator.insert(Instance->first,
1490 std::make_pair(Instance->second, Factory->second));
1491
Douglas Gregor83941df2009-04-25 17:48:32 +00001492 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001493 Empty = false;
1494 }
1495
1496 // Now iterate through the factory method pool, to pick up any
1497 // selectors that weren't already in the instance method pool.
1498 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001499 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001500 FactoryEnd = SemaRef.FactoryMethodPool.end();
1501 Factory != FactoryEnd; ++Factory) {
1502 // Check whether there is an instance method with the same
1503 // selector. If so, there is no work to do here.
1504 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1505 = SemaRef.InstanceMethodPool.find(Factory->first);
1506
Douglas Gregor83941df2009-04-25 17:48:32 +00001507 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001508 Generator.insert(Factory->first,
1509 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001510 ++NumSelectorsInMethodPool;
1511 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001512
1513 Empty = false;
1514 }
1515
Douglas Gregor83941df2009-04-25 17:48:32 +00001516 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001517 return;
1518
1519 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001520 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001521 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001522 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001523 {
1524 PCHMethodPoolTrait Trait(*this);
1525 llvm::raw_svector_ostream Out(MethodPool);
1526 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001527 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001528 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001529
1530 // For every selector that we have seen but which was not
1531 // written into the hash table, write the selector itself and
1532 // record it's offset.
1533 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1534 if (SelectorOffsets[I] == 0)
1535 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001536 }
1537
1538 // Create a blob abbreviation
1539 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1540 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1544 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1545
Douglas Gregor83941df2009-04-25 17:48:32 +00001546 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001547 RecordData Record;
1548 Record.push_back(pch::METHOD_POOL);
1549 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001550 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001551 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001552
1553 // Create a blob abbreviation for the selector table offsets.
1554 Abbrev = new BitCodeAbbrev();
1555 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1558 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1559
1560 // Write the selector offsets table.
1561 Record.clear();
1562 Record.push_back(pch::SELECTOR_OFFSETS);
1563 Record.push_back(SelectorOffsets.size());
1564 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1565 (const char *)&SelectorOffsets.front(),
1566 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001567 }
1568}
1569
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001570//===----------------------------------------------------------------------===//
1571// Identifier Table Serialization
1572//===----------------------------------------------------------------------===//
1573
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001574namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001575class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001576 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001577 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001578
Douglas Gregora92193e2009-04-28 21:18:29 +00001579 /// \brief Determines whether this is an "interesting" identifier
1580 /// that needs a full IdentifierInfo structure written into the hash
1581 /// table.
1582 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1583 return II->isPoisoned() ||
1584 II->isExtensionToken() ||
1585 II->hasMacroDefinition() ||
1586 II->getObjCOrBuiltinID() ||
1587 II->getFETokenInfo<void>();
1588 }
1589
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001590public:
1591 typedef const IdentifierInfo* key_type;
1592 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001594 typedef pch::IdentID data_type;
1595 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001596
1597 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001598 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001599
1600 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001601 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001602 }
Mike Stump1eb44332009-09-09 15:08:12 +00001603
1604 std::pair<unsigned,unsigned>
1605 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001606 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001607 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001608 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1609 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001610 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001611 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001612 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001613 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001614 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1615 DEnd = IdentifierResolver::end();
1616 D != DEnd; ++D)
1617 DataLen += sizeof(pch::DeclID);
1618 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001619 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001620 // We emit the key length after the data length so that every
1621 // string is preceded by a 16-bit length. This matches the PTH
1622 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001623 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001624 return std::make_pair(KeyLen, DataLen);
1625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
1627 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001628 unsigned KeyLen) {
1629 // Record the location of the key data. This is used when generating
1630 // the mapping from persistent IDs to strings.
1631 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001632 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001633 }
Mike Stump1eb44332009-09-09 15:08:12 +00001634
1635 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001636 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001637 if (!isInterestingIdentifier(II)) {
1638 clang::io::Emit32(Out, ID << 1);
1639 return;
1640 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001641
Douglas Gregora92193e2009-04-28 21:18:29 +00001642 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001643 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001644 bool hasMacroDefinition =
1645 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001646 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001647 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor2deaea32009-04-22 18:49:13 +00001648 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001649 Bits = (Bits << 1) | II->isExtensionToken();
1650 Bits = (Bits << 1) | II->isPoisoned();
1651 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregor5998da52009-04-28 21:32:13 +00001652 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001653
Douglas Gregor37e26842009-04-21 23:56:24 +00001654 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001655 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001656
Douglas Gregor668c1a42009-04-21 22:25:48 +00001657 // Emit the declaration IDs in reverse order, because the
1658 // IdentifierResolver provides the declarations as they would be
1659 // visible (e.g., the function "stat" would come before the struct
1660 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1661 // adds declarations to the end of the list (so we need to see the
1662 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001663 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001664 IdentifierResolver::end());
1665 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1666 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001667 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001668 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001669 }
1670};
1671} // end anonymous namespace
1672
Douglas Gregorafaf3082009-04-11 00:14:32 +00001673/// \brief Write the identifier table into the PCH file.
1674///
1675/// The identifier table consists of a blob containing string data
1676/// (the actual identifiers themselves) and a separate "offsets" index
1677/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001678void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001679 using namespace llvm;
1680
1681 // Create and write out the blob that contains the identifier
1682 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001683 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001684 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Douglas Gregor92b059e2009-04-28 20:33:11 +00001686 // Look for any identifiers that were named while processing the
1687 // headers, but are otherwise not needed. We add these to the hash
1688 // table to enable checking of the predefines buffer in the case
1689 // where the user adds new macro definitions when building the PCH
1690 // file.
1691 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1692 IDEnd = PP.getIdentifierTable().end();
1693 ID != IDEnd; ++ID)
1694 getIdentifierRef(ID->second);
1695
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001696 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001697 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001698 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1699 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1700 ID != IDEnd; ++ID) {
1701 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001702 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001703 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001704
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001705 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001706 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001707 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001708 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001709 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001710 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001711 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001712 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001713 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001714 }
1715
1716 // Create a blob abbreviation
1717 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1718 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001720 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001721 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001722
1723 // Write the identifier table
1724 RecordData Record;
1725 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001726 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001727 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001728 }
1729
1730 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001731 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1732 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1735 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1736
1737 RecordData Record;
1738 Record.push_back(pch::IDENTIFIER_OFFSET);
1739 Record.push_back(IdentifierOffsets.size());
1740 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1741 (const char *)&IdentifierOffsets.front(),
1742 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001743}
1744
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001745//===----------------------------------------------------------------------===//
1746// General Serialization Routines
1747//===----------------------------------------------------------------------===//
1748
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001749/// \brief Write a record containing the given attributes.
1750void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1751 RecordData Record;
1752 for (; Attr; Attr = Attr->getNext()) {
1753 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1754 Record.push_back(Attr->isInherited());
1755 switch (Attr->getKind()) {
1756 case Attr::Alias:
1757 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1758 break;
1759
1760 case Attr::Aligned:
1761 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1762 break;
1763
1764 case Attr::AlwaysInline:
1765 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001767 case Attr::AnalyzerNoReturn:
1768 break;
1769
1770 case Attr::Annotate:
1771 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1772 break;
1773
1774 case Attr::AsmLabel:
1775 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1776 break;
1777
Sean Hunt7725e672009-11-25 04:20:27 +00001778 case Attr::BaseCheck:
1779 break;
1780
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001781 case Attr::Blocks:
1782 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1783 break;
1784
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001785 case Attr::CDecl:
1786 break;
1787
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001788 case Attr::Cleanup:
1789 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1790 break;
1791
1792 case Attr::Const:
1793 break;
1794
1795 case Attr::Constructor:
1796 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1797 break;
1798
1799 case Attr::DLLExport:
1800 case Attr::DLLImport:
1801 case Attr::Deprecated:
1802 break;
1803
1804 case Attr::Destructor:
1805 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1806 break;
1807
1808 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001809 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001810 break;
1811
1812 case Attr::Format: {
1813 const FormatAttr *Format = cast<FormatAttr>(Attr);
1814 AddString(Format->getType(), Record);
1815 Record.push_back(Format->getFormatIdx());
1816 Record.push_back(Format->getFirstArg());
1817 break;
1818 }
1819
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001820 case Attr::FormatArg: {
1821 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1822 Record.push_back(Format->getFormatIdx());
1823 break;
1824 }
1825
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001826 case Attr::Sentinel : {
1827 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1828 Record.push_back(Sentinel->getSentinel());
1829 Record.push_back(Sentinel->getNullPos());
1830 break;
1831 }
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Chris Lattnercf2a7212009-04-20 19:12:28 +00001833 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001834 case Attr::Hiding:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001835 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001836 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001837 case Attr::NoDebug:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001838 case Attr::NoReturn:
1839 case Attr::NoThrow:
Mike Stump1feade82009-08-26 22:31:08 +00001840 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001841 break;
1842
1843 case Attr::NonNull: {
1844 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1845 Record.push_back(NonNull->size());
1846 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1847 break;
1848 }
1849
1850 case Attr::ObjCException:
1851 case Attr::ObjCNSObject:
Ted Kremenekb71368d2009-05-09 02:44:38 +00001852 case Attr::CFReturnsRetained:
1853 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001854 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001855 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001856 break;
1857
Anders Carlssona860e752009-08-08 18:23:56 +00001858 case Attr::PragmaPack:
1859 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001860 break;
1861
Anders Carlssona860e752009-08-08 18:23:56 +00001862 case Attr::Packed:
1863 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001865 case Attr::Pure:
1866 break;
1867
1868 case Attr::Regparm:
1869 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1870 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Nate Begeman6f3d8382009-06-26 06:32:41 +00001872 case Attr::ReqdWorkGroupSize:
1873 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1874 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1875 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1876 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001877
1878 case Attr::Section:
1879 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1880 break;
1881
1882 case Attr::StdCall:
1883 case Attr::TransparentUnion:
1884 case Attr::Unavailable:
1885 case Attr::Unused:
1886 case Attr::Used:
1887 break;
1888
1889 case Attr::Visibility:
1890 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001891 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001892 break;
1893
1894 case Attr::WarnUnusedResult:
1895 case Attr::Weak:
1896 case Attr::WeakImport:
1897 break;
1898 }
1899 }
1900
Douglas Gregorc9490c02009-04-16 22:23:12 +00001901 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001902}
1903
1904void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1905 Record.push_back(Str.size());
1906 Record.insert(Record.end(), Str.begin(), Str.end());
1907}
1908
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001909/// \brief Note that the identifier II occurs at the given offset
1910/// within the identifier table.
1911void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001912 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001913}
1914
Douglas Gregor83941df2009-04-25 17:48:32 +00001915/// \brief Note that the selector Sel occurs at the given offset
1916/// within the method pool/selector table.
1917void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1918 unsigned ID = SelectorIDs[Sel];
1919 assert(ID && "Unknown selector");
1920 SelectorOffsets[ID - 1] = Offset;
1921}
1922
Mike Stump1eb44332009-09-09 15:08:12 +00001923PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1924 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00001925 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1926 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001927
Douglas Gregore650c8c2009-07-07 00:12:59 +00001928void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1929 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001930 using namespace llvm;
1931
Douglas Gregore7785042009-04-20 15:53:59 +00001932 ASTContext &Context = SemaRef.Context;
1933 Preprocessor &PP = SemaRef.PP;
1934
Douglas Gregor2cf26342009-04-09 22:27:44 +00001935 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001936 Stream.Emit((unsigned)'C', 8);
1937 Stream.Emit((unsigned)'P', 8);
1938 Stream.Emit((unsigned)'C', 8);
1939 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001941 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001942
1943 // The translation unit is the first declaration we'll emit.
1944 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001945 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001946
Douglas Gregor2deaea32009-04-22 18:49:13 +00001947 // Make sure that we emit IdentifierInfos (and any attached
1948 // declarations) for builtins.
1949 {
1950 IdentifierTable &Table = PP.getIdentifierTable();
1951 llvm::SmallVector<const char *, 32> BuiltinNames;
1952 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1953 Context.getLangOptions().NoBuiltin);
1954 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1955 getIdentifierRef(&Table.get(BuiltinNames[I]));
1956 }
1957
Chris Lattner63d65f82009-09-08 18:19:27 +00001958 // Build a record containing all of the tentative definitions in this file, in
1959 // TentativeDefinitionList order. Generally, this record will be empty for
1960 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001961 RecordData TentativeDefinitions;
Chris Lattner63d65f82009-09-08 18:19:27 +00001962 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1963 VarDecl *VD =
1964 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1965 if (VD) AddDeclRef(VD, TentativeDefinitions);
1966 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001967
Douglas Gregor14c22f22009-04-22 22:18:58 +00001968 // Build a record containing all of the locally-scoped external
1969 // declarations in this header file. Generally, this record will be
1970 // empty.
1971 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00001972 // FIXME: This is filling in the PCH file in densemap order which is
1973 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00001974 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00001975 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1976 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1977 TD != TDEnd; ++TD)
1978 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1979
Douglas Gregorb81c1702009-04-27 20:06:05 +00001980 // Build a record containing all of the ext_vector declarations.
1981 RecordData ExtVectorDecls;
1982 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1983 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1984
Douglas Gregor2cf26342009-04-09 22:27:44 +00001985 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001986 RecordData Record;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001987 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001988 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001989 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001990 if (StatCalls && !isysroot)
1991 WriteStatCache(*StatCalls, isysroot);
1992 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump1eb44332009-09-09 15:08:12 +00001993 WriteComments(Context);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001994 // Write the record of special types.
1995 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001997 AddTypeRef(Context.getBuiltinVaListType(), Record);
1998 AddTypeRef(Context.getObjCIdType(), Record);
1999 AddTypeRef(Context.getObjCSelType(), Record);
2000 AddTypeRef(Context.getObjCProtoType(), Record);
2001 AddTypeRef(Context.getObjCClassType(), Record);
2002 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2003 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2004 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002005 AddTypeRef(Context.getjmp_bufType(), Record);
2006 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002007 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2008 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002009#if 0
2010 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00002011 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002012#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00002013 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002014 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002015 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Douglas Gregor366809a2009-04-26 03:49:13 +00002017 // Keep writing types and declarations until all types and
2018 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002019 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2020 WriteDeclsBlockAbbrevs();
2021 while (!DeclTypesToEmit.empty()) {
2022 DeclOrType DOT = DeclTypesToEmit.front();
2023 DeclTypesToEmit.pop();
2024 if (DOT.isType())
2025 WriteType(DOT.getType());
2026 else
2027 WriteDecl(Context, DOT.getDecl());
2028 }
2029 Stream.ExitBlock();
2030
Douglas Gregor813a97b2009-10-17 17:25:45 +00002031 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002032 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002033 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002034
2035 // Write the type offsets array
2036 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2037 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2040 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2041 Record.clear();
2042 Record.push_back(pch::TYPE_OFFSET);
2043 Record.push_back(TypeOffsets.size());
2044 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002045 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002046 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002048 // Write the declaration offsets array
2049 Abbrev = new BitCodeAbbrev();
2050 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2052 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2053 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2054 Record.clear();
2055 Record.push_back(pch::DECL_OFFSET);
2056 Record.push_back(DeclOffsets.size());
2057 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002058 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002059 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002060
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002061 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002062 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002063 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002064
2065 // Write the record containing tentative definitions.
2066 if (!TentativeDefinitions.empty())
2067 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002068
2069 // Write the record containing locally-scoped external definitions.
2070 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002071 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002072 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002073
2074 // Write the record containing ext_vector type names.
2075 if (!ExtVectorDecls.empty())
2076 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Douglas Gregor3e1af842009-04-17 22:13:46 +00002078 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002079 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002080 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002081 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002082 Record.push_back(NumLexicalDeclContexts);
2083 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002084 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002085 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002086}
2087
2088void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2089 Record.push_back(Loc.getRawEncoding());
2090}
2091
2092void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2093 Record.push_back(Value.getBitWidth());
2094 unsigned N = Value.getNumWords();
2095 const uint64_t* Words = Value.getRawData();
2096 for (unsigned I = 0; I != N; ++I)
2097 Record.push_back(Words[I]);
2098}
2099
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002100void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2101 Record.push_back(Value.isUnsigned());
2102 AddAPInt(Value, Record);
2103}
2104
Douglas Gregor17fc2232009-04-14 21:55:33 +00002105void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2106 AddAPInt(Value.bitcastToAPInt(), Record);
2107}
2108
Douglas Gregor2cf26342009-04-09 22:27:44 +00002109void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002110 Record.push_back(getIdentifierRef(II));
2111}
2112
2113pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2114 if (II == 0)
2115 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002116
2117 pch::IdentID &ID = IdentifierIDs[II];
2118 if (ID == 0)
2119 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002120 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002121}
2122
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002123void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2124 if (SelRef.getAsOpaquePtr() == 0) {
2125 Record.push_back(0);
2126 return;
2127 }
2128
2129 pch::SelectorID &SID = SelectorIDs[SelRef];
2130 if (SID == 0) {
2131 SID = SelectorIDs.size();
2132 SelVector.push_back(SelRef);
2133 }
2134 Record.push_back(SID);
2135}
2136
John McCall833ca992009-10-29 08:12:44 +00002137void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2138 RecordData &Record) {
2139 switch (Arg.getArgument().getKind()) {
2140 case TemplateArgument::Expression:
2141 AddStmt(Arg.getLocInfo().getAsExpr());
2142 break;
2143 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002144 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002145 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002146 case TemplateArgument::Template:
2147 Record.push_back(
2148 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2149 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2150 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2151 break;
John McCall833ca992009-10-29 08:12:44 +00002152 case TemplateArgument::Null:
2153 case TemplateArgument::Integral:
2154 case TemplateArgument::Declaration:
2155 case TemplateArgument::Pack:
2156 break;
2157 }
2158}
2159
John McCalla93c9342009-12-07 02:54:59 +00002160void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2161 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002162 AddTypeRef(QualType(), Record);
2163 return;
2164 }
2165
John McCalla93c9342009-12-07 02:54:59 +00002166 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002167 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002168 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002169 TLW.Visit(TL);
2170}
2171
Douglas Gregor2cf26342009-04-09 22:27:44 +00002172void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2173 if (T.isNull()) {
2174 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2175 return;
2176 }
2177
Douglas Gregora4923eb2009-11-16 21:35:15 +00002178 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002179 T.removeFastQualifiers();
2180
Douglas Gregora4923eb2009-11-16 21:35:15 +00002181 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002182 pch::TypeID &ID = TypeIDs[T];
2183 if (ID == 0) {
2184 // We haven't seen these qualifiers applied to this type before.
2185 // Assign it a new ID. This is the only time we enqueue a
2186 // qualified type, and it has no CV qualifiers.
2187 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002188 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002189 }
2190
2191 // Encode the type qualifiers in the type reference.
2192 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2193 return;
2194 }
2195
Douglas Gregora4923eb2009-11-16 21:35:15 +00002196 assert(!T.hasLocalQualifiers());
John McCall0953e762009-09-24 19:53:00 +00002197
Douglas Gregor2cf26342009-04-09 22:27:44 +00002198 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002199 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002200 switch (BT->getKind()) {
2201 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2202 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2203 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2204 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2205 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2206 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2207 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2208 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002209 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002210 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2211 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2212 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2213 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2214 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2215 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2216 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002217 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002218 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2219 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2220 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002221 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002222 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2223 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002224 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2225 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002226 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2227 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002228 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002229 case BuiltinType::UndeducedAuto:
2230 assert(0 && "Should not see undeduced auto here");
2231 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002232 }
2233
John McCall0953e762009-09-24 19:53:00 +00002234 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002235 return;
2236 }
2237
John McCall0953e762009-09-24 19:53:00 +00002238 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002239 if (ID == 0) {
2240 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002241 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002242 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002243 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002244 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002245
2246 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002247 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002248}
2249
2250void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2251 if (D == 0) {
2252 Record.push_back(0);
2253 return;
2254 }
2255
Douglas Gregor8038d512009-04-10 17:25:41 +00002256 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002257 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002258 // We haven't seen this declaration before. Give it a new ID and
2259 // enqueue it in the list of declarations to emit.
2260 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002261 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002262 }
2263
2264 Record.push_back(ID);
2265}
2266
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002267pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2268 if (D == 0)
2269 return 0;
2270
2271 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2272 return DeclIDs[D];
2273}
2274
Douglas Gregor2cf26342009-04-09 22:27:44 +00002275void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002276 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002277 Record.push_back(Name.getNameKind());
2278 switch (Name.getNameKind()) {
2279 case DeclarationName::Identifier:
2280 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2281 break;
2282
2283 case DeclarationName::ObjCZeroArgSelector:
2284 case DeclarationName::ObjCOneArgSelector:
2285 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002286 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002287 break;
2288
2289 case DeclarationName::CXXConstructorName:
2290 case DeclarationName::CXXDestructorName:
2291 case DeclarationName::CXXConversionFunctionName:
2292 AddTypeRef(Name.getCXXNameType(), Record);
2293 break;
2294
2295 case DeclarationName::CXXOperatorName:
2296 Record.push_back(Name.getCXXOverloadedOperator());
2297 break;
2298
Sean Hunt3e518bd2009-11-29 07:34:05 +00002299 case DeclarationName::CXXLiteralOperatorName:
2300 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2301 break;
2302
Douglas Gregor2cf26342009-04-09 22:27:44 +00002303 case DeclarationName::CXXUsingDirective:
2304 // No extra data to emit
2305 break;
2306 }
2307}
Douglas Gregor0b748912009-04-14 21:18:50 +00002308