blob: 8684a06eb0bbede0c21b49f6856eee4313a05f1d [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)
Douglas Gregor2cf26342009-04-09 22:27:44 +000064#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
Douglas Gregor2cf26342009-04-09 22:27:44 +000072void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
73 Writer.AddTypeRef(T->getElementType(), Record);
74 Code = pch::TYPE_COMPLEX;
75}
76
77void PCHTypeWriter::VisitPointerType(const PointerType *T) {
78 Writer.AddTypeRef(T->getPointeeType(), Record);
79 Code = pch::TYPE_POINTER;
80}
81
82void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000084 Code = pch::TYPE_BLOCK_POINTER;
85}
86
87void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_LVALUE_REFERENCE;
90}
91
92void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_RVALUE_REFERENCE;
95}
96
97void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100 Code = pch::TYPE_MEMBER_POINTER;
101}
102
103void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
104 Writer.AddTypeRef(T->getElementType(), Record);
105 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000106 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
109void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
110 VisitArrayType(T);
111 Writer.AddAPInt(T->getSize(), Record);
112 Code = pch::TYPE_CONSTANT_ARRAY;
113}
114
115void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
116 VisitArrayType(T);
117 Code = pch::TYPE_INCOMPLETE_ARRAY;
118}
119
120void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
121 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000122 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
123 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000124 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125 Code = pch::TYPE_VARIABLE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVectorType(const VectorType *T) {
129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getNumElements());
Chris Lattner788b0fd2010-06-23 06:00:24 +0000131 Record.push_back(T->getAltiVecSpecific());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132 Code = pch::TYPE_VECTOR;
133}
134
135void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
136 VisitVectorType(T);
137 Code = pch::TYPE_EXT_VECTOR;
138}
139
140void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
141 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000142 FunctionType::ExtInfo C = T->getExtInfo();
143 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000144 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000145 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000146 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147}
148
149void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
150 VisitFunctionType(T);
151 Code = pch::TYPE_FUNCTION_NO_PROTO;
152}
153
154void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
155 VisitFunctionType(T);
156 Record.push_back(T->getNumArgs());
157 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
158 Writer.AddTypeRef(T->getArgType(I), Record);
159 Record.push_back(T->isVariadic());
160 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000161 Record.push_back(T->hasExceptionSpec());
162 Record.push_back(T->hasAnyExceptionSpec());
163 Record.push_back(T->getNumExceptions());
164 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
165 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 Code = pch::TYPE_FUNCTION_PROTO;
167}
168
John McCalled976492009-12-04 22:46:56 +0000169void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
170 Writer.AddDeclRef(T->getDecl(), Record);
171 Code = pch::TYPE_UNRESOLVED_USING;
172}
John McCalled976492009-12-04 22:46:56 +0000173
Douglas Gregor2cf26342009-04-09 22:27:44 +0000174void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
175 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000176 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
177 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178 Code = pch::TYPE_TYPEDEF;
179}
180
181void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000182 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 Code = pch::TYPE_TYPEOF_EXPR;
184}
185
186void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
187 Writer.AddTypeRef(T->getUnderlyingType(), Record);
188 Code = pch::TYPE_TYPEOF;
189}
190
Anders Carlsson395b4752009-06-24 19:06:50 +0000191void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
192 Writer.AddStmt(T->getUnderlyingExpr());
193 Code = pch::TYPE_DECLTYPE;
194}
195
Douglas Gregor2cf26342009-04-09 22:27:44 +0000196void PCHTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000197 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000199 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200 "Cannot serialize in the middle of a type definition");
201}
202
203void PCHTypeWriter::VisitRecordType(const RecordType *T) {
204 VisitTagType(T);
205 Code = pch::TYPE_RECORD;
206}
207
208void PCHTypeWriter::VisitEnumType(const EnumType *T) {
209 VisitTagType(T);
210 Code = pch::TYPE_ENUM;
211}
212
Mike Stump1eb44332009-09-09 15:08:12 +0000213void
John McCall49a832b2009-10-18 09:09:24 +0000214PCHTypeWriter::VisitSubstTemplateTypeParmType(
215 const SubstTemplateTypeParmType *T) {
216 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
217 Writer.AddTypeRef(T->getReplacementType(), Record);
218 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
219}
220
221void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222PCHTypeWriter::VisitTemplateSpecializationType(
223 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000224 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000225 Writer.AddTemplateName(T->getTemplateName(), Record);
226 Record.push_back(T->getNumArgs());
227 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
228 ArgI != ArgE; ++ArgI)
229 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000230 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
231 : T->getCanonicalTypeInternal(),
232 Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000233 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
234}
235
236void
237PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000238 VisitArrayType(T);
239 Writer.AddStmt(T->getSizeExpr());
240 Writer.AddSourceRange(T->getBracketsRange(), Record);
241 Code = pch::TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000242}
243
244void
245PCHTypeWriter::VisitDependentSizedExtVectorType(
246 const DependentSizedExtVectorType *T) {
247 // FIXME: Serialize this type (C++ only)
248 assert(false && "Cannot serialize dependent sized extended vector types");
249}
250
251void
252PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
253 Record.push_back(T->getDepth());
254 Record.push_back(T->getIndex());
255 Record.push_back(T->isParameterPack());
256 Writer.AddIdentifierRef(T->getName(), Record);
257 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
258}
259
260void
261PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000262 Record.push_back(T->getKeyword());
263 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
264 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000265 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
266 : T->getCanonicalTypeInternal(),
267 Record);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000268 Code = pch::TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000269}
270
271void
272PCHTypeWriter::VisitDependentTemplateSpecializationType(
273 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000274 Record.push_back(T->getKeyword());
275 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
276 Writer.AddIdentifierRef(T->getIdentifier(), Record);
277 Record.push_back(T->getNumArgs());
278 for (DependentTemplateSpecializationType::iterator
279 I = T->begin(), E = T->end(); I != E; ++I)
280 Writer.AddTemplateArgument(*I, Record);
281 Code = pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000282}
283
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000284void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000285 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000286 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
287 Writer.AddTypeRef(T->getNamedType(), Record);
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000288 Code = pch::TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000289}
290
John McCall3cb0ebd2010-03-10 03:28:59 +0000291void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
292 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000293 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000294 Code = pch::TYPE_INJECTED_CLASS_NAME;
295}
296
Douglas Gregor2cf26342009-04-09 22:27:44 +0000297void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
298 Writer.AddDeclRef(T->getDecl(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000299 Code = pch::TYPE_OBJC_INTERFACE;
300}
301
302void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
303 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000304 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000305 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000306 E = T->qual_end(); I != E; ++I)
307 Writer.AddDeclRef(*I, Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000308 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000309}
310
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000311void
312PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000313 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000314 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000315}
316
John McCalla1ee0c52009-10-16 21:56:05 +0000317namespace {
318
319class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
320 PCHWriter &Writer;
321 PCHWriter::RecordData &Record;
322
323public:
324 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
325 : Writer(Writer), Record(Record) { }
326
John McCall51bd8032009-10-18 01:05:36 +0000327#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000328#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000329 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000330#include "clang/AST/TypeLocNodes.def"
331
John McCall51bd8032009-10-18 01:05:36 +0000332 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
333 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000334};
335
336}
337
John McCall51bd8032009-10-18 01:05:36 +0000338void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
339 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000340}
John McCall51bd8032009-10-18 01:05:36 +0000341void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000342 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
343 if (TL.needsExtraLocalData()) {
344 Record.push_back(TL.getWrittenTypeSpec());
345 Record.push_back(TL.getWrittenSignSpec());
346 Record.push_back(TL.getWrittenWidthSpec());
347 Record.push_back(TL.hasModeAttr());
348 }
John McCalla1ee0c52009-10-16 21:56:05 +0000349}
John McCall51bd8032009-10-18 01:05:36 +0000350void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
351 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000352}
John McCall51bd8032009-10-18 01:05:36 +0000353void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
354 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000355}
John McCall51bd8032009-10-18 01:05:36 +0000356void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
357 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000358}
John McCall51bd8032009-10-18 01:05:36 +0000359void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
360 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000361}
John McCall51bd8032009-10-18 01:05:36 +0000362void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
363 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000364}
John McCall51bd8032009-10-18 01:05:36 +0000365void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
366 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000367}
John McCall51bd8032009-10-18 01:05:36 +0000368void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
369 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
370 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
371 Record.push_back(TL.getSizeExpr() ? 1 : 0);
372 if (TL.getSizeExpr())
373 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000374}
John McCall51bd8032009-10-18 01:05:36 +0000375void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
376 VisitArrayTypeLoc(TL);
377}
378void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
379 VisitArrayTypeLoc(TL);
380}
381void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
382 VisitArrayTypeLoc(TL);
383}
384void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
385 DependentSizedArrayTypeLoc TL) {
386 VisitArrayTypeLoc(TL);
387}
388void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
389 DependentSizedExtVectorTypeLoc TL) {
390 Writer.AddSourceLocation(TL.getNameLoc(), Record);
391}
392void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
393 Writer.AddSourceLocation(TL.getNameLoc(), Record);
394}
395void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
396 Writer.AddSourceLocation(TL.getNameLoc(), Record);
397}
398void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
399 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
400 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
401 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
402 Writer.AddDeclRef(TL.getArg(i), Record);
403}
404void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
405 VisitFunctionTypeLoc(TL);
406}
407void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
408 VisitFunctionTypeLoc(TL);
409}
John McCalled976492009-12-04 22:46:56 +0000410void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getNameLoc(), Record);
412}
John McCall51bd8032009-10-18 01:05:36 +0000413void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
414 Writer.AddSourceLocation(TL.getNameLoc(), Record);
415}
416void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000417 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
418 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
419 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000420}
421void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000422 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
423 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
424 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
425 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000426}
427void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
428 Writer.AddSourceLocation(TL.getNameLoc(), Record);
429}
430void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getNameLoc(), Record);
432}
433void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getNameLoc(), Record);
435}
John McCall51bd8032009-10-18 01:05:36 +0000436void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getNameLoc(), Record);
438}
John McCall49a832b2009-10-18 09:09:24 +0000439void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
440 SubstTemplateTypeParmTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
442}
John McCall51bd8032009-10-18 01:05:36 +0000443void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
444 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000445 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
446 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
447 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
448 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000449 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
450 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000451}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000452void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000453 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
454 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000455}
John McCall3cb0ebd2010-03-10 03:28:59 +0000456void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getNameLoc(), Record);
458}
Douglas Gregor4714c122010-03-31 17:34:00 +0000459void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000460 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
461 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000462 Writer.AddSourceLocation(TL.getNameLoc(), Record);
463}
John McCall33500952010-06-11 00:33:02 +0000464void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
465 DependentTemplateSpecializationTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
467 Writer.AddSourceRange(TL.getQualifierRange(), Record);
468 Writer.AddSourceLocation(TL.getNameLoc(), Record);
469 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
470 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
471 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000472 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
473 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000474}
John McCall51bd8032009-10-18 01:05:36 +0000475void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000477}
478void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
479 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000480 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
481 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
482 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
483 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000484}
John McCall54e14c42009-10-22 22:37:11 +0000485void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000487}
John McCalla1ee0c52009-10-16 21:56:05 +0000488
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000489//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000490// PCHWriter Implementation
491//===----------------------------------------------------------------------===//
492
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000493static void EmitBlockID(unsigned ID, const char *Name,
494 llvm::BitstreamWriter &Stream,
495 PCHWriter::RecordData &Record) {
496 Record.clear();
497 Record.push_back(ID);
498 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
499
500 // Emit the block name if present.
501 if (Name == 0 || Name[0] == 0) return;
502 Record.clear();
503 while (*Name)
504 Record.push_back(*Name++);
505 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
506}
507
508static void EmitRecordID(unsigned ID, const char *Name,
509 llvm::BitstreamWriter &Stream,
510 PCHWriter::RecordData &Record) {
511 Record.clear();
512 Record.push_back(ID);
513 while (*Name)
514 Record.push_back(*Name++);
515 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000516}
517
518static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
519 PCHWriter::RecordData &Record) {
520#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
521 RECORD(STMT_STOP);
522 RECORD(STMT_NULL_PTR);
523 RECORD(STMT_NULL);
524 RECORD(STMT_COMPOUND);
525 RECORD(STMT_CASE);
526 RECORD(STMT_DEFAULT);
527 RECORD(STMT_LABEL);
528 RECORD(STMT_IF);
529 RECORD(STMT_SWITCH);
530 RECORD(STMT_WHILE);
531 RECORD(STMT_DO);
532 RECORD(STMT_FOR);
533 RECORD(STMT_GOTO);
534 RECORD(STMT_INDIRECT_GOTO);
535 RECORD(STMT_CONTINUE);
536 RECORD(STMT_BREAK);
537 RECORD(STMT_RETURN);
538 RECORD(STMT_DECL);
539 RECORD(STMT_ASM);
540 RECORD(EXPR_PREDEFINED);
541 RECORD(EXPR_DECL_REF);
542 RECORD(EXPR_INTEGER_LITERAL);
543 RECORD(EXPR_FLOATING_LITERAL);
544 RECORD(EXPR_IMAGINARY_LITERAL);
545 RECORD(EXPR_STRING_LITERAL);
546 RECORD(EXPR_CHARACTER_LITERAL);
547 RECORD(EXPR_PAREN);
548 RECORD(EXPR_UNARY_OPERATOR);
549 RECORD(EXPR_SIZEOF_ALIGN_OF);
550 RECORD(EXPR_ARRAY_SUBSCRIPT);
551 RECORD(EXPR_CALL);
552 RECORD(EXPR_MEMBER);
553 RECORD(EXPR_BINARY_OPERATOR);
554 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
555 RECORD(EXPR_CONDITIONAL_OPERATOR);
556 RECORD(EXPR_IMPLICIT_CAST);
557 RECORD(EXPR_CSTYLE_CAST);
558 RECORD(EXPR_COMPOUND_LITERAL);
559 RECORD(EXPR_EXT_VECTOR_ELEMENT);
560 RECORD(EXPR_INIT_LIST);
561 RECORD(EXPR_DESIGNATED_INIT);
562 RECORD(EXPR_IMPLICIT_VALUE_INIT);
563 RECORD(EXPR_VA_ARG);
564 RECORD(EXPR_ADDR_LABEL);
565 RECORD(EXPR_STMT);
566 RECORD(EXPR_TYPES_COMPATIBLE);
567 RECORD(EXPR_CHOOSE);
568 RECORD(EXPR_GNU_NULL);
569 RECORD(EXPR_SHUFFLE_VECTOR);
570 RECORD(EXPR_BLOCK);
571 RECORD(EXPR_BLOCK_DECL_REF);
572 RECORD(EXPR_OBJC_STRING_LITERAL);
573 RECORD(EXPR_OBJC_ENCODE);
574 RECORD(EXPR_OBJC_SELECTOR_EXPR);
575 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
576 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
577 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
578 RECORD(EXPR_OBJC_KVC_REF_EXPR);
579 RECORD(EXPR_OBJC_MESSAGE_EXPR);
580 RECORD(EXPR_OBJC_SUPER_EXPR);
581 RECORD(STMT_OBJC_FOR_COLLECTION);
582 RECORD(STMT_OBJC_CATCH);
583 RECORD(STMT_OBJC_FINALLY);
584 RECORD(STMT_OBJC_AT_TRY);
585 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
586 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000587 RECORD(EXPR_CXX_OPERATOR_CALL);
588 RECORD(EXPR_CXX_CONSTRUCT);
589 RECORD(EXPR_CXX_STATIC_CAST);
590 RECORD(EXPR_CXX_DYNAMIC_CAST);
591 RECORD(EXPR_CXX_REINTERPRET_CAST);
592 RECORD(EXPR_CXX_CONST_CAST);
593 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
594 RECORD(EXPR_CXX_BOOL_LITERAL);
595 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000596#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000597}
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000599void PCHWriter::WriteBlockInfoBlock() {
600 RecordData Record;
601 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Chris Lattner2f4efd12009-04-27 00:40:25 +0000603#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000604#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000606 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000607 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000608 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000609 RECORD(TYPE_OFFSET);
610 RECORD(DECL_OFFSET);
611 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000612 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000613 RECORD(IDENTIFIER_OFFSET);
614 RECORD(IDENTIFIER_TABLE);
615 RECORD(EXTERNAL_DEFINITIONS);
616 RECORD(SPECIAL_TYPES);
617 RECORD(STATISTICS);
618 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000619 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000620 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
621 RECORD(SELECTOR_OFFSETS);
622 RECORD(METHOD_POOL);
623 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000624 RECORD(SOURCE_LOCATION_OFFSETS);
625 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000626 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000627 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000628 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000629 RECORD(UNUSED_STATIC_FUNCS);
630 RECORD(MACRO_DEFINITION_OFFSETS);
631
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000633 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000634 RECORD(SM_SLOC_FILE_ENTRY);
635 RECORD(SM_SLOC_BUFFER_ENTRY);
636 RECORD(SM_SLOC_BUFFER_BLOB);
637 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
638 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000640 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000641 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000642 RECORD(PP_MACRO_OBJECT_LIKE);
643 RECORD(PP_MACRO_FUNCTION_LIKE);
644 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000645 RECORD(PP_MACRO_INSTANTIATION);
646 RECORD(PP_MACRO_DEFINITION);
647
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000648 // Decls and Types block.
649 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000650 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000651 RECORD(TYPE_COMPLEX);
652 RECORD(TYPE_POINTER);
653 RECORD(TYPE_BLOCK_POINTER);
654 RECORD(TYPE_LVALUE_REFERENCE);
655 RECORD(TYPE_RVALUE_REFERENCE);
656 RECORD(TYPE_MEMBER_POINTER);
657 RECORD(TYPE_CONSTANT_ARRAY);
658 RECORD(TYPE_INCOMPLETE_ARRAY);
659 RECORD(TYPE_VARIABLE_ARRAY);
660 RECORD(TYPE_VECTOR);
661 RECORD(TYPE_EXT_VECTOR);
662 RECORD(TYPE_FUNCTION_PROTO);
663 RECORD(TYPE_FUNCTION_NO_PROTO);
664 RECORD(TYPE_TYPEDEF);
665 RECORD(TYPE_TYPEOF_EXPR);
666 RECORD(TYPE_TYPEOF);
667 RECORD(TYPE_RECORD);
668 RECORD(TYPE_ENUM);
669 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000670 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000671 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000672 RECORD(DECL_ATTR);
673 RECORD(DECL_TRANSLATION_UNIT);
674 RECORD(DECL_TYPEDEF);
675 RECORD(DECL_ENUM);
676 RECORD(DECL_RECORD);
677 RECORD(DECL_ENUM_CONSTANT);
678 RECORD(DECL_FUNCTION);
679 RECORD(DECL_OBJC_METHOD);
680 RECORD(DECL_OBJC_INTERFACE);
681 RECORD(DECL_OBJC_PROTOCOL);
682 RECORD(DECL_OBJC_IVAR);
683 RECORD(DECL_OBJC_AT_DEFS_FIELD);
684 RECORD(DECL_OBJC_CLASS);
685 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
686 RECORD(DECL_OBJC_CATEGORY);
687 RECORD(DECL_OBJC_CATEGORY_IMPL);
688 RECORD(DECL_OBJC_IMPLEMENTATION);
689 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
690 RECORD(DECL_OBJC_PROPERTY);
691 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000692 RECORD(DECL_FIELD);
693 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000694 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000695 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000696 RECORD(DECL_FILE_SCOPE_ASM);
697 RECORD(DECL_BLOCK);
698 RECORD(DECL_CONTEXT_LEXICAL);
699 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000700 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000701 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000702#undef RECORD
703#undef BLOCK
704 Stream.ExitBlock();
705}
706
Douglas Gregore650c8c2009-07-07 00:12:59 +0000707/// \brief Adjusts the given filename to only write out the portion of the
708/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000709///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000710/// \param Filename the file name to adjust.
711///
712/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
713/// the returned filename will be adjusted by this system root.
714///
715/// \returns either the original filename (if it needs no adjustment) or the
716/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000717static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000718adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
719 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Douglas Gregore650c8c2009-07-07 00:12:59 +0000721 if (!isysroot)
722 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Douglas Gregore650c8c2009-07-07 00:12:59 +0000724 // Verify that the filename and the system root have the same prefix.
725 unsigned Pos = 0;
726 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
727 if (Filename[Pos] != isysroot[Pos])
728 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Douglas Gregore650c8c2009-07-07 00:12:59 +0000730 // We hit the end of the filename before we hit the end of the system root.
731 if (!Filename[Pos])
732 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Douglas Gregore650c8c2009-07-07 00:12:59 +0000734 // If the file name has a '/' at the current position, skip over the '/'.
735 // We distinguish sysroot-based includes from absolute includes by the
736 // absence of '/' at the beginning of sysroot-based includes.
737 if (Filename[Pos] == '/')
738 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Douglas Gregore650c8c2009-07-07 00:12:59 +0000740 return Filename + Pos;
741}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000742
Douglas Gregorab41e632009-04-27 22:23:34 +0000743/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000744void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000745 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000746
Douglas Gregore650c8c2009-07-07 00:12:59 +0000747 // Metadata
748 const TargetInfo &Target = Context.Target;
749 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
750 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
751 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
752 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
753 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
754 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
755 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
756 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
757 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Douglas Gregore650c8c2009-07-07 00:12:59 +0000759 RecordData Record;
760 Record.push_back(pch::METADATA);
761 Record.push_back(pch::VERSION_MAJOR);
762 Record.push_back(pch::VERSION_MINOR);
763 Record.push_back(CLANG_VERSION_MAJOR);
764 Record.push_back(CLANG_VERSION_MINOR);
765 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000766 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000767 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Douglas Gregorb64c1932009-05-12 01:31:05 +0000769 // Original file name
770 SourceManager &SM = Context.getSourceManager();
771 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
772 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
773 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
774 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
775 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
776
777 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000779 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000780
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000781 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000782 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000783 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000784 RecordData Record;
785 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000786 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000787 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000788
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000789 // Repository branch/version information.
790 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
791 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
792 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
793 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000794 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000795 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000796 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
797 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000798}
799
800/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000801void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
802 RecordData Record;
803 Record.push_back(LangOpts.Trigraphs);
804 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
805 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
806 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
807 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000808 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000809 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
810 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
811 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
812 Record.push_back(LangOpts.C99); // C99 Support
813 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
814 Record.push_back(LangOpts.CPlusPlus); // C++ Support
815 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000816 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000818 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
819 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000820 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000821 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000822 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000823 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000824 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000826 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000827 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
828 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000829 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000830 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000831 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000832
833 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
834 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
835 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
836
Chris Lattnerea5ce472009-04-27 07:35:58 +0000837 // Whether static initializers are protected by locks.
838 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000839 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000840 Record.push_back(LangOpts.Blocks); // block extension to C
841 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
842 // they are unused.
843 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
844 // (modulo the platform support).
845
Chris Lattnera4d71452010-06-26 21:25:03 +0000846 Record.push_back(LangOpts.getSignedOverflowBehavior());
847 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000848
849 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000850 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000851 // defined.
852 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
853 // opposed to __DYNAMIC__).
854 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
855
856 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
857 // used (instead of C99 semantics).
858 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000859 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
860 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000861 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
862 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000863 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000864 Record.push_back(LangOpts.getGCMode());
865 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000866 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000867 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000868 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000869 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000870 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000871 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000872}
873
Douglas Gregor14f79002009-04-10 03:52:48 +0000874//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000875// stat cache Serialization
876//===----------------------------------------------------------------------===//
877
878namespace {
879// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000880class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000881public:
882 typedef const char * key_type;
883 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000885 typedef std::pair<int, struct stat> data_type;
886 typedef const data_type& data_type_ref;
887
888 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000889 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000890 }
Mike Stump1eb44332009-09-09 15:08:12 +0000891
892 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000893 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
894 data_type_ref Data) {
895 unsigned StrLen = strlen(path);
896 clang::io::Emit16(Out, StrLen);
897 unsigned DataLen = 1; // result value
898 if (Data.first == 0)
899 DataLen += 4 + 4 + 2 + 8 + 8;
900 clang::io::Emit8(Out, DataLen);
901 return std::make_pair(StrLen + 1, DataLen);
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000904 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
905 Out.write(path, KeyLen);
906 }
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000908 void EmitData(llvm::raw_ostream& Out, key_type_ref,
909 data_type_ref Data, unsigned DataLen) {
910 using namespace clang::io;
911 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000913 // Result of stat()
914 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000916 if (Data.first == 0) {
917 Emit32(Out, (uint32_t) Data.second.st_ino);
918 Emit32(Out, (uint32_t) Data.second.st_dev);
919 Emit16(Out, (uint16_t) Data.second.st_mode);
920 Emit64(Out, (uint64_t) Data.second.st_mtime);
921 Emit64(Out, (uint64_t) Data.second.st_size);
922 }
923
924 assert(Out.tell() - Start == DataLen && "Wrong data length");
925 }
926};
927} // end anonymous namespace
928
929/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000930void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
931 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000932 // Build the on-disk hash table containing information about every
933 // stat() call.
934 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
935 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000936 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000937 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000938 Stat != StatEnd; ++Stat, ++NumStatEntries) {
939 const char *Filename = Stat->first();
940 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
941 Generator.insert(Filename, Stat->second);
942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000944 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000945 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000946 uint32_t BucketOffset;
947 {
948 llvm::raw_svector_ostream Out(StatCacheData);
949 // Make sure that no bucket is at offset 0
950 clang::io::Emit32(Out, 0);
951 BucketOffset = Generator.Emit(Out);
952 }
953
954 // Create a blob abbreviation
955 using namespace llvm;
956 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
957 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
961 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
962
963 // Write the stat cache
964 RecordData Record;
965 Record.push_back(pch::STAT_CACHE);
966 Record.push_back(BucketOffset);
967 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000968 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000969}
970
971//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000972// Source Manager Serialization
973//===----------------------------------------------------------------------===//
974
975/// \brief Create an abbreviation for the SLocEntry that refers to a
976/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000977static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000978 using namespace llvm;
979 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
980 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
981 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000985 // FileEntry fields.
986 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
987 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000988 // HeaderFileInfo fields.
989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000994 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000995}
996
997/// \brief Create an abbreviation for the SLocEntry that refers to a
998/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000999static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001000 using namespace llvm;
1001 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1002 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001008 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001009}
1010
1011/// \brief Create an abbreviation for the SLocEntry that refers to a
1012/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001013static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001014 using namespace llvm;
1015 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1016 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001018 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001019}
1020
1021/// \brief Create an abbreviation for the SLocEntry that refers to an
1022/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001023static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001024 using namespace llvm;
1025 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1026 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001032 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001033}
1034
1035/// \brief Writes the block containing the serialized form of the
1036/// source manager.
1037///
1038/// TODO: We should probably use an on-disk hash table (stored in a
1039/// blob), indexed based on the file name, so that we only create
1040/// entries for files that we actually need. In the common case (no
1041/// errors), we probably won't have to create file entries for any of
1042/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001043void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001044 const Preprocessor &PP,
1045 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001046 RecordData Record;
1047
Chris Lattnerf04ad692009-04-10 17:16:57 +00001048 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001049 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001050
1051 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001052 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1053 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1054 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1055 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001056
Douglas Gregorbd945002009-04-13 16:31:14 +00001057 // Write the line table.
1058 if (SourceMgr.hasLineTable()) {
1059 LineTableInfo &LineTable = SourceMgr.getLineTable();
1060
1061 // Emit the file names
1062 Record.push_back(LineTable.getNumFilenames());
1063 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1064 // Emit the file name
1065 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001066 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001067 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1068 Record.push_back(FilenameLen);
1069 if (FilenameLen)
1070 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1071 }
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Douglas Gregorbd945002009-04-13 16:31:14 +00001073 // Emit the line entries
1074 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1075 L != LEnd; ++L) {
1076 // Emit the file ID
1077 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Douglas Gregorbd945002009-04-13 16:31:14 +00001079 // Emit the line entries
1080 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001081 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001082 LEEnd = L->second.end();
1083 LE != LEEnd; ++LE) {
1084 Record.push_back(LE->FileOffset);
1085 Record.push_back(LE->LineNo);
1086 Record.push_back(LE->FilenameID);
1087 Record.push_back((unsigned)LE->FileKind);
1088 Record.push_back(LE->IncludeOffset);
1089 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001090 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001091 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001092 }
1093
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001094 // Write out the source location entry table. We skip the first
1095 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001096 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001097 RecordData PreloadSLocs;
1098 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001099 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1100 // Get this source location entry.
1101 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001102
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001103 // Record the offset of this source-location entry.
1104 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1105
1106 // Figure out which record code to use.
1107 unsigned Code;
1108 if (SLoc->isFile()) {
1109 if (SLoc->getFile().getContentCache()->Entry)
1110 Code = pch::SM_SLOC_FILE_ENTRY;
1111 else
1112 Code = pch::SM_SLOC_BUFFER_ENTRY;
1113 } else
1114 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1115 Record.clear();
1116 Record.push_back(Code);
1117
1118 Record.push_back(SLoc->getOffset());
1119 if (SLoc->isFile()) {
1120 const SrcMgr::FileInfo &File = SLoc->getFile();
1121 Record.push_back(File.getIncludeLoc().getRawEncoding());
1122 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1123 Record.push_back(File.hasLineDirectives());
1124
1125 const SrcMgr::ContentCache *Content = File.getContentCache();
1126 if (Content->Entry) {
1127 // The source location entry is a file. The blob associated
1128 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Douglas Gregor2d52be52010-03-21 22:49:54 +00001130 // Emit size/modification time for this file.
1131 Record.push_back(Content->Entry->getSize());
1132 Record.push_back(Content->Entry->getModificationTime());
1133
Douglas Gregor12fab312010-03-16 16:35:32 +00001134 // Emit header-search information associated with this file.
1135 HeaderFileInfo HFI;
1136 HeaderSearch &HS = PP.getHeaderSearchInfo();
1137 if (Content->Entry->getUID() < HS.header_file_size())
1138 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1139 Record.push_back(HFI.isImport);
1140 Record.push_back(HFI.DirInfo);
1141 Record.push_back(HFI.NumIncludes);
1142 AddIdentifierRef(HFI.ControllingMacro, Record);
1143
Douglas Gregore650c8c2009-07-07 00:12:59 +00001144 // Turn the file name into an absolute path, if it isn't already.
1145 const char *Filename = Content->Entry->getName();
1146 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001147 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001148 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Douglas Gregore650c8c2009-07-07 00:12:59 +00001150 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001151 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001152
1153 // FIXME: For now, preload all file source locations, so that
1154 // we get the appropriate File entries in the reader. This is
1155 // a temporary measure.
1156 PreloadSLocs.push_back(SLocEntryOffsets.size());
1157 } else {
1158 // The source location entry is a buffer. The blob associated
1159 // with this entry contains the contents of the buffer.
1160
1161 // We add one to the size so that we capture the trailing NULL
1162 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1163 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001164 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001165 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001166 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001167 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1168 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001169 Record.clear();
1170 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1171 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001172 llvm::StringRef(Buffer->getBufferStart(),
1173 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001174
1175 if (strcmp(Name, "<built-in>") == 0)
1176 PreloadSLocs.push_back(SLocEntryOffsets.size());
1177 }
1178 } else {
1179 // The source location entry is an instantiation.
1180 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1181 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1182 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1183 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1184
1185 // Compute the token length for this macro expansion.
1186 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001187 if (I + 1 != N)
1188 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001189 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1190 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1191 }
1192 }
1193
Douglas Gregorc9490c02009-04-16 22:23:12 +00001194 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001195
1196 if (SLocEntryOffsets.empty())
1197 return;
1198
1199 // Write the source-location offsets table into the PCH block. This
1200 // table is used for lazily loading source-location information.
1201 using namespace llvm;
1202 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1203 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1207 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001209 Record.clear();
1210 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1211 Record.push_back(SLocEntryOffsets.size());
1212 Record.push_back(SourceMgr.getNextOffset());
1213 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001214 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001215 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001216
1217 // Write the source location entry preloads array, telling the PCH
1218 // reader which source locations entries it should load eagerly.
1219 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001220}
1221
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001222//===----------------------------------------------------------------------===//
1223// Preprocessor Serialization
1224//===----------------------------------------------------------------------===//
1225
Chris Lattner0b1fb982009-04-10 17:15:23 +00001226/// \brief Writes the block containing the serialized form of the
1227/// preprocessor.
1228///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001229void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001230 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001231
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001232 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1233 if (PP.getCounterValue() != 0) {
1234 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001235 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001236 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001237 }
1238
1239 // Enter the preprocessor block.
1240 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001242 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1243 // FIXME: use diagnostics subsystem for localization etc.
1244 if (PP.SawDateOrTime())
1245 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001247 // Loop over all the macro definitions that are live at the end of the file,
1248 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001249 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001250 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1251 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001252 // FIXME: This emits macros in hash table order, we should do it in a stable
1253 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001254 MacroInfo *MI = I->second;
1255
1256 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1257 // been redefined by the header (in which case they are not isBuiltinMacro).
1258 if (MI->isBuiltinMacro())
1259 continue;
1260
Chris Lattner7356a312009-04-11 21:15:38 +00001261 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001262 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001263 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1264 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001266 unsigned Code;
1267 if (MI->isObjectLike()) {
1268 Code = pch::PP_MACRO_OBJECT_LIKE;
1269 } else {
1270 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001272 Record.push_back(MI->isC99Varargs());
1273 Record.push_back(MI->isGNUVarargs());
1274 Record.push_back(MI->getNumArgs());
1275 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1276 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001277 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001278 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001279
1280 // If we have a detailed preprocessing record, record the macro definition
1281 // ID that corresponds to this macro.
1282 if (PPRec)
1283 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1284
Douglas Gregorc9490c02009-04-16 22:23:12 +00001285 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001286 Record.clear();
1287
Chris Lattnerdf961c22009-04-10 18:08:30 +00001288 // Emit the tokens array.
1289 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1290 // Note that we know that the preprocessor does not have any annotation
1291 // tokens in it because they are created by the parser, and thus can't be
1292 // in a macro definition.
1293 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Chris Lattnerdf961c22009-04-10 18:08:30 +00001295 Record.push_back(Tok.getLocation().getRawEncoding());
1296 Record.push_back(Tok.getLength());
1297
Chris Lattnerdf961c22009-04-10 18:08:30 +00001298 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1299 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001300 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Chris Lattnerdf961c22009-04-10 18:08:30 +00001302 // FIXME: Should translate token kind to a stable encoding.
1303 Record.push_back(Tok.getKind());
1304 // FIXME: Should translate token flags to a stable encoding.
1305 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Douglas Gregorc9490c02009-04-16 22:23:12 +00001307 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001308 Record.clear();
1309 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001310 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001311 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001312
1313 // If the preprocessor has a preprocessing record, emit it.
1314 unsigned NumPreprocessingRecords = 0;
1315 if (PPRec) {
1316 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1317 E != EEnd; ++E) {
1318 Record.clear();
1319
1320 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1321 Record.push_back(NumPreprocessingRecords++);
1322 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1323 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1324 AddIdentifierRef(MI->getName(), Record);
1325 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1326 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1327 continue;
1328 }
1329
1330 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1331 // Record this macro definition's location.
1332 pch::IdentID ID = getMacroDefinitionID(MD);
1333 if (ID != MacroDefinitionOffsets.size()) {
1334 if (ID > MacroDefinitionOffsets.size())
1335 MacroDefinitionOffsets.resize(ID + 1);
1336
1337 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1338 } else
1339 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1340
1341 Record.push_back(NumPreprocessingRecords++);
1342 Record.push_back(ID);
1343 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1344 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1345 AddIdentifierRef(MD->getName(), Record);
1346 AddSourceLocation(MD->getLocation(), Record);
1347 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1348 continue;
1349 }
1350 }
1351 }
1352
Douglas Gregorc9490c02009-04-16 22:23:12 +00001353 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001354
1355 // Write the offsets table for the preprocessing record.
1356 if (NumPreprocessingRecords > 0) {
1357 // Write the offsets table for identifier IDs.
1358 using namespace llvm;
1359 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1360 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1361 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1362 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1364 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1365
1366 Record.clear();
1367 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1368 Record.push_back(NumPreprocessingRecords);
1369 Record.push_back(MacroDefinitionOffsets.size());
1370 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1371 (const char *)&MacroDefinitionOffsets.front(),
1372 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1373 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001374}
1375
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001376//===----------------------------------------------------------------------===//
1377// Type Serialization
1378//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001379
Douglas Gregor2cf26342009-04-09 22:27:44 +00001380/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001381void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001382 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001383 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001384 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Douglas Gregor2cf26342009-04-09 22:27:44 +00001386 // Record the offset for this type.
1387 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001388 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001389 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1390 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001391 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001392 }
1393
1394 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Douglas Gregor2cf26342009-04-09 22:27:44 +00001396 // Emit the type's representation.
1397 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001398
Douglas Gregora4923eb2009-11-16 21:35:15 +00001399 if (T.hasLocalNonFastQualifiers()) {
1400 Qualifiers Qs = T.getLocalQualifiers();
1401 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001402 Record.push_back(Qs.getAsOpaqueValue());
1403 W.Code = pch::TYPE_EXT_QUAL;
1404 } else {
1405 switch (T->getTypeClass()) {
1406 // For all of the concrete, non-dependent types, call the
1407 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001408#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001409 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001410#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001411#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001412 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001413 }
1414
1415 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001416 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001417
1418 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001419 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001420}
1421
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001422//===----------------------------------------------------------------------===//
1423// Declaration Serialization
1424//===----------------------------------------------------------------------===//
1425
Douglas Gregor2cf26342009-04-09 22:27:44 +00001426/// \brief Write the block containing all of the declaration IDs
1427/// lexically declared within the given DeclContext.
1428///
1429/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1430/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001431uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001432 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001433 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001434 return 0;
1435
Douglas Gregorc9490c02009-04-16 22:23:12 +00001436 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001437 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001438 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1439 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001440 AddDeclRef(*D, Record);
1441
Douglas Gregor25123082009-04-22 22:34:57 +00001442 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001443 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001444 return Offset;
1445}
1446
1447/// \brief Write the block containing all of the declaration IDs
1448/// visible from the given DeclContext.
1449///
1450/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1451/// bistream, or 0 if no block was written.
1452uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1453 DeclContext *DC) {
1454 if (DC->getPrimaryContext() != DC)
1455 return 0;
1456
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +00001457 // Since there is no name lookup into functions or methods, don't bother to
1458 // build a visible-declarations table for these entities.
1459 if (DC->isFunctionOrMethod())
1460 return 0;
1461
1462 // If not in C++, we perform name lookup for the translation unit via the
1463 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1464 // FIXME: In C++ we need the visible declarations in order to "see" the
1465 // friend declarations, is there a way to do this without writing the table ?
1466 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor58f06992009-04-18 15:49:20 +00001467 return 0;
1468
Douglas Gregor2cf26342009-04-09 22:27:44 +00001469 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001470 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001471
1472 // Serialize the contents of the mapping used for lookup. Note that,
1473 // although we have two very different code paths, the serialized
1474 // representation is the same for both cases: a declaration name,
1475 // followed by a size, followed by references to the visible
1476 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001477 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001478 RecordData Record;
1479 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001480 if (!Map)
1481 return 0;
1482
Douglas Gregor2cf26342009-04-09 22:27:44 +00001483 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1484 D != DEnd; ++D) {
1485 AddDeclarationName(D->first, Record);
1486 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1487 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001488 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001489 AddDeclRef(*Result.first, Record);
1490 }
1491
1492 if (Record.size() == 0)
1493 return 0;
1494
Douglas Gregorc9490c02009-04-16 22:23:12 +00001495 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001496 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001497 return Offset;
1498}
1499
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001500//===----------------------------------------------------------------------===//
1501// Global Method Pool and Selector Serialization
1502//===----------------------------------------------------------------------===//
1503
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001504namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001505// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001506class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001507 PCHWriter &Writer;
1508
1509public:
1510 typedef Selector key_type;
1511 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001513 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1514 typedef const data_type& data_type_ref;
1515
1516 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001518 static unsigned ComputeHash(Selector Sel) {
1519 unsigned N = Sel.getNumArgs();
1520 if (N == 0)
1521 ++N;
1522 unsigned R = 5381;
1523 for (unsigned I = 0; I != N; ++I)
1524 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001525 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001526 return R;
1527 }
Mike Stump1eb44332009-09-09 15:08:12 +00001528
1529 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001530 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1531 data_type_ref Methods) {
1532 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1533 clang::io::Emit16(Out, KeyLen);
1534 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001535 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001536 Method = Method->Next)
1537 if (Method->Method)
1538 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001539 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001540 Method = Method->Next)
1541 if (Method->Method)
1542 DataLen += 4;
1543 clang::io::Emit16(Out, DataLen);
1544 return std::make_pair(KeyLen, DataLen);
1545 }
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor83941df2009-04-25 17:48:32 +00001547 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001548 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001549 assert((Start >> 32) == 0 && "Selector key offset too large");
1550 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001551 unsigned N = Sel.getNumArgs();
1552 clang::io::Emit16(Out, N);
1553 if (N == 0)
1554 N = 1;
1555 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001556 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001557 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001560 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001561 data_type_ref Methods, unsigned DataLen) {
1562 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001563 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001564 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001565 Method = Method->Next)
1566 if (Method->Method)
1567 ++NumInstanceMethods;
1568
1569 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001570 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001571 Method = Method->Next)
1572 if (Method->Method)
1573 ++NumFactoryMethods;
1574
1575 clang::io::Emit16(Out, NumInstanceMethods);
1576 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001577 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001578 Method = Method->Next)
1579 if (Method->Method)
1580 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001581 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001582 Method = Method->Next)
1583 if (Method->Method)
1584 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001585
1586 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001587 }
1588};
1589} // end anonymous namespace
1590
1591/// \brief Write the method pool into the PCH file.
1592///
1593/// The method pool contains both instance and factory methods, stored
1594/// in an on-disk hash table indexed by the selector.
1595void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1596 using namespace llvm;
1597
1598 // Create and write out the blob that contains the instance and
1599 // factor method pools.
1600 bool Empty = true;
1601 {
1602 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001604 // Create the on-disk hash table representation. Start by
1605 // iterating through the instance method pool.
1606 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001607 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001608 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001609 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001610 InstanceEnd = SemaRef.InstanceMethodPool.end();
1611 Instance != InstanceEnd; ++Instance) {
1612 // Check whether there is a factory method with the same
1613 // selector.
1614 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1615 = SemaRef.FactoryMethodPool.find(Instance->first);
1616
1617 if (Factory == SemaRef.FactoryMethodPool.end())
1618 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001619 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001620 ObjCMethodList()));
1621 else
1622 Generator.insert(Instance->first,
1623 std::make_pair(Instance->second, Factory->second));
1624
Douglas Gregor83941df2009-04-25 17:48:32 +00001625 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001626 Empty = false;
1627 }
1628
1629 // Now iterate through the factory method pool, to pick up any
1630 // selectors that weren't already in the instance method pool.
1631 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001632 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001633 FactoryEnd = SemaRef.FactoryMethodPool.end();
1634 Factory != FactoryEnd; ++Factory) {
1635 // Check whether there is an instance method with the same
1636 // selector. If so, there is no work to do here.
1637 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1638 = SemaRef.InstanceMethodPool.find(Factory->first);
1639
Douglas Gregor83941df2009-04-25 17:48:32 +00001640 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001641 Generator.insert(Factory->first,
1642 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001643 ++NumSelectorsInMethodPool;
1644 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001645
1646 Empty = false;
1647 }
1648
Douglas Gregor83941df2009-04-25 17:48:32 +00001649 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001650 return;
1651
1652 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001653 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001654 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001655 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001656 {
1657 PCHMethodPoolTrait Trait(*this);
1658 llvm::raw_svector_ostream Out(MethodPool);
1659 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001660 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001661 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001662
1663 // For every selector that we have seen but which was not
1664 // written into the hash table, write the selector itself and
1665 // record it's offset.
1666 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1667 if (SelectorOffsets[I] == 0)
1668 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001669 }
1670
1671 // Create a blob abbreviation
1672 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1673 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1674 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001675 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1677 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1678
Douglas Gregor83941df2009-04-25 17:48:32 +00001679 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001680 RecordData Record;
1681 Record.push_back(pch::METHOD_POOL);
1682 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001683 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001684 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001685
1686 // Create a blob abbreviation for the selector table offsets.
1687 Abbrev = new BitCodeAbbrev();
1688 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1689 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1690 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1691 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1692
1693 // Write the selector offsets table.
1694 Record.clear();
1695 Record.push_back(pch::SELECTOR_OFFSETS);
1696 Record.push_back(SelectorOffsets.size());
1697 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1698 (const char *)&SelectorOffsets.front(),
1699 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001700 }
1701}
1702
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001703//===----------------------------------------------------------------------===//
1704// Identifier Table Serialization
1705//===----------------------------------------------------------------------===//
1706
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001707namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001708class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001709 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001710 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001711
Douglas Gregora92193e2009-04-28 21:18:29 +00001712 /// \brief Determines whether this is an "interesting" identifier
1713 /// that needs a full IdentifierInfo structure written into the hash
1714 /// table.
1715 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1716 return II->isPoisoned() ||
1717 II->isExtensionToken() ||
1718 II->hasMacroDefinition() ||
1719 II->getObjCOrBuiltinID() ||
1720 II->getFETokenInfo<void>();
1721 }
1722
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001723public:
1724 typedef const IdentifierInfo* key_type;
1725 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001727 typedef pch::IdentID data_type;
1728 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001729
1730 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001731 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001732
1733 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001734 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001735 }
Mike Stump1eb44332009-09-09 15:08:12 +00001736
1737 std::pair<unsigned,unsigned>
1738 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001739 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001740 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001741 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1742 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001743 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001744 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001745 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001746 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001747 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1748 DEnd = IdentifierResolver::end();
1749 D != DEnd; ++D)
1750 DataLen += sizeof(pch::DeclID);
1751 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001752 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001753 // We emit the key length after the data length so that every
1754 // string is preceded by a 16-bit length. This matches the PTH
1755 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001756 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001757 return std::make_pair(KeyLen, DataLen);
1758 }
Mike Stump1eb44332009-09-09 15:08:12 +00001759
1760 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001761 unsigned KeyLen) {
1762 // Record the location of the key data. This is used when generating
1763 // the mapping from persistent IDs to strings.
1764 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001765 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001766 }
Mike Stump1eb44332009-09-09 15:08:12 +00001767
1768 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001769 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001770 if (!isInterestingIdentifier(II)) {
1771 clang::io::Emit32(Out, ID << 1);
1772 return;
1773 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001774
Douglas Gregora92193e2009-04-28 21:18:29 +00001775 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001776 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001777 bool hasMacroDefinition =
1778 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001779 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001780 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001781 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1782 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1783 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1784 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001785 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001786
Douglas Gregor37e26842009-04-21 23:56:24 +00001787 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001788 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001789
Douglas Gregor668c1a42009-04-21 22:25:48 +00001790 // Emit the declaration IDs in reverse order, because the
1791 // IdentifierResolver provides the declarations as they would be
1792 // visible (e.g., the function "stat" would come before the struct
1793 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1794 // adds declarations to the end of the list (so we need to see the
1795 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001796 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001797 IdentifierResolver::end());
1798 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1799 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001800 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001801 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001802 }
1803};
1804} // end anonymous namespace
1805
Douglas Gregorafaf3082009-04-11 00:14:32 +00001806/// \brief Write the identifier table into the PCH file.
1807///
1808/// The identifier table consists of a blob containing string data
1809/// (the actual identifiers themselves) and a separate "offsets" index
1810/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001811void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001812 using namespace llvm;
1813
1814 // Create and write out the blob that contains the identifier
1815 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001816 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001817 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Douglas Gregor92b059e2009-04-28 20:33:11 +00001819 // Look for any identifiers that were named while processing the
1820 // headers, but are otherwise not needed. We add these to the hash
1821 // table to enable checking of the predefines buffer in the case
1822 // where the user adds new macro definitions when building the PCH
1823 // file.
1824 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1825 IDEnd = PP.getIdentifierTable().end();
1826 ID != IDEnd; ++ID)
1827 getIdentifierRef(ID->second);
1828
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001829 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001830 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001831 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1832 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1833 ID != IDEnd; ++ID) {
1834 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001835 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001836 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001837
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001838 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001839 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001840 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001841 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001842 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001843 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001844 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001845 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001846 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001847 }
1848
1849 // Create a blob abbreviation
1850 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1851 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001852 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001853 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001854 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001855
1856 // Write the identifier table
1857 RecordData Record;
1858 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001859 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001860 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001861 }
1862
1863 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001864 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1865 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1868 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1869
1870 RecordData Record;
1871 Record.push_back(pch::IDENTIFIER_OFFSET);
1872 Record.push_back(IdentifierOffsets.size());
1873 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1874 (const char *)&IdentifierOffsets.front(),
1875 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001876}
1877
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001878//===----------------------------------------------------------------------===//
1879// General Serialization Routines
1880//===----------------------------------------------------------------------===//
1881
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001882/// \brief Write a record containing the given attributes.
1883void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1884 RecordData Record;
1885 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001886 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001887 Record.push_back(Attr->isInherited());
1888 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001889 default:
1890 assert(0 && "Does not support PCH writing for this attribute yet!");
1891 break;
Sean Hunt387475d2010-06-16 23:43:53 +00001892 case attr::Alias:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001893 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1894 break;
1895
Sean Hunt387475d2010-06-16 23:43:53 +00001896 case attr::AlignMac68k:
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001897 break;
1898
Sean Hunt387475d2010-06-16 23:43:53 +00001899 case attr::Aligned:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001900 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1901 break;
1902
Sean Hunt387475d2010-06-16 23:43:53 +00001903 case attr::AlwaysInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001904 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Sean Hunt387475d2010-06-16 23:43:53 +00001906 case attr::AnalyzerNoReturn:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001907 break;
1908
Sean Hunt387475d2010-06-16 23:43:53 +00001909 case attr::Annotate:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001910 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1911 break;
1912
Sean Hunt387475d2010-06-16 23:43:53 +00001913 case attr::AsmLabel:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001914 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1915 break;
1916
Sean Hunt387475d2010-06-16 23:43:53 +00001917 case attr::BaseCheck:
Sean Hunt7725e672009-11-25 04:20:27 +00001918 break;
1919
Sean Hunt387475d2010-06-16 23:43:53 +00001920 case attr::Blocks:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001921 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1922 break;
1923
Sean Hunt387475d2010-06-16 23:43:53 +00001924 case attr::CDecl:
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001925 break;
1926
Sean Hunt387475d2010-06-16 23:43:53 +00001927 case attr::Cleanup:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001928 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1929 break;
1930
Sean Hunt387475d2010-06-16 23:43:53 +00001931 case attr::Const:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001932 break;
1933
Sean Hunt387475d2010-06-16 23:43:53 +00001934 case attr::Constructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001935 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1936 break;
1937
Sean Hunt387475d2010-06-16 23:43:53 +00001938 case attr::DLLExport:
1939 case attr::DLLImport:
1940 case attr::Deprecated:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001941 break;
1942
Sean Hunt387475d2010-06-16 23:43:53 +00001943 case attr::Destructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001944 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1945 break;
1946
Sean Hunt387475d2010-06-16 23:43:53 +00001947 case attr::FastCall:
1948 case attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001949 break;
1950
Sean Hunt387475d2010-06-16 23:43:53 +00001951 case attr::Format: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001952 const FormatAttr *Format = cast<FormatAttr>(Attr);
1953 AddString(Format->getType(), Record);
1954 Record.push_back(Format->getFormatIdx());
1955 Record.push_back(Format->getFirstArg());
1956 break;
1957 }
1958
Sean Hunt387475d2010-06-16 23:43:53 +00001959 case attr::FormatArg: {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001960 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1961 Record.push_back(Format->getFormatIdx());
1962 break;
1963 }
1964
Sean Hunt387475d2010-06-16 23:43:53 +00001965 case attr::Sentinel : {
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001966 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1967 Record.push_back(Sentinel->getSentinel());
1968 Record.push_back(Sentinel->getNullPos());
1969 break;
1970 }
Mike Stump1eb44332009-09-09 15:08:12 +00001971
Sean Hunt387475d2010-06-16 23:43:53 +00001972 case attr::GNUInline:
1973 case attr::Hiding:
1974 case attr::IBAction:
1975 case attr::IBOutlet:
1976 case attr::Malloc:
1977 case attr::NoDebug:
1978 case attr::NoInline:
1979 case attr::NoReturn:
1980 case attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001981 break;
1982
Sean Hunt387475d2010-06-16 23:43:53 +00001983 case attr::IBOutletCollection: {
Ted Kremenek857e9182010-05-19 17:38:06 +00001984 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
1985 AddDeclRef(ICA->getClass(), Record);
1986 break;
1987 }
1988
Sean Hunt387475d2010-06-16 23:43:53 +00001989 case attr::NonNull: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001990 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1991 Record.push_back(NonNull->size());
1992 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1993 break;
1994 }
1995
Sean Hunt387475d2010-06-16 23:43:53 +00001996 case attr::CFReturnsNotRetained:
1997 case attr::CFReturnsRetained:
1998 case attr::NSReturnsNotRetained:
1999 case attr::NSReturnsRetained:
2000 case attr::ObjCException:
2001 case attr::ObjCNSObject:
2002 case attr::Overloadable:
2003 case attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002004 break;
2005
Sean Hunt387475d2010-06-16 23:43:53 +00002006 case attr::MaxFieldAlignment:
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00002007 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002008 break;
2009
Sean Hunt387475d2010-06-16 23:43:53 +00002010 case attr::Packed:
Anders Carlssona860e752009-08-08 18:23:56 +00002011 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Sean Hunt387475d2010-06-16 23:43:53 +00002013 case attr::Pure:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002014 break;
2015
Sean Hunt387475d2010-06-16 23:43:53 +00002016 case attr::Regparm:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002017 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2018 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Sean Hunt387475d2010-06-16 23:43:53 +00002020 case attr::ReqdWorkGroupSize:
Nate Begeman6f3d8382009-06-26 06:32:41 +00002021 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2022 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2023 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2024 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002025
Sean Hunt387475d2010-06-16 23:43:53 +00002026 case attr::Section:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002027 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2028 break;
2029
Sean Hunt387475d2010-06-16 23:43:53 +00002030 case attr::StdCall:
2031 case attr::TransparentUnion:
2032 case attr::Unavailable:
2033 case attr::Unused:
2034 case attr::Used:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002035 break;
2036
Sean Hunt387475d2010-06-16 23:43:53 +00002037 case attr::Visibility:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002038 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00002039 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002040 break;
2041
Sean Hunt387475d2010-06-16 23:43:53 +00002042 case attr::WarnUnusedResult:
2043 case attr::Weak:
2044 case attr::WeakRef:
2045 case attr::WeakImport:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002046 break;
2047 }
2048 }
2049
Douglas Gregorc9490c02009-04-16 22:23:12 +00002050 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002051}
2052
2053void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2054 Record.push_back(Str.size());
2055 Record.insert(Record.end(), Str.begin(), Str.end());
2056}
2057
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002058/// \brief Note that the identifier II occurs at the given offset
2059/// within the identifier table.
2060void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002061 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002062}
2063
Douglas Gregor83941df2009-04-25 17:48:32 +00002064/// \brief Note that the selector Sel occurs at the given offset
2065/// within the method pool/selector table.
2066void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2067 unsigned ID = SelectorIDs[Sel];
2068 assert(ID && "Unknown selector");
2069 SelectorOffsets[ID - 1] = Offset;
2070}
2071
Mike Stump1eb44332009-09-09 15:08:12 +00002072PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2073 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002074 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
2075 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002076
Douglas Gregore650c8c2009-07-07 00:12:59 +00002077void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2078 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002079 using namespace llvm;
2080
Douglas Gregore7785042009-04-20 15:53:59 +00002081 ASTContext &Context = SemaRef.Context;
2082 Preprocessor &PP = SemaRef.PP;
2083
Douglas Gregor2cf26342009-04-09 22:27:44 +00002084 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002085 Stream.Emit((unsigned)'C', 8);
2086 Stream.Emit((unsigned)'P', 8);
2087 Stream.Emit((unsigned)'C', 8);
2088 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002090 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002091
2092 // The translation unit is the first declaration we'll emit.
2093 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002094 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002095
Douglas Gregor2deaea32009-04-22 18:49:13 +00002096 // Make sure that we emit IdentifierInfos (and any attached
2097 // declarations) for builtins.
2098 {
2099 IdentifierTable &Table = PP.getIdentifierTable();
2100 llvm::SmallVector<const char *, 32> BuiltinNames;
2101 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2102 Context.getLangOptions().NoBuiltin);
2103 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2104 getIdentifierRef(&Table.get(BuiltinNames[I]));
2105 }
2106
Chris Lattner63d65f82009-09-08 18:19:27 +00002107 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002108 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002109 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002110 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002111 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2112 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002113 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002114
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002115 // Build a record containing all of the static unused functions in this file.
2116 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002117 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002118 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002119
Douglas Gregor14c22f22009-04-22 22:18:58 +00002120 // Build a record containing all of the locally-scoped external
2121 // declarations in this header file. Generally, this record will be
2122 // empty.
2123 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002124 // FIXME: This is filling in the PCH file in densemap order which is
2125 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002126 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002127 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2128 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2129 TD != TDEnd; ++TD)
2130 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2131
Douglas Gregorb81c1702009-04-27 20:06:05 +00002132 // Build a record containing all of the ext_vector declarations.
2133 RecordData ExtVectorDecls;
2134 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2135 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2136
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002137 // Build a record containing all of the VTable uses information.
2138 RecordData VTableUses;
2139 VTableUses.push_back(SemaRef.VTableUses.size());
2140 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2141 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2142 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2143 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2144 }
2145
2146 // Build a record containing all of dynamic classes declarations.
2147 RecordData DynamicClasses;
2148 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2149 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2150
Douglas Gregor2cf26342009-04-09 22:27:44 +00002151 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002152 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002153 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002154 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002155 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002156 if (StatCalls && !isysroot)
2157 WriteStatCache(*StatCalls, isysroot);
2158 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002159 // Write the record of special types.
2160 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002162 AddTypeRef(Context.getBuiltinVaListType(), Record);
2163 AddTypeRef(Context.getObjCIdType(), Record);
2164 AddTypeRef(Context.getObjCSelType(), Record);
2165 AddTypeRef(Context.getObjCProtoType(), Record);
2166 AddTypeRef(Context.getObjCClassType(), Record);
2167 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2168 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2169 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002170 AddTypeRef(Context.getjmp_bufType(), Record);
2171 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002172 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2173 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002174 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002175 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002176 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2177 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002178 Record.push_back(Context.isInt128Installed());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002179 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Douglas Gregor366809a2009-04-26 03:49:13 +00002181 // Keep writing types and declarations until all types and
2182 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002183 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2184 WriteDeclsBlockAbbrevs();
2185 while (!DeclTypesToEmit.empty()) {
2186 DeclOrType DOT = DeclTypesToEmit.front();
2187 DeclTypesToEmit.pop();
2188 if (DOT.isType())
2189 WriteType(DOT.getType());
2190 else
2191 WriteDecl(Context, DOT.getDecl());
2192 }
2193 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002194
Douglas Gregor813a97b2009-10-17 17:25:45 +00002195 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002196 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002197 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002198
2199 // Write the type offsets array
2200 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2201 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2204 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2205 Record.clear();
2206 Record.push_back(pch::TYPE_OFFSET);
2207 Record.push_back(TypeOffsets.size());
2208 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002209 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002210 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002212 // Write the declaration offsets array
2213 Abbrev = new BitCodeAbbrev();
2214 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2217 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2218 Record.clear();
2219 Record.push_back(pch::DECL_OFFSET);
2220 Record.push_back(DeclOffsets.size());
2221 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002222 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002223 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002224
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002225 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002226 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002227 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002228
2229 // Write the record containing tentative definitions.
2230 if (!TentativeDefinitions.empty())
2231 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002232
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002233 // Write the record containing unused static functions.
2234 if (!UnusedStaticFuncs.empty())
2235 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002236
Douglas Gregor14c22f22009-04-22 22:18:58 +00002237 // Write the record containing locally-scoped external definitions.
2238 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002239 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002240 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002241
2242 // Write the record containing ext_vector type names.
2243 if (!ExtVectorDecls.empty())
2244 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002246 // Write the record containing VTable uses information.
2247 if (!VTableUses.empty())
2248 Stream.EmitRecord(pch::VTABLE_USES, VTableUses);
2249
2250 // Write the record containing dynamic classes declarations.
2251 if (!DynamicClasses.empty())
2252 Stream.EmitRecord(pch::DYNAMIC_CLASSES, DynamicClasses);
2253
Douglas Gregor3e1af842009-04-17 22:13:46 +00002254 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002255 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002256 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002257 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002258 Record.push_back(NumLexicalDeclContexts);
2259 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002260 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002261 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002262}
2263
2264void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2265 Record.push_back(Loc.getRawEncoding());
2266}
2267
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002268void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2269 AddSourceLocation(Range.getBegin(), Record);
2270 AddSourceLocation(Range.getEnd(), Record);
2271}
2272
Douglas Gregor2cf26342009-04-09 22:27:44 +00002273void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2274 Record.push_back(Value.getBitWidth());
2275 unsigned N = Value.getNumWords();
2276 const uint64_t* Words = Value.getRawData();
2277 for (unsigned I = 0; I != N; ++I)
2278 Record.push_back(Words[I]);
2279}
2280
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002281void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2282 Record.push_back(Value.isUnsigned());
2283 AddAPInt(Value, Record);
2284}
2285
Douglas Gregor17fc2232009-04-14 21:55:33 +00002286void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2287 AddAPInt(Value.bitcastToAPInt(), Record);
2288}
2289
Douglas Gregor2cf26342009-04-09 22:27:44 +00002290void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002291 Record.push_back(getIdentifierRef(II));
2292}
2293
2294pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2295 if (II == 0)
2296 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002297
2298 pch::IdentID &ID = IdentifierIDs[II];
2299 if (ID == 0)
2300 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002301 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002302}
2303
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002304pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2305 if (MD == 0)
2306 return 0;
2307
2308 pch::IdentID &ID = MacroDefinitions[MD];
2309 if (ID == 0)
2310 ID = MacroDefinitions.size();
2311 return ID;
2312}
2313
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002314void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2315 if (SelRef.getAsOpaquePtr() == 0) {
2316 Record.push_back(0);
2317 return;
2318 }
2319
2320 pch::SelectorID &SID = SelectorIDs[SelRef];
2321 if (SID == 0) {
2322 SID = SelectorIDs.size();
2323 SelVector.push_back(SelRef);
2324 }
2325 Record.push_back(SID);
2326}
2327
Chris Lattnerd2598362010-05-10 00:25:06 +00002328void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2329 AddDeclRef(Temp->getDestructor(), Record);
2330}
2331
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002332void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2333 const TemplateArgumentLocInfo &Arg,
2334 RecordData &Record) {
2335 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002336 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002337 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002338 break;
2339 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002340 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002341 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002342 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002343 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2344 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002345 break;
John McCall833ca992009-10-29 08:12:44 +00002346 case TemplateArgument::Null:
2347 case TemplateArgument::Integral:
2348 case TemplateArgument::Declaration:
2349 case TemplateArgument::Pack:
2350 break;
2351 }
2352}
2353
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002354void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2355 RecordData &Record) {
2356 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002357
2358 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2359 bool InfoHasSameExpr
2360 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2361 Record.push_back(InfoHasSameExpr);
2362 if (InfoHasSameExpr)
2363 return; // Avoid storing the same expr twice.
2364 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002365 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2366 Record);
2367}
2368
John McCalla93c9342009-12-07 02:54:59 +00002369void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2370 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002371 AddTypeRef(QualType(), Record);
2372 return;
2373 }
2374
John McCalla93c9342009-12-07 02:54:59 +00002375 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002376 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002377 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002378 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002379}
2380
Douglas Gregor2cf26342009-04-09 22:27:44 +00002381void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2382 if (T.isNull()) {
2383 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2384 return;
2385 }
2386
Douglas Gregora4923eb2009-11-16 21:35:15 +00002387 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002388 T.removeFastQualifiers();
2389
Douglas Gregora4923eb2009-11-16 21:35:15 +00002390 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002391 pch::TypeID &ID = TypeIDs[T];
2392 if (ID == 0) {
2393 // We haven't seen these qualifiers applied to this type before.
2394 // Assign it a new ID. This is the only time we enqueue a
2395 // qualified type, and it has no CV qualifiers.
2396 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002397 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002398 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002399
John McCall0953e762009-09-24 19:53:00 +00002400 // Encode the type qualifiers in the type reference.
2401 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2402 return;
2403 }
2404
Douglas Gregora4923eb2009-11-16 21:35:15 +00002405 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002406
Douglas Gregor2cf26342009-04-09 22:27:44 +00002407 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002408 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002409 switch (BT->getKind()) {
2410 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2411 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2412 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2413 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2414 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2415 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2416 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2417 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002418 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002419 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2420 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2421 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2422 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2423 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2424 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2425 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002426 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002427 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2428 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2429 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002430 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002431 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2432 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002433 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2434 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002435 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2436 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002437 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002438 case BuiltinType::UndeducedAuto:
2439 assert(0 && "Should not see undeduced auto here");
2440 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002441 }
2442
John McCall0953e762009-09-24 19:53:00 +00002443 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002444 return;
2445 }
2446
John McCall0953e762009-09-24 19:53:00 +00002447 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002448 if (ID == 0) {
2449 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002450 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002451 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002452 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002453 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002454
2455 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002456 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002457}
2458
2459void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2460 if (D == 0) {
2461 Record.push_back(0);
2462 return;
2463 }
2464
Douglas Gregor8038d512009-04-10 17:25:41 +00002465 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002466 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002467 // We haven't seen this declaration before. Give it a new ID and
2468 // enqueue it in the list of declarations to emit.
2469 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002470 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002471 }
2472
2473 Record.push_back(ID);
2474}
2475
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002476pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2477 if (D == 0)
2478 return 0;
2479
2480 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2481 return DeclIDs[D];
2482}
2483
Douglas Gregor2cf26342009-04-09 22:27:44 +00002484void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002485 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002486 Record.push_back(Name.getNameKind());
2487 switch (Name.getNameKind()) {
2488 case DeclarationName::Identifier:
2489 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2490 break;
2491
2492 case DeclarationName::ObjCZeroArgSelector:
2493 case DeclarationName::ObjCOneArgSelector:
2494 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002495 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002496 break;
2497
2498 case DeclarationName::CXXConstructorName:
2499 case DeclarationName::CXXDestructorName:
2500 case DeclarationName::CXXConversionFunctionName:
2501 AddTypeRef(Name.getCXXNameType(), Record);
2502 break;
2503
2504 case DeclarationName::CXXOperatorName:
2505 Record.push_back(Name.getCXXOverloadedOperator());
2506 break;
2507
Sean Hunt3e518bd2009-11-29 07:34:05 +00002508 case DeclarationName::CXXLiteralOperatorName:
2509 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2510 break;
2511
Douglas Gregor2cf26342009-04-09 22:27:44 +00002512 case DeclarationName::CXXUsingDirective:
2513 // No extra data to emit
2514 break;
2515 }
2516}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002517
2518void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2519 RecordData &Record) {
2520 // Nested name specifiers usually aren't too long. I think that 8 would
2521 // typically accomodate the vast majority.
2522 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2523
2524 // Push each of the NNS's onto a stack for serialization in reverse order.
2525 while (NNS) {
2526 NestedNames.push_back(NNS);
2527 NNS = NNS->getPrefix();
2528 }
2529
2530 Record.push_back(NestedNames.size());
2531 while(!NestedNames.empty()) {
2532 NNS = NestedNames.pop_back_val();
2533 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2534 Record.push_back(Kind);
2535 switch (Kind) {
2536 case NestedNameSpecifier::Identifier:
2537 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2538 break;
2539
2540 case NestedNameSpecifier::Namespace:
2541 AddDeclRef(NNS->getAsNamespace(), Record);
2542 break;
2543
2544 case NestedNameSpecifier::TypeSpec:
2545 case NestedNameSpecifier::TypeSpecWithTemplate:
2546 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2547 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2548 break;
2549
2550 case NestedNameSpecifier::Global:
2551 // Don't need to write an associated value.
2552 break;
2553 }
2554 }
2555}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002556
2557void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2558 TemplateName::NameKind Kind = Name.getKind();
2559 Record.push_back(Kind);
2560 switch (Kind) {
2561 case TemplateName::Template:
2562 AddDeclRef(Name.getAsTemplateDecl(), Record);
2563 break;
2564
2565 case TemplateName::OverloadedTemplate: {
2566 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2567 Record.push_back(OvT->size());
2568 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2569 I != E; ++I)
2570 AddDeclRef(*I, Record);
2571 break;
2572 }
2573
2574 case TemplateName::QualifiedTemplate: {
2575 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2576 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2577 Record.push_back(QualT->hasTemplateKeyword());
2578 AddDeclRef(QualT->getTemplateDecl(), Record);
2579 break;
2580 }
2581
2582 case TemplateName::DependentTemplate: {
2583 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2584 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2585 Record.push_back(DepT->isIdentifier());
2586 if (DepT->isIdentifier())
2587 AddIdentifierRef(DepT->getIdentifier(), Record);
2588 else
2589 Record.push_back(DepT->getOperator());
2590 break;
2591 }
2592 }
2593}
2594
2595void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2596 RecordData &Record) {
2597 Record.push_back(Arg.getKind());
2598 switch (Arg.getKind()) {
2599 case TemplateArgument::Null:
2600 break;
2601 case TemplateArgument::Type:
2602 AddTypeRef(Arg.getAsType(), Record);
2603 break;
2604 case TemplateArgument::Declaration:
2605 AddDeclRef(Arg.getAsDecl(), Record);
2606 break;
2607 case TemplateArgument::Integral:
2608 AddAPSInt(*Arg.getAsIntegral(), Record);
2609 AddTypeRef(Arg.getIntegralType(), Record);
2610 break;
2611 case TemplateArgument::Template:
2612 AddTemplateName(Arg.getAsTemplate(), Record);
2613 break;
2614 case TemplateArgument::Expression:
2615 AddStmt(Arg.getAsExpr());
2616 break;
2617 case TemplateArgument::Pack:
2618 Record.push_back(Arg.pack_size());
2619 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2620 I != E; ++I)
2621 AddTemplateArgument(*I, Record);
2622 break;
2623 }
2624}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002625
2626void
2627PCHWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
2628 RecordData &Record) {
2629 assert(TemplateParams && "No TemplateParams!");
2630 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2631 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2632 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2633 Record.push_back(TemplateParams->size());
2634 for (TemplateParameterList::const_iterator
2635 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2636 P != PEnd; ++P)
2637 AddDeclRef(*P, Record);
2638}
2639
2640/// \brief Emit a template argument list.
2641void
2642PCHWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
2643 RecordData &Record) {
2644 assert(TemplateArgs && "No TemplateArgs!");
2645 Record.push_back(TemplateArgs->flat_size());
2646 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2647 AddTemplateArgument(TemplateArgs->get(i), Record);
2648}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00002649
2650
2651void
2652PCHWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
2653 Record.push_back(Set.size());
2654 for (UnresolvedSetImpl::const_iterator
2655 I = Set.begin(), E = Set.end(); I != E; ++I) {
2656 AddDeclRef(I.getDecl(), Record);
2657 Record.push_back(I.getAccess());
2658 }
2659}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00002660
2661void PCHWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
2662 RecordData &Record) {
2663 Record.push_back(Base.isVirtual());
2664 Record.push_back(Base.isBaseOfClass());
2665 Record.push_back(Base.getAccessSpecifierAsWritten());
2666 AddTypeRef(Base.getType(), Record);
2667 AddSourceRange(Base.getSourceRange(), Record);
2668}