blob: 18ceef99c98aefdb0c7ff29e9699d81940324686 [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);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000631 RECORD(CHAINED_METADATA);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000632
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000634 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000635 RECORD(SM_SLOC_FILE_ENTRY);
636 RECORD(SM_SLOC_BUFFER_ENTRY);
637 RECORD(SM_SLOC_BUFFER_BLOB);
638 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
639 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000641 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000642 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000643 RECORD(PP_MACRO_OBJECT_LIKE);
644 RECORD(PP_MACRO_FUNCTION_LIKE);
645 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000646 RECORD(PP_MACRO_INSTANTIATION);
647 RECORD(PP_MACRO_DEFINITION);
648
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000649 // Decls and Types block.
650 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000651 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000652 RECORD(TYPE_COMPLEX);
653 RECORD(TYPE_POINTER);
654 RECORD(TYPE_BLOCK_POINTER);
655 RECORD(TYPE_LVALUE_REFERENCE);
656 RECORD(TYPE_RVALUE_REFERENCE);
657 RECORD(TYPE_MEMBER_POINTER);
658 RECORD(TYPE_CONSTANT_ARRAY);
659 RECORD(TYPE_INCOMPLETE_ARRAY);
660 RECORD(TYPE_VARIABLE_ARRAY);
661 RECORD(TYPE_VECTOR);
662 RECORD(TYPE_EXT_VECTOR);
663 RECORD(TYPE_FUNCTION_PROTO);
664 RECORD(TYPE_FUNCTION_NO_PROTO);
665 RECORD(TYPE_TYPEDEF);
666 RECORD(TYPE_TYPEOF_EXPR);
667 RECORD(TYPE_TYPEOF);
668 RECORD(TYPE_RECORD);
669 RECORD(TYPE_ENUM);
670 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000671 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000672 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000673 RECORD(DECL_ATTR);
674 RECORD(DECL_TRANSLATION_UNIT);
675 RECORD(DECL_TYPEDEF);
676 RECORD(DECL_ENUM);
677 RECORD(DECL_RECORD);
678 RECORD(DECL_ENUM_CONSTANT);
679 RECORD(DECL_FUNCTION);
680 RECORD(DECL_OBJC_METHOD);
681 RECORD(DECL_OBJC_INTERFACE);
682 RECORD(DECL_OBJC_PROTOCOL);
683 RECORD(DECL_OBJC_IVAR);
684 RECORD(DECL_OBJC_AT_DEFS_FIELD);
685 RECORD(DECL_OBJC_CLASS);
686 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
687 RECORD(DECL_OBJC_CATEGORY);
688 RECORD(DECL_OBJC_CATEGORY_IMPL);
689 RECORD(DECL_OBJC_IMPLEMENTATION);
690 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
691 RECORD(DECL_OBJC_PROPERTY);
692 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000693 RECORD(DECL_FIELD);
694 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000695 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000696 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000697 RECORD(DECL_FILE_SCOPE_ASM);
698 RECORD(DECL_BLOCK);
699 RECORD(DECL_CONTEXT_LEXICAL);
700 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000701 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000702 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000703#undef RECORD
704#undef BLOCK
705 Stream.ExitBlock();
706}
707
Douglas Gregore650c8c2009-07-07 00:12:59 +0000708/// \brief Adjusts the given filename to only write out the portion of the
709/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000710///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000711/// \param Filename the file name to adjust.
712///
713/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
714/// the returned filename will be adjusted by this system root.
715///
716/// \returns either the original filename (if it needs no adjustment) or the
717/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000718static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000719adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
720 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregore650c8c2009-07-07 00:12:59 +0000722 if (!isysroot)
723 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Douglas Gregore650c8c2009-07-07 00:12:59 +0000725 // Verify that the filename and the system root have the same prefix.
726 unsigned Pos = 0;
727 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
728 if (Filename[Pos] != isysroot[Pos])
729 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Douglas Gregore650c8c2009-07-07 00:12:59 +0000731 // We hit the end of the filename before we hit the end of the system root.
732 if (!Filename[Pos])
733 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Douglas Gregore650c8c2009-07-07 00:12:59 +0000735 // If the file name has a '/' at the current position, skip over the '/'.
736 // We distinguish sysroot-based includes from absolute includes by the
737 // absence of '/' at the beginning of sysroot-based includes.
738 if (Filename[Pos] == '/')
739 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Douglas Gregore650c8c2009-07-07 00:12:59 +0000741 return Filename + Pos;
742}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000743
Douglas Gregorab41e632009-04-27 22:23:34 +0000744/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000745void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000746 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000747
Douglas Gregore650c8c2009-07-07 00:12:59 +0000748 // Metadata
749 const TargetInfo &Target = Context.Target;
750 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
751 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
752 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
753 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
754 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
755 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
756 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
757 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
758 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Douglas Gregore650c8c2009-07-07 00:12:59 +0000760 RecordData Record;
761 Record.push_back(pch::METADATA);
762 Record.push_back(pch::VERSION_MAJOR);
763 Record.push_back(pch::VERSION_MINOR);
764 Record.push_back(CLANG_VERSION_MAJOR);
765 Record.push_back(CLANG_VERSION_MINOR);
766 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000767 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000768 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Douglas Gregorb64c1932009-05-12 01:31:05 +0000770 // Original file name
771 SourceManager &SM = Context.getSourceManager();
772 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
773 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
774 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
775 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
776 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
777
778 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000780 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000781
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000782 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000783 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000784 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000785 RecordData Record;
786 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000787 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000788 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000789
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000790 // Repository branch/version information.
791 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
792 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
793 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
794 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000795 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000796 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000797 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
798 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000799}
800
801/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000802void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
803 RecordData Record;
804 Record.push_back(LangOpts.Trigraphs);
805 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
806 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
807 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
808 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000809 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000810 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
811 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
812 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
813 Record.push_back(LangOpts.C99); // C99 Support
814 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
815 Record.push_back(LangOpts.CPlusPlus); // C++ Support
816 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000817 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000819 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
820 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000821 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000822 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000823 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000824 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000825 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000827 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000828 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
829 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000830 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000831 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000832 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000833
834 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
835 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
836 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
837
Chris Lattnerea5ce472009-04-27 07:35:58 +0000838 // Whether static initializers are protected by locks.
839 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000840 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000841 Record.push_back(LangOpts.Blocks); // block extension to C
842 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
843 // they are unused.
844 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
845 // (modulo the platform support).
846
Chris Lattnera4d71452010-06-26 21:25:03 +0000847 Record.push_back(LangOpts.getSignedOverflowBehavior());
848 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000849
850 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000851 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000852 // defined.
853 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
854 // opposed to __DYNAMIC__).
855 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
856
857 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
858 // used (instead of C99 semantics).
859 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000860 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
861 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000862 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
863 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000864 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000865 Record.push_back(LangOpts.getGCMode());
866 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000867 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000868 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000869 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000870 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000871 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000872 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000873}
874
Douglas Gregor14f79002009-04-10 03:52:48 +0000875//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000876// stat cache Serialization
877//===----------------------------------------------------------------------===//
878
879namespace {
880// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000881class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000882public:
883 typedef const char * key_type;
884 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000886 typedef std::pair<int, struct stat> data_type;
887 typedef const data_type& data_type_ref;
888
889 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000890 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
893 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000894 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
895 data_type_ref Data) {
896 unsigned StrLen = strlen(path);
897 clang::io::Emit16(Out, StrLen);
898 unsigned DataLen = 1; // result value
899 if (Data.first == 0)
900 DataLen += 4 + 4 + 2 + 8 + 8;
901 clang::io::Emit8(Out, DataLen);
902 return std::make_pair(StrLen + 1, DataLen);
903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000905 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
906 Out.write(path, KeyLen);
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000909 void EmitData(llvm::raw_ostream& Out, key_type_ref,
910 data_type_ref Data, unsigned DataLen) {
911 using namespace clang::io;
912 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000914 // Result of stat()
915 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000917 if (Data.first == 0) {
918 Emit32(Out, (uint32_t) Data.second.st_ino);
919 Emit32(Out, (uint32_t) Data.second.st_dev);
920 Emit16(Out, (uint16_t) Data.second.st_mode);
921 Emit64(Out, (uint64_t) Data.second.st_mtime);
922 Emit64(Out, (uint64_t) Data.second.st_size);
923 }
924
925 assert(Out.tell() - Start == DataLen && "Wrong data length");
926 }
927};
928} // end anonymous namespace
929
930/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000931void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
932 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000933 // Build the on-disk hash table containing information about every
934 // stat() call.
935 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
936 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000937 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000938 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000939 Stat != StatEnd; ++Stat, ++NumStatEntries) {
940 const char *Filename = Stat->first();
941 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
942 Generator.insert(Filename, Stat->second);
943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000945 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000946 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000947 uint32_t BucketOffset;
948 {
949 llvm::raw_svector_ostream Out(StatCacheData);
950 // Make sure that no bucket is at offset 0
951 clang::io::Emit32(Out, 0);
952 BucketOffset = Generator.Emit(Out);
953 }
954
955 // Create a blob abbreviation
956 using namespace llvm;
957 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
958 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
962 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
963
964 // Write the stat cache
965 RecordData Record;
966 Record.push_back(pch::STAT_CACHE);
967 Record.push_back(BucketOffset);
968 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000969 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000970}
971
972//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000973// Source Manager Serialization
974//===----------------------------------------------------------------------===//
975
976/// \brief Create an abbreviation for the SLocEntry that refers to a
977/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000978static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000979 using namespace llvm;
980 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
981 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000986 // FileEntry fields.
987 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000989 // HeaderFileInfo fields.
990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000995 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000996}
997
998/// \brief Create an abbreviation for the SLocEntry that refers to a
999/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001000static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001001 using namespace llvm;
1002 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1003 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001009 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001010}
1011
1012/// \brief Create an abbreviation for the SLocEntry that refers to a
1013/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001014static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001015 using namespace llvm;
1016 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1017 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001019 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001020}
1021
1022/// \brief Create an abbreviation for the SLocEntry that refers to an
1023/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001024static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001025 using namespace llvm;
1026 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1027 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001033 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001034}
1035
1036/// \brief Writes the block containing the serialized form of the
1037/// source manager.
1038///
1039/// TODO: We should probably use an on-disk hash table (stored in a
1040/// blob), indexed based on the file name, so that we only create
1041/// entries for files that we actually need. In the common case (no
1042/// errors), we probably won't have to create file entries for any of
1043/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001044void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001045 const Preprocessor &PP,
1046 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001047 RecordData Record;
1048
Chris Lattnerf04ad692009-04-10 17:16:57 +00001049 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001050 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001051
1052 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001053 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1054 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1055 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1056 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001057
Douglas Gregorbd945002009-04-13 16:31:14 +00001058 // Write the line table.
1059 if (SourceMgr.hasLineTable()) {
1060 LineTableInfo &LineTable = SourceMgr.getLineTable();
1061
1062 // Emit the file names
1063 Record.push_back(LineTable.getNumFilenames());
1064 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1065 // Emit the file name
1066 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001067 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001068 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1069 Record.push_back(FilenameLen);
1070 if (FilenameLen)
1071 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1072 }
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Douglas Gregorbd945002009-04-13 16:31:14 +00001074 // Emit the line entries
1075 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1076 L != LEnd; ++L) {
1077 // Emit the file ID
1078 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Douglas Gregorbd945002009-04-13 16:31:14 +00001080 // Emit the line entries
1081 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001082 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001083 LEEnd = L->second.end();
1084 LE != LEEnd; ++LE) {
1085 Record.push_back(LE->FileOffset);
1086 Record.push_back(LE->LineNo);
1087 Record.push_back(LE->FilenameID);
1088 Record.push_back((unsigned)LE->FileKind);
1089 Record.push_back(LE->IncludeOffset);
1090 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001091 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001092 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001093 }
1094
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001095 // Write out the source location entry table. We skip the first
1096 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001097 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001098 RecordData PreloadSLocs;
1099 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001100 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1101 // Get this source location entry.
1102 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001103
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001104 // Record the offset of this source-location entry.
1105 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1106
1107 // Figure out which record code to use.
1108 unsigned Code;
1109 if (SLoc->isFile()) {
1110 if (SLoc->getFile().getContentCache()->Entry)
1111 Code = pch::SM_SLOC_FILE_ENTRY;
1112 else
1113 Code = pch::SM_SLOC_BUFFER_ENTRY;
1114 } else
1115 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1116 Record.clear();
1117 Record.push_back(Code);
1118
1119 Record.push_back(SLoc->getOffset());
1120 if (SLoc->isFile()) {
1121 const SrcMgr::FileInfo &File = SLoc->getFile();
1122 Record.push_back(File.getIncludeLoc().getRawEncoding());
1123 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1124 Record.push_back(File.hasLineDirectives());
1125
1126 const SrcMgr::ContentCache *Content = File.getContentCache();
1127 if (Content->Entry) {
1128 // The source location entry is a file. The blob associated
1129 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Douglas Gregor2d52be52010-03-21 22:49:54 +00001131 // Emit size/modification time for this file.
1132 Record.push_back(Content->Entry->getSize());
1133 Record.push_back(Content->Entry->getModificationTime());
1134
Douglas Gregor12fab312010-03-16 16:35:32 +00001135 // Emit header-search information associated with this file.
1136 HeaderFileInfo HFI;
1137 HeaderSearch &HS = PP.getHeaderSearchInfo();
1138 if (Content->Entry->getUID() < HS.header_file_size())
1139 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1140 Record.push_back(HFI.isImport);
1141 Record.push_back(HFI.DirInfo);
1142 Record.push_back(HFI.NumIncludes);
1143 AddIdentifierRef(HFI.ControllingMacro, Record);
1144
Douglas Gregore650c8c2009-07-07 00:12:59 +00001145 // Turn the file name into an absolute path, if it isn't already.
1146 const char *Filename = Content->Entry->getName();
1147 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001148 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001149 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Douglas Gregore650c8c2009-07-07 00:12:59 +00001151 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001152 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001153
1154 // FIXME: For now, preload all file source locations, so that
1155 // we get the appropriate File entries in the reader. This is
1156 // a temporary measure.
1157 PreloadSLocs.push_back(SLocEntryOffsets.size());
1158 } else {
1159 // The source location entry is a buffer. The blob associated
1160 // with this entry contains the contents of the buffer.
1161
1162 // We add one to the size so that we capture the trailing NULL
1163 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1164 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001165 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001166 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001167 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001168 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1169 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001170 Record.clear();
1171 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1172 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001173 llvm::StringRef(Buffer->getBufferStart(),
1174 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001175
1176 if (strcmp(Name, "<built-in>") == 0)
1177 PreloadSLocs.push_back(SLocEntryOffsets.size());
1178 }
1179 } else {
1180 // The source location entry is an instantiation.
1181 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1182 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1183 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1184 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1185
1186 // Compute the token length for this macro expansion.
1187 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001188 if (I + 1 != N)
1189 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001190 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1191 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1192 }
1193 }
1194
Douglas Gregorc9490c02009-04-16 22:23:12 +00001195 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001196
1197 if (SLocEntryOffsets.empty())
1198 return;
1199
1200 // Write the source-location offsets table into the PCH block. This
1201 // table is used for lazily loading source-location information.
1202 using namespace llvm;
1203 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1204 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1208 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210 Record.clear();
1211 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1212 Record.push_back(SLocEntryOffsets.size());
1213 Record.push_back(SourceMgr.getNextOffset());
1214 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001215 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001216 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001217
1218 // Write the source location entry preloads array, telling the PCH
1219 // reader which source locations entries it should load eagerly.
1220 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001221}
1222
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001223//===----------------------------------------------------------------------===//
1224// Preprocessor Serialization
1225//===----------------------------------------------------------------------===//
1226
Chris Lattner0b1fb982009-04-10 17:15:23 +00001227/// \brief Writes the block containing the serialized form of the
1228/// preprocessor.
1229///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001230void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001231 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001232
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001233 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1234 if (PP.getCounterValue() != 0) {
1235 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001236 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001237 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001238 }
1239
1240 // Enter the preprocessor block.
1241 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001243 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1244 // FIXME: use diagnostics subsystem for localization etc.
1245 if (PP.SawDateOrTime())
1246 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001248 // Loop over all the macro definitions that are live at the end of the file,
1249 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001250 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001251 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1252 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001253 // FIXME: This emits macros in hash table order, we should do it in a stable
1254 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001255 MacroInfo *MI = I->second;
1256
1257 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1258 // been redefined by the header (in which case they are not isBuiltinMacro).
1259 if (MI->isBuiltinMacro())
1260 continue;
1261
Chris Lattner7356a312009-04-11 21:15:38 +00001262 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001263 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001264 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1265 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001267 unsigned Code;
1268 if (MI->isObjectLike()) {
1269 Code = pch::PP_MACRO_OBJECT_LIKE;
1270 } else {
1271 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001273 Record.push_back(MI->isC99Varargs());
1274 Record.push_back(MI->isGNUVarargs());
1275 Record.push_back(MI->getNumArgs());
1276 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1277 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001278 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001279 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001280
1281 // If we have a detailed preprocessing record, record the macro definition
1282 // ID that corresponds to this macro.
1283 if (PPRec)
1284 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1285
Douglas Gregorc9490c02009-04-16 22:23:12 +00001286 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001287 Record.clear();
1288
Chris Lattnerdf961c22009-04-10 18:08:30 +00001289 // Emit the tokens array.
1290 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1291 // Note that we know that the preprocessor does not have any annotation
1292 // tokens in it because they are created by the parser, and thus can't be
1293 // in a macro definition.
1294 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Chris Lattnerdf961c22009-04-10 18:08:30 +00001296 Record.push_back(Tok.getLocation().getRawEncoding());
1297 Record.push_back(Tok.getLength());
1298
Chris Lattnerdf961c22009-04-10 18:08:30 +00001299 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1300 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001301 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Chris Lattnerdf961c22009-04-10 18:08:30 +00001303 // FIXME: Should translate token kind to a stable encoding.
1304 Record.push_back(Tok.getKind());
1305 // FIXME: Should translate token flags to a stable encoding.
1306 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Douglas Gregorc9490c02009-04-16 22:23:12 +00001308 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001309 Record.clear();
1310 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001311 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001312 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001313
1314 // If the preprocessor has a preprocessing record, emit it.
1315 unsigned NumPreprocessingRecords = 0;
1316 if (PPRec) {
1317 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1318 E != EEnd; ++E) {
1319 Record.clear();
1320
1321 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1322 Record.push_back(NumPreprocessingRecords++);
1323 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1324 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1325 AddIdentifierRef(MI->getName(), Record);
1326 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1327 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1328 continue;
1329 }
1330
1331 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1332 // Record this macro definition's location.
1333 pch::IdentID ID = getMacroDefinitionID(MD);
1334 if (ID != MacroDefinitionOffsets.size()) {
1335 if (ID > MacroDefinitionOffsets.size())
1336 MacroDefinitionOffsets.resize(ID + 1);
1337
1338 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1339 } else
1340 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1341
1342 Record.push_back(NumPreprocessingRecords++);
1343 Record.push_back(ID);
1344 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1345 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1346 AddIdentifierRef(MD->getName(), Record);
1347 AddSourceLocation(MD->getLocation(), Record);
1348 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1349 continue;
1350 }
1351 }
1352 }
1353
Douglas Gregorc9490c02009-04-16 22:23:12 +00001354 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001355
1356 // Write the offsets table for the preprocessing record.
1357 if (NumPreprocessingRecords > 0) {
1358 // Write the offsets table for identifier IDs.
1359 using namespace llvm;
1360 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1361 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1362 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1365 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1366
1367 Record.clear();
1368 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1369 Record.push_back(NumPreprocessingRecords);
1370 Record.push_back(MacroDefinitionOffsets.size());
1371 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1372 (const char *)&MacroDefinitionOffsets.front(),
1373 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1374 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001375}
1376
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001377//===----------------------------------------------------------------------===//
1378// Type Serialization
1379//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001380
Douglas Gregor2cf26342009-04-09 22:27:44 +00001381/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001382void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001383 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001384 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001385 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Douglas Gregor2cf26342009-04-09 22:27:44 +00001387 // Record the offset for this type.
1388 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001389 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001390 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1391 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001392 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001393 }
1394
1395 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Douglas Gregor2cf26342009-04-09 22:27:44 +00001397 // Emit the type's representation.
1398 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001399
Douglas Gregora4923eb2009-11-16 21:35:15 +00001400 if (T.hasLocalNonFastQualifiers()) {
1401 Qualifiers Qs = T.getLocalQualifiers();
1402 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001403 Record.push_back(Qs.getAsOpaqueValue());
1404 W.Code = pch::TYPE_EXT_QUAL;
1405 } else {
1406 switch (T->getTypeClass()) {
1407 // For all of the concrete, non-dependent types, call the
1408 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001409#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001410 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001411#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001412#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001413 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001414 }
1415
1416 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001417 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001418
1419 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001420 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001421}
1422
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001423//===----------------------------------------------------------------------===//
1424// Declaration Serialization
1425//===----------------------------------------------------------------------===//
1426
Douglas Gregor2cf26342009-04-09 22:27:44 +00001427/// \brief Write the block containing all of the declaration IDs
1428/// lexically declared within the given DeclContext.
1429///
1430/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1431/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001432uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001433 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001434 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001435 return 0;
1436
Douglas Gregorc9490c02009-04-16 22:23:12 +00001437 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001438 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001439 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1440 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001441 AddDeclRef(*D, Record);
1442
Douglas Gregor25123082009-04-22 22:34:57 +00001443 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001444 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001445 return Offset;
1446}
1447
1448/// \brief Write the block containing all of the declaration IDs
1449/// visible from the given DeclContext.
1450///
1451/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1452/// bistream, or 0 if no block was written.
1453uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1454 DeclContext *DC) {
1455 if (DC->getPrimaryContext() != DC)
1456 return 0;
1457
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +00001458 // Since there is no name lookup into functions or methods, don't bother to
1459 // build a visible-declarations table for these entities.
1460 if (DC->isFunctionOrMethod())
1461 return 0;
1462
1463 // If not in C++, we perform name lookup for the translation unit via the
1464 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1465 // FIXME: In C++ we need the visible declarations in order to "see" the
1466 // friend declarations, is there a way to do this without writing the table ?
1467 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor58f06992009-04-18 15:49:20 +00001468 return 0;
1469
Douglas Gregor2cf26342009-04-09 22:27:44 +00001470 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001471 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001472
1473 // Serialize the contents of the mapping used for lookup. Note that,
1474 // although we have two very different code paths, the serialized
1475 // representation is the same for both cases: a declaration name,
1476 // followed by a size, followed by references to the visible
1477 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001478 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001479 RecordData Record;
1480 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001481 if (!Map)
1482 return 0;
1483
Douglas Gregor2cf26342009-04-09 22:27:44 +00001484 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1485 D != DEnd; ++D) {
1486 AddDeclarationName(D->first, Record);
1487 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1488 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001489 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001490 AddDeclRef(*Result.first, Record);
1491 }
1492
1493 if (Record.size() == 0)
1494 return 0;
1495
Douglas Gregorc9490c02009-04-16 22:23:12 +00001496 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001497 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001498 return Offset;
1499}
1500
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001501//===----------------------------------------------------------------------===//
1502// Global Method Pool and Selector Serialization
1503//===----------------------------------------------------------------------===//
1504
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001505namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001506// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001507class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001508 PCHWriter &Writer;
1509
1510public:
1511 typedef Selector key_type;
1512 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001514 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1515 typedef const data_type& data_type_ref;
1516
1517 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001519 static unsigned ComputeHash(Selector Sel) {
1520 unsigned N = Sel.getNumArgs();
1521 if (N == 0)
1522 ++N;
1523 unsigned R = 5381;
1524 for (unsigned I = 0; I != N; ++I)
1525 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001526 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001527 return R;
1528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
1530 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001531 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1532 data_type_ref Methods) {
1533 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1534 clang::io::Emit16(Out, KeyLen);
1535 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001536 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001537 Method = Method->Next)
1538 if (Method->Method)
1539 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001540 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001541 Method = Method->Next)
1542 if (Method->Method)
1543 DataLen += 4;
1544 clang::io::Emit16(Out, DataLen);
1545 return std::make_pair(KeyLen, DataLen);
1546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Douglas Gregor83941df2009-04-25 17:48:32 +00001548 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001549 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001550 assert((Start >> 32) == 0 && "Selector key offset too large");
1551 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001552 unsigned N = Sel.getNumArgs();
1553 clang::io::Emit16(Out, N);
1554 if (N == 0)
1555 N = 1;
1556 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001557 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001558 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001561 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001562 data_type_ref Methods, unsigned DataLen) {
1563 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001564 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001565 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001566 Method = Method->Next)
1567 if (Method->Method)
1568 ++NumInstanceMethods;
1569
1570 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001571 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001572 Method = Method->Next)
1573 if (Method->Method)
1574 ++NumFactoryMethods;
1575
1576 clang::io::Emit16(Out, NumInstanceMethods);
1577 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001578 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001579 Method = Method->Next)
1580 if (Method->Method)
1581 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001582 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001583 Method = Method->Next)
1584 if (Method->Method)
1585 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001586
1587 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001588 }
1589};
1590} // end anonymous namespace
1591
1592/// \brief Write the method pool into the PCH file.
1593///
1594/// The method pool contains both instance and factory methods, stored
1595/// in an on-disk hash table indexed by the selector.
1596void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1597 using namespace llvm;
1598
1599 // Create and write out the blob that contains the instance and
1600 // factor method pools.
1601 bool Empty = true;
1602 {
1603 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001605 // Create the on-disk hash table representation. Start by
1606 // iterating through the instance method pool.
1607 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001608 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001609 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001610 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001611 InstanceEnd = SemaRef.InstanceMethodPool.end();
1612 Instance != InstanceEnd; ++Instance) {
1613 // Check whether there is a factory method with the same
1614 // selector.
1615 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1616 = SemaRef.FactoryMethodPool.find(Instance->first);
1617
1618 if (Factory == SemaRef.FactoryMethodPool.end())
1619 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001620 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001621 ObjCMethodList()));
1622 else
1623 Generator.insert(Instance->first,
1624 std::make_pair(Instance->second, Factory->second));
1625
Douglas Gregor83941df2009-04-25 17:48:32 +00001626 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001627 Empty = false;
1628 }
1629
1630 // Now iterate through the factory method pool, to pick up any
1631 // selectors that weren't already in the instance method pool.
1632 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001633 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001634 FactoryEnd = SemaRef.FactoryMethodPool.end();
1635 Factory != FactoryEnd; ++Factory) {
1636 // Check whether there is an instance method with the same
1637 // selector. If so, there is no work to do here.
1638 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1639 = SemaRef.InstanceMethodPool.find(Factory->first);
1640
Douglas Gregor83941df2009-04-25 17:48:32 +00001641 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001642 Generator.insert(Factory->first,
1643 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001644 ++NumSelectorsInMethodPool;
1645 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001646
1647 Empty = false;
1648 }
1649
Douglas Gregor83941df2009-04-25 17:48:32 +00001650 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001651 return;
1652
1653 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001654 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001655 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001656 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001657 {
1658 PCHMethodPoolTrait Trait(*this);
1659 llvm::raw_svector_ostream Out(MethodPool);
1660 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001661 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001662 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001663
1664 // For every selector that we have seen but which was not
1665 // written into the hash table, write the selector itself and
1666 // record it's offset.
1667 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1668 if (SelectorOffsets[I] == 0)
1669 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001670 }
1671
1672 // Create a blob abbreviation
1673 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1674 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1675 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001677 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1678 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1679
Douglas Gregor83941df2009-04-25 17:48:32 +00001680 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001681 RecordData Record;
1682 Record.push_back(pch::METHOD_POOL);
1683 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001684 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001685 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001686
1687 // Create a blob abbreviation for the selector table offsets.
1688 Abbrev = new BitCodeAbbrev();
1689 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1690 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1691 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1692 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1693
1694 // Write the selector offsets table.
1695 Record.clear();
1696 Record.push_back(pch::SELECTOR_OFFSETS);
1697 Record.push_back(SelectorOffsets.size());
1698 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1699 (const char *)&SelectorOffsets.front(),
1700 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001701 }
1702}
1703
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001704//===----------------------------------------------------------------------===//
1705// Identifier Table Serialization
1706//===----------------------------------------------------------------------===//
1707
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001708namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001709class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001710 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001711 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001712
Douglas Gregora92193e2009-04-28 21:18:29 +00001713 /// \brief Determines whether this is an "interesting" identifier
1714 /// that needs a full IdentifierInfo structure written into the hash
1715 /// table.
1716 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1717 return II->isPoisoned() ||
1718 II->isExtensionToken() ||
1719 II->hasMacroDefinition() ||
1720 II->getObjCOrBuiltinID() ||
1721 II->getFETokenInfo<void>();
1722 }
1723
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001724public:
1725 typedef const IdentifierInfo* key_type;
1726 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001728 typedef pch::IdentID data_type;
1729 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001730
1731 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001732 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001733
1734 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001735 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001736 }
Mike Stump1eb44332009-09-09 15:08:12 +00001737
1738 std::pair<unsigned,unsigned>
1739 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001740 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001741 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001742 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1743 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001744 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001745 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001746 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001747 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001748 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1749 DEnd = IdentifierResolver::end();
1750 D != DEnd; ++D)
1751 DataLen += sizeof(pch::DeclID);
1752 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001753 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001754 // We emit the key length after the data length so that every
1755 // string is preceded by a 16-bit length. This matches the PTH
1756 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001757 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001758 return std::make_pair(KeyLen, DataLen);
1759 }
Mike Stump1eb44332009-09-09 15:08:12 +00001760
1761 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001762 unsigned KeyLen) {
1763 // Record the location of the key data. This is used when generating
1764 // the mapping from persistent IDs to strings.
1765 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001766 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001767 }
Mike Stump1eb44332009-09-09 15:08:12 +00001768
1769 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001770 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001771 if (!isInterestingIdentifier(II)) {
1772 clang::io::Emit32(Out, ID << 1);
1773 return;
1774 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001775
Douglas Gregora92193e2009-04-28 21:18:29 +00001776 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001777 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001778 bool hasMacroDefinition =
1779 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001780 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001781 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001782 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1783 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1784 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1785 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001786 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001787
Douglas Gregor37e26842009-04-21 23:56:24 +00001788 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001789 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001790
Douglas Gregor668c1a42009-04-21 22:25:48 +00001791 // Emit the declaration IDs in reverse order, because the
1792 // IdentifierResolver provides the declarations as they would be
1793 // visible (e.g., the function "stat" would come before the struct
1794 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1795 // adds declarations to the end of the list (so we need to see the
1796 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001797 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001798 IdentifierResolver::end());
1799 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1800 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001801 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001802 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001803 }
1804};
1805} // end anonymous namespace
1806
Douglas Gregorafaf3082009-04-11 00:14:32 +00001807/// \brief Write the identifier table into the PCH file.
1808///
1809/// The identifier table consists of a blob containing string data
1810/// (the actual identifiers themselves) and a separate "offsets" index
1811/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001812void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001813 using namespace llvm;
1814
1815 // Create and write out the blob that contains the identifier
1816 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001817 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001818 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Douglas Gregor92b059e2009-04-28 20:33:11 +00001820 // Look for any identifiers that were named while processing the
1821 // headers, but are otherwise not needed. We add these to the hash
1822 // table to enable checking of the predefines buffer in the case
1823 // where the user adds new macro definitions when building the PCH
1824 // file.
1825 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1826 IDEnd = PP.getIdentifierTable().end();
1827 ID != IDEnd; ++ID)
1828 getIdentifierRef(ID->second);
1829
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001830 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001831 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001832 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1833 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1834 ID != IDEnd; ++ID) {
1835 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001836 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001837 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001838
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001839 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001840 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001841 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001842 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001843 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001844 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001845 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001846 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001847 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001848 }
1849
1850 // Create a blob abbreviation
1851 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1852 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001853 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001854 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001855 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001856
1857 // Write the identifier table
1858 RecordData Record;
1859 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001860 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001861 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001862 }
1863
1864 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001865 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1866 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1869 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1870
1871 RecordData Record;
1872 Record.push_back(pch::IDENTIFIER_OFFSET);
1873 Record.push_back(IdentifierOffsets.size());
1874 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1875 (const char *)&IdentifierOffsets.front(),
1876 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001877}
1878
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001879//===----------------------------------------------------------------------===//
1880// General Serialization Routines
1881//===----------------------------------------------------------------------===//
1882
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001883/// \brief Write a record containing the given attributes.
1884void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1885 RecordData Record;
1886 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001887 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001888 Record.push_back(Attr->isInherited());
1889 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001890 default:
1891 assert(0 && "Does not support PCH writing for this attribute yet!");
1892 break;
Sean Hunt387475d2010-06-16 23:43:53 +00001893 case attr::Alias:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001894 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1895 break;
1896
Sean Hunt387475d2010-06-16 23:43:53 +00001897 case attr::AlignMac68k:
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001898 break;
1899
Sean Hunt387475d2010-06-16 23:43:53 +00001900 case attr::Aligned:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001901 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1902 break;
1903
Sean Hunt387475d2010-06-16 23:43:53 +00001904 case attr::AlwaysInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001905 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Sean Hunt387475d2010-06-16 23:43:53 +00001907 case attr::AnalyzerNoReturn:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001908 break;
1909
Sean Hunt387475d2010-06-16 23:43:53 +00001910 case attr::Annotate:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001911 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1912 break;
1913
Sean Hunt387475d2010-06-16 23:43:53 +00001914 case attr::AsmLabel:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001915 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1916 break;
1917
Sean Hunt387475d2010-06-16 23:43:53 +00001918 case attr::BaseCheck:
Sean Hunt7725e672009-11-25 04:20:27 +00001919 break;
1920
Sean Hunt387475d2010-06-16 23:43:53 +00001921 case attr::Blocks:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001922 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1923 break;
1924
Sean Hunt387475d2010-06-16 23:43:53 +00001925 case attr::CDecl:
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001926 break;
1927
Sean Hunt387475d2010-06-16 23:43:53 +00001928 case attr::Cleanup:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001929 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1930 break;
1931
Sean Hunt387475d2010-06-16 23:43:53 +00001932 case attr::Const:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001933 break;
1934
Sean Hunt387475d2010-06-16 23:43:53 +00001935 case attr::Constructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001936 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1937 break;
1938
Sean Hunt387475d2010-06-16 23:43:53 +00001939 case attr::DLLExport:
1940 case attr::DLLImport:
1941 case attr::Deprecated:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001942 break;
1943
Sean Hunt387475d2010-06-16 23:43:53 +00001944 case attr::Destructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001945 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1946 break;
1947
Sean Hunt387475d2010-06-16 23:43:53 +00001948 case attr::FastCall:
1949 case attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001950 break;
1951
Sean Hunt387475d2010-06-16 23:43:53 +00001952 case attr::Format: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001953 const FormatAttr *Format = cast<FormatAttr>(Attr);
1954 AddString(Format->getType(), Record);
1955 Record.push_back(Format->getFormatIdx());
1956 Record.push_back(Format->getFirstArg());
1957 break;
1958 }
1959
Sean Hunt387475d2010-06-16 23:43:53 +00001960 case attr::FormatArg: {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001961 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1962 Record.push_back(Format->getFormatIdx());
1963 break;
1964 }
1965
Sean Hunt387475d2010-06-16 23:43:53 +00001966 case attr::Sentinel : {
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001967 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1968 Record.push_back(Sentinel->getSentinel());
1969 Record.push_back(Sentinel->getNullPos());
1970 break;
1971 }
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Sean Hunt387475d2010-06-16 23:43:53 +00001973 case attr::GNUInline:
1974 case attr::Hiding:
1975 case attr::IBAction:
1976 case attr::IBOutlet:
1977 case attr::Malloc:
1978 case attr::NoDebug:
1979 case attr::NoInline:
1980 case attr::NoReturn:
1981 case attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001982 break;
1983
Sean Hunt387475d2010-06-16 23:43:53 +00001984 case attr::IBOutletCollection: {
Ted Kremenek857e9182010-05-19 17:38:06 +00001985 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
1986 AddDeclRef(ICA->getClass(), Record);
1987 break;
1988 }
1989
Sean Hunt387475d2010-06-16 23:43:53 +00001990 case attr::NonNull: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001991 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1992 Record.push_back(NonNull->size());
1993 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1994 break;
1995 }
1996
Sean Hunt387475d2010-06-16 23:43:53 +00001997 case attr::CFReturnsNotRetained:
1998 case attr::CFReturnsRetained:
1999 case attr::NSReturnsNotRetained:
2000 case attr::NSReturnsRetained:
2001 case attr::ObjCException:
2002 case attr::ObjCNSObject:
2003 case attr::Overloadable:
2004 case attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002005 break;
2006
Sean Hunt387475d2010-06-16 23:43:53 +00002007 case attr::MaxFieldAlignment:
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00002008 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002009 break;
2010
Sean Hunt387475d2010-06-16 23:43:53 +00002011 case attr::Packed:
Anders Carlssona860e752009-08-08 18:23:56 +00002012 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Sean Hunt387475d2010-06-16 23:43:53 +00002014 case attr::Pure:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002015 break;
2016
Sean Hunt387475d2010-06-16 23:43:53 +00002017 case attr::Regparm:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002018 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2019 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Sean Hunt387475d2010-06-16 23:43:53 +00002021 case attr::ReqdWorkGroupSize:
Nate Begeman6f3d8382009-06-26 06:32:41 +00002022 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2023 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2024 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2025 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002026
Sean Hunt387475d2010-06-16 23:43:53 +00002027 case attr::Section:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002028 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2029 break;
2030
Sean Hunt387475d2010-06-16 23:43:53 +00002031 case attr::StdCall:
2032 case attr::TransparentUnion:
2033 case attr::Unavailable:
2034 case attr::Unused:
2035 case attr::Used:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002036 break;
2037
Sean Hunt387475d2010-06-16 23:43:53 +00002038 case attr::Visibility:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002039 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00002040 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002041 break;
2042
Sean Hunt387475d2010-06-16 23:43:53 +00002043 case attr::WarnUnusedResult:
2044 case attr::Weak:
2045 case attr::WeakRef:
2046 case attr::WeakImport:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002047 break;
2048 }
2049 }
2050
Douglas Gregorc9490c02009-04-16 22:23:12 +00002051 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002052}
2053
2054void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2055 Record.push_back(Str.size());
2056 Record.insert(Record.end(), Str.begin(), Str.end());
2057}
2058
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002059/// \brief Note that the identifier II occurs at the given offset
2060/// within the identifier table.
2061void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002062 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002063}
2064
Douglas Gregor83941df2009-04-25 17:48:32 +00002065/// \brief Note that the selector Sel occurs at the given offset
2066/// within the method pool/selector table.
2067void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2068 unsigned ID = SelectorIDs[Sel];
2069 assert(ID && "Unknown selector");
2070 SelectorOffsets[ID - 1] = Offset;
2071}
2072
Mike Stump1eb44332009-09-09 15:08:12 +00002073PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2074 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002075 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
2076 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002077
Douglas Gregore650c8c2009-07-07 00:12:59 +00002078void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redla93e3b52010-07-08 22:01:51 +00002079 const PCHReader *Chain, const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002080 using namespace llvm;
2081
Douglas Gregore7785042009-04-20 15:53:59 +00002082 ASTContext &Context = SemaRef.Context;
2083 Preprocessor &PP = SemaRef.PP;
2084
Douglas Gregor2cf26342009-04-09 22:27:44 +00002085 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002086 Stream.Emit((unsigned)'C', 8);
2087 Stream.Emit((unsigned)'P', 8);
2088 Stream.Emit((unsigned)'C', 8);
2089 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002090
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002091 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002092
2093 // The translation unit is the first declaration we'll emit.
2094 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002095 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002096
Douglas Gregor2deaea32009-04-22 18:49:13 +00002097 // Make sure that we emit IdentifierInfos (and any attached
2098 // declarations) for builtins.
2099 {
2100 IdentifierTable &Table = PP.getIdentifierTable();
2101 llvm::SmallVector<const char *, 32> BuiltinNames;
2102 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2103 Context.getLangOptions().NoBuiltin);
2104 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2105 getIdentifierRef(&Table.get(BuiltinNames[I]));
2106 }
2107
Chris Lattner63d65f82009-09-08 18:19:27 +00002108 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002109 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002110 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002111 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002112 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2113 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002114 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002115
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002116 // Build a record containing all of the static unused functions in this file.
2117 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002118 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002119 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002120
Douglas Gregor14c22f22009-04-22 22:18:58 +00002121 // Build a record containing all of the locally-scoped external
2122 // declarations in this header file. Generally, this record will be
2123 // empty.
2124 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002125 // FIXME: This is filling in the PCH file in densemap order which is
2126 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002127 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002128 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2129 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2130 TD != TDEnd; ++TD)
2131 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2132
Douglas Gregorb81c1702009-04-27 20:06:05 +00002133 // Build a record containing all of the ext_vector declarations.
2134 RecordData ExtVectorDecls;
2135 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2136 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2137
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002138 // Build a record containing all of the VTable uses information.
2139 RecordData VTableUses;
2140 VTableUses.push_back(SemaRef.VTableUses.size());
2141 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2142 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2143 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2144 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2145 }
2146
2147 // Build a record containing all of dynamic classes declarations.
2148 RecordData DynamicClasses;
2149 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2150 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2151
Douglas Gregor2cf26342009-04-09 22:27:44 +00002152 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002153 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002154 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002155 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002156 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002157 if (StatCalls && !isysroot)
2158 WriteStatCache(*StatCalls, isysroot);
2159 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002160 // Write the record of special types.
2161 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002162
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002163 AddTypeRef(Context.getBuiltinVaListType(), Record);
2164 AddTypeRef(Context.getObjCIdType(), Record);
2165 AddTypeRef(Context.getObjCSelType(), Record);
2166 AddTypeRef(Context.getObjCProtoType(), Record);
2167 AddTypeRef(Context.getObjCClassType(), Record);
2168 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2169 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2170 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002171 AddTypeRef(Context.getjmp_bufType(), Record);
2172 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002173 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2174 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002175 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002176 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002177 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2178 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002179 Record.push_back(Context.isInt128Installed());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002180 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Douglas Gregor366809a2009-04-26 03:49:13 +00002182 // Keep writing types and declarations until all types and
2183 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002184 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2185 WriteDeclsBlockAbbrevs();
2186 while (!DeclTypesToEmit.empty()) {
2187 DeclOrType DOT = DeclTypesToEmit.front();
2188 DeclTypesToEmit.pop();
2189 if (DOT.isType())
2190 WriteType(DOT.getType());
2191 else
2192 WriteDecl(Context, DOT.getDecl());
2193 }
2194 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002195
Douglas Gregor813a97b2009-10-17 17:25:45 +00002196 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002197 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002198 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002199
2200 // Write the type offsets array
2201 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2202 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2205 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2206 Record.clear();
2207 Record.push_back(pch::TYPE_OFFSET);
2208 Record.push_back(TypeOffsets.size());
2209 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002210 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002211 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002212
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002213 // Write the declaration offsets array
2214 Abbrev = new BitCodeAbbrev();
2215 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2218 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2219 Record.clear();
2220 Record.push_back(pch::DECL_OFFSET);
2221 Record.push_back(DeclOffsets.size());
2222 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002223 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002224 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002225
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002226 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002227 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002228 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002229
2230 // Write the record containing tentative definitions.
2231 if (!TentativeDefinitions.empty())
2232 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002233
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002234 // Write the record containing unused static functions.
2235 if (!UnusedStaticFuncs.empty())
2236 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002237
Douglas Gregor14c22f22009-04-22 22:18:58 +00002238 // Write the record containing locally-scoped external definitions.
2239 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002240 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002241 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002242
2243 // Write the record containing ext_vector type names.
2244 if (!ExtVectorDecls.empty())
2245 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002247 // Write the record containing VTable uses information.
2248 if (!VTableUses.empty())
2249 Stream.EmitRecord(pch::VTABLE_USES, VTableUses);
2250
2251 // Write the record containing dynamic classes declarations.
2252 if (!DynamicClasses.empty())
2253 Stream.EmitRecord(pch::DYNAMIC_CLASSES, DynamicClasses);
2254
Douglas Gregor3e1af842009-04-17 22:13:46 +00002255 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002256 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002257 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002258 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002259 Record.push_back(NumLexicalDeclContexts);
2260 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002261 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002262 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002263}
2264
2265void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2266 Record.push_back(Loc.getRawEncoding());
2267}
2268
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002269void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2270 AddSourceLocation(Range.getBegin(), Record);
2271 AddSourceLocation(Range.getEnd(), Record);
2272}
2273
Douglas Gregor2cf26342009-04-09 22:27:44 +00002274void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2275 Record.push_back(Value.getBitWidth());
2276 unsigned N = Value.getNumWords();
2277 const uint64_t* Words = Value.getRawData();
2278 for (unsigned I = 0; I != N; ++I)
2279 Record.push_back(Words[I]);
2280}
2281
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002282void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2283 Record.push_back(Value.isUnsigned());
2284 AddAPInt(Value, Record);
2285}
2286
Douglas Gregor17fc2232009-04-14 21:55:33 +00002287void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2288 AddAPInt(Value.bitcastToAPInt(), Record);
2289}
2290
Douglas Gregor2cf26342009-04-09 22:27:44 +00002291void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002292 Record.push_back(getIdentifierRef(II));
2293}
2294
2295pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2296 if (II == 0)
2297 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002298
2299 pch::IdentID &ID = IdentifierIDs[II];
2300 if (ID == 0)
2301 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002302 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002303}
2304
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002305pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2306 if (MD == 0)
2307 return 0;
2308
2309 pch::IdentID &ID = MacroDefinitions[MD];
2310 if (ID == 0)
2311 ID = MacroDefinitions.size();
2312 return ID;
2313}
2314
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002315void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2316 if (SelRef.getAsOpaquePtr() == 0) {
2317 Record.push_back(0);
2318 return;
2319 }
2320
2321 pch::SelectorID &SID = SelectorIDs[SelRef];
2322 if (SID == 0) {
2323 SID = SelectorIDs.size();
2324 SelVector.push_back(SelRef);
2325 }
2326 Record.push_back(SID);
2327}
2328
Chris Lattnerd2598362010-05-10 00:25:06 +00002329void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2330 AddDeclRef(Temp->getDestructor(), Record);
2331}
2332
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002333void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2334 const TemplateArgumentLocInfo &Arg,
2335 RecordData &Record) {
2336 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002337 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002338 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002339 break;
2340 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002341 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002342 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002343 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002344 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2345 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002346 break;
John McCall833ca992009-10-29 08:12:44 +00002347 case TemplateArgument::Null:
2348 case TemplateArgument::Integral:
2349 case TemplateArgument::Declaration:
2350 case TemplateArgument::Pack:
2351 break;
2352 }
2353}
2354
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002355void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2356 RecordData &Record) {
2357 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002358
2359 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2360 bool InfoHasSameExpr
2361 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2362 Record.push_back(InfoHasSameExpr);
2363 if (InfoHasSameExpr)
2364 return; // Avoid storing the same expr twice.
2365 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002366 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2367 Record);
2368}
2369
John McCalla93c9342009-12-07 02:54:59 +00002370void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2371 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002372 AddTypeRef(QualType(), Record);
2373 return;
2374 }
2375
John McCalla93c9342009-12-07 02:54:59 +00002376 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002377 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002378 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002379 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002380}
2381
Douglas Gregor2cf26342009-04-09 22:27:44 +00002382void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2383 if (T.isNull()) {
2384 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2385 return;
2386 }
2387
Douglas Gregora4923eb2009-11-16 21:35:15 +00002388 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002389 T.removeFastQualifiers();
2390
Douglas Gregora4923eb2009-11-16 21:35:15 +00002391 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002392 pch::TypeID &ID = TypeIDs[T];
2393 if (ID == 0) {
2394 // We haven't seen these qualifiers applied to this type before.
2395 // Assign it a new ID. This is the only time we enqueue a
2396 // qualified type, and it has no CV qualifiers.
2397 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002398 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002399 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002400
John McCall0953e762009-09-24 19:53:00 +00002401 // Encode the type qualifiers in the type reference.
2402 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2403 return;
2404 }
2405
Douglas Gregora4923eb2009-11-16 21:35:15 +00002406 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002407
Douglas Gregor2cf26342009-04-09 22:27:44 +00002408 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002409 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002410 switch (BT->getKind()) {
2411 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2412 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2413 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2414 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2415 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2416 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2417 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2418 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002419 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002420 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2421 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2422 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2423 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2424 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2425 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2426 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002427 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002428 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2429 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2430 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002431 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002432 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2433 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002434 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2435 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002436 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2437 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002438 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002439 case BuiltinType::UndeducedAuto:
2440 assert(0 && "Should not see undeduced auto here");
2441 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002442 }
2443
John McCall0953e762009-09-24 19:53:00 +00002444 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002445 return;
2446 }
2447
John McCall0953e762009-09-24 19:53:00 +00002448 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002449 if (ID == 0) {
2450 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002451 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002452 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002453 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002454 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002455
2456 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002457 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002458}
2459
2460void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2461 if (D == 0) {
2462 Record.push_back(0);
2463 return;
2464 }
2465
Douglas Gregor8038d512009-04-10 17:25:41 +00002466 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002467 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002468 // We haven't seen this declaration before. Give it a new ID and
2469 // enqueue it in the list of declarations to emit.
2470 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002471 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002472 }
2473
2474 Record.push_back(ID);
2475}
2476
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002477pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2478 if (D == 0)
2479 return 0;
2480
2481 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2482 return DeclIDs[D];
2483}
2484
Douglas Gregor2cf26342009-04-09 22:27:44 +00002485void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002486 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002487 Record.push_back(Name.getNameKind());
2488 switch (Name.getNameKind()) {
2489 case DeclarationName::Identifier:
2490 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2491 break;
2492
2493 case DeclarationName::ObjCZeroArgSelector:
2494 case DeclarationName::ObjCOneArgSelector:
2495 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002496 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002497 break;
2498
2499 case DeclarationName::CXXConstructorName:
2500 case DeclarationName::CXXDestructorName:
2501 case DeclarationName::CXXConversionFunctionName:
2502 AddTypeRef(Name.getCXXNameType(), Record);
2503 break;
2504
2505 case DeclarationName::CXXOperatorName:
2506 Record.push_back(Name.getCXXOverloadedOperator());
2507 break;
2508
Sean Hunt3e518bd2009-11-29 07:34:05 +00002509 case DeclarationName::CXXLiteralOperatorName:
2510 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2511 break;
2512
Douglas Gregor2cf26342009-04-09 22:27:44 +00002513 case DeclarationName::CXXUsingDirective:
2514 // No extra data to emit
2515 break;
2516 }
2517}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002518
2519void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2520 RecordData &Record) {
2521 // Nested name specifiers usually aren't too long. I think that 8 would
2522 // typically accomodate the vast majority.
2523 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2524
2525 // Push each of the NNS's onto a stack for serialization in reverse order.
2526 while (NNS) {
2527 NestedNames.push_back(NNS);
2528 NNS = NNS->getPrefix();
2529 }
2530
2531 Record.push_back(NestedNames.size());
2532 while(!NestedNames.empty()) {
2533 NNS = NestedNames.pop_back_val();
2534 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2535 Record.push_back(Kind);
2536 switch (Kind) {
2537 case NestedNameSpecifier::Identifier:
2538 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2539 break;
2540
2541 case NestedNameSpecifier::Namespace:
2542 AddDeclRef(NNS->getAsNamespace(), Record);
2543 break;
2544
2545 case NestedNameSpecifier::TypeSpec:
2546 case NestedNameSpecifier::TypeSpecWithTemplate:
2547 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2548 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2549 break;
2550
2551 case NestedNameSpecifier::Global:
2552 // Don't need to write an associated value.
2553 break;
2554 }
2555 }
2556}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002557
2558void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2559 TemplateName::NameKind Kind = Name.getKind();
2560 Record.push_back(Kind);
2561 switch (Kind) {
2562 case TemplateName::Template:
2563 AddDeclRef(Name.getAsTemplateDecl(), Record);
2564 break;
2565
2566 case TemplateName::OverloadedTemplate: {
2567 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2568 Record.push_back(OvT->size());
2569 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2570 I != E; ++I)
2571 AddDeclRef(*I, Record);
2572 break;
2573 }
2574
2575 case TemplateName::QualifiedTemplate: {
2576 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2577 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2578 Record.push_back(QualT->hasTemplateKeyword());
2579 AddDeclRef(QualT->getTemplateDecl(), Record);
2580 break;
2581 }
2582
2583 case TemplateName::DependentTemplate: {
2584 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2585 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2586 Record.push_back(DepT->isIdentifier());
2587 if (DepT->isIdentifier())
2588 AddIdentifierRef(DepT->getIdentifier(), Record);
2589 else
2590 Record.push_back(DepT->getOperator());
2591 break;
2592 }
2593 }
2594}
2595
2596void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2597 RecordData &Record) {
2598 Record.push_back(Arg.getKind());
2599 switch (Arg.getKind()) {
2600 case TemplateArgument::Null:
2601 break;
2602 case TemplateArgument::Type:
2603 AddTypeRef(Arg.getAsType(), Record);
2604 break;
2605 case TemplateArgument::Declaration:
2606 AddDeclRef(Arg.getAsDecl(), Record);
2607 break;
2608 case TemplateArgument::Integral:
2609 AddAPSInt(*Arg.getAsIntegral(), Record);
2610 AddTypeRef(Arg.getIntegralType(), Record);
2611 break;
2612 case TemplateArgument::Template:
2613 AddTemplateName(Arg.getAsTemplate(), Record);
2614 break;
2615 case TemplateArgument::Expression:
2616 AddStmt(Arg.getAsExpr());
2617 break;
2618 case TemplateArgument::Pack:
2619 Record.push_back(Arg.pack_size());
2620 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2621 I != E; ++I)
2622 AddTemplateArgument(*I, Record);
2623 break;
2624 }
2625}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002626
2627void
2628PCHWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
2629 RecordData &Record) {
2630 assert(TemplateParams && "No TemplateParams!");
2631 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2632 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2633 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2634 Record.push_back(TemplateParams->size());
2635 for (TemplateParameterList::const_iterator
2636 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2637 P != PEnd; ++P)
2638 AddDeclRef(*P, Record);
2639}
2640
2641/// \brief Emit a template argument list.
2642void
2643PCHWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
2644 RecordData &Record) {
2645 assert(TemplateArgs && "No TemplateArgs!");
2646 Record.push_back(TemplateArgs->flat_size());
2647 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2648 AddTemplateArgument(TemplateArgs->get(i), Record);
2649}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00002650
2651
2652void
2653PCHWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
2654 Record.push_back(Set.size());
2655 for (UnresolvedSetImpl::const_iterator
2656 I = Set.begin(), E = Set.end(); I != E; ++I) {
2657 AddDeclRef(I.getDecl(), Record);
2658 Record.push_back(I.getAccess());
2659 }
2660}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00002661
2662void PCHWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
2663 RecordData &Record) {
2664 Record.push_back(Base.isVirtual());
2665 Record.push_back(Base.isBaseOfClass());
2666 Record.push_back(Base.getAccessSpecifierAsWritten());
2667 AddTypeRef(Base.getType(), Record);
2668 AddSourceRange(Base.getSourceRange(), Record);
2669}