blob: 225772b30d3ddc3da637a983819d0d1f45499377 [file] [log] [blame]
Chris Lattnere127a0d2010-04-20 20:35:58 +00001//===--- PCHWriter.cpp - Precompiled Headers Writer -----------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
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"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000033#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000045
Douglas Gregor2cf26342009-04-09 22:27:44 +000046namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000047 class PCHTypeWriter {
Douglas Gregor2cf26342009-04-09 22:27:44 +000048 PCHWriter &Writer;
49 PCHWriter::RecordData &Record;
50
51 public:
52 /// \brief Type code that corresponds to the record generated.
53 pch::TypeCode Code;
54
Mike Stump1eb44332009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
58 void VisitArrayType(const ArrayType *T);
59 void VisitFunctionType(const FunctionType *T);
60 void VisitTagType(const TagType *T);
61
62#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63#define ABSTRACT_TYPE(Class, Base)
64#define DEPENDENT_TYPE(Class, Base)
65#include "clang/AST/TypeNodes.def"
John McCall31f17ec2010-04-27 00:57:59 +000066 void VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor2cf26342009-04-09 22:27:44 +000067 };
68}
69
Douglas Gregor2cf26342009-04-09 22:27:44 +000070void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
71 assert(false && "Built-in types are never serialized");
72}
73
Douglas Gregor2cf26342009-04-09 22:27:44 +000074void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
75 Writer.AddTypeRef(T->getElementType(), Record);
76 Code = pch::TYPE_COMPLEX;
77}
78
79void PCHTypeWriter::VisitPointerType(const PointerType *T) {
80 Writer.AddTypeRef(T->getPointeeType(), Record);
81 Code = pch::TYPE_POINTER;
82}
83
84void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000085 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000086 Code = pch::TYPE_BLOCK_POINTER;
87}
88
89void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
90 Writer.AddTypeRef(T->getPointeeType(), Record);
91 Code = pch::TYPE_LVALUE_REFERENCE;
92}
93
94void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
95 Writer.AddTypeRef(T->getPointeeType(), Record);
96 Code = pch::TYPE_RVALUE_REFERENCE;
97}
98
99void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000100 Writer.AddTypeRef(T->getPointeeType(), Record);
101 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Code = pch::TYPE_MEMBER_POINTER;
103}
104
105void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
106 Writer.AddTypeRef(T->getElementType(), Record);
107 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000108 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
111void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
112 VisitArrayType(T);
113 Writer.AddAPInt(T->getSize(), Record);
114 Code = pch::TYPE_CONSTANT_ARRAY;
115}
116
117void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
118 VisitArrayType(T);
119 Code = pch::TYPE_INCOMPLETE_ARRAY;
120}
121
122void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
123 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000124 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
125 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000126 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127 Code = pch::TYPE_VARIABLE_ARRAY;
128}
129
130void PCHTypeWriter::VisitVectorType(const VectorType *T) {
131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getNumElements());
John Thompson82287d12010-02-05 00:12:22 +0000133 Record.push_back(T->isAltiVec());
134 Record.push_back(T->isPixel());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135 Code = pch::TYPE_VECTOR;
136}
137
138void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
139 VisitVectorType(T);
140 Code = pch::TYPE_EXT_VECTOR;
141}
142
143void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
144 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000145 FunctionType::ExtInfo C = T->getExtInfo();
146 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000147 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000148 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000149 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000150}
151
152void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
153 VisitFunctionType(T);
154 Code = pch::TYPE_FUNCTION_NO_PROTO;
155}
156
157void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
158 VisitFunctionType(T);
159 Record.push_back(T->getNumArgs());
160 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
161 Writer.AddTypeRef(T->getArgType(I), Record);
162 Record.push_back(T->isVariadic());
163 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000164 Record.push_back(T->hasExceptionSpec());
165 Record.push_back(T->hasAnyExceptionSpec());
166 Record.push_back(T->getNumExceptions());
167 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
168 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169 Code = pch::TYPE_FUNCTION_PROTO;
170}
171
John McCalled976492009-12-04 22:46:56 +0000172#if 0
173// For when we want it....
174void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
175 Writer.AddDeclRef(T->getDecl(), Record);
176 Code = pch::TYPE_UNRESOLVED_USING;
177}
178#endif
179
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
181 Writer.AddDeclRef(T->getDecl(), Record);
182 Code = pch::TYPE_TYPEDEF;
183}
184
185void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000186 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000187 Code = pch::TYPE_TYPEOF_EXPR;
188}
189
190void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
191 Writer.AddTypeRef(T->getUnderlyingType(), Record);
192 Code = pch::TYPE_TYPEOF;
193}
194
Anders Carlsson395b4752009-06-24 19:06:50 +0000195void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
196 Writer.AddStmt(T->getUnderlyingExpr());
197 Code = pch::TYPE_DECLTYPE;
198}
199
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200void PCHTypeWriter::VisitTagType(const TagType *T) {
201 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000202 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000203 "Cannot serialize in the middle of a type definition");
204}
205
206void PCHTypeWriter::VisitRecordType(const RecordType *T) {
207 VisitTagType(T);
208 Code = pch::TYPE_RECORD;
209}
210
211void PCHTypeWriter::VisitEnumType(const EnumType *T) {
212 VisitTagType(T);
213 Code = pch::TYPE_ENUM;
214}
215
John McCall7da24312009-09-05 00:15:47 +0000216void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
217 Writer.AddTypeRef(T->getUnderlyingType(), Record);
218 Record.push_back(T->getTagKind());
219 Code = pch::TYPE_ELABORATED;
220}
221
Mike Stump1eb44332009-09-09 15:08:12 +0000222void
John McCall49a832b2009-10-18 09:09:24 +0000223PCHTypeWriter::VisitSubstTemplateTypeParmType(
224 const SubstTemplateTypeParmType *T) {
225 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
226 Writer.AddTypeRef(T->getReplacementType(), Record);
227 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
228}
229
230void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231PCHTypeWriter::VisitTemplateSpecializationType(
232 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000233 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000234 assert(false && "Cannot serialize template specialization types");
235}
236
237void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000238 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000239 assert(false && "Cannot serialize qualified name types");
240}
241
John McCall3cb0ebd2010-03-10 03:28:59 +0000242void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
243 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000244 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000245 Code = pch::TYPE_INJECTED_CLASS_NAME;
246}
247
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
249 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000250 Record.push_back(T->getNumProtocols());
Steve Naroff446ee4e2009-05-27 16:21:00 +0000251 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
252 E = T->qual_end(); I != E; ++I)
253 Writer.AddDeclRef(*I, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000254 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255}
256
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000257void
258PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000259 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000260 Record.push_back(T->getNumProtocols());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000261 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000262 E = T->qual_end(); I != E; ++I)
263 Writer.AddDeclRef(*I, Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000264 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000265}
266
John McCalla1ee0c52009-10-16 21:56:05 +0000267namespace {
268
269class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
270 PCHWriter &Writer;
271 PCHWriter::RecordData &Record;
272
273public:
274 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
275 : Writer(Writer), Record(Record) { }
276
John McCall51bd8032009-10-18 01:05:36 +0000277#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000278#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000279 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000280#include "clang/AST/TypeLocNodes.def"
281
John McCall51bd8032009-10-18 01:05:36 +0000282 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
283 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000284};
285
286}
287
John McCall51bd8032009-10-18 01:05:36 +0000288void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
289 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000290}
John McCall51bd8032009-10-18 01:05:36 +0000291void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000292 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
293 if (TL.needsExtraLocalData()) {
294 Record.push_back(TL.getWrittenTypeSpec());
295 Record.push_back(TL.getWrittenSignSpec());
296 Record.push_back(TL.getWrittenWidthSpec());
297 Record.push_back(TL.hasModeAttr());
298 }
John McCalla1ee0c52009-10-16 21:56:05 +0000299}
John McCall51bd8032009-10-18 01:05:36 +0000300void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
301 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000302}
John McCall51bd8032009-10-18 01:05:36 +0000303void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
304 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000305}
John McCall51bd8032009-10-18 01:05:36 +0000306void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
307 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000308}
John McCall51bd8032009-10-18 01:05:36 +0000309void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
310 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000311}
John McCall51bd8032009-10-18 01:05:36 +0000312void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
313 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000314}
John McCall51bd8032009-10-18 01:05:36 +0000315void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
316 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000317}
John McCall51bd8032009-10-18 01:05:36 +0000318void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
319 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
320 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
321 Record.push_back(TL.getSizeExpr() ? 1 : 0);
322 if (TL.getSizeExpr())
323 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000324}
John McCall51bd8032009-10-18 01:05:36 +0000325void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
326 VisitArrayTypeLoc(TL);
327}
328void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
329 VisitArrayTypeLoc(TL);
330}
331void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
332 VisitArrayTypeLoc(TL);
333}
334void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
335 DependentSizedArrayTypeLoc TL) {
336 VisitArrayTypeLoc(TL);
337}
338void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
339 DependentSizedExtVectorTypeLoc TL) {
340 Writer.AddSourceLocation(TL.getNameLoc(), Record);
341}
342void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
343 Writer.AddSourceLocation(TL.getNameLoc(), Record);
344}
345void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
346 Writer.AddSourceLocation(TL.getNameLoc(), Record);
347}
348void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
349 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
350 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
351 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
352 Writer.AddDeclRef(TL.getArg(i), Record);
353}
354void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
355 VisitFunctionTypeLoc(TL);
356}
357void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
358 VisitFunctionTypeLoc(TL);
359}
John McCalled976492009-12-04 22:46:56 +0000360void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getNameLoc(), Record);
362}
John McCall51bd8032009-10-18 01:05:36 +0000363void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getNameLoc(), Record);
365}
366void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000367 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
368 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
369 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000370}
371void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000372 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
373 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
374 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
375 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000376}
377void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
378 Writer.AddSourceLocation(TL.getNameLoc(), Record);
379}
380void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
381 Writer.AddSourceLocation(TL.getNameLoc(), Record);
382}
383void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
384 Writer.AddSourceLocation(TL.getNameLoc(), Record);
385}
386void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
387 Writer.AddSourceLocation(TL.getNameLoc(), Record);
388}
389void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
390 Writer.AddSourceLocation(TL.getNameLoc(), Record);
391}
John McCall49a832b2009-10-18 09:09:24 +0000392void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
393 SubstTemplateTypeParmTypeLoc TL) {
394 Writer.AddSourceLocation(TL.getNameLoc(), Record);
395}
John McCall51bd8032009-10-18 01:05:36 +0000396void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
397 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000398 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
399 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
400 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
401 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
402 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000403}
404void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
405 Writer.AddSourceLocation(TL.getNameLoc(), Record);
406}
John McCall3cb0ebd2010-03-10 03:28:59 +0000407void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getNameLoc(), Record);
409}
Douglas Gregor4714c122010-03-31 17:34:00 +0000410void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
John McCall51bd8032009-10-18 01:05:36 +0000411 Writer.AddSourceLocation(TL.getNameLoc(), Record);
412}
413void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
414 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000415 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
416 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
417 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
418 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000419}
John McCall54e14c42009-10-22 22:37:11 +0000420void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getStarLoc(), Record);
422 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
423 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
424 Record.push_back(TL.hasBaseTypeAsWritten());
425 Record.push_back(TL.hasProtocolsAsWritten());
426 if (TL.hasProtocolsAsWritten())
427 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
428 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
429}
John McCalla1ee0c52009-10-16 21:56:05 +0000430
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000431//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000432// PCHWriter Implementation
433//===----------------------------------------------------------------------===//
434
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000435static void EmitBlockID(unsigned ID, const char *Name,
436 llvm::BitstreamWriter &Stream,
437 PCHWriter::RecordData &Record) {
438 Record.clear();
439 Record.push_back(ID);
440 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
441
442 // Emit the block name if present.
443 if (Name == 0 || Name[0] == 0) return;
444 Record.clear();
445 while (*Name)
446 Record.push_back(*Name++);
447 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
448}
449
450static void EmitRecordID(unsigned ID, const char *Name,
451 llvm::BitstreamWriter &Stream,
452 PCHWriter::RecordData &Record) {
453 Record.clear();
454 Record.push_back(ID);
455 while (*Name)
456 Record.push_back(*Name++);
457 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000458}
459
460static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
461 PCHWriter::RecordData &Record) {
462#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
463 RECORD(STMT_STOP);
464 RECORD(STMT_NULL_PTR);
465 RECORD(STMT_NULL);
466 RECORD(STMT_COMPOUND);
467 RECORD(STMT_CASE);
468 RECORD(STMT_DEFAULT);
469 RECORD(STMT_LABEL);
470 RECORD(STMT_IF);
471 RECORD(STMT_SWITCH);
472 RECORD(STMT_WHILE);
473 RECORD(STMT_DO);
474 RECORD(STMT_FOR);
475 RECORD(STMT_GOTO);
476 RECORD(STMT_INDIRECT_GOTO);
477 RECORD(STMT_CONTINUE);
478 RECORD(STMT_BREAK);
479 RECORD(STMT_RETURN);
480 RECORD(STMT_DECL);
481 RECORD(STMT_ASM);
482 RECORD(EXPR_PREDEFINED);
483 RECORD(EXPR_DECL_REF);
484 RECORD(EXPR_INTEGER_LITERAL);
485 RECORD(EXPR_FLOATING_LITERAL);
486 RECORD(EXPR_IMAGINARY_LITERAL);
487 RECORD(EXPR_STRING_LITERAL);
488 RECORD(EXPR_CHARACTER_LITERAL);
489 RECORD(EXPR_PAREN);
490 RECORD(EXPR_UNARY_OPERATOR);
491 RECORD(EXPR_SIZEOF_ALIGN_OF);
492 RECORD(EXPR_ARRAY_SUBSCRIPT);
493 RECORD(EXPR_CALL);
494 RECORD(EXPR_MEMBER);
495 RECORD(EXPR_BINARY_OPERATOR);
496 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
497 RECORD(EXPR_CONDITIONAL_OPERATOR);
498 RECORD(EXPR_IMPLICIT_CAST);
499 RECORD(EXPR_CSTYLE_CAST);
500 RECORD(EXPR_COMPOUND_LITERAL);
501 RECORD(EXPR_EXT_VECTOR_ELEMENT);
502 RECORD(EXPR_INIT_LIST);
503 RECORD(EXPR_DESIGNATED_INIT);
504 RECORD(EXPR_IMPLICIT_VALUE_INIT);
505 RECORD(EXPR_VA_ARG);
506 RECORD(EXPR_ADDR_LABEL);
507 RECORD(EXPR_STMT);
508 RECORD(EXPR_TYPES_COMPATIBLE);
509 RECORD(EXPR_CHOOSE);
510 RECORD(EXPR_GNU_NULL);
511 RECORD(EXPR_SHUFFLE_VECTOR);
512 RECORD(EXPR_BLOCK);
513 RECORD(EXPR_BLOCK_DECL_REF);
514 RECORD(EXPR_OBJC_STRING_LITERAL);
515 RECORD(EXPR_OBJC_ENCODE);
516 RECORD(EXPR_OBJC_SELECTOR_EXPR);
517 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
518 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
519 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
520 RECORD(EXPR_OBJC_KVC_REF_EXPR);
521 RECORD(EXPR_OBJC_MESSAGE_EXPR);
522 RECORD(EXPR_OBJC_SUPER_EXPR);
523 RECORD(STMT_OBJC_FOR_COLLECTION);
524 RECORD(STMT_OBJC_CATCH);
525 RECORD(STMT_OBJC_FINALLY);
526 RECORD(STMT_OBJC_AT_TRY);
527 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
528 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000529 RECORD(EXPR_CXX_OPERATOR_CALL);
530 RECORD(EXPR_CXX_CONSTRUCT);
531 RECORD(EXPR_CXX_STATIC_CAST);
532 RECORD(EXPR_CXX_DYNAMIC_CAST);
533 RECORD(EXPR_CXX_REINTERPRET_CAST);
534 RECORD(EXPR_CXX_CONST_CAST);
535 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
536 RECORD(EXPR_CXX_BOOL_LITERAL);
537 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000538#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000539}
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000541void PCHWriter::WriteBlockInfoBlock() {
542 RecordData Record;
543 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattner2f4efd12009-04-27 00:40:25 +0000545#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000546#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000548 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000549 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000550 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000551 RECORD(TYPE_OFFSET);
552 RECORD(DECL_OFFSET);
553 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000554 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000555 RECORD(IDENTIFIER_OFFSET);
556 RECORD(IDENTIFIER_TABLE);
557 RECORD(EXTERNAL_DEFINITIONS);
558 RECORD(SPECIAL_TYPES);
559 RECORD(STATISTICS);
560 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000561 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000562 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
563 RECORD(SELECTOR_OFFSETS);
564 RECORD(METHOD_POOL);
565 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000566 RECORD(SOURCE_LOCATION_OFFSETS);
567 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000568 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000569 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000570 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000571 RECORD(UNUSED_STATIC_FUNCS);
572 RECORD(MACRO_DEFINITION_OFFSETS);
573
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000574 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000575 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000576 RECORD(SM_SLOC_FILE_ENTRY);
577 RECORD(SM_SLOC_BUFFER_ENTRY);
578 RECORD(SM_SLOC_BUFFER_BLOB);
579 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
580 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000582 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000583 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000584 RECORD(PP_MACRO_OBJECT_LIKE);
585 RECORD(PP_MACRO_FUNCTION_LIKE);
586 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000587 RECORD(PP_MACRO_INSTANTIATION);
588 RECORD(PP_MACRO_DEFINITION);
589
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000590 // Decls and Types block.
591 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000592 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000593 RECORD(TYPE_COMPLEX);
594 RECORD(TYPE_POINTER);
595 RECORD(TYPE_BLOCK_POINTER);
596 RECORD(TYPE_LVALUE_REFERENCE);
597 RECORD(TYPE_RVALUE_REFERENCE);
598 RECORD(TYPE_MEMBER_POINTER);
599 RECORD(TYPE_CONSTANT_ARRAY);
600 RECORD(TYPE_INCOMPLETE_ARRAY);
601 RECORD(TYPE_VARIABLE_ARRAY);
602 RECORD(TYPE_VECTOR);
603 RECORD(TYPE_EXT_VECTOR);
604 RECORD(TYPE_FUNCTION_PROTO);
605 RECORD(TYPE_FUNCTION_NO_PROTO);
606 RECORD(TYPE_TYPEDEF);
607 RECORD(TYPE_TYPEOF_EXPR);
608 RECORD(TYPE_TYPEOF);
609 RECORD(TYPE_RECORD);
610 RECORD(TYPE_ENUM);
611 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000612 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000613 RECORD(DECL_ATTR);
614 RECORD(DECL_TRANSLATION_UNIT);
615 RECORD(DECL_TYPEDEF);
616 RECORD(DECL_ENUM);
617 RECORD(DECL_RECORD);
618 RECORD(DECL_ENUM_CONSTANT);
619 RECORD(DECL_FUNCTION);
620 RECORD(DECL_OBJC_METHOD);
621 RECORD(DECL_OBJC_INTERFACE);
622 RECORD(DECL_OBJC_PROTOCOL);
623 RECORD(DECL_OBJC_IVAR);
624 RECORD(DECL_OBJC_AT_DEFS_FIELD);
625 RECORD(DECL_OBJC_CLASS);
626 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
627 RECORD(DECL_OBJC_CATEGORY);
628 RECORD(DECL_OBJC_CATEGORY_IMPL);
629 RECORD(DECL_OBJC_IMPLEMENTATION);
630 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
631 RECORD(DECL_OBJC_PROPERTY);
632 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 RECORD(DECL_FIELD);
634 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000635 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000636 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000637 RECORD(DECL_FILE_SCOPE_ASM);
638 RECORD(DECL_BLOCK);
639 RECORD(DECL_CONTEXT_LEXICAL);
640 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000641 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000642 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000643#undef RECORD
644#undef BLOCK
645 Stream.ExitBlock();
646}
647
Douglas Gregore650c8c2009-07-07 00:12:59 +0000648/// \brief Adjusts the given filename to only write out the portion of the
649/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000650///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000651/// \param Filename the file name to adjust.
652///
653/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
654/// the returned filename will be adjusted by this system root.
655///
656/// \returns either the original filename (if it needs no adjustment) or the
657/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000658static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000659adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
660 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Douglas Gregore650c8c2009-07-07 00:12:59 +0000662 if (!isysroot)
663 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregore650c8c2009-07-07 00:12:59 +0000665 // Verify that the filename and the system root have the same prefix.
666 unsigned Pos = 0;
667 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
668 if (Filename[Pos] != isysroot[Pos])
669 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Douglas Gregore650c8c2009-07-07 00:12:59 +0000671 // We hit the end of the filename before we hit the end of the system root.
672 if (!Filename[Pos])
673 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Douglas Gregore650c8c2009-07-07 00:12:59 +0000675 // If the file name has a '/' at the current position, skip over the '/'.
676 // We distinguish sysroot-based includes from absolute includes by the
677 // absence of '/' at the beginning of sysroot-based includes.
678 if (Filename[Pos] == '/')
679 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Douglas Gregore650c8c2009-07-07 00:12:59 +0000681 return Filename + Pos;
682}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000683
Douglas Gregorab41e632009-04-27 22:23:34 +0000684/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000685void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000686 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000687
Douglas Gregore650c8c2009-07-07 00:12:59 +0000688 // Metadata
689 const TargetInfo &Target = Context.Target;
690 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
691 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
692 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
693 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
694 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
695 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
696 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
697 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
698 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Douglas Gregore650c8c2009-07-07 00:12:59 +0000700 RecordData Record;
701 Record.push_back(pch::METADATA);
702 Record.push_back(pch::VERSION_MAJOR);
703 Record.push_back(pch::VERSION_MINOR);
704 Record.push_back(CLANG_VERSION_MAJOR);
705 Record.push_back(CLANG_VERSION_MINOR);
706 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000707 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000708 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Douglas Gregorb64c1932009-05-12 01:31:05 +0000710 // Original file name
711 SourceManager &SM = Context.getSourceManager();
712 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
713 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
714 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
715 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
716 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
717
718 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000720 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000721
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000722 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000723 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000724 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000725 RecordData Record;
726 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000727 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000728 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000729
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000730 // Repository branch/version information.
731 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
732 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
733 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
734 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000735 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000736 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000737 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
738 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000739}
740
741/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000742void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
743 RecordData Record;
744 Record.push_back(LangOpts.Trigraphs);
745 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
746 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
747 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
748 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000749 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000750 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
751 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
752 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
753 Record.push_back(LangOpts.C99); // C99 Support
754 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
755 Record.push_back(LangOpts.CPlusPlus); // C++ Support
756 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000757 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000759 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
760 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000761 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000762 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000763 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000764 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000765 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000767 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000768 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
769 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000770 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000771 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000772 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000773
774 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
775 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
776 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
777
Chris Lattnerea5ce472009-04-27 07:35:58 +0000778 // Whether static initializers are protected by locks.
779 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000780 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000781 Record.push_back(LangOpts.Blocks); // block extension to C
782 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
783 // they are unused.
784 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
785 // (modulo the platform support).
786
787 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
788 // signed integer arithmetic overflows.
789
790 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
791 // may be ripped out at any time.
792
793 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000794 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000795 // defined.
796 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
797 // opposed to __DYNAMIC__).
798 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
799
800 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
801 // used (instead of C99 semantics).
802 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000803 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
804 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000805 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
806 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000807 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000808 Record.push_back(LangOpts.getGCMode());
809 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000810 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000811 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000812 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000813 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000814 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000815 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000816}
817
Douglas Gregor14f79002009-04-10 03:52:48 +0000818//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000819// stat cache Serialization
820//===----------------------------------------------------------------------===//
821
822namespace {
823// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000824class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000825public:
826 typedef const char * key_type;
827 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000829 typedef std::pair<int, struct stat> data_type;
830 typedef const data_type& data_type_ref;
831
832 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000833 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000834 }
Mike Stump1eb44332009-09-09 15:08:12 +0000835
836 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000837 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
838 data_type_ref Data) {
839 unsigned StrLen = strlen(path);
840 clang::io::Emit16(Out, StrLen);
841 unsigned DataLen = 1; // result value
842 if (Data.first == 0)
843 DataLen += 4 + 4 + 2 + 8 + 8;
844 clang::io::Emit8(Out, DataLen);
845 return std::make_pair(StrLen + 1, DataLen);
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000848 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
849 Out.write(path, KeyLen);
850 }
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000852 void EmitData(llvm::raw_ostream& Out, key_type_ref,
853 data_type_ref Data, unsigned DataLen) {
854 using namespace clang::io;
855 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000857 // Result of stat()
858 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000860 if (Data.first == 0) {
861 Emit32(Out, (uint32_t) Data.second.st_ino);
862 Emit32(Out, (uint32_t) Data.second.st_dev);
863 Emit16(Out, (uint16_t) Data.second.st_mode);
864 Emit64(Out, (uint64_t) Data.second.st_mtime);
865 Emit64(Out, (uint64_t) Data.second.st_size);
866 }
867
868 assert(Out.tell() - Start == DataLen && "Wrong data length");
869 }
870};
871} // end anonymous namespace
872
873/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000874void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
875 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000876 // Build the on-disk hash table containing information about every
877 // stat() call.
878 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
879 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000880 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000881 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000882 Stat != StatEnd; ++Stat, ++NumStatEntries) {
883 const char *Filename = Stat->first();
884 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
885 Generator.insert(Filename, Stat->second);
886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000888 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000889 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000890 uint32_t BucketOffset;
891 {
892 llvm::raw_svector_ostream Out(StatCacheData);
893 // Make sure that no bucket is at offset 0
894 clang::io::Emit32(Out, 0);
895 BucketOffset = Generator.Emit(Out);
896 }
897
898 // Create a blob abbreviation
899 using namespace llvm;
900 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
901 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
905 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
906
907 // Write the stat cache
908 RecordData Record;
909 Record.push_back(pch::STAT_CACHE);
910 Record.push_back(BucketOffset);
911 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000912 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000913}
914
915//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000916// Source Manager Serialization
917//===----------------------------------------------------------------------===//
918
919/// \brief Create an abbreviation for the SLocEntry that refers to a
920/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000921static unsigned CreateSLocFileAbbrev(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_FILE_ENTRY));
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000929 // FileEntry fields.
930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000932 // HeaderFileInfo fields.
933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000938 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000939}
940
941/// \brief Create an abbreviation for the SLocEntry that refers to a
942/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000943static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000944 using namespace llvm;
945 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
946 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000952 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000953}
954
955/// \brief Create an abbreviation for the SLocEntry that refers to a
956/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000957static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000958 using namespace llvm;
959 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
960 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000962 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000963}
964
965/// \brief Create an abbreviation for the SLocEntry that refers to an
966/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000967static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000968 using namespace llvm;
969 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
970 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000976 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000977}
978
979/// \brief Writes the block containing the serialized form of the
980/// source manager.
981///
982/// TODO: We should probably use an on-disk hash table (stored in a
983/// blob), indexed based on the file name, so that we only create
984/// entries for files that we actually need. In the common case (no
985/// errors), we probably won't have to create file entries for any of
986/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000987void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000988 const Preprocessor &PP,
989 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000990 RecordData Record;
991
Chris Lattnerf04ad692009-04-10 17:16:57 +0000992 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000993 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000994
995 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000996 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
997 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
998 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
999 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001000
Douglas Gregorbd945002009-04-13 16:31:14 +00001001 // Write the line table.
1002 if (SourceMgr.hasLineTable()) {
1003 LineTableInfo &LineTable = SourceMgr.getLineTable();
1004
1005 // Emit the file names
1006 Record.push_back(LineTable.getNumFilenames());
1007 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1008 // Emit the file name
1009 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001010 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001011 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1012 Record.push_back(FilenameLen);
1013 if (FilenameLen)
1014 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1015 }
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Douglas Gregorbd945002009-04-13 16:31:14 +00001017 // Emit the line entries
1018 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1019 L != LEnd; ++L) {
1020 // Emit the file ID
1021 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Douglas Gregorbd945002009-04-13 16:31:14 +00001023 // Emit the line entries
1024 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001025 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001026 LEEnd = L->second.end();
1027 LE != LEEnd; ++LE) {
1028 Record.push_back(LE->FileOffset);
1029 Record.push_back(LE->LineNo);
1030 Record.push_back(LE->FilenameID);
1031 Record.push_back((unsigned)LE->FileKind);
1032 Record.push_back(LE->IncludeOffset);
1033 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001034 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001035 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001036 }
1037
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001038 // Write out the source location entry table. We skip the first
1039 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001040 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001041 RecordData PreloadSLocs;
1042 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001043 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1044 // Get this source location entry.
1045 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001046
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001047 // Record the offset of this source-location entry.
1048 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1049
1050 // Figure out which record code to use.
1051 unsigned Code;
1052 if (SLoc->isFile()) {
1053 if (SLoc->getFile().getContentCache()->Entry)
1054 Code = pch::SM_SLOC_FILE_ENTRY;
1055 else
1056 Code = pch::SM_SLOC_BUFFER_ENTRY;
1057 } else
1058 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1059 Record.clear();
1060 Record.push_back(Code);
1061
1062 Record.push_back(SLoc->getOffset());
1063 if (SLoc->isFile()) {
1064 const SrcMgr::FileInfo &File = SLoc->getFile();
1065 Record.push_back(File.getIncludeLoc().getRawEncoding());
1066 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1067 Record.push_back(File.hasLineDirectives());
1068
1069 const SrcMgr::ContentCache *Content = File.getContentCache();
1070 if (Content->Entry) {
1071 // The source location entry is a file. The blob associated
1072 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Douglas Gregor2d52be52010-03-21 22:49:54 +00001074 // Emit size/modification time for this file.
1075 Record.push_back(Content->Entry->getSize());
1076 Record.push_back(Content->Entry->getModificationTime());
1077
Douglas Gregor12fab312010-03-16 16:35:32 +00001078 // Emit header-search information associated with this file.
1079 HeaderFileInfo HFI;
1080 HeaderSearch &HS = PP.getHeaderSearchInfo();
1081 if (Content->Entry->getUID() < HS.header_file_size())
1082 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1083 Record.push_back(HFI.isImport);
1084 Record.push_back(HFI.DirInfo);
1085 Record.push_back(HFI.NumIncludes);
1086 AddIdentifierRef(HFI.ControllingMacro, Record);
1087
Douglas Gregore650c8c2009-07-07 00:12:59 +00001088 // Turn the file name into an absolute path, if it isn't already.
1089 const char *Filename = Content->Entry->getName();
1090 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001091 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001092 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregore650c8c2009-07-07 00:12:59 +00001094 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001095 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001096
1097 // FIXME: For now, preload all file source locations, so that
1098 // we get the appropriate File entries in the reader. This is
1099 // a temporary measure.
1100 PreloadSLocs.push_back(SLocEntryOffsets.size());
1101 } else {
1102 // The source location entry is a buffer. The blob associated
1103 // with this entry contains the contents of the buffer.
1104
1105 // We add one to the size so that we capture the trailing NULL
1106 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1107 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001108 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001109 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001110 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001111 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1112 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001113 Record.clear();
1114 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1115 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001116 llvm::StringRef(Buffer->getBufferStart(),
1117 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001118
1119 if (strcmp(Name, "<built-in>") == 0)
1120 PreloadSLocs.push_back(SLocEntryOffsets.size());
1121 }
1122 } else {
1123 // The source location entry is an instantiation.
1124 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1125 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1126 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1127 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1128
1129 // Compute the token length for this macro expansion.
1130 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001131 if (I + 1 != N)
1132 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001133 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1134 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1135 }
1136 }
1137
Douglas Gregorc9490c02009-04-16 22:23:12 +00001138 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139
1140 if (SLocEntryOffsets.empty())
1141 return;
1142
1143 // Write the source-location offsets table into the PCH block. This
1144 // table is used for lazily loading source-location information.
1145 using namespace llvm;
1146 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1147 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1151 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001153 Record.clear();
1154 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1155 Record.push_back(SLocEntryOffsets.size());
1156 Record.push_back(SourceMgr.getNextOffset());
1157 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001158 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001159 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001160
1161 // Write the source location entry preloads array, telling the PCH
1162 // reader which source locations entries it should load eagerly.
1163 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001164}
1165
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001166//===----------------------------------------------------------------------===//
1167// Preprocessor Serialization
1168//===----------------------------------------------------------------------===//
1169
Chris Lattner0b1fb982009-04-10 17:15:23 +00001170/// \brief Writes the block containing the serialized form of the
1171/// preprocessor.
1172///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001173void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001174 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001175
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001176 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1177 if (PP.getCounterValue() != 0) {
1178 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001179 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001180 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001181 }
1182
1183 // Enter the preprocessor block.
1184 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001186 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1187 // FIXME: use diagnostics subsystem for localization etc.
1188 if (PP.SawDateOrTime())
1189 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001191 // Loop over all the macro definitions that are live at the end of the file,
1192 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001193 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001194 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1195 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001196 // FIXME: This emits macros in hash table order, we should do it in a stable
1197 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001198 MacroInfo *MI = I->second;
1199
1200 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1201 // been redefined by the header (in which case they are not isBuiltinMacro).
1202 if (MI->isBuiltinMacro())
1203 continue;
1204
Chris Lattner7356a312009-04-11 21:15:38 +00001205 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001206 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001207 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1208 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001210 unsigned Code;
1211 if (MI->isObjectLike()) {
1212 Code = pch::PP_MACRO_OBJECT_LIKE;
1213 } else {
1214 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001216 Record.push_back(MI->isC99Varargs());
1217 Record.push_back(MI->isGNUVarargs());
1218 Record.push_back(MI->getNumArgs());
1219 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1220 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001221 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001222 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001223
1224 // If we have a detailed preprocessing record, record the macro definition
1225 // ID that corresponds to this macro.
1226 if (PPRec)
1227 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1228
Douglas Gregorc9490c02009-04-16 22:23:12 +00001229 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001230 Record.clear();
1231
Chris Lattnerdf961c22009-04-10 18:08:30 +00001232 // Emit the tokens array.
1233 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1234 // Note that we know that the preprocessor does not have any annotation
1235 // tokens in it because they are created by the parser, and thus can't be
1236 // in a macro definition.
1237 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Chris Lattnerdf961c22009-04-10 18:08:30 +00001239 Record.push_back(Tok.getLocation().getRawEncoding());
1240 Record.push_back(Tok.getLength());
1241
Chris Lattnerdf961c22009-04-10 18:08:30 +00001242 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1243 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001244 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattnerdf961c22009-04-10 18:08:30 +00001246 // FIXME: Should translate token kind to a stable encoding.
1247 Record.push_back(Tok.getKind());
1248 // FIXME: Should translate token flags to a stable encoding.
1249 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Douglas Gregorc9490c02009-04-16 22:23:12 +00001251 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001252 Record.clear();
1253 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001254 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001255 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001256
1257 // If the preprocessor has a preprocessing record, emit it.
1258 unsigned NumPreprocessingRecords = 0;
1259 if (PPRec) {
1260 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1261 E != EEnd; ++E) {
1262 Record.clear();
1263
1264 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1265 Record.push_back(NumPreprocessingRecords++);
1266 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1267 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1268 AddIdentifierRef(MI->getName(), Record);
1269 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1270 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1271 continue;
1272 }
1273
1274 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1275 // Record this macro definition's location.
1276 pch::IdentID ID = getMacroDefinitionID(MD);
1277 if (ID != MacroDefinitionOffsets.size()) {
1278 if (ID > MacroDefinitionOffsets.size())
1279 MacroDefinitionOffsets.resize(ID + 1);
1280
1281 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1282 } else
1283 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1284
1285 Record.push_back(NumPreprocessingRecords++);
1286 Record.push_back(ID);
1287 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1288 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1289 AddIdentifierRef(MD->getName(), Record);
1290 AddSourceLocation(MD->getLocation(), Record);
1291 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1292 continue;
1293 }
1294 }
1295 }
1296
Douglas Gregorc9490c02009-04-16 22:23:12 +00001297 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001298
1299 // Write the offsets table for the preprocessing record.
1300 if (NumPreprocessingRecords > 0) {
1301 // Write the offsets table for identifier IDs.
1302 using namespace llvm;
1303 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1304 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1308 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1309
1310 Record.clear();
1311 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1312 Record.push_back(NumPreprocessingRecords);
1313 Record.push_back(MacroDefinitionOffsets.size());
1314 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1315 (const char *)&MacroDefinitionOffsets.front(),
1316 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1317 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001318}
1319
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001320//===----------------------------------------------------------------------===//
1321// Type Serialization
1322//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001323
Douglas Gregor2cf26342009-04-09 22:27:44 +00001324/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001325void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001326 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001327 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001328 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Douglas Gregor2cf26342009-04-09 22:27:44 +00001330 // Record the offset for this type.
1331 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001332 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001333 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1334 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001335 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001336 }
1337
1338 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Douglas Gregor2cf26342009-04-09 22:27:44 +00001340 // Emit the type's representation.
1341 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001342
Douglas Gregora4923eb2009-11-16 21:35:15 +00001343 if (T.hasLocalNonFastQualifiers()) {
1344 Qualifiers Qs = T.getLocalQualifiers();
1345 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001346 Record.push_back(Qs.getAsOpaqueValue());
1347 W.Code = pch::TYPE_EXT_QUAL;
1348 } else {
1349 switch (T->getTypeClass()) {
1350 // For all of the concrete, non-dependent types, call the
1351 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001352#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001353 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001354#define ABSTRACT_TYPE(Class, Base)
1355#define DEPENDENT_TYPE(Class, Base)
1356#include "clang/AST/TypeNodes.def"
1357
John McCall0953e762009-09-24 19:53:00 +00001358 // For all of the dependent type nodes (which only occur in C++
1359 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001360#define TYPE(Class, Base)
1361#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1362#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001363 assert(false && "Cannot serialize dependent type nodes");
1364 break;
1365 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001366 }
1367
1368 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001369 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001370
1371 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001372 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001373}
1374
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001375//===----------------------------------------------------------------------===//
1376// Declaration Serialization
1377//===----------------------------------------------------------------------===//
1378
Douglas Gregor2cf26342009-04-09 22:27:44 +00001379/// \brief Write the block containing all of the declaration IDs
1380/// lexically declared within the given DeclContext.
1381///
1382/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1383/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001384uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001385 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001386 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001387 return 0;
1388
Douglas Gregorc9490c02009-04-16 22:23:12 +00001389 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001390 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001391 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1392 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001393 AddDeclRef(*D, Record);
1394
Douglas Gregor25123082009-04-22 22:34:57 +00001395 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001396 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001397 return Offset;
1398}
1399
1400/// \brief Write the block containing all of the declaration IDs
1401/// visible from the given DeclContext.
1402///
1403/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1404/// bistream, or 0 if no block was written.
1405uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1406 DeclContext *DC) {
1407 if (DC->getPrimaryContext() != DC)
1408 return 0;
1409
Douglas Gregoraff22df2009-04-21 22:32:33 +00001410 // Since there is no name lookup into functions or methods, and we
1411 // perform name lookup for the translation unit via the
1412 // IdentifierInfo chains, don't bother to build a
1413 // visible-declarations table for these entities.
1414 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001415 return 0;
1416
Douglas Gregor2cf26342009-04-09 22:27:44 +00001417 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001418 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001419
1420 // Serialize the contents of the mapping used for lookup. Note that,
1421 // although we have two very different code paths, the serialized
1422 // representation is the same for both cases: a declaration name,
1423 // followed by a size, followed by references to the visible
1424 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001425 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001426 RecordData Record;
1427 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001428 if (!Map)
1429 return 0;
1430
Douglas Gregor2cf26342009-04-09 22:27:44 +00001431 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1432 D != DEnd; ++D) {
1433 AddDeclarationName(D->first, Record);
1434 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1435 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001436 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001437 AddDeclRef(*Result.first, Record);
1438 }
1439
1440 if (Record.size() == 0)
1441 return 0;
1442
Douglas Gregorc9490c02009-04-16 22:23:12 +00001443 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001444 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001445 return Offset;
1446}
1447
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001448//===----------------------------------------------------------------------===//
1449// Global Method Pool and Selector Serialization
1450//===----------------------------------------------------------------------===//
1451
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001452namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001453// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001454class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001455 PCHWriter &Writer;
1456
1457public:
1458 typedef Selector key_type;
1459 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001461 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1462 typedef const data_type& data_type_ref;
1463
1464 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001466 static unsigned ComputeHash(Selector Sel) {
1467 unsigned N = Sel.getNumArgs();
1468 if (N == 0)
1469 ++N;
1470 unsigned R = 5381;
1471 for (unsigned I = 0; I != N; ++I)
1472 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001473 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001474 return R;
1475 }
Mike Stump1eb44332009-09-09 15:08:12 +00001476
1477 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001478 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1479 data_type_ref Methods) {
1480 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1481 clang::io::Emit16(Out, KeyLen);
1482 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001483 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001484 Method = Method->Next)
1485 if (Method->Method)
1486 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001487 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001488 Method = Method->Next)
1489 if (Method->Method)
1490 DataLen += 4;
1491 clang::io::Emit16(Out, DataLen);
1492 return std::make_pair(KeyLen, DataLen);
1493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Douglas Gregor83941df2009-04-25 17:48:32 +00001495 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001496 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001497 assert((Start >> 32) == 0 && "Selector key offset too large");
1498 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001499 unsigned N = Sel.getNumArgs();
1500 clang::io::Emit16(Out, N);
1501 if (N == 0)
1502 N = 1;
1503 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001504 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001505 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1506 }
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001508 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001509 data_type_ref Methods, unsigned DataLen) {
1510 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001511 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001512 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001513 Method = Method->Next)
1514 if (Method->Method)
1515 ++NumInstanceMethods;
1516
1517 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001518 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001519 Method = Method->Next)
1520 if (Method->Method)
1521 ++NumFactoryMethods;
1522
1523 clang::io::Emit16(Out, NumInstanceMethods);
1524 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001525 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001526 Method = Method->Next)
1527 if (Method->Method)
1528 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001529 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001530 Method = Method->Next)
1531 if (Method->Method)
1532 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001533
1534 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001535 }
1536};
1537} // end anonymous namespace
1538
1539/// \brief Write the method pool into the PCH file.
1540///
1541/// The method pool contains both instance and factory methods, stored
1542/// in an on-disk hash table indexed by the selector.
1543void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1544 using namespace llvm;
1545
1546 // Create and write out the blob that contains the instance and
1547 // factor method pools.
1548 bool Empty = true;
1549 {
1550 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001552 // Create the on-disk hash table representation. Start by
1553 // iterating through the instance method pool.
1554 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001555 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001556 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001557 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001558 InstanceEnd = SemaRef.InstanceMethodPool.end();
1559 Instance != InstanceEnd; ++Instance) {
1560 // Check whether there is a factory method with the same
1561 // selector.
1562 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1563 = SemaRef.FactoryMethodPool.find(Instance->first);
1564
1565 if (Factory == SemaRef.FactoryMethodPool.end())
1566 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001567 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001568 ObjCMethodList()));
1569 else
1570 Generator.insert(Instance->first,
1571 std::make_pair(Instance->second, Factory->second));
1572
Douglas Gregor83941df2009-04-25 17:48:32 +00001573 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001574 Empty = false;
1575 }
1576
1577 // Now iterate through the factory method pool, to pick up any
1578 // selectors that weren't already in the instance method pool.
1579 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001580 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001581 FactoryEnd = SemaRef.FactoryMethodPool.end();
1582 Factory != FactoryEnd; ++Factory) {
1583 // Check whether there is an instance method with the same
1584 // selector. If so, there is no work to do here.
1585 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1586 = SemaRef.InstanceMethodPool.find(Factory->first);
1587
Douglas Gregor83941df2009-04-25 17:48:32 +00001588 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001589 Generator.insert(Factory->first,
1590 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001591 ++NumSelectorsInMethodPool;
1592 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001593
1594 Empty = false;
1595 }
1596
Douglas Gregor83941df2009-04-25 17:48:32 +00001597 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001598 return;
1599
1600 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001601 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001602 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001603 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001604 {
1605 PCHMethodPoolTrait Trait(*this);
1606 llvm::raw_svector_ostream Out(MethodPool);
1607 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001608 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001609 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001610
1611 // For every selector that we have seen but which was not
1612 // written into the hash table, write the selector itself and
1613 // record it's offset.
1614 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1615 if (SelectorOffsets[I] == 0)
1616 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001617 }
1618
1619 // Create a blob abbreviation
1620 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1621 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001623 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1625 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1626
Douglas Gregor83941df2009-04-25 17:48:32 +00001627 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001628 RecordData Record;
1629 Record.push_back(pch::METHOD_POOL);
1630 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001631 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001632 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001633
1634 // Create a blob abbreviation for the selector table offsets.
1635 Abbrev = new BitCodeAbbrev();
1636 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1637 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1639 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1640
1641 // Write the selector offsets table.
1642 Record.clear();
1643 Record.push_back(pch::SELECTOR_OFFSETS);
1644 Record.push_back(SelectorOffsets.size());
1645 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1646 (const char *)&SelectorOffsets.front(),
1647 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001648 }
1649}
1650
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001651//===----------------------------------------------------------------------===//
1652// Identifier Table Serialization
1653//===----------------------------------------------------------------------===//
1654
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001655namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001656class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001657 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001658 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001659
Douglas Gregora92193e2009-04-28 21:18:29 +00001660 /// \brief Determines whether this is an "interesting" identifier
1661 /// that needs a full IdentifierInfo structure written into the hash
1662 /// table.
1663 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1664 return II->isPoisoned() ||
1665 II->isExtensionToken() ||
1666 II->hasMacroDefinition() ||
1667 II->getObjCOrBuiltinID() ||
1668 II->getFETokenInfo<void>();
1669 }
1670
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001671public:
1672 typedef const IdentifierInfo* key_type;
1673 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001675 typedef pch::IdentID data_type;
1676 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001677
1678 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001679 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001680
1681 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001682 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001683 }
Mike Stump1eb44332009-09-09 15:08:12 +00001684
1685 std::pair<unsigned,unsigned>
1686 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001687 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001688 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001689 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1690 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001691 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001692 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001693 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001694 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001695 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1696 DEnd = IdentifierResolver::end();
1697 D != DEnd; ++D)
1698 DataLen += sizeof(pch::DeclID);
1699 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001700 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001701 // We emit the key length after the data length so that every
1702 // string is preceded by a 16-bit length. This matches the PTH
1703 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001704 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001705 return std::make_pair(KeyLen, DataLen);
1706 }
Mike Stump1eb44332009-09-09 15:08:12 +00001707
1708 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001709 unsigned KeyLen) {
1710 // Record the location of the key data. This is used when generating
1711 // the mapping from persistent IDs to strings.
1712 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001713 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001714 }
Mike Stump1eb44332009-09-09 15:08:12 +00001715
1716 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001717 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001718 if (!isInterestingIdentifier(II)) {
1719 clang::io::Emit32(Out, ID << 1);
1720 return;
1721 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001722
Douglas Gregora92193e2009-04-28 21:18:29 +00001723 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001724 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001725 bool hasMacroDefinition =
1726 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001727 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001728 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001729 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1730 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1731 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1732 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001733 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001734
Douglas Gregor37e26842009-04-21 23:56:24 +00001735 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001736 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001737
Douglas Gregor668c1a42009-04-21 22:25:48 +00001738 // Emit the declaration IDs in reverse order, because the
1739 // IdentifierResolver provides the declarations as they would be
1740 // visible (e.g., the function "stat" would come before the struct
1741 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1742 // adds declarations to the end of the list (so we need to see the
1743 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001744 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001745 IdentifierResolver::end());
1746 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1747 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001748 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001749 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001750 }
1751};
1752} // end anonymous namespace
1753
Douglas Gregorafaf3082009-04-11 00:14:32 +00001754/// \brief Write the identifier table into the PCH file.
1755///
1756/// The identifier table consists of a blob containing string data
1757/// (the actual identifiers themselves) and a separate "offsets" index
1758/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001759void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001760 using namespace llvm;
1761
1762 // Create and write out the blob that contains the identifier
1763 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001764 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001765 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Douglas Gregor92b059e2009-04-28 20:33:11 +00001767 // Look for any identifiers that were named while processing the
1768 // headers, but are otherwise not needed. We add these to the hash
1769 // table to enable checking of the predefines buffer in the case
1770 // where the user adds new macro definitions when building the PCH
1771 // file.
1772 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1773 IDEnd = PP.getIdentifierTable().end();
1774 ID != IDEnd; ++ID)
1775 getIdentifierRef(ID->second);
1776
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001777 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001778 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001779 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1780 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1781 ID != IDEnd; ++ID) {
1782 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001783 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001784 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001785
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001786 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001787 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001788 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001789 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001790 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001791 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001792 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001793 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001794 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001795 }
1796
1797 // Create a blob abbreviation
1798 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1799 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001800 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001801 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001802 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001803
1804 // Write the identifier table
1805 RecordData Record;
1806 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001807 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001808 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001809 }
1810
1811 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001812 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1813 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1816 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1817
1818 RecordData Record;
1819 Record.push_back(pch::IDENTIFIER_OFFSET);
1820 Record.push_back(IdentifierOffsets.size());
1821 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1822 (const char *)&IdentifierOffsets.front(),
1823 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001824}
1825
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001826//===----------------------------------------------------------------------===//
1827// General Serialization Routines
1828//===----------------------------------------------------------------------===//
1829
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001830/// \brief Write a record containing the given attributes.
1831void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1832 RecordData Record;
1833 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001834 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001835 Record.push_back(Attr->isInherited());
1836 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001837 default:
1838 assert(0 && "Does not support PCH writing for this attribute yet!");
1839 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001840 case Attr::Alias:
1841 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1842 break;
1843
1844 case Attr::Aligned:
1845 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1846 break;
1847
1848 case Attr::AlwaysInline:
1849 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001851 case Attr::AnalyzerNoReturn:
1852 break;
1853
1854 case Attr::Annotate:
1855 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1856 break;
1857
1858 case Attr::AsmLabel:
1859 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1860 break;
1861
Sean Hunt7725e672009-11-25 04:20:27 +00001862 case Attr::BaseCheck:
1863 break;
1864
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001865 case Attr::Blocks:
1866 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1867 break;
1868
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001869 case Attr::CDecl:
1870 break;
1871
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001872 case Attr::Cleanup:
1873 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1874 break;
1875
1876 case Attr::Const:
1877 break;
1878
1879 case Attr::Constructor:
1880 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1881 break;
1882
1883 case Attr::DLLExport:
1884 case Attr::DLLImport:
1885 case Attr::Deprecated:
1886 break;
1887
1888 case Attr::Destructor:
1889 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1890 break;
1891
1892 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001893 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001894 break;
1895
1896 case Attr::Format: {
1897 const FormatAttr *Format = cast<FormatAttr>(Attr);
1898 AddString(Format->getType(), Record);
1899 Record.push_back(Format->getFormatIdx());
1900 Record.push_back(Format->getFirstArg());
1901 break;
1902 }
1903
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001904 case Attr::FormatArg: {
1905 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1906 Record.push_back(Format->getFormatIdx());
1907 break;
1908 }
1909
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001910 case Attr::Sentinel : {
1911 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1912 Record.push_back(Sentinel->getSentinel());
1913 Record.push_back(Sentinel->getNullPos());
1914 break;
1915 }
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Chris Lattnercf2a7212009-04-20 19:12:28 +00001917 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001918 case Attr::Hiding:
Ted Kremenekefbddd22010-02-17 02:37:45 +00001919 case Attr::IBActionKind:
Ted Kremenek47e69902010-02-18 00:05:52 +00001920 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001921 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001922 case Attr::NoDebug:
Ted Kremenek47e69902010-02-18 00:05:52 +00001923 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001924 case Attr::NoReturn:
1925 case Attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001926 break;
1927
1928 case Attr::NonNull: {
1929 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1930 Record.push_back(NonNull->size());
1931 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1932 break;
1933 }
1934
Ted Kremenek31c780d2010-02-18 00:05:45 +00001935 case Attr::CFReturnsNotRetained:
1936 case Attr::CFReturnsRetained:
1937 case Attr::NSReturnsNotRetained:
1938 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001939 case Attr::ObjCException:
1940 case Attr::ObjCNSObject:
1941 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001942 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001943 break;
1944
Anders Carlssona860e752009-08-08 18:23:56 +00001945 case Attr::PragmaPack:
1946 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001947 break;
1948
Anders Carlssona860e752009-08-08 18:23:56 +00001949 case Attr::Packed:
1950 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001952 case Attr::Pure:
1953 break;
1954
1955 case Attr::Regparm:
1956 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1957 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Nate Begeman6f3d8382009-06-26 06:32:41 +00001959 case Attr::ReqdWorkGroupSize:
1960 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1961 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1962 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1963 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001964
1965 case Attr::Section:
1966 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1967 break;
1968
1969 case Attr::StdCall:
1970 case Attr::TransparentUnion:
1971 case Attr::Unavailable:
1972 case Attr::Unused:
1973 case Attr::Used:
1974 break;
1975
1976 case Attr::Visibility:
1977 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001978 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001979 break;
1980
1981 case Attr::WarnUnusedResult:
1982 case Attr::Weak:
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001983 case Attr::WeakRef:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001984 case Attr::WeakImport:
1985 break;
1986 }
1987 }
1988
Douglas Gregorc9490c02009-04-16 22:23:12 +00001989 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001990}
1991
1992void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1993 Record.push_back(Str.size());
1994 Record.insert(Record.end(), Str.begin(), Str.end());
1995}
1996
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001997/// \brief Note that the identifier II occurs at the given offset
1998/// within the identifier table.
1999void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002000 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002001}
2002
Douglas Gregor83941df2009-04-25 17:48:32 +00002003/// \brief Note that the selector Sel occurs at the given offset
2004/// within the method pool/selector table.
2005void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2006 unsigned ID = SelectorIDs[Sel];
2007 assert(ID && "Unknown selector");
2008 SelectorOffsets[ID - 1] = Offset;
2009}
2010
Mike Stump1eb44332009-09-09 15:08:12 +00002011PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2012 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00002013 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2014 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002015
Douglas Gregore650c8c2009-07-07 00:12:59 +00002016void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2017 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002018 using namespace llvm;
2019
Douglas Gregore7785042009-04-20 15:53:59 +00002020 ASTContext &Context = SemaRef.Context;
2021 Preprocessor &PP = SemaRef.PP;
2022
Douglas Gregor2cf26342009-04-09 22:27:44 +00002023 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002024 Stream.Emit((unsigned)'C', 8);
2025 Stream.Emit((unsigned)'P', 8);
2026 Stream.Emit((unsigned)'C', 8);
2027 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002029 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002030
2031 // The translation unit is the first declaration we'll emit.
2032 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002033 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002034
Douglas Gregor2deaea32009-04-22 18:49:13 +00002035 // Make sure that we emit IdentifierInfos (and any attached
2036 // declarations) for builtins.
2037 {
2038 IdentifierTable &Table = PP.getIdentifierTable();
2039 llvm::SmallVector<const char *, 32> BuiltinNames;
2040 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2041 Context.getLangOptions().NoBuiltin);
2042 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2043 getIdentifierRef(&Table.get(BuiltinNames[I]));
2044 }
2045
Chris Lattner63d65f82009-09-08 18:19:27 +00002046 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002047 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002048 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002049 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002050 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2051 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002052 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002053
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002054 // Build a record containing all of the static unused functions in this file.
2055 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002056 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002057 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002058
Douglas Gregor14c22f22009-04-22 22:18:58 +00002059 // Build a record containing all of the locally-scoped external
2060 // declarations in this header file. Generally, this record will be
2061 // empty.
2062 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002063 // FIXME: This is filling in the PCH file in densemap order which is
2064 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002065 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002066 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2067 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2068 TD != TDEnd; ++TD)
2069 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2070
Douglas Gregorb81c1702009-04-27 20:06:05 +00002071 // Build a record containing all of the ext_vector declarations.
2072 RecordData ExtVectorDecls;
2073 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2074 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2075
Douglas Gregor2cf26342009-04-09 22:27:44 +00002076 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002077 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002078 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002079 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002080 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002081 if (StatCalls && !isysroot)
2082 WriteStatCache(*StatCalls, isysroot);
2083 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002084 // Write the record of special types.
2085 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002086
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002087 AddTypeRef(Context.getBuiltinVaListType(), Record);
2088 AddTypeRef(Context.getObjCIdType(), Record);
2089 AddTypeRef(Context.getObjCSelType(), Record);
2090 AddTypeRef(Context.getObjCProtoType(), Record);
2091 AddTypeRef(Context.getObjCClassType(), Record);
2092 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2093 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2094 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002095 AddTypeRef(Context.getjmp_bufType(), Record);
2096 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002097 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2098 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002099 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002100 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002101 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2102 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002103 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Douglas Gregor366809a2009-04-26 03:49:13 +00002105 // Keep writing types and declarations until all types and
2106 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002107 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2108 WriteDeclsBlockAbbrevs();
2109 while (!DeclTypesToEmit.empty()) {
2110 DeclOrType DOT = DeclTypesToEmit.front();
2111 DeclTypesToEmit.pop();
2112 if (DOT.isType())
2113 WriteType(DOT.getType());
2114 else
2115 WriteDecl(Context, DOT.getDecl());
2116 }
2117 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002118
Douglas Gregor813a97b2009-10-17 17:25:45 +00002119 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002120 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002121 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002122
2123 // Write the type offsets array
2124 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2125 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2128 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2129 Record.clear();
2130 Record.push_back(pch::TYPE_OFFSET);
2131 Record.push_back(TypeOffsets.size());
2132 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002133 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002134 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002136 // Write the declaration offsets array
2137 Abbrev = new BitCodeAbbrev();
2138 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2139 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2140 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2141 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2142 Record.clear();
2143 Record.push_back(pch::DECL_OFFSET);
2144 Record.push_back(DeclOffsets.size());
2145 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002146 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002147 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002148
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002149 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002150 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002151 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002152
2153 // Write the record containing tentative definitions.
2154 if (!TentativeDefinitions.empty())
2155 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002156
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002157 // Write the record containing unused static functions.
2158 if (!UnusedStaticFuncs.empty())
2159 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002160
Douglas Gregor14c22f22009-04-22 22:18:58 +00002161 // Write the record containing locally-scoped external definitions.
2162 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002163 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002164 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002165
2166 // Write the record containing ext_vector type names.
2167 if (!ExtVectorDecls.empty())
2168 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002169
Douglas Gregor3e1af842009-04-17 22:13:46 +00002170 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002171 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002172 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002173 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002174 Record.push_back(NumLexicalDeclContexts);
2175 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002176 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002177 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002178}
2179
2180void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2181 Record.push_back(Loc.getRawEncoding());
2182}
2183
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002184void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2185 AddSourceLocation(Range.getBegin(), Record);
2186 AddSourceLocation(Range.getEnd(), Record);
2187}
2188
Douglas Gregor2cf26342009-04-09 22:27:44 +00002189void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2190 Record.push_back(Value.getBitWidth());
2191 unsigned N = Value.getNumWords();
2192 const uint64_t* Words = Value.getRawData();
2193 for (unsigned I = 0; I != N; ++I)
2194 Record.push_back(Words[I]);
2195}
2196
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002197void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2198 Record.push_back(Value.isUnsigned());
2199 AddAPInt(Value, Record);
2200}
2201
Douglas Gregor17fc2232009-04-14 21:55:33 +00002202void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2203 AddAPInt(Value.bitcastToAPInt(), Record);
2204}
2205
Douglas Gregor2cf26342009-04-09 22:27:44 +00002206void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002207 Record.push_back(getIdentifierRef(II));
2208}
2209
2210pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2211 if (II == 0)
2212 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002213
2214 pch::IdentID &ID = IdentifierIDs[II];
2215 if (ID == 0)
2216 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002217 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002218}
2219
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002220pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2221 if (MD == 0)
2222 return 0;
2223
2224 pch::IdentID &ID = MacroDefinitions[MD];
2225 if (ID == 0)
2226 ID = MacroDefinitions.size();
2227 return ID;
2228}
2229
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002230void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2231 if (SelRef.getAsOpaquePtr() == 0) {
2232 Record.push_back(0);
2233 return;
2234 }
2235
2236 pch::SelectorID &SID = SelectorIDs[SelRef];
2237 if (SID == 0) {
2238 SID = SelectorIDs.size();
2239 SelVector.push_back(SelRef);
2240 }
2241 Record.push_back(SID);
2242}
2243
John McCall833ca992009-10-29 08:12:44 +00002244void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2245 RecordData &Record) {
2246 switch (Arg.getArgument().getKind()) {
2247 case TemplateArgument::Expression:
2248 AddStmt(Arg.getLocInfo().getAsExpr());
2249 break;
2250 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002251 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002252 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002253 case TemplateArgument::Template:
2254 Record.push_back(
2255 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2256 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2257 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2258 break;
John McCall833ca992009-10-29 08:12:44 +00002259 case TemplateArgument::Null:
2260 case TemplateArgument::Integral:
2261 case TemplateArgument::Declaration:
2262 case TemplateArgument::Pack:
2263 break;
2264 }
2265}
2266
John McCalla93c9342009-12-07 02:54:59 +00002267void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2268 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002269 AddTypeRef(QualType(), Record);
2270 return;
2271 }
2272
John McCalla93c9342009-12-07 02:54:59 +00002273 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002274 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002275 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002276 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002277}
2278
Douglas Gregor2cf26342009-04-09 22:27:44 +00002279void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2280 if (T.isNull()) {
2281 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2282 return;
2283 }
2284
Douglas Gregora4923eb2009-11-16 21:35:15 +00002285 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002286 T.removeFastQualifiers();
2287
Douglas Gregora4923eb2009-11-16 21:35:15 +00002288 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002289 pch::TypeID &ID = TypeIDs[T];
2290 if (ID == 0) {
2291 // We haven't seen these qualifiers applied to this type before.
2292 // Assign it a new ID. This is the only time we enqueue a
2293 // qualified type, and it has no CV qualifiers.
2294 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002295 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002296 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002297
John McCall0953e762009-09-24 19:53:00 +00002298 // Encode the type qualifiers in the type reference.
2299 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2300 return;
2301 }
2302
Douglas Gregora4923eb2009-11-16 21:35:15 +00002303 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002304
Douglas Gregor2cf26342009-04-09 22:27:44 +00002305 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002306 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002307 switch (BT->getKind()) {
2308 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2309 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2310 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2311 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2312 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2313 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2314 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2315 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002316 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002317 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2318 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2319 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2320 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2321 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2322 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2323 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002324 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002325 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2326 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2327 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002328 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002329 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2330 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002331 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2332 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002333 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2334 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002335 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002336 case BuiltinType::UndeducedAuto:
2337 assert(0 && "Should not see undeduced auto here");
2338 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002339 }
2340
John McCall0953e762009-09-24 19:53:00 +00002341 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002342 return;
2343 }
2344
John McCall0953e762009-09-24 19:53:00 +00002345 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002346 if (ID == 0) {
2347 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002348 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002349 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002350 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002351 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002352
2353 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002354 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002355}
2356
2357void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2358 if (D == 0) {
2359 Record.push_back(0);
2360 return;
2361 }
2362
Douglas Gregor8038d512009-04-10 17:25:41 +00002363 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002364 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002365 // We haven't seen this declaration before. Give it a new ID and
2366 // enqueue it in the list of declarations to emit.
2367 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002368 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002369 }
2370
2371 Record.push_back(ID);
2372}
2373
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002374pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2375 if (D == 0)
2376 return 0;
2377
2378 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2379 return DeclIDs[D];
2380}
2381
Douglas Gregor2cf26342009-04-09 22:27:44 +00002382void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002383 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002384 Record.push_back(Name.getNameKind());
2385 switch (Name.getNameKind()) {
2386 case DeclarationName::Identifier:
2387 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2388 break;
2389
2390 case DeclarationName::ObjCZeroArgSelector:
2391 case DeclarationName::ObjCOneArgSelector:
2392 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002393 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002394 break;
2395
2396 case DeclarationName::CXXConstructorName:
2397 case DeclarationName::CXXDestructorName:
2398 case DeclarationName::CXXConversionFunctionName:
2399 AddTypeRef(Name.getCXXNameType(), Record);
2400 break;
2401
2402 case DeclarationName::CXXOperatorName:
2403 Record.push_back(Name.getCXXOverloadedOperator());
2404 break;
2405
Sean Hunt3e518bd2009-11-29 07:34:05 +00002406 case DeclarationName::CXXLiteralOperatorName:
2407 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2408 break;
2409
Douglas Gregor2cf26342009-04-09 22:27:44 +00002410 case DeclarationName::CXXUsingDirective:
2411 // No extra data to emit
2412 break;
2413 }
2414}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002415
2416void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2417 RecordData &Record) {
2418 // Nested name specifiers usually aren't too long. I think that 8 would
2419 // typically accomodate the vast majority.
2420 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2421
2422 // Push each of the NNS's onto a stack for serialization in reverse order.
2423 while (NNS) {
2424 NestedNames.push_back(NNS);
2425 NNS = NNS->getPrefix();
2426 }
2427
2428 Record.push_back(NestedNames.size());
2429 while(!NestedNames.empty()) {
2430 NNS = NestedNames.pop_back_val();
2431 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2432 Record.push_back(Kind);
2433 switch (Kind) {
2434 case NestedNameSpecifier::Identifier:
2435 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2436 break;
2437
2438 case NestedNameSpecifier::Namespace:
2439 AddDeclRef(NNS->getAsNamespace(), Record);
2440 break;
2441
2442 case NestedNameSpecifier::TypeSpec:
2443 case NestedNameSpecifier::TypeSpecWithTemplate:
2444 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2445 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2446 break;
2447
2448 case NestedNameSpecifier::Global:
2449 // Don't need to write an associated value.
2450 break;
2451 }
2452 }
2453}