blob: 124df63aade073f1f4503c44a9fc127ce355f100 [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);
Douglas Gregor91236662009-12-22 18:11:50 +0000147 Record.push_back(T->getNoReturnAttr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
150void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
151 VisitFunctionType(T);
152 Code = pch::TYPE_FUNCTION_NO_PROTO;
153}
154
155void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
156 VisitFunctionType(T);
157 Record.push_back(T->getNumArgs());
158 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
159 Writer.AddTypeRef(T->getArgType(I), Record);
160 Record.push_back(T->isVariadic());
161 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000162 Record.push_back(T->hasExceptionSpec());
163 Record.push_back(T->hasAnyExceptionSpec());
164 Record.push_back(T->getNumExceptions());
165 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
166 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Code = pch::TYPE_FUNCTION_PROTO;
168}
169
John McCalled976492009-12-04 22:46:56 +0000170#if 0
171// For when we want it....
172void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
173 Writer.AddDeclRef(T->getDecl(), Record);
174 Code = pch::TYPE_UNRESOLVED_USING;
175}
176#endif
177
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
179 Writer.AddDeclRef(T->getDecl(), Record);
180 Code = pch::TYPE_TYPEDEF;
181}
182
183void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000184 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000185 Code = pch::TYPE_TYPEOF_EXPR;
186}
187
188void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
189 Writer.AddTypeRef(T->getUnderlyingType(), Record);
190 Code = pch::TYPE_TYPEOF;
191}
192
Anders Carlsson395b4752009-06-24 19:06:50 +0000193void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
194 Writer.AddStmt(T->getUnderlyingExpr());
195 Code = pch::TYPE_DECLTYPE;
196}
197
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198void PCHTypeWriter::VisitTagType(const TagType *T) {
199 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000200 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201 "Cannot serialize in the middle of a type definition");
202}
203
204void PCHTypeWriter::VisitRecordType(const RecordType *T) {
205 VisitTagType(T);
206 Code = pch::TYPE_RECORD;
207}
208
209void PCHTypeWriter::VisitEnumType(const EnumType *T) {
210 VisitTagType(T);
211 Code = pch::TYPE_ENUM;
212}
213
John McCall7da24312009-09-05 00:15:47 +0000214void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
215 Writer.AddTypeRef(T->getUnderlyingType(), Record);
216 Record.push_back(T->getTagKind());
217 Code = pch::TYPE_ELABORATED;
218}
219
Mike Stump1eb44332009-09-09 15:08:12 +0000220void
John McCall49a832b2009-10-18 09:09:24 +0000221PCHTypeWriter::VisitSubstTemplateTypeParmType(
222 const SubstTemplateTypeParmType *T) {
223 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
224 Writer.AddTypeRef(T->getReplacementType(), Record);
225 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
226}
227
228void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229PCHTypeWriter::VisitTemplateSpecializationType(
230 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000231 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000232 assert(false && "Cannot serialize template specialization types");
233}
234
235void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000236 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000237 assert(false && "Cannot serialize qualified name types");
238}
239
240void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
241 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000242 Record.push_back(T->getNumProtocols());
Steve Naroff446ee4e2009-05-27 16:21:00 +0000243 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
244 E = T->qual_end(); I != E; ++I)
245 Writer.AddDeclRef(*I, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000246 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000247}
248
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000249void
250PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000251 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000252 Record.push_back(T->getNumProtocols());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000253 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000254 E = T->qual_end(); I != E; ++I)
255 Writer.AddDeclRef(*I, Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000256 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257}
258
John McCalla1ee0c52009-10-16 21:56:05 +0000259namespace {
260
261class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
262 PCHWriter &Writer;
263 PCHWriter::RecordData &Record;
264
265public:
266 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
267 : Writer(Writer), Record(Record) { }
268
John McCall51bd8032009-10-18 01:05:36 +0000269#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000270#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000271 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000272#include "clang/AST/TypeLocNodes.def"
273
John McCall51bd8032009-10-18 01:05:36 +0000274 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
275 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000276};
277
278}
279
John McCall51bd8032009-10-18 01:05:36 +0000280void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
281 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000282}
John McCall51bd8032009-10-18 01:05:36 +0000283void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
284 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000285}
John McCall51bd8032009-10-18 01:05:36 +0000286void TypeLocWriter::VisitFixedWidthIntTypeLoc(FixedWidthIntTypeLoc TL) {
287 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000288}
John McCall51bd8032009-10-18 01:05:36 +0000289void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
290 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000291}
John McCall51bd8032009-10-18 01:05:36 +0000292void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
293 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000294}
John McCall51bd8032009-10-18 01:05:36 +0000295void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
296 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000297}
John McCall51bd8032009-10-18 01:05:36 +0000298void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
299 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000300}
John McCall51bd8032009-10-18 01:05:36 +0000301void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
302 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000303}
John McCall51bd8032009-10-18 01:05:36 +0000304void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
305 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000306}
John McCall51bd8032009-10-18 01:05:36 +0000307void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
308 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
309 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
310 Record.push_back(TL.getSizeExpr() ? 1 : 0);
311 if (TL.getSizeExpr())
312 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000313}
John McCall51bd8032009-10-18 01:05:36 +0000314void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
315 VisitArrayTypeLoc(TL);
316}
317void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
318 VisitArrayTypeLoc(TL);
319}
320void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
321 VisitArrayTypeLoc(TL);
322}
323void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
324 DependentSizedArrayTypeLoc TL) {
325 VisitArrayTypeLoc(TL);
326}
327void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
328 DependentSizedExtVectorTypeLoc TL) {
329 Writer.AddSourceLocation(TL.getNameLoc(), Record);
330}
331void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
332 Writer.AddSourceLocation(TL.getNameLoc(), Record);
333}
334void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
335 Writer.AddSourceLocation(TL.getNameLoc(), Record);
336}
337void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
338 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
339 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
340 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
341 Writer.AddDeclRef(TL.getArg(i), Record);
342}
343void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
344 VisitFunctionTypeLoc(TL);
345}
346void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
347 VisitFunctionTypeLoc(TL);
348}
John McCalled976492009-12-04 22:46:56 +0000349void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
350 Writer.AddSourceLocation(TL.getNameLoc(), Record);
351}
John McCall51bd8032009-10-18 01:05:36 +0000352void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
353 Writer.AddSourceLocation(TL.getNameLoc(), Record);
354}
355void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
356 Writer.AddSourceLocation(TL.getNameLoc(), Record);
357}
358void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
359 Writer.AddSourceLocation(TL.getNameLoc(), Record);
360}
361void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
362 Writer.AddSourceLocation(TL.getNameLoc(), Record);
363}
364void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
365 Writer.AddSourceLocation(TL.getNameLoc(), Record);
366}
367void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
368 Writer.AddSourceLocation(TL.getNameLoc(), Record);
369}
370void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getNameLoc(), Record);
372}
373void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getNameLoc(), Record);
375}
John McCall49a832b2009-10-18 09:09:24 +0000376void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
377 SubstTemplateTypeParmTypeLoc TL) {
378 Writer.AddSourceLocation(TL.getNameLoc(), Record);
379}
John McCall51bd8032009-10-18 01:05:36 +0000380void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
381 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000382 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
383 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
384 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
385 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
386 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000387}
388void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
389 Writer.AddSourceLocation(TL.getNameLoc(), Record);
390}
391void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
392 Writer.AddSourceLocation(TL.getNameLoc(), Record);
393}
394void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
395 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000396 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
397 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
398 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
399 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000400}
John McCall54e14c42009-10-22 22:37:11 +0000401void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
402 Writer.AddSourceLocation(TL.getStarLoc(), Record);
403 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
404 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
405 Record.push_back(TL.hasBaseTypeAsWritten());
406 Record.push_back(TL.hasProtocolsAsWritten());
407 if (TL.hasProtocolsAsWritten())
408 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
409 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
410}
John McCalla1ee0c52009-10-16 21:56:05 +0000411
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000412//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000413// PCHWriter Implementation
414//===----------------------------------------------------------------------===//
415
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000416static void EmitBlockID(unsigned ID, const char *Name,
417 llvm::BitstreamWriter &Stream,
418 PCHWriter::RecordData &Record) {
419 Record.clear();
420 Record.push_back(ID);
421 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
422
423 // Emit the block name if present.
424 if (Name == 0 || Name[0] == 0) return;
425 Record.clear();
426 while (*Name)
427 Record.push_back(*Name++);
428 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
429}
430
431static void EmitRecordID(unsigned ID, const char *Name,
432 llvm::BitstreamWriter &Stream,
433 PCHWriter::RecordData &Record) {
434 Record.clear();
435 Record.push_back(ID);
436 while (*Name)
437 Record.push_back(*Name++);
438 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000439}
440
441static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
442 PCHWriter::RecordData &Record) {
443#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
444 RECORD(STMT_STOP);
445 RECORD(STMT_NULL_PTR);
446 RECORD(STMT_NULL);
447 RECORD(STMT_COMPOUND);
448 RECORD(STMT_CASE);
449 RECORD(STMT_DEFAULT);
450 RECORD(STMT_LABEL);
451 RECORD(STMT_IF);
452 RECORD(STMT_SWITCH);
453 RECORD(STMT_WHILE);
454 RECORD(STMT_DO);
455 RECORD(STMT_FOR);
456 RECORD(STMT_GOTO);
457 RECORD(STMT_INDIRECT_GOTO);
458 RECORD(STMT_CONTINUE);
459 RECORD(STMT_BREAK);
460 RECORD(STMT_RETURN);
461 RECORD(STMT_DECL);
462 RECORD(STMT_ASM);
463 RECORD(EXPR_PREDEFINED);
464 RECORD(EXPR_DECL_REF);
465 RECORD(EXPR_INTEGER_LITERAL);
466 RECORD(EXPR_FLOATING_LITERAL);
467 RECORD(EXPR_IMAGINARY_LITERAL);
468 RECORD(EXPR_STRING_LITERAL);
469 RECORD(EXPR_CHARACTER_LITERAL);
470 RECORD(EXPR_PAREN);
471 RECORD(EXPR_UNARY_OPERATOR);
472 RECORD(EXPR_SIZEOF_ALIGN_OF);
473 RECORD(EXPR_ARRAY_SUBSCRIPT);
474 RECORD(EXPR_CALL);
475 RECORD(EXPR_MEMBER);
476 RECORD(EXPR_BINARY_OPERATOR);
477 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
478 RECORD(EXPR_CONDITIONAL_OPERATOR);
479 RECORD(EXPR_IMPLICIT_CAST);
480 RECORD(EXPR_CSTYLE_CAST);
481 RECORD(EXPR_COMPOUND_LITERAL);
482 RECORD(EXPR_EXT_VECTOR_ELEMENT);
483 RECORD(EXPR_INIT_LIST);
484 RECORD(EXPR_DESIGNATED_INIT);
485 RECORD(EXPR_IMPLICIT_VALUE_INIT);
486 RECORD(EXPR_VA_ARG);
487 RECORD(EXPR_ADDR_LABEL);
488 RECORD(EXPR_STMT);
489 RECORD(EXPR_TYPES_COMPATIBLE);
490 RECORD(EXPR_CHOOSE);
491 RECORD(EXPR_GNU_NULL);
492 RECORD(EXPR_SHUFFLE_VECTOR);
493 RECORD(EXPR_BLOCK);
494 RECORD(EXPR_BLOCK_DECL_REF);
495 RECORD(EXPR_OBJC_STRING_LITERAL);
496 RECORD(EXPR_OBJC_ENCODE);
497 RECORD(EXPR_OBJC_SELECTOR_EXPR);
498 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
499 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
500 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
501 RECORD(EXPR_OBJC_KVC_REF_EXPR);
502 RECORD(EXPR_OBJC_MESSAGE_EXPR);
503 RECORD(EXPR_OBJC_SUPER_EXPR);
504 RECORD(STMT_OBJC_FOR_COLLECTION);
505 RECORD(STMT_OBJC_CATCH);
506 RECORD(STMT_OBJC_FINALLY);
507 RECORD(STMT_OBJC_AT_TRY);
508 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
509 RECORD(STMT_OBJC_AT_THROW);
510#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000511}
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000513void PCHWriter::WriteBlockInfoBlock() {
514 RecordData Record;
515 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner2f4efd12009-04-27 00:40:25 +0000517#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000518#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000520 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000521 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000522 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000523 RECORD(TYPE_OFFSET);
524 RECORD(DECL_OFFSET);
525 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000526 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000527 RECORD(IDENTIFIER_OFFSET);
528 RECORD(IDENTIFIER_TABLE);
529 RECORD(EXTERNAL_DEFINITIONS);
530 RECORD(SPECIAL_TYPES);
531 RECORD(STATISTICS);
532 RECORD(TENTATIVE_DEFINITIONS);
533 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
534 RECORD(SELECTOR_OFFSETS);
535 RECORD(METHOD_POOL);
536 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000537 RECORD(SOURCE_LOCATION_OFFSETS);
538 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000539 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000540 RECORD(EXT_VECTOR_DECLS);
Douglas Gregor2e222532009-07-02 17:08:52 +0000541 RECORD(COMMENT_RANGES);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000542 RECORD(SVN_BRANCH_REVISION);
543
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000544 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000545 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000546 RECORD(SM_SLOC_FILE_ENTRY);
547 RECORD(SM_SLOC_BUFFER_ENTRY);
548 RECORD(SM_SLOC_BUFFER_BLOB);
549 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
550 RECORD(SM_LINE_TABLE);
551 RECORD(SM_HEADER_FILE_INFO);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000553 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000554 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000555 RECORD(PP_MACRO_OBJECT_LIKE);
556 RECORD(PP_MACRO_FUNCTION_LIKE);
557 RECORD(PP_TOKEN);
558
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000559 // Decls and Types block.
560 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000561 RECORD(TYPE_EXT_QUAL);
562 RECORD(TYPE_FIXED_WIDTH_INT);
563 RECORD(TYPE_COMPLEX);
564 RECORD(TYPE_POINTER);
565 RECORD(TYPE_BLOCK_POINTER);
566 RECORD(TYPE_LVALUE_REFERENCE);
567 RECORD(TYPE_RVALUE_REFERENCE);
568 RECORD(TYPE_MEMBER_POINTER);
569 RECORD(TYPE_CONSTANT_ARRAY);
570 RECORD(TYPE_INCOMPLETE_ARRAY);
571 RECORD(TYPE_VARIABLE_ARRAY);
572 RECORD(TYPE_VECTOR);
573 RECORD(TYPE_EXT_VECTOR);
574 RECORD(TYPE_FUNCTION_PROTO);
575 RECORD(TYPE_FUNCTION_NO_PROTO);
576 RECORD(TYPE_TYPEDEF);
577 RECORD(TYPE_TYPEOF_EXPR);
578 RECORD(TYPE_TYPEOF);
579 RECORD(TYPE_RECORD);
580 RECORD(TYPE_ENUM);
581 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000582 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000583 RECORD(DECL_ATTR);
584 RECORD(DECL_TRANSLATION_UNIT);
585 RECORD(DECL_TYPEDEF);
586 RECORD(DECL_ENUM);
587 RECORD(DECL_RECORD);
588 RECORD(DECL_ENUM_CONSTANT);
589 RECORD(DECL_FUNCTION);
590 RECORD(DECL_OBJC_METHOD);
591 RECORD(DECL_OBJC_INTERFACE);
592 RECORD(DECL_OBJC_PROTOCOL);
593 RECORD(DECL_OBJC_IVAR);
594 RECORD(DECL_OBJC_AT_DEFS_FIELD);
595 RECORD(DECL_OBJC_CLASS);
596 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
597 RECORD(DECL_OBJC_CATEGORY);
598 RECORD(DECL_OBJC_CATEGORY_IMPL);
599 RECORD(DECL_OBJC_IMPLEMENTATION);
600 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
601 RECORD(DECL_OBJC_PROPERTY);
602 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000603 RECORD(DECL_FIELD);
604 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000605 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000606 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000607 RECORD(DECL_FILE_SCOPE_ASM);
608 RECORD(DECL_BLOCK);
609 RECORD(DECL_CONTEXT_LEXICAL);
610 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000611 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000612 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000613#undef RECORD
614#undef BLOCK
615 Stream.ExitBlock();
616}
617
Douglas Gregore650c8c2009-07-07 00:12:59 +0000618/// \brief Adjusts the given filename to only write out the portion of the
619/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000620///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000621/// \param Filename the file name to adjust.
622///
623/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
624/// the returned filename will be adjusted by this system root.
625///
626/// \returns either the original filename (if it needs no adjustment) or the
627/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000628static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000629adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
630 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Douglas Gregore650c8c2009-07-07 00:12:59 +0000632 if (!isysroot)
633 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Douglas Gregore650c8c2009-07-07 00:12:59 +0000635 // Verify that the filename and the system root have the same prefix.
636 unsigned Pos = 0;
637 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
638 if (Filename[Pos] != isysroot[Pos])
639 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Douglas Gregore650c8c2009-07-07 00:12:59 +0000641 // We hit the end of the filename before we hit the end of the system root.
642 if (!Filename[Pos])
643 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Douglas Gregore650c8c2009-07-07 00:12:59 +0000645 // If the file name has a '/' at the current position, skip over the '/'.
646 // We distinguish sysroot-based includes from absolute includes by the
647 // absence of '/' at the beginning of sysroot-based includes.
648 if (Filename[Pos] == '/')
649 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Douglas Gregore650c8c2009-07-07 00:12:59 +0000651 return Filename + Pos;
652}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000653
Douglas Gregorab41e632009-04-27 22:23:34 +0000654/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000655void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000656 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000657
Douglas Gregore650c8c2009-07-07 00:12:59 +0000658 // Metadata
659 const TargetInfo &Target = Context.Target;
660 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
661 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
662 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
663 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
664 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
665 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
666 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
667 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
668 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Douglas Gregore650c8c2009-07-07 00:12:59 +0000670 RecordData Record;
671 Record.push_back(pch::METADATA);
672 Record.push_back(pch::VERSION_MAJOR);
673 Record.push_back(pch::VERSION_MINOR);
674 Record.push_back(CLANG_VERSION_MAJOR);
675 Record.push_back(CLANG_VERSION_MINOR);
676 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000677 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000678 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Douglas Gregorb64c1932009-05-12 01:31:05 +0000680 // Original file name
681 SourceManager &SM = Context.getSourceManager();
682 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
683 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
684 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
685 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
686 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
687
688 llvm::sys::Path MainFilePath(MainFile->getName());
689 std::string MainFileName;
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Douglas Gregorb64c1932009-05-12 01:31:05 +0000691 if (!MainFilePath.isAbsolute()) {
692 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000693 P.appendComponent(MainFilePath.str());
694 MainFileName = P.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000695 } else {
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000696 MainFileName = MainFilePath.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000697 }
698
Douglas Gregore650c8c2009-07-07 00:12:59 +0000699 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000700 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000701 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000702 RecordData Record;
703 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000704 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000705 }
Douglas Gregor445e23e2009-10-05 21:07:28 +0000706
707 // Subversion branch/version information.
708 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
709 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
710 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
711 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
712 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
713 Record.clear();
714 Record.push_back(pch::SVN_BRANCH_REVISION);
715 Record.push_back(getClangSubversionRevision());
716 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000717}
718
719/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000720void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
721 RecordData Record;
722 Record.push_back(LangOpts.Trigraphs);
723 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
724 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
725 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
726 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
727 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
728 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
729 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
730 Record.push_back(LangOpts.C99); // C99 Support
731 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
732 Record.push_back(LangOpts.CPlusPlus); // C++ Support
733 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000734 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000736 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
737 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
738 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000740 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000741 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
742 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000743 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000744 Record.push_back(LangOpts.Exceptions); // Support exception handling.
745
746 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
747 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
748 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
749
Chris Lattnerea5ce472009-04-27 07:35:58 +0000750 // Whether static initializers are protected by locks.
751 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000752 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000753 Record.push_back(LangOpts.Blocks); // block extension to C
754 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
755 // they are unused.
756 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
757 // (modulo the platform support).
758
759 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
760 // signed integer arithmetic overflows.
761
762 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
763 // may be ripped out at any time.
764
765 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000766 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000767 // defined.
768 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
769 // opposed to __DYNAMIC__).
770 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
771
772 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
773 // used (instead of C99 semantics).
774 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000775 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
776 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000777 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
778 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000779 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000780 Record.push_back(LangOpts.getGCMode());
781 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000782 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000783 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000784 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000785 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000786 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000787 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000788}
789
Douglas Gregor14f79002009-04-10 03:52:48 +0000790//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000791// stat cache Serialization
792//===----------------------------------------------------------------------===//
793
794namespace {
795// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000796class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000797public:
798 typedef const char * key_type;
799 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000801 typedef std::pair<int, struct stat> data_type;
802 typedef const data_type& data_type_ref;
803
804 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000805 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
808 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000809 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
810 data_type_ref Data) {
811 unsigned StrLen = strlen(path);
812 clang::io::Emit16(Out, StrLen);
813 unsigned DataLen = 1; // result value
814 if (Data.first == 0)
815 DataLen += 4 + 4 + 2 + 8 + 8;
816 clang::io::Emit8(Out, DataLen);
817 return std::make_pair(StrLen + 1, DataLen);
818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000820 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
821 Out.write(path, KeyLen);
822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000824 void EmitData(llvm::raw_ostream& Out, key_type_ref,
825 data_type_ref Data, unsigned DataLen) {
826 using namespace clang::io;
827 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000829 // Result of stat()
830 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000832 if (Data.first == 0) {
833 Emit32(Out, (uint32_t) Data.second.st_ino);
834 Emit32(Out, (uint32_t) Data.second.st_dev);
835 Emit16(Out, (uint16_t) Data.second.st_mode);
836 Emit64(Out, (uint64_t) Data.second.st_mtime);
837 Emit64(Out, (uint64_t) Data.second.st_size);
838 }
839
840 assert(Out.tell() - Start == DataLen && "Wrong data length");
841 }
842};
843} // end anonymous namespace
844
845/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000846void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
847 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000848 // Build the on-disk hash table containing information about every
849 // stat() call.
850 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
851 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000852 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000853 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000854 Stat != StatEnd; ++Stat, ++NumStatEntries) {
855 const char *Filename = Stat->first();
856 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
857 Generator.insert(Filename, Stat->second);
858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000860 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000861 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000862 uint32_t BucketOffset;
863 {
864 llvm::raw_svector_ostream Out(StatCacheData);
865 // Make sure that no bucket is at offset 0
866 clang::io::Emit32(Out, 0);
867 BucketOffset = Generator.Emit(Out);
868 }
869
870 // Create a blob abbreviation
871 using namespace llvm;
872 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
873 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
877 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
878
879 // Write the stat cache
880 RecordData Record;
881 Record.push_back(pch::STAT_CACHE);
882 Record.push_back(BucketOffset);
883 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000884 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000885}
886
887//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000888// Source Manager Serialization
889//===----------------------------------------------------------------------===//
890
891/// \brief Create an abbreviation for the SLocEntry that refers to a
892/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000893static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000894 using namespace llvm;
895 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
896 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000902 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000903}
904
905/// \brief Create an abbreviation for the SLocEntry that refers to a
906/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000907static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000908 using namespace llvm;
909 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
910 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000916 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000917}
918
919/// \brief Create an abbreviation for the SLocEntry that refers to a
920/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000921static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000922 using namespace llvm;
923 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
924 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000926 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000927}
928
929/// \brief Create an abbreviation for the SLocEntry that refers to an
930/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000931static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000932 using namespace llvm;
933 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
934 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000940 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000941}
942
943/// \brief Writes the block containing the serialized form of the
944/// source manager.
945///
946/// TODO: We should probably use an on-disk hash table (stored in a
947/// blob), indexed based on the file name, so that we only create
948/// entries for files that we actually need. In the common case (no
949/// errors), we probably won't have to create file entries for any of
950/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000951void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000952 const Preprocessor &PP,
953 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000954 RecordData Record;
955
Chris Lattnerf04ad692009-04-10 17:16:57 +0000956 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000957 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000958
959 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000960 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
961 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
962 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
963 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000964
Douglas Gregorbd945002009-04-13 16:31:14 +0000965 // Write the line table.
966 if (SourceMgr.hasLineTable()) {
967 LineTableInfo &LineTable = SourceMgr.getLineTable();
968
969 // Emit the file names
970 Record.push_back(LineTable.getNumFilenames());
971 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
972 // Emit the file name
973 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000974 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +0000975 unsigned FilenameLen = Filename? strlen(Filename) : 0;
976 Record.push_back(FilenameLen);
977 if (FilenameLen)
978 Record.insert(Record.end(), Filename, Filename + FilenameLen);
979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Douglas Gregorbd945002009-04-13 16:31:14 +0000981 // Emit the line entries
982 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
983 L != LEnd; ++L) {
984 // Emit the file ID
985 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Douglas Gregorbd945002009-04-13 16:31:14 +0000987 // Emit the line entries
988 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000989 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +0000990 LEEnd = L->second.end();
991 LE != LEEnd; ++LE) {
992 Record.push_back(LE->FileOffset);
993 Record.push_back(LE->LineNo);
994 Record.push_back(LE->FilenameID);
995 Record.push_back((unsigned)LE->FileKind);
996 Record.push_back(LE->IncludeOffset);
997 }
Douglas Gregorbd945002009-04-13 16:31:14 +0000998 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +0000999 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001000 }
1001
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001002 // Write out entries for all of the header files we know about.
Mike Stump1eb44332009-09-09 15:08:12 +00001003 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001004 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001005 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001006 E = HS.header_file_end();
1007 I != E; ++I) {
1008 Record.push_back(I->isImport);
1009 Record.push_back(I->DirInfo);
1010 Record.push_back(I->NumIncludes);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001011 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001012 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1013 Record.clear();
1014 }
1015
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001016 // Write out the source location entry table. We skip the first
1017 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001018 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001019 RecordData PreloadSLocs;
1020 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001021 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1022 // Get this source location entry.
1023 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1024
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001025 // Record the offset of this source-location entry.
1026 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1027
1028 // Figure out which record code to use.
1029 unsigned Code;
1030 if (SLoc->isFile()) {
1031 if (SLoc->getFile().getContentCache()->Entry)
1032 Code = pch::SM_SLOC_FILE_ENTRY;
1033 else
1034 Code = pch::SM_SLOC_BUFFER_ENTRY;
1035 } else
1036 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1037 Record.clear();
1038 Record.push_back(Code);
1039
1040 Record.push_back(SLoc->getOffset());
1041 if (SLoc->isFile()) {
1042 const SrcMgr::FileInfo &File = SLoc->getFile();
1043 Record.push_back(File.getIncludeLoc().getRawEncoding());
1044 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1045 Record.push_back(File.hasLineDirectives());
1046
1047 const SrcMgr::ContentCache *Content = File.getContentCache();
1048 if (Content->Entry) {
1049 // The source location entry is a file. The blob associated
1050 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Douglas Gregore650c8c2009-07-07 00:12:59 +00001052 // Turn the file name into an absolute path, if it isn't already.
1053 const char *Filename = Content->Entry->getName();
1054 llvm::sys::Path FilePath(Filename, strlen(Filename));
1055 std::string FilenameStr;
1056 if (!FilePath.isAbsolute()) {
1057 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +00001058 P.appendComponent(FilePath.str());
1059 FilenameStr = P.str();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001060 Filename = FilenameStr.c_str();
1061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Douglas Gregore650c8c2009-07-07 00:12:59 +00001063 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001064 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001065
1066 // FIXME: For now, preload all file source locations, so that
1067 // we get the appropriate File entries in the reader. This is
1068 // a temporary measure.
1069 PreloadSLocs.push_back(SLocEntryOffsets.size());
1070 } else {
1071 // The source location entry is a buffer. The blob associated
1072 // with this entry contains the contents of the buffer.
1073
1074 // We add one to the size so that we capture the trailing NULL
1075 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1076 // the reader side).
1077 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1078 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001079 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1080 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001081 Record.clear();
1082 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1083 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001084 llvm::StringRef(Buffer->getBufferStart(),
1085 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001086
1087 if (strcmp(Name, "<built-in>") == 0)
1088 PreloadSLocs.push_back(SLocEntryOffsets.size());
1089 }
1090 } else {
1091 // The source location entry is an instantiation.
1092 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1093 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1094 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1095 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1096
1097 // Compute the token length for this macro expansion.
1098 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001099 if (I + 1 != N)
1100 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001101 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1102 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1103 }
1104 }
1105
Douglas Gregorc9490c02009-04-16 22:23:12 +00001106 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001107
1108 if (SLocEntryOffsets.empty())
1109 return;
1110
1111 // Write the source-location offsets table into the PCH block. This
1112 // table is used for lazily loading source-location information.
1113 using namespace llvm;
1114 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1115 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1117 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1118 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1119 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001121 Record.clear();
1122 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1123 Record.push_back(SLocEntryOffsets.size());
1124 Record.push_back(SourceMgr.getNextOffset());
1125 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001126 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001127 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128
1129 // Write the source location entry preloads array, telling the PCH
1130 // reader which source locations entries it should load eagerly.
1131 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001132}
1133
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001134//===----------------------------------------------------------------------===//
1135// Preprocessor Serialization
1136//===----------------------------------------------------------------------===//
1137
Chris Lattner0b1fb982009-04-10 17:15:23 +00001138/// \brief Writes the block containing the serialized form of the
1139/// preprocessor.
1140///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001141void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001142 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001143
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001144 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1145 if (PP.getCounterValue() != 0) {
1146 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001147 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001148 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001149 }
1150
1151 // Enter the preprocessor block.
1152 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001154 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1155 // FIXME: use diagnostics subsystem for localization etc.
1156 if (PP.SawDateOrTime())
1157 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001159 // Loop over all the macro definitions that are live at the end of the file,
1160 // emitting each to the PP section.
Douglas Gregor813a97b2009-10-17 17:25:45 +00001161 // FIXME: Make sure that this sees macros defined in included PCH files.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001162 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1163 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001164 // FIXME: This emits macros in hash table order, we should do it in a stable
1165 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001166 MacroInfo *MI = I->second;
1167
1168 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1169 // been redefined by the header (in which case they are not isBuiltinMacro).
1170 if (MI->isBuiltinMacro())
1171 continue;
1172
Douglas Gregor37e26842009-04-21 23:56:24 +00001173 // FIXME: Remove this identifier reference?
Chris Lattner7356a312009-04-11 21:15:38 +00001174 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001175 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001176 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1177 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001179 unsigned Code;
1180 if (MI->isObjectLike()) {
1181 Code = pch::PP_MACRO_OBJECT_LIKE;
1182 } else {
1183 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001185 Record.push_back(MI->isC99Varargs());
1186 Record.push_back(MI->isGNUVarargs());
1187 Record.push_back(MI->getNumArgs());
1188 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1189 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001190 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001191 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001192 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001193 Record.clear();
1194
Chris Lattnerdf961c22009-04-10 18:08:30 +00001195 // Emit the tokens array.
1196 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1197 // Note that we know that the preprocessor does not have any annotation
1198 // tokens in it because they are created by the parser, and thus can't be
1199 // in a macro definition.
1200 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Chris Lattnerdf961c22009-04-10 18:08:30 +00001202 Record.push_back(Tok.getLocation().getRawEncoding());
1203 Record.push_back(Tok.getLength());
1204
Chris Lattnerdf961c22009-04-10 18:08:30 +00001205 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1206 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001207 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Chris Lattnerdf961c22009-04-10 18:08:30 +00001209 // FIXME: Should translate token kind to a stable encoding.
1210 Record.push_back(Tok.getKind());
1211 // FIXME: Should translate token flags to a stable encoding.
1212 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Douglas Gregorc9490c02009-04-16 22:23:12 +00001214 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001215 Record.clear();
1216 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001217 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001218 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001219 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001220}
1221
Douglas Gregor2e222532009-07-02 17:08:52 +00001222void PCHWriter::WriteComments(ASTContext &Context) {
1223 using namespace llvm;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Douglas Gregor2e222532009-07-02 17:08:52 +00001225 if (Context.Comments.empty())
1226 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregor2e222532009-07-02 17:08:52 +00001228 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1229 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1230 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1231 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Douglas Gregor2e222532009-07-02 17:08:52 +00001233 RecordData Record;
1234 Record.push_back(pch::COMMENT_RANGES);
Mike Stump1eb44332009-09-09 15:08:12 +00001235 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregor2e222532009-07-02 17:08:52 +00001236 (const char*)&Context.Comments[0],
1237 Context.Comments.size() * sizeof(SourceRange));
1238}
1239
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001240//===----------------------------------------------------------------------===//
1241// Type Serialization
1242//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001243
Douglas Gregor2cf26342009-04-09 22:27:44 +00001244/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001245void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001246 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001247 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001248 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Douglas Gregor2cf26342009-04-09 22:27:44 +00001250 // Record the offset for this type.
1251 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001252 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001253 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1254 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001255 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001256 }
1257
1258 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Douglas Gregor2cf26342009-04-09 22:27:44 +00001260 // Emit the type's representation.
1261 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001262
Douglas Gregora4923eb2009-11-16 21:35:15 +00001263 if (T.hasLocalNonFastQualifiers()) {
1264 Qualifiers Qs = T.getLocalQualifiers();
1265 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001266 Record.push_back(Qs.getAsOpaqueValue());
1267 W.Code = pch::TYPE_EXT_QUAL;
1268 } else {
1269 switch (T->getTypeClass()) {
1270 // For all of the concrete, non-dependent types, call the
1271 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001272#define TYPE(Class, Base) \
John McCall0953e762009-09-24 19:53:00 +00001273 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001274#define ABSTRACT_TYPE(Class, Base)
1275#define DEPENDENT_TYPE(Class, Base)
1276#include "clang/AST/TypeNodes.def"
1277
John McCall0953e762009-09-24 19:53:00 +00001278 // For all of the dependent type nodes (which only occur in C++
1279 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001280#define TYPE(Class, Base)
1281#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1282#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001283 assert(false && "Cannot serialize dependent type nodes");
1284 break;
1285 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001286 }
1287
1288 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001289 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001290
1291 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001292 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001293}
1294
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001295//===----------------------------------------------------------------------===//
1296// Declaration Serialization
1297//===----------------------------------------------------------------------===//
1298
Douglas Gregor2cf26342009-04-09 22:27:44 +00001299/// \brief Write the block containing all of the declaration IDs
1300/// lexically declared within the given DeclContext.
1301///
1302/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1303/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001304uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001305 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001306 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001307 return 0;
1308
Douglas Gregorc9490c02009-04-16 22:23:12 +00001309 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001310 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001311 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1312 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001313 AddDeclRef(*D, Record);
1314
Douglas Gregor25123082009-04-22 22:34:57 +00001315 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001316 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001317 return Offset;
1318}
1319
1320/// \brief Write the block containing all of the declaration IDs
1321/// visible from the given DeclContext.
1322///
1323/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1324/// bistream, or 0 if no block was written.
1325uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1326 DeclContext *DC) {
1327 if (DC->getPrimaryContext() != DC)
1328 return 0;
1329
Douglas Gregoraff22df2009-04-21 22:32:33 +00001330 // Since there is no name lookup into functions or methods, and we
1331 // perform name lookup for the translation unit via the
1332 // IdentifierInfo chains, don't bother to build a
1333 // visible-declarations table for these entities.
1334 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001335 return 0;
1336
Douglas Gregor2cf26342009-04-09 22:27:44 +00001337 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001338 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001339
1340 // Serialize the contents of the mapping used for lookup. Note that,
1341 // although we have two very different code paths, the serialized
1342 // representation is the same for both cases: a declaration name,
1343 // followed by a size, followed by references to the visible
1344 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001345 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001346 RecordData Record;
1347 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001348 if (!Map)
1349 return 0;
1350
Douglas Gregor2cf26342009-04-09 22:27:44 +00001351 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1352 D != DEnd; ++D) {
1353 AddDeclarationName(D->first, Record);
1354 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1355 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001356 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001357 AddDeclRef(*Result.first, Record);
1358 }
1359
1360 if (Record.size() == 0)
1361 return 0;
1362
Douglas Gregorc9490c02009-04-16 22:23:12 +00001363 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001364 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001365 return Offset;
1366}
1367
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001368//===----------------------------------------------------------------------===//
1369// Global Method Pool and Selector Serialization
1370//===----------------------------------------------------------------------===//
1371
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001372namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001373// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001374class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001375 PCHWriter &Writer;
1376
1377public:
1378 typedef Selector key_type;
1379 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001381 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1382 typedef const data_type& data_type_ref;
1383
1384 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001386 static unsigned ComputeHash(Selector Sel) {
1387 unsigned N = Sel.getNumArgs();
1388 if (N == 0)
1389 ++N;
1390 unsigned R = 5381;
1391 for (unsigned I = 0; I != N; ++I)
1392 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001393 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001394 return R;
1395 }
Mike Stump1eb44332009-09-09 15:08:12 +00001396
1397 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001398 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1399 data_type_ref Methods) {
1400 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1401 clang::io::Emit16(Out, KeyLen);
1402 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001403 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001404 Method = Method->Next)
1405 if (Method->Method)
1406 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001407 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001408 Method = Method->Next)
1409 if (Method->Method)
1410 DataLen += 4;
1411 clang::io::Emit16(Out, DataLen);
1412 return std::make_pair(KeyLen, DataLen);
1413 }
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Douglas Gregor83941df2009-04-25 17:48:32 +00001415 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001416 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001417 assert((Start >> 32) == 0 && "Selector key offset too large");
1418 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001419 unsigned N = Sel.getNumArgs();
1420 clang::io::Emit16(Out, N);
1421 if (N == 0)
1422 N = 1;
1423 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001424 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001425 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1426 }
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001428 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001429 data_type_ref Methods, unsigned DataLen) {
1430 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001431 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001432 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001433 Method = Method->Next)
1434 if (Method->Method)
1435 ++NumInstanceMethods;
1436
1437 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001438 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001439 Method = Method->Next)
1440 if (Method->Method)
1441 ++NumFactoryMethods;
1442
1443 clang::io::Emit16(Out, NumInstanceMethods);
1444 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001445 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001446 Method = Method->Next)
1447 if (Method->Method)
1448 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001449 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001450 Method = Method->Next)
1451 if (Method->Method)
1452 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001453
1454 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001455 }
1456};
1457} // end anonymous namespace
1458
1459/// \brief Write the method pool into the PCH file.
1460///
1461/// The method pool contains both instance and factory methods, stored
1462/// in an on-disk hash table indexed by the selector.
1463void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1464 using namespace llvm;
1465
1466 // Create and write out the blob that contains the instance and
1467 // factor method pools.
1468 bool Empty = true;
1469 {
1470 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001472 // Create the on-disk hash table representation. Start by
1473 // iterating through the instance method pool.
1474 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001475 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001476 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001477 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001478 InstanceEnd = SemaRef.InstanceMethodPool.end();
1479 Instance != InstanceEnd; ++Instance) {
1480 // Check whether there is a factory method with the same
1481 // selector.
1482 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1483 = SemaRef.FactoryMethodPool.find(Instance->first);
1484
1485 if (Factory == SemaRef.FactoryMethodPool.end())
1486 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001487 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001488 ObjCMethodList()));
1489 else
1490 Generator.insert(Instance->first,
1491 std::make_pair(Instance->second, Factory->second));
1492
Douglas Gregor83941df2009-04-25 17:48:32 +00001493 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001494 Empty = false;
1495 }
1496
1497 // Now iterate through the factory method pool, to pick up any
1498 // selectors that weren't already in the instance method pool.
1499 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001500 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001501 FactoryEnd = SemaRef.FactoryMethodPool.end();
1502 Factory != FactoryEnd; ++Factory) {
1503 // Check whether there is an instance method with the same
1504 // selector. If so, there is no work to do here.
1505 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1506 = SemaRef.InstanceMethodPool.find(Factory->first);
1507
Douglas Gregor83941df2009-04-25 17:48:32 +00001508 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001509 Generator.insert(Factory->first,
1510 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001511 ++NumSelectorsInMethodPool;
1512 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001513
1514 Empty = false;
1515 }
1516
Douglas Gregor83941df2009-04-25 17:48:32 +00001517 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001518 return;
1519
1520 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001521 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001522 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001523 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001524 {
1525 PCHMethodPoolTrait Trait(*this);
1526 llvm::raw_svector_ostream Out(MethodPool);
1527 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001528 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001529 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001530
1531 // For every selector that we have seen but which was not
1532 // written into the hash table, write the selector itself and
1533 // record it's offset.
1534 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1535 if (SelectorOffsets[I] == 0)
1536 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001537 }
1538
1539 // Create a blob abbreviation
1540 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1541 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1545 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1546
Douglas Gregor83941df2009-04-25 17:48:32 +00001547 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001548 RecordData Record;
1549 Record.push_back(pch::METHOD_POOL);
1550 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001551 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001552 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001553
1554 // Create a blob abbreviation for the selector table offsets.
1555 Abbrev = new BitCodeAbbrev();
1556 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1559 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1560
1561 // Write the selector offsets table.
1562 Record.clear();
1563 Record.push_back(pch::SELECTOR_OFFSETS);
1564 Record.push_back(SelectorOffsets.size());
1565 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1566 (const char *)&SelectorOffsets.front(),
1567 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001568 }
1569}
1570
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001571//===----------------------------------------------------------------------===//
1572// Identifier Table Serialization
1573//===----------------------------------------------------------------------===//
1574
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001575namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001576class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001577 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001578 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001579
Douglas Gregora92193e2009-04-28 21:18:29 +00001580 /// \brief Determines whether this is an "interesting" identifier
1581 /// that needs a full IdentifierInfo structure written into the hash
1582 /// table.
1583 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1584 return II->isPoisoned() ||
1585 II->isExtensionToken() ||
1586 II->hasMacroDefinition() ||
1587 II->getObjCOrBuiltinID() ||
1588 II->getFETokenInfo<void>();
1589 }
1590
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001591public:
1592 typedef const IdentifierInfo* key_type;
1593 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001595 typedef pch::IdentID data_type;
1596 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001597
1598 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001599 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001600
1601 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001602 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001603 }
Mike Stump1eb44332009-09-09 15:08:12 +00001604
1605 std::pair<unsigned,unsigned>
1606 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001607 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001608 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001609 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1610 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001611 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001612 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001613 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001614 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001615 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1616 DEnd = IdentifierResolver::end();
1617 D != DEnd; ++D)
1618 DataLen += sizeof(pch::DeclID);
1619 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001620 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001621 // We emit the key length after the data length so that every
1622 // string is preceded by a 16-bit length. This matches the PTH
1623 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001624 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001625 return std::make_pair(KeyLen, DataLen);
1626 }
Mike Stump1eb44332009-09-09 15:08:12 +00001627
1628 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001629 unsigned KeyLen) {
1630 // Record the location of the key data. This is used when generating
1631 // the mapping from persistent IDs to strings.
1632 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001633 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001634 }
Mike Stump1eb44332009-09-09 15:08:12 +00001635
1636 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001637 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001638 if (!isInterestingIdentifier(II)) {
1639 clang::io::Emit32(Out, ID << 1);
1640 return;
1641 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001642
Douglas Gregora92193e2009-04-28 21:18:29 +00001643 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001644 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001645 bool hasMacroDefinition =
1646 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001647 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001648 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001649 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1650 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1651 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1652 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001653 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001654
Douglas Gregor37e26842009-04-21 23:56:24 +00001655 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001656 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001657
Douglas Gregor668c1a42009-04-21 22:25:48 +00001658 // Emit the declaration IDs in reverse order, because the
1659 // IdentifierResolver provides the declarations as they would be
1660 // visible (e.g., the function "stat" would come before the struct
1661 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1662 // adds declarations to the end of the list (so we need to see the
1663 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001664 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001665 IdentifierResolver::end());
1666 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1667 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001668 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001669 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001670 }
1671};
1672} // end anonymous namespace
1673
Douglas Gregorafaf3082009-04-11 00:14:32 +00001674/// \brief Write the identifier table into the PCH file.
1675///
1676/// The identifier table consists of a blob containing string data
1677/// (the actual identifiers themselves) and a separate "offsets" index
1678/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001679void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001680 using namespace llvm;
1681
1682 // Create and write out the blob that contains the identifier
1683 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001684 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001685 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Douglas Gregor92b059e2009-04-28 20:33:11 +00001687 // Look for any identifiers that were named while processing the
1688 // headers, but are otherwise not needed. We add these to the hash
1689 // table to enable checking of the predefines buffer in the case
1690 // where the user adds new macro definitions when building the PCH
1691 // file.
1692 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1693 IDEnd = PP.getIdentifierTable().end();
1694 ID != IDEnd; ++ID)
1695 getIdentifierRef(ID->second);
1696
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001697 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001698 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001699 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1700 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1701 ID != IDEnd; ++ID) {
1702 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001703 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001704 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001705
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001706 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001707 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001708 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001709 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001710 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001711 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001712 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001713 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001714 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001715 }
1716
1717 // Create a blob abbreviation
1718 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1719 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001720 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001721 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001722 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001723
1724 // Write the identifier table
1725 RecordData Record;
1726 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001727 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001728 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001729 }
1730
1731 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001732 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1733 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1736 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1737
1738 RecordData Record;
1739 Record.push_back(pch::IDENTIFIER_OFFSET);
1740 Record.push_back(IdentifierOffsets.size());
1741 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1742 (const char *)&IdentifierOffsets.front(),
1743 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001744}
1745
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001746//===----------------------------------------------------------------------===//
1747// General Serialization Routines
1748//===----------------------------------------------------------------------===//
1749
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001750/// \brief Write a record containing the given attributes.
1751void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1752 RecordData Record;
1753 for (; Attr; Attr = Attr->getNext()) {
1754 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1755 Record.push_back(Attr->isInherited());
1756 switch (Attr->getKind()) {
1757 case Attr::Alias:
1758 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1759 break;
1760
1761 case Attr::Aligned:
1762 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1763 break;
1764
1765 case Attr::AlwaysInline:
1766 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001768 case Attr::AnalyzerNoReturn:
1769 break;
1770
1771 case Attr::Annotate:
1772 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1773 break;
1774
1775 case Attr::AsmLabel:
1776 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1777 break;
1778
Sean Hunt7725e672009-11-25 04:20:27 +00001779 case Attr::BaseCheck:
1780 break;
1781
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001782 case Attr::Blocks:
1783 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1784 break;
1785
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001786 case Attr::CDecl:
1787 break;
1788
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001789 case Attr::Cleanup:
1790 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1791 break;
1792
1793 case Attr::Const:
1794 break;
1795
1796 case Attr::Constructor:
1797 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1798 break;
1799
1800 case Attr::DLLExport:
1801 case Attr::DLLImport:
1802 case Attr::Deprecated:
1803 break;
1804
1805 case Attr::Destructor:
1806 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1807 break;
1808
1809 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001810 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001811 break;
1812
1813 case Attr::Format: {
1814 const FormatAttr *Format = cast<FormatAttr>(Attr);
1815 AddString(Format->getType(), Record);
1816 Record.push_back(Format->getFormatIdx());
1817 Record.push_back(Format->getFirstArg());
1818 break;
1819 }
1820
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001821 case Attr::FormatArg: {
1822 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1823 Record.push_back(Format->getFormatIdx());
1824 break;
1825 }
1826
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001827 case Attr::Sentinel : {
1828 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1829 Record.push_back(Sentinel->getSentinel());
1830 Record.push_back(Sentinel->getNullPos());
1831 break;
1832 }
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Chris Lattnercf2a7212009-04-20 19:12:28 +00001834 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001835 case Attr::Hiding:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001836 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001837 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001838 case Attr::NoDebug:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001839 case Attr::NoReturn:
1840 case Attr::NoThrow:
Mike Stump1feade82009-08-26 22:31:08 +00001841 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001842 break;
1843
1844 case Attr::NonNull: {
1845 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1846 Record.push_back(NonNull->size());
1847 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1848 break;
1849 }
1850
1851 case Attr::ObjCException:
1852 case Attr::ObjCNSObject:
Ted Kremenekb71368d2009-05-09 02:44:38 +00001853 case Attr::CFReturnsRetained:
1854 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001855 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001856 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001857 break;
1858
Anders Carlssona860e752009-08-08 18:23:56 +00001859 case Attr::PragmaPack:
1860 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001861 break;
1862
Anders Carlssona860e752009-08-08 18:23:56 +00001863 case Attr::Packed:
1864 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001866 case Attr::Pure:
1867 break;
1868
1869 case Attr::Regparm:
1870 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1871 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Nate Begeman6f3d8382009-06-26 06:32:41 +00001873 case Attr::ReqdWorkGroupSize:
1874 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1875 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1876 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1877 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001878
1879 case Attr::Section:
1880 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1881 break;
1882
1883 case Attr::StdCall:
1884 case Attr::TransparentUnion:
1885 case Attr::Unavailable:
1886 case Attr::Unused:
1887 case Attr::Used:
1888 break;
1889
1890 case Attr::Visibility:
1891 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001892 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001893 break;
1894
1895 case Attr::WarnUnusedResult:
1896 case Attr::Weak:
1897 case Attr::WeakImport:
1898 break;
1899 }
1900 }
1901
Douglas Gregorc9490c02009-04-16 22:23:12 +00001902 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001903}
1904
1905void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1906 Record.push_back(Str.size());
1907 Record.insert(Record.end(), Str.begin(), Str.end());
1908}
1909
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001910/// \brief Note that the identifier II occurs at the given offset
1911/// within the identifier table.
1912void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001913 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001914}
1915
Douglas Gregor83941df2009-04-25 17:48:32 +00001916/// \brief Note that the selector Sel occurs at the given offset
1917/// within the method pool/selector table.
1918void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1919 unsigned ID = SelectorIDs[Sel];
1920 assert(ID && "Unknown selector");
1921 SelectorOffsets[ID - 1] = Offset;
1922}
1923
Mike Stump1eb44332009-09-09 15:08:12 +00001924PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1925 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00001926 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1927 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001928
Douglas Gregore650c8c2009-07-07 00:12:59 +00001929void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1930 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001931 using namespace llvm;
1932
Douglas Gregore7785042009-04-20 15:53:59 +00001933 ASTContext &Context = SemaRef.Context;
1934 Preprocessor &PP = SemaRef.PP;
1935
Douglas Gregor2cf26342009-04-09 22:27:44 +00001936 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001937 Stream.Emit((unsigned)'C', 8);
1938 Stream.Emit((unsigned)'P', 8);
1939 Stream.Emit((unsigned)'C', 8);
1940 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001942 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001943
1944 // The translation unit is the first declaration we'll emit.
1945 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001946 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001947
Douglas Gregor2deaea32009-04-22 18:49:13 +00001948 // Make sure that we emit IdentifierInfos (and any attached
1949 // declarations) for builtins.
1950 {
1951 IdentifierTable &Table = PP.getIdentifierTable();
1952 llvm::SmallVector<const char *, 32> BuiltinNames;
1953 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1954 Context.getLangOptions().NoBuiltin);
1955 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1956 getIdentifierRef(&Table.get(BuiltinNames[I]));
1957 }
1958
Chris Lattner63d65f82009-09-08 18:19:27 +00001959 // Build a record containing all of the tentative definitions in this file, in
1960 // TentativeDefinitionList order. Generally, this record will be empty for
1961 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001962 RecordData TentativeDefinitions;
Chris Lattner63d65f82009-09-08 18:19:27 +00001963 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1964 VarDecl *VD =
1965 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1966 if (VD) AddDeclRef(VD, TentativeDefinitions);
1967 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001968
Douglas Gregor14c22f22009-04-22 22:18:58 +00001969 // Build a record containing all of the locally-scoped external
1970 // declarations in this header file. Generally, this record will be
1971 // empty.
1972 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00001973 // FIXME: This is filling in the PCH file in densemap order which is
1974 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00001975 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00001976 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1977 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1978 TD != TDEnd; ++TD)
1979 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1980
Douglas Gregorb81c1702009-04-27 20:06:05 +00001981 // Build a record containing all of the ext_vector declarations.
1982 RecordData ExtVectorDecls;
1983 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1984 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1985
Douglas Gregor2cf26342009-04-09 22:27:44 +00001986 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001987 RecordData Record;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001988 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001989 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001990 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001991 if (StatCalls && !isysroot)
1992 WriteStatCache(*StatCalls, isysroot);
1993 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump1eb44332009-09-09 15:08:12 +00001994 WriteComments(Context);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001995 // Write the record of special types.
1996 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001998 AddTypeRef(Context.getBuiltinVaListType(), Record);
1999 AddTypeRef(Context.getObjCIdType(), Record);
2000 AddTypeRef(Context.getObjCSelType(), Record);
2001 AddTypeRef(Context.getObjCProtoType(), Record);
2002 AddTypeRef(Context.getObjCClassType(), Record);
2003 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2004 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2005 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002006 AddTypeRef(Context.getjmp_bufType(), Record);
2007 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002008 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2009 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002010#if 0
2011 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00002012 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002013#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00002014 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002015 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002016 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Douglas Gregor366809a2009-04-26 03:49:13 +00002018 // Keep writing types and declarations until all types and
2019 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002020 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2021 WriteDeclsBlockAbbrevs();
2022 while (!DeclTypesToEmit.empty()) {
2023 DeclOrType DOT = DeclTypesToEmit.front();
2024 DeclTypesToEmit.pop();
2025 if (DOT.isType())
2026 WriteType(DOT.getType());
2027 else
2028 WriteDecl(Context, DOT.getDecl());
2029 }
2030 Stream.ExitBlock();
2031
Douglas Gregor813a97b2009-10-17 17:25:45 +00002032 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002033 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002034 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002035
2036 // Write the type offsets array
2037 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2038 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2041 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2042 Record.clear();
2043 Record.push_back(pch::TYPE_OFFSET);
2044 Record.push_back(TypeOffsets.size());
2045 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002046 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002047 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002049 // Write the declaration offsets array
2050 Abbrev = new BitCodeAbbrev();
2051 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2052 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2054 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2055 Record.clear();
2056 Record.push_back(pch::DECL_OFFSET);
2057 Record.push_back(DeclOffsets.size());
2058 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002059 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002060 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002061
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002062 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002063 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002064 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002065
2066 // Write the record containing tentative definitions.
2067 if (!TentativeDefinitions.empty())
2068 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002069
2070 // Write the record containing locally-scoped external definitions.
2071 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002072 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002073 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002074
2075 // Write the record containing ext_vector type names.
2076 if (!ExtVectorDecls.empty())
2077 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Douglas Gregor3e1af842009-04-17 22:13:46 +00002079 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002080 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002081 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002082 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002083 Record.push_back(NumLexicalDeclContexts);
2084 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002085 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002086 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002087}
2088
2089void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2090 Record.push_back(Loc.getRawEncoding());
2091}
2092
2093void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2094 Record.push_back(Value.getBitWidth());
2095 unsigned N = Value.getNumWords();
2096 const uint64_t* Words = Value.getRawData();
2097 for (unsigned I = 0; I != N; ++I)
2098 Record.push_back(Words[I]);
2099}
2100
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002101void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2102 Record.push_back(Value.isUnsigned());
2103 AddAPInt(Value, Record);
2104}
2105
Douglas Gregor17fc2232009-04-14 21:55:33 +00002106void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2107 AddAPInt(Value.bitcastToAPInt(), Record);
2108}
2109
Douglas Gregor2cf26342009-04-09 22:27:44 +00002110void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002111 Record.push_back(getIdentifierRef(II));
2112}
2113
2114pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2115 if (II == 0)
2116 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002117
2118 pch::IdentID &ID = IdentifierIDs[II];
2119 if (ID == 0)
2120 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002121 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002122}
2123
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002124void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2125 if (SelRef.getAsOpaquePtr() == 0) {
2126 Record.push_back(0);
2127 return;
2128 }
2129
2130 pch::SelectorID &SID = SelectorIDs[SelRef];
2131 if (SID == 0) {
2132 SID = SelectorIDs.size();
2133 SelVector.push_back(SelRef);
2134 }
2135 Record.push_back(SID);
2136}
2137
John McCall833ca992009-10-29 08:12:44 +00002138void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2139 RecordData &Record) {
2140 switch (Arg.getArgument().getKind()) {
2141 case TemplateArgument::Expression:
2142 AddStmt(Arg.getLocInfo().getAsExpr());
2143 break;
2144 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002145 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002146 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002147 case TemplateArgument::Template:
2148 Record.push_back(
2149 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2150 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2151 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2152 break;
John McCall833ca992009-10-29 08:12:44 +00002153 case TemplateArgument::Null:
2154 case TemplateArgument::Integral:
2155 case TemplateArgument::Declaration:
2156 case TemplateArgument::Pack:
2157 break;
2158 }
2159}
2160
John McCalla93c9342009-12-07 02:54:59 +00002161void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2162 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002163 AddTypeRef(QualType(), Record);
2164 return;
2165 }
2166
John McCalla93c9342009-12-07 02:54:59 +00002167 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002168 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002169 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002170 TLW.Visit(TL);
2171}
2172
Douglas Gregor2cf26342009-04-09 22:27:44 +00002173void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2174 if (T.isNull()) {
2175 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2176 return;
2177 }
2178
Douglas Gregora4923eb2009-11-16 21:35:15 +00002179 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002180 T.removeFastQualifiers();
2181
Douglas Gregora4923eb2009-11-16 21:35:15 +00002182 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002183 pch::TypeID &ID = TypeIDs[T];
2184 if (ID == 0) {
2185 // We haven't seen these qualifiers applied to this type before.
2186 // Assign it a new ID. This is the only time we enqueue a
2187 // qualified type, and it has no CV qualifiers.
2188 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002189 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002190 }
2191
2192 // Encode the type qualifiers in the type reference.
2193 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2194 return;
2195 }
2196
Douglas Gregora4923eb2009-11-16 21:35:15 +00002197 assert(!T.hasLocalQualifiers());
John McCall0953e762009-09-24 19:53:00 +00002198
Douglas Gregor2cf26342009-04-09 22:27:44 +00002199 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002200 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002201 switch (BT->getKind()) {
2202 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2203 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2204 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2205 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2206 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2207 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2208 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2209 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002210 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002211 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2212 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2213 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2214 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2215 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2216 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2217 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002218 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002219 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2220 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2221 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002222 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002223 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2224 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002225 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2226 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002227 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2228 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002229 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002230 case BuiltinType::UndeducedAuto:
2231 assert(0 && "Should not see undeduced auto here");
2232 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002233 }
2234
John McCall0953e762009-09-24 19:53:00 +00002235 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002236 return;
2237 }
2238
John McCall0953e762009-09-24 19:53:00 +00002239 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002240 if (ID == 0) {
2241 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002242 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002243 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002244 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002245 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002246
2247 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002248 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002249}
2250
2251void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2252 if (D == 0) {
2253 Record.push_back(0);
2254 return;
2255 }
2256
Douglas Gregor8038d512009-04-10 17:25:41 +00002257 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002258 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002259 // We haven't seen this declaration before. Give it a new ID and
2260 // enqueue it in the list of declarations to emit.
2261 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002262 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002263 }
2264
2265 Record.push_back(ID);
2266}
2267
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002268pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2269 if (D == 0)
2270 return 0;
2271
2272 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2273 return DeclIDs[D];
2274}
2275
Douglas Gregor2cf26342009-04-09 22:27:44 +00002276void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002277 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002278 Record.push_back(Name.getNameKind());
2279 switch (Name.getNameKind()) {
2280 case DeclarationName::Identifier:
2281 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2282 break;
2283
2284 case DeclarationName::ObjCZeroArgSelector:
2285 case DeclarationName::ObjCOneArgSelector:
2286 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002287 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002288 break;
2289
2290 case DeclarationName::CXXConstructorName:
2291 case DeclarationName::CXXDestructorName:
2292 case DeclarationName::CXXConversionFunctionName:
2293 AddTypeRef(Name.getCXXNameType(), Record);
2294 break;
2295
2296 case DeclarationName::CXXOperatorName:
2297 Record.push_back(Name.getCXXOverloadedOperator());
2298 break;
2299
Sean Hunt3e518bd2009-11-29 07:34:05 +00002300 case DeclarationName::CXXLiteralOperatorName:
2301 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2302 break;
2303
Douglas Gregor2cf26342009-04-09 22:27:44 +00002304 case DeclarationName::CXXUsingDirective:
2305 // No extra data to emit
2306 break;
2307 }
2308}
Douglas Gregor0b748912009-04-14 21:18:50 +00002309