blob: 52db0ee1e7eb7c756a3d6ad06ac782c48c532805 [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) {
197 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000198 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199 "Cannot serialize in the middle of a type definition");
200}
201
202void PCHTypeWriter::VisitRecordType(const RecordType *T) {
203 VisitTagType(T);
204 Code = pch::TYPE_RECORD;
205}
206
207void PCHTypeWriter::VisitEnumType(const EnumType *T) {
208 VisitTagType(T);
209 Code = pch::TYPE_ENUM;
210}
211
Mike Stump1eb44332009-09-09 15:08:12 +0000212void
John McCall49a832b2009-10-18 09:09:24 +0000213PCHTypeWriter::VisitSubstTemplateTypeParmType(
214 const SubstTemplateTypeParmType *T) {
215 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
216 Writer.AddTypeRef(T->getReplacementType(), Record);
217 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
218}
219
220void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221PCHTypeWriter::VisitTemplateSpecializationType(
222 const TemplateSpecializationType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000223 Writer.AddTemplateName(T->getTemplateName(), Record);
224 Record.push_back(T->getNumArgs());
225 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
226 ArgI != ArgE; ++ArgI)
227 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000228 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
229 : T->getCanonicalTypeInternal(),
230 Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000231 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
232}
233
234void
235PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000236 VisitArrayType(T);
237 Writer.AddStmt(T->getSizeExpr());
238 Writer.AddSourceRange(T->getBracketsRange(), Record);
239 Code = pch::TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000240}
241
242void
243PCHTypeWriter::VisitDependentSizedExtVectorType(
244 const DependentSizedExtVectorType *T) {
245 // FIXME: Serialize this type (C++ only)
246 assert(false && "Cannot serialize dependent sized extended vector types");
247}
248
249void
250PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
251 Record.push_back(T->getDepth());
252 Record.push_back(T->getIndex());
253 Record.push_back(T->isParameterPack());
254 Writer.AddIdentifierRef(T->getName(), Record);
255 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
256}
257
258void
259PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000260 Record.push_back(T->getKeyword());
261 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
262 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000263 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
264 : T->getCanonicalTypeInternal(),
265 Record);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000266 Code = pch::TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000267}
268
269void
270PCHTypeWriter::VisitDependentTemplateSpecializationType(
271 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000272 Record.push_back(T->getKeyword());
273 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
274 Writer.AddIdentifierRef(T->getIdentifier(), Record);
275 Record.push_back(T->getNumArgs());
276 for (DependentTemplateSpecializationType::iterator
277 I = T->begin(), E = T->end(); I != E; ++I)
278 Writer.AddTemplateArgument(*I, Record);
279 Code = pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000280}
281
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000282void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000283 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000284 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
285 Writer.AddTypeRef(T->getNamedType(), Record);
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000286 Code = pch::TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000287}
288
John McCall3cb0ebd2010-03-10 03:28:59 +0000289void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
290 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000291 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000292 Code = pch::TYPE_INJECTED_CLASS_NAME;
293}
294
Douglas Gregor2cf26342009-04-09 22:27:44 +0000295void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
296 Writer.AddDeclRef(T->getDecl(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000297 Code = pch::TYPE_OBJC_INTERFACE;
298}
299
300void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
301 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000302 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000303 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000304 E = T->qual_end(); I != E; ++I)
305 Writer.AddDeclRef(*I, Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000306 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000307}
308
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000309void
310PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000311 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000312 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000313}
314
John McCalla1ee0c52009-10-16 21:56:05 +0000315namespace {
316
317class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
318 PCHWriter &Writer;
319 PCHWriter::RecordData &Record;
320
321public:
322 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
323 : Writer(Writer), Record(Record) { }
324
John McCall51bd8032009-10-18 01:05:36 +0000325#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000326#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000327 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000328#include "clang/AST/TypeLocNodes.def"
329
John McCall51bd8032009-10-18 01:05:36 +0000330 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
331 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000332};
333
334}
335
John McCall51bd8032009-10-18 01:05:36 +0000336void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
337 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000338}
John McCall51bd8032009-10-18 01:05:36 +0000339void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000340 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
341 if (TL.needsExtraLocalData()) {
342 Record.push_back(TL.getWrittenTypeSpec());
343 Record.push_back(TL.getWrittenSignSpec());
344 Record.push_back(TL.getWrittenWidthSpec());
345 Record.push_back(TL.hasModeAttr());
346 }
John McCalla1ee0c52009-10-16 21:56:05 +0000347}
John McCall51bd8032009-10-18 01:05:36 +0000348void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
349 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000350}
John McCall51bd8032009-10-18 01:05:36 +0000351void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
352 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000353}
John McCall51bd8032009-10-18 01:05:36 +0000354void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
355 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000356}
John McCall51bd8032009-10-18 01:05:36 +0000357void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
358 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000359}
John McCall51bd8032009-10-18 01:05:36 +0000360void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000362}
John McCall51bd8032009-10-18 01:05:36 +0000363void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000365}
John McCall51bd8032009-10-18 01:05:36 +0000366void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
368 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
369 Record.push_back(TL.getSizeExpr() ? 1 : 0);
370 if (TL.getSizeExpr())
371 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000372}
John McCall51bd8032009-10-18 01:05:36 +0000373void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
374 VisitArrayTypeLoc(TL);
375}
376void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
377 VisitArrayTypeLoc(TL);
378}
379void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
380 VisitArrayTypeLoc(TL);
381}
382void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
383 DependentSizedArrayTypeLoc TL) {
384 VisitArrayTypeLoc(TL);
385}
386void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
387 DependentSizedExtVectorTypeLoc TL) {
388 Writer.AddSourceLocation(TL.getNameLoc(), Record);
389}
390void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getNameLoc(), Record);
392}
393void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
394 Writer.AddSourceLocation(TL.getNameLoc(), Record);
395}
396void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
397 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
398 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
399 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
400 Writer.AddDeclRef(TL.getArg(i), Record);
401}
402void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
403 VisitFunctionTypeLoc(TL);
404}
405void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
406 VisitFunctionTypeLoc(TL);
407}
John McCalled976492009-12-04 22:46:56 +0000408void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getNameLoc(), Record);
410}
John McCall51bd8032009-10-18 01:05:36 +0000411void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getNameLoc(), Record);
413}
414void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000415 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
416 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
417 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000418}
419void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000420 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
421 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
422 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
423 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000424}
425void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getNameLoc(), Record);
427}
428void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getNameLoc(), Record);
430}
431void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getNameLoc(), Record);
433}
John McCall51bd8032009-10-18 01:05:36 +0000434void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getNameLoc(), Record);
436}
John McCall49a832b2009-10-18 09:09:24 +0000437void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
438 SubstTemplateTypeParmTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
442 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000443 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
444 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
445 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
446 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000447 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
448 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000449}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000450void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000451 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
452 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000453}
John McCall3cb0ebd2010-03-10 03:28:59 +0000454void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getNameLoc(), Record);
456}
Douglas Gregor4714c122010-03-31 17:34:00 +0000457void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000458 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
459 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000460 Writer.AddSourceLocation(TL.getNameLoc(), Record);
461}
John McCall33500952010-06-11 00:33:02 +0000462void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
463 DependentTemplateSpecializationTypeLoc TL) {
464 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
465 Writer.AddSourceRange(TL.getQualifierRange(), Record);
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
468 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
469 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000470 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
471 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000472}
John McCall51bd8032009-10-18 01:05:36 +0000473void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000475}
476void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
477 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000478 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
479 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
480 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
481 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000482}
John McCall54e14c42009-10-22 22:37:11 +0000483void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000485}
John McCalla1ee0c52009-10-16 21:56:05 +0000486
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000487//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000488// PCHWriter Implementation
489//===----------------------------------------------------------------------===//
490
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000491static void EmitBlockID(unsigned ID, const char *Name,
492 llvm::BitstreamWriter &Stream,
493 PCHWriter::RecordData &Record) {
494 Record.clear();
495 Record.push_back(ID);
496 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
497
498 // Emit the block name if present.
499 if (Name == 0 || Name[0] == 0) return;
500 Record.clear();
501 while (*Name)
502 Record.push_back(*Name++);
503 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
504}
505
506static void EmitRecordID(unsigned ID, const char *Name,
507 llvm::BitstreamWriter &Stream,
508 PCHWriter::RecordData &Record) {
509 Record.clear();
510 Record.push_back(ID);
511 while (*Name)
512 Record.push_back(*Name++);
513 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000514}
515
516static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
517 PCHWriter::RecordData &Record) {
518#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
519 RECORD(STMT_STOP);
520 RECORD(STMT_NULL_PTR);
521 RECORD(STMT_NULL);
522 RECORD(STMT_COMPOUND);
523 RECORD(STMT_CASE);
524 RECORD(STMT_DEFAULT);
525 RECORD(STMT_LABEL);
526 RECORD(STMT_IF);
527 RECORD(STMT_SWITCH);
528 RECORD(STMT_WHILE);
529 RECORD(STMT_DO);
530 RECORD(STMT_FOR);
531 RECORD(STMT_GOTO);
532 RECORD(STMT_INDIRECT_GOTO);
533 RECORD(STMT_CONTINUE);
534 RECORD(STMT_BREAK);
535 RECORD(STMT_RETURN);
536 RECORD(STMT_DECL);
537 RECORD(STMT_ASM);
538 RECORD(EXPR_PREDEFINED);
539 RECORD(EXPR_DECL_REF);
540 RECORD(EXPR_INTEGER_LITERAL);
541 RECORD(EXPR_FLOATING_LITERAL);
542 RECORD(EXPR_IMAGINARY_LITERAL);
543 RECORD(EXPR_STRING_LITERAL);
544 RECORD(EXPR_CHARACTER_LITERAL);
545 RECORD(EXPR_PAREN);
546 RECORD(EXPR_UNARY_OPERATOR);
547 RECORD(EXPR_SIZEOF_ALIGN_OF);
548 RECORD(EXPR_ARRAY_SUBSCRIPT);
549 RECORD(EXPR_CALL);
550 RECORD(EXPR_MEMBER);
551 RECORD(EXPR_BINARY_OPERATOR);
552 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
553 RECORD(EXPR_CONDITIONAL_OPERATOR);
554 RECORD(EXPR_IMPLICIT_CAST);
555 RECORD(EXPR_CSTYLE_CAST);
556 RECORD(EXPR_COMPOUND_LITERAL);
557 RECORD(EXPR_EXT_VECTOR_ELEMENT);
558 RECORD(EXPR_INIT_LIST);
559 RECORD(EXPR_DESIGNATED_INIT);
560 RECORD(EXPR_IMPLICIT_VALUE_INIT);
561 RECORD(EXPR_VA_ARG);
562 RECORD(EXPR_ADDR_LABEL);
563 RECORD(EXPR_STMT);
564 RECORD(EXPR_TYPES_COMPATIBLE);
565 RECORD(EXPR_CHOOSE);
566 RECORD(EXPR_GNU_NULL);
567 RECORD(EXPR_SHUFFLE_VECTOR);
568 RECORD(EXPR_BLOCK);
569 RECORD(EXPR_BLOCK_DECL_REF);
570 RECORD(EXPR_OBJC_STRING_LITERAL);
571 RECORD(EXPR_OBJC_ENCODE);
572 RECORD(EXPR_OBJC_SELECTOR_EXPR);
573 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
574 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
575 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
576 RECORD(EXPR_OBJC_KVC_REF_EXPR);
577 RECORD(EXPR_OBJC_MESSAGE_EXPR);
578 RECORD(EXPR_OBJC_SUPER_EXPR);
579 RECORD(STMT_OBJC_FOR_COLLECTION);
580 RECORD(STMT_OBJC_CATCH);
581 RECORD(STMT_OBJC_FINALLY);
582 RECORD(STMT_OBJC_AT_TRY);
583 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
584 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000585 RECORD(EXPR_CXX_OPERATOR_CALL);
586 RECORD(EXPR_CXX_CONSTRUCT);
587 RECORD(EXPR_CXX_STATIC_CAST);
588 RECORD(EXPR_CXX_DYNAMIC_CAST);
589 RECORD(EXPR_CXX_REINTERPRET_CAST);
590 RECORD(EXPR_CXX_CONST_CAST);
591 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
592 RECORD(EXPR_CXX_BOOL_LITERAL);
593 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000594#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000595}
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000597void PCHWriter::WriteBlockInfoBlock() {
598 RecordData Record;
599 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Chris Lattner2f4efd12009-04-27 00:40:25 +0000601#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000602#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000604 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000605 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000606 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000607 RECORD(TYPE_OFFSET);
608 RECORD(DECL_OFFSET);
609 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000610 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000611 RECORD(IDENTIFIER_OFFSET);
612 RECORD(IDENTIFIER_TABLE);
613 RECORD(EXTERNAL_DEFINITIONS);
614 RECORD(SPECIAL_TYPES);
615 RECORD(STATISTICS);
616 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000617 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000618 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
619 RECORD(SELECTOR_OFFSETS);
620 RECORD(METHOD_POOL);
621 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000622 RECORD(SOURCE_LOCATION_OFFSETS);
623 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000624 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000625 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000626 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000627 RECORD(UNUSED_STATIC_FUNCS);
628 RECORD(MACRO_DEFINITION_OFFSETS);
629
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000630 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000631 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 RECORD(SM_SLOC_FILE_ENTRY);
633 RECORD(SM_SLOC_BUFFER_ENTRY);
634 RECORD(SM_SLOC_BUFFER_BLOB);
635 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
636 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000638 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000639 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000640 RECORD(PP_MACRO_OBJECT_LIKE);
641 RECORD(PP_MACRO_FUNCTION_LIKE);
642 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000643 RECORD(PP_MACRO_INSTANTIATION);
644 RECORD(PP_MACRO_DEFINITION);
645
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000646 // Decls and Types block.
647 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000648 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000649 RECORD(TYPE_COMPLEX);
650 RECORD(TYPE_POINTER);
651 RECORD(TYPE_BLOCK_POINTER);
652 RECORD(TYPE_LVALUE_REFERENCE);
653 RECORD(TYPE_RVALUE_REFERENCE);
654 RECORD(TYPE_MEMBER_POINTER);
655 RECORD(TYPE_CONSTANT_ARRAY);
656 RECORD(TYPE_INCOMPLETE_ARRAY);
657 RECORD(TYPE_VARIABLE_ARRAY);
658 RECORD(TYPE_VECTOR);
659 RECORD(TYPE_EXT_VECTOR);
660 RECORD(TYPE_FUNCTION_PROTO);
661 RECORD(TYPE_FUNCTION_NO_PROTO);
662 RECORD(TYPE_TYPEDEF);
663 RECORD(TYPE_TYPEOF_EXPR);
664 RECORD(TYPE_TYPEOF);
665 RECORD(TYPE_RECORD);
666 RECORD(TYPE_ENUM);
667 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000668 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000669 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000670 RECORD(DECL_ATTR);
671 RECORD(DECL_TRANSLATION_UNIT);
672 RECORD(DECL_TYPEDEF);
673 RECORD(DECL_ENUM);
674 RECORD(DECL_RECORD);
675 RECORD(DECL_ENUM_CONSTANT);
676 RECORD(DECL_FUNCTION);
677 RECORD(DECL_OBJC_METHOD);
678 RECORD(DECL_OBJC_INTERFACE);
679 RECORD(DECL_OBJC_PROTOCOL);
680 RECORD(DECL_OBJC_IVAR);
681 RECORD(DECL_OBJC_AT_DEFS_FIELD);
682 RECORD(DECL_OBJC_CLASS);
683 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
684 RECORD(DECL_OBJC_CATEGORY);
685 RECORD(DECL_OBJC_CATEGORY_IMPL);
686 RECORD(DECL_OBJC_IMPLEMENTATION);
687 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
688 RECORD(DECL_OBJC_PROPERTY);
689 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000690 RECORD(DECL_FIELD);
691 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000692 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000693 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000694 RECORD(DECL_FILE_SCOPE_ASM);
695 RECORD(DECL_BLOCK);
696 RECORD(DECL_CONTEXT_LEXICAL);
697 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000698 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000699 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000700#undef RECORD
701#undef BLOCK
702 Stream.ExitBlock();
703}
704
Douglas Gregore650c8c2009-07-07 00:12:59 +0000705/// \brief Adjusts the given filename to only write out the portion of the
706/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000707///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000708/// \param Filename the file name to adjust.
709///
710/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
711/// the returned filename will be adjusted by this system root.
712///
713/// \returns either the original filename (if it needs no adjustment) or the
714/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000715static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000716adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
717 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Douglas Gregore650c8c2009-07-07 00:12:59 +0000719 if (!isysroot)
720 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregore650c8c2009-07-07 00:12:59 +0000722 // Verify that the filename and the system root have the same prefix.
723 unsigned Pos = 0;
724 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
725 if (Filename[Pos] != isysroot[Pos])
726 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Douglas Gregore650c8c2009-07-07 00:12:59 +0000728 // We hit the end of the filename before we hit the end of the system root.
729 if (!Filename[Pos])
730 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregore650c8c2009-07-07 00:12:59 +0000732 // If the file name has a '/' at the current position, skip over the '/'.
733 // We distinguish sysroot-based includes from absolute includes by the
734 // absence of '/' at the beginning of sysroot-based includes.
735 if (Filename[Pos] == '/')
736 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Douglas Gregore650c8c2009-07-07 00:12:59 +0000738 return Filename + Pos;
739}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000740
Douglas Gregorab41e632009-04-27 22:23:34 +0000741/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000742void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000743 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000744
Douglas Gregore650c8c2009-07-07 00:12:59 +0000745 // Metadata
746 const TargetInfo &Target = Context.Target;
747 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
748 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
749 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
750 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
751 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
752 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
753 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
754 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
755 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Douglas Gregore650c8c2009-07-07 00:12:59 +0000757 RecordData Record;
758 Record.push_back(pch::METADATA);
759 Record.push_back(pch::VERSION_MAJOR);
760 Record.push_back(pch::VERSION_MINOR);
761 Record.push_back(CLANG_VERSION_MAJOR);
762 Record.push_back(CLANG_VERSION_MINOR);
763 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000764 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000765 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Douglas Gregorb64c1932009-05-12 01:31:05 +0000767 // Original file name
768 SourceManager &SM = Context.getSourceManager();
769 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
770 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
771 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
772 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
773 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
774
775 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000777 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000778
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000779 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000780 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000781 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000782 RecordData Record;
783 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000784 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000785 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000786
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000787 // Repository branch/version information.
788 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
789 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
790 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
791 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000792 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000793 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000794 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
795 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000796}
797
798/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000799void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
800 RecordData Record;
801 Record.push_back(LangOpts.Trigraphs);
802 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
803 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
804 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
805 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000806 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000807 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
808 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
809 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
810 Record.push_back(LangOpts.C99); // C99 Support
811 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
812 Record.push_back(LangOpts.CPlusPlus); // C++ Support
813 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000814 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000816 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
817 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000818 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000819 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000820 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000821 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000822 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000824 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000825 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
826 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000827 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000828 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000829 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000830
831 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
832 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
833 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
834
Chris Lattnerea5ce472009-04-27 07:35:58 +0000835 // Whether static initializers are protected by locks.
836 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000837 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000838 Record.push_back(LangOpts.Blocks); // block extension to C
839 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
840 // they are unused.
841 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
842 // (modulo the platform support).
843
Chris Lattnera4d71452010-06-26 21:25:03 +0000844 Record.push_back(LangOpts.getSignedOverflowBehavior());
845 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000846
847 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000848 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000849 // defined.
850 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
851 // opposed to __DYNAMIC__).
852 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
853
854 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
855 // used (instead of C99 semantics).
856 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000857 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
858 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000859 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
860 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000861 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000862 Record.push_back(LangOpts.getGCMode());
863 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000864 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000865 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000866 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000867 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000868 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000869 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000870}
871
Douglas Gregor14f79002009-04-10 03:52:48 +0000872//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000873// stat cache Serialization
874//===----------------------------------------------------------------------===//
875
876namespace {
877// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000878class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000879public:
880 typedef const char * key_type;
881 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000883 typedef std::pair<int, struct stat> data_type;
884 typedef const data_type& data_type_ref;
885
886 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000887 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000888 }
Mike Stump1eb44332009-09-09 15:08:12 +0000889
890 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000891 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
892 data_type_ref Data) {
893 unsigned StrLen = strlen(path);
894 clang::io::Emit16(Out, StrLen);
895 unsigned DataLen = 1; // result value
896 if (Data.first == 0)
897 DataLen += 4 + 4 + 2 + 8 + 8;
898 clang::io::Emit8(Out, DataLen);
899 return std::make_pair(StrLen + 1, DataLen);
900 }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000902 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
903 Out.write(path, KeyLen);
904 }
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000906 void EmitData(llvm::raw_ostream& Out, key_type_ref,
907 data_type_ref Data, unsigned DataLen) {
908 using namespace clang::io;
909 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000911 // Result of stat()
912 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000914 if (Data.first == 0) {
915 Emit32(Out, (uint32_t) Data.second.st_ino);
916 Emit32(Out, (uint32_t) Data.second.st_dev);
917 Emit16(Out, (uint16_t) Data.second.st_mode);
918 Emit64(Out, (uint64_t) Data.second.st_mtime);
919 Emit64(Out, (uint64_t) Data.second.st_size);
920 }
921
922 assert(Out.tell() - Start == DataLen && "Wrong data length");
923 }
924};
925} // end anonymous namespace
926
927/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000928void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
929 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000930 // Build the on-disk hash table containing information about every
931 // stat() call.
932 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
933 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000934 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000935 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000936 Stat != StatEnd; ++Stat, ++NumStatEntries) {
937 const char *Filename = Stat->first();
938 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
939 Generator.insert(Filename, Stat->second);
940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000942 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000943 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000944 uint32_t BucketOffset;
945 {
946 llvm::raw_svector_ostream Out(StatCacheData);
947 // Make sure that no bucket is at offset 0
948 clang::io::Emit32(Out, 0);
949 BucketOffset = Generator.Emit(Out);
950 }
951
952 // Create a blob abbreviation
953 using namespace llvm;
954 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
955 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
959 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
960
961 // Write the stat cache
962 RecordData Record;
963 Record.push_back(pch::STAT_CACHE);
964 Record.push_back(BucketOffset);
965 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000966 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000967}
968
969//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000970// Source Manager Serialization
971//===----------------------------------------------------------------------===//
972
973/// \brief Create an abbreviation for the SLocEntry that refers to a
974/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000975static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000976 using namespace llvm;
977 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
978 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
981 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000983 // FileEntry fields.
984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000986 // HeaderFileInfo fields.
987 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000992 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000993}
994
995/// \brief Create an abbreviation for the SLocEntry that refers to a
996/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000997static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000998 using namespace llvm;
999 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1000 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001006 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001007}
1008
1009/// \brief Create an abbreviation for the SLocEntry that refers to a
1010/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001011static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001012 using namespace llvm;
1013 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1014 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001016 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001017}
1018
1019/// \brief Create an abbreviation for the SLocEntry that refers to an
1020/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001021static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001022 using namespace llvm;
1023 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1024 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001030 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001031}
1032
1033/// \brief Writes the block containing the serialized form of the
1034/// source manager.
1035///
1036/// TODO: We should probably use an on-disk hash table (stored in a
1037/// blob), indexed based on the file name, so that we only create
1038/// entries for files that we actually need. In the common case (no
1039/// errors), we probably won't have to create file entries for any of
1040/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001041void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001042 const Preprocessor &PP,
1043 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001044 RecordData Record;
1045
Chris Lattnerf04ad692009-04-10 17:16:57 +00001046 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001047 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001048
1049 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001050 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1051 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1052 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1053 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001054
Douglas Gregorbd945002009-04-13 16:31:14 +00001055 // Write the line table.
1056 if (SourceMgr.hasLineTable()) {
1057 LineTableInfo &LineTable = SourceMgr.getLineTable();
1058
1059 // Emit the file names
1060 Record.push_back(LineTable.getNumFilenames());
1061 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1062 // Emit the file name
1063 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001064 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001065 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1066 Record.push_back(FilenameLen);
1067 if (FilenameLen)
1068 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Douglas Gregorbd945002009-04-13 16:31:14 +00001071 // Emit the line entries
1072 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1073 L != LEnd; ++L) {
1074 // Emit the file ID
1075 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Douglas Gregorbd945002009-04-13 16:31:14 +00001077 // Emit the line entries
1078 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001079 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001080 LEEnd = L->second.end();
1081 LE != LEEnd; ++LE) {
1082 Record.push_back(LE->FileOffset);
1083 Record.push_back(LE->LineNo);
1084 Record.push_back(LE->FilenameID);
1085 Record.push_back((unsigned)LE->FileKind);
1086 Record.push_back(LE->IncludeOffset);
1087 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001088 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001089 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001090 }
1091
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001092 // Write out the source location entry table. We skip the first
1093 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001094 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001095 RecordData PreloadSLocs;
1096 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001097 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1098 // Get this source location entry.
1099 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001100
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001101 // Record the offset of this source-location entry.
1102 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1103
1104 // Figure out which record code to use.
1105 unsigned Code;
1106 if (SLoc->isFile()) {
1107 if (SLoc->getFile().getContentCache()->Entry)
1108 Code = pch::SM_SLOC_FILE_ENTRY;
1109 else
1110 Code = pch::SM_SLOC_BUFFER_ENTRY;
1111 } else
1112 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1113 Record.clear();
1114 Record.push_back(Code);
1115
1116 Record.push_back(SLoc->getOffset());
1117 if (SLoc->isFile()) {
1118 const SrcMgr::FileInfo &File = SLoc->getFile();
1119 Record.push_back(File.getIncludeLoc().getRawEncoding());
1120 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1121 Record.push_back(File.hasLineDirectives());
1122
1123 const SrcMgr::ContentCache *Content = File.getContentCache();
1124 if (Content->Entry) {
1125 // The source location entry is a file. The blob associated
1126 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Douglas Gregor2d52be52010-03-21 22:49:54 +00001128 // Emit size/modification time for this file.
1129 Record.push_back(Content->Entry->getSize());
1130 Record.push_back(Content->Entry->getModificationTime());
1131
Douglas Gregor12fab312010-03-16 16:35:32 +00001132 // Emit header-search information associated with this file.
1133 HeaderFileInfo HFI;
1134 HeaderSearch &HS = PP.getHeaderSearchInfo();
1135 if (Content->Entry->getUID() < HS.header_file_size())
1136 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1137 Record.push_back(HFI.isImport);
1138 Record.push_back(HFI.DirInfo);
1139 Record.push_back(HFI.NumIncludes);
1140 AddIdentifierRef(HFI.ControllingMacro, Record);
1141
Douglas Gregore650c8c2009-07-07 00:12:59 +00001142 // Turn the file name into an absolute path, if it isn't already.
1143 const char *Filename = Content->Entry->getName();
1144 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001145 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001146 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Douglas Gregore650c8c2009-07-07 00:12:59 +00001148 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001149 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001150
1151 // FIXME: For now, preload all file source locations, so that
1152 // we get the appropriate File entries in the reader. This is
1153 // a temporary measure.
1154 PreloadSLocs.push_back(SLocEntryOffsets.size());
1155 } else {
1156 // The source location entry is a buffer. The blob associated
1157 // with this entry contains the contents of the buffer.
1158
1159 // We add one to the size so that we capture the trailing NULL
1160 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1161 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001162 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001163 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001164 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001165 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1166 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001167 Record.clear();
1168 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1169 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001170 llvm::StringRef(Buffer->getBufferStart(),
1171 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001172
1173 if (strcmp(Name, "<built-in>") == 0)
1174 PreloadSLocs.push_back(SLocEntryOffsets.size());
1175 }
1176 } else {
1177 // The source location entry is an instantiation.
1178 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1179 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1180 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1181 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1182
1183 // Compute the token length for this macro expansion.
1184 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001185 if (I + 1 != N)
1186 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001187 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1188 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1189 }
1190 }
1191
Douglas Gregorc9490c02009-04-16 22:23:12 +00001192 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001193
1194 if (SLocEntryOffsets.empty())
1195 return;
1196
1197 // Write the source-location offsets table into the PCH block. This
1198 // table is used for lazily loading source-location information.
1199 using namespace llvm;
1200 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1201 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1205 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001207 Record.clear();
1208 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1209 Record.push_back(SLocEntryOffsets.size());
1210 Record.push_back(SourceMgr.getNextOffset());
1211 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001212 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001213 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214
1215 // Write the source location entry preloads array, telling the PCH
1216 // reader which source locations entries it should load eagerly.
1217 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001218}
1219
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001220//===----------------------------------------------------------------------===//
1221// Preprocessor Serialization
1222//===----------------------------------------------------------------------===//
1223
Chris Lattner0b1fb982009-04-10 17:15:23 +00001224/// \brief Writes the block containing the serialized form of the
1225/// preprocessor.
1226///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001227void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001228 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001229
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001230 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1231 if (PP.getCounterValue() != 0) {
1232 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001233 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001234 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001235 }
1236
1237 // Enter the preprocessor block.
1238 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001240 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1241 // FIXME: use diagnostics subsystem for localization etc.
1242 if (PP.SawDateOrTime())
1243 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001245 // Loop over all the macro definitions that are live at the end of the file,
1246 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001247 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001248 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1249 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001250 // FIXME: This emits macros in hash table order, we should do it in a stable
1251 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001252 MacroInfo *MI = I->second;
1253
1254 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1255 // been redefined by the header (in which case they are not isBuiltinMacro).
1256 if (MI->isBuiltinMacro())
1257 continue;
1258
Chris Lattner7356a312009-04-11 21:15:38 +00001259 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001260 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001261 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1262 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001264 unsigned Code;
1265 if (MI->isObjectLike()) {
1266 Code = pch::PP_MACRO_OBJECT_LIKE;
1267 } else {
1268 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001270 Record.push_back(MI->isC99Varargs());
1271 Record.push_back(MI->isGNUVarargs());
1272 Record.push_back(MI->getNumArgs());
1273 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1274 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001275 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001276 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001277
1278 // If we have a detailed preprocessing record, record the macro definition
1279 // ID that corresponds to this macro.
1280 if (PPRec)
1281 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1282
Douglas Gregorc9490c02009-04-16 22:23:12 +00001283 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001284 Record.clear();
1285
Chris Lattnerdf961c22009-04-10 18:08:30 +00001286 // Emit the tokens array.
1287 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1288 // Note that we know that the preprocessor does not have any annotation
1289 // tokens in it because they are created by the parser, and thus can't be
1290 // in a macro definition.
1291 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Chris Lattnerdf961c22009-04-10 18:08:30 +00001293 Record.push_back(Tok.getLocation().getRawEncoding());
1294 Record.push_back(Tok.getLength());
1295
Chris Lattnerdf961c22009-04-10 18:08:30 +00001296 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1297 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001298 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Chris Lattnerdf961c22009-04-10 18:08:30 +00001300 // FIXME: Should translate token kind to a stable encoding.
1301 Record.push_back(Tok.getKind());
1302 // FIXME: Should translate token flags to a stable encoding.
1303 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Douglas Gregorc9490c02009-04-16 22:23:12 +00001305 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001306 Record.clear();
1307 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001308 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001309 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001310
1311 // If the preprocessor has a preprocessing record, emit it.
1312 unsigned NumPreprocessingRecords = 0;
1313 if (PPRec) {
1314 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1315 E != EEnd; ++E) {
1316 Record.clear();
1317
1318 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1319 Record.push_back(NumPreprocessingRecords++);
1320 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1321 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1322 AddIdentifierRef(MI->getName(), Record);
1323 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1324 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1325 continue;
1326 }
1327
1328 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1329 // Record this macro definition's location.
1330 pch::IdentID ID = getMacroDefinitionID(MD);
1331 if (ID != MacroDefinitionOffsets.size()) {
1332 if (ID > MacroDefinitionOffsets.size())
1333 MacroDefinitionOffsets.resize(ID + 1);
1334
1335 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1336 } else
1337 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1338
1339 Record.push_back(NumPreprocessingRecords++);
1340 Record.push_back(ID);
1341 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1342 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1343 AddIdentifierRef(MD->getName(), Record);
1344 AddSourceLocation(MD->getLocation(), Record);
1345 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1346 continue;
1347 }
1348 }
1349 }
1350
Douglas Gregorc9490c02009-04-16 22:23:12 +00001351 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001352
1353 // Write the offsets table for the preprocessing record.
1354 if (NumPreprocessingRecords > 0) {
1355 // Write the offsets table for identifier IDs.
1356 using namespace llvm;
1357 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1358 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1361 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1362 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1363
1364 Record.clear();
1365 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1366 Record.push_back(NumPreprocessingRecords);
1367 Record.push_back(MacroDefinitionOffsets.size());
1368 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1369 (const char *)&MacroDefinitionOffsets.front(),
1370 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1371 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001372}
1373
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001374//===----------------------------------------------------------------------===//
1375// Type Serialization
1376//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001377
Douglas Gregor2cf26342009-04-09 22:27:44 +00001378/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001379void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001380 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001381 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001382 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Douglas Gregor2cf26342009-04-09 22:27:44 +00001384 // Record the offset for this type.
1385 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001386 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001387 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1388 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001389 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001390 }
1391
1392 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Douglas Gregor2cf26342009-04-09 22:27:44 +00001394 // Emit the type's representation.
1395 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001396
Douglas Gregora4923eb2009-11-16 21:35:15 +00001397 if (T.hasLocalNonFastQualifiers()) {
1398 Qualifiers Qs = T.getLocalQualifiers();
1399 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001400 Record.push_back(Qs.getAsOpaqueValue());
1401 W.Code = pch::TYPE_EXT_QUAL;
1402 } else {
1403 switch (T->getTypeClass()) {
1404 // For all of the concrete, non-dependent types, call the
1405 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001406#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001407 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001408#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001409#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001410 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001411 }
1412
1413 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001414 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001415
1416 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001417 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001418}
1419
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001420//===----------------------------------------------------------------------===//
1421// Declaration Serialization
1422//===----------------------------------------------------------------------===//
1423
Douglas Gregor2cf26342009-04-09 22:27:44 +00001424/// \brief Write the block containing all of the declaration IDs
1425/// lexically declared within the given DeclContext.
1426///
1427/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1428/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001429uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001430 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001431 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001432 return 0;
1433
Douglas Gregorc9490c02009-04-16 22:23:12 +00001434 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001435 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001436 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1437 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001438 AddDeclRef(*D, Record);
1439
Douglas Gregor25123082009-04-22 22:34:57 +00001440 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001441 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001442 return Offset;
1443}
1444
1445/// \brief Write the block containing all of the declaration IDs
1446/// visible from the given DeclContext.
1447///
1448/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1449/// bistream, or 0 if no block was written.
1450uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1451 DeclContext *DC) {
1452 if (DC->getPrimaryContext() != DC)
1453 return 0;
1454
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +00001455 // Since there is no name lookup into functions or methods, don't bother to
1456 // build a visible-declarations table for these entities.
1457 if (DC->isFunctionOrMethod())
1458 return 0;
1459
1460 // If not in C++, we perform name lookup for the translation unit via the
1461 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1462 // FIXME: In C++ we need the visible declarations in order to "see" the
1463 // friend declarations, is there a way to do this without writing the table ?
1464 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor58f06992009-04-18 15:49:20 +00001465 return 0;
1466
Douglas Gregor2cf26342009-04-09 22:27:44 +00001467 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001468 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001469
1470 // Serialize the contents of the mapping used for lookup. Note that,
1471 // although we have two very different code paths, the serialized
1472 // representation is the same for both cases: a declaration name,
1473 // followed by a size, followed by references to the visible
1474 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001475 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001476 RecordData Record;
1477 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001478 if (!Map)
1479 return 0;
1480
Douglas Gregor2cf26342009-04-09 22:27:44 +00001481 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1482 D != DEnd; ++D) {
1483 AddDeclarationName(D->first, Record);
1484 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1485 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001486 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001487 AddDeclRef(*Result.first, Record);
1488 }
1489
1490 if (Record.size() == 0)
1491 return 0;
1492
Douglas Gregorc9490c02009-04-16 22:23:12 +00001493 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001494 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001495 return Offset;
1496}
1497
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001498//===----------------------------------------------------------------------===//
1499// Global Method Pool and Selector Serialization
1500//===----------------------------------------------------------------------===//
1501
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001502namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001503// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001504class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001505 PCHWriter &Writer;
1506
1507public:
1508 typedef Selector key_type;
1509 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001511 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1512 typedef const data_type& data_type_ref;
1513
1514 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001516 static unsigned ComputeHash(Selector Sel) {
1517 unsigned N = Sel.getNumArgs();
1518 if (N == 0)
1519 ++N;
1520 unsigned R = 5381;
1521 for (unsigned I = 0; I != N; ++I)
1522 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001523 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001524 return R;
1525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
1527 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001528 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1529 data_type_ref Methods) {
1530 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1531 clang::io::Emit16(Out, KeyLen);
1532 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001533 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001534 Method = Method->Next)
1535 if (Method->Method)
1536 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001537 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001538 Method = Method->Next)
1539 if (Method->Method)
1540 DataLen += 4;
1541 clang::io::Emit16(Out, DataLen);
1542 return std::make_pair(KeyLen, DataLen);
1543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Douglas Gregor83941df2009-04-25 17:48:32 +00001545 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001546 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001547 assert((Start >> 32) == 0 && "Selector key offset too large");
1548 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001549 unsigned N = Sel.getNumArgs();
1550 clang::io::Emit16(Out, N);
1551 if (N == 0)
1552 N = 1;
1553 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001554 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001555 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1556 }
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001558 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001559 data_type_ref Methods, unsigned DataLen) {
1560 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001561 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001562 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001563 Method = Method->Next)
1564 if (Method->Method)
1565 ++NumInstanceMethods;
1566
1567 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001568 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001569 Method = Method->Next)
1570 if (Method->Method)
1571 ++NumFactoryMethods;
1572
1573 clang::io::Emit16(Out, NumInstanceMethods);
1574 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001575 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001576 Method = Method->Next)
1577 if (Method->Method)
1578 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001579 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001580 Method = Method->Next)
1581 if (Method->Method)
1582 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001583
1584 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001585 }
1586};
1587} // end anonymous namespace
1588
1589/// \brief Write the method pool into the PCH file.
1590///
1591/// The method pool contains both instance and factory methods, stored
1592/// in an on-disk hash table indexed by the selector.
1593void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1594 using namespace llvm;
1595
1596 // Create and write out the blob that contains the instance and
1597 // factor method pools.
1598 bool Empty = true;
1599 {
1600 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001602 // Create the on-disk hash table representation. Start by
1603 // iterating through the instance method pool.
1604 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001605 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001606 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001607 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001608 InstanceEnd = SemaRef.InstanceMethodPool.end();
1609 Instance != InstanceEnd; ++Instance) {
1610 // Check whether there is a factory method with the same
1611 // selector.
1612 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1613 = SemaRef.FactoryMethodPool.find(Instance->first);
1614
1615 if (Factory == SemaRef.FactoryMethodPool.end())
1616 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001617 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001618 ObjCMethodList()));
1619 else
1620 Generator.insert(Instance->first,
1621 std::make_pair(Instance->second, Factory->second));
1622
Douglas Gregor83941df2009-04-25 17:48:32 +00001623 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001624 Empty = false;
1625 }
1626
1627 // Now iterate through the factory method pool, to pick up any
1628 // selectors that weren't already in the instance method pool.
1629 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001630 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001631 FactoryEnd = SemaRef.FactoryMethodPool.end();
1632 Factory != FactoryEnd; ++Factory) {
1633 // Check whether there is an instance method with the same
1634 // selector. If so, there is no work to do here.
1635 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1636 = SemaRef.InstanceMethodPool.find(Factory->first);
1637
Douglas Gregor83941df2009-04-25 17:48:32 +00001638 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001639 Generator.insert(Factory->first,
1640 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001641 ++NumSelectorsInMethodPool;
1642 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001643
1644 Empty = false;
1645 }
1646
Douglas Gregor83941df2009-04-25 17:48:32 +00001647 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001648 return;
1649
1650 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001651 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001652 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001653 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001654 {
1655 PCHMethodPoolTrait Trait(*this);
1656 llvm::raw_svector_ostream Out(MethodPool);
1657 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001658 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001659 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001660
1661 // For every selector that we have seen but which was not
1662 // written into the hash table, write the selector itself and
1663 // record it's offset.
1664 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1665 if (SelectorOffsets[I] == 0)
1666 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001667 }
1668
1669 // Create a blob abbreviation
1670 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1671 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1672 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001673 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001674 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1675 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1676
Douglas Gregor83941df2009-04-25 17:48:32 +00001677 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001678 RecordData Record;
1679 Record.push_back(pch::METHOD_POOL);
1680 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001681 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001682 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001683
1684 // Create a blob abbreviation for the selector table offsets.
1685 Abbrev = new BitCodeAbbrev();
1686 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1688 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1689 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1690
1691 // Write the selector offsets table.
1692 Record.clear();
1693 Record.push_back(pch::SELECTOR_OFFSETS);
1694 Record.push_back(SelectorOffsets.size());
1695 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1696 (const char *)&SelectorOffsets.front(),
1697 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001698 }
1699}
1700
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001701//===----------------------------------------------------------------------===//
1702// Identifier Table Serialization
1703//===----------------------------------------------------------------------===//
1704
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001705namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001706class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001707 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001708 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001709
Douglas Gregora92193e2009-04-28 21:18:29 +00001710 /// \brief Determines whether this is an "interesting" identifier
1711 /// that needs a full IdentifierInfo structure written into the hash
1712 /// table.
1713 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1714 return II->isPoisoned() ||
1715 II->isExtensionToken() ||
1716 II->hasMacroDefinition() ||
1717 II->getObjCOrBuiltinID() ||
1718 II->getFETokenInfo<void>();
1719 }
1720
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001721public:
1722 typedef const IdentifierInfo* key_type;
1723 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001725 typedef pch::IdentID data_type;
1726 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001727
1728 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001729 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001730
1731 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001732 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001733 }
Mike Stump1eb44332009-09-09 15:08:12 +00001734
1735 std::pair<unsigned,unsigned>
1736 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001737 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001738 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001739 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1740 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001741 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001742 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001743 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001744 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001745 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1746 DEnd = IdentifierResolver::end();
1747 D != DEnd; ++D)
1748 DataLen += sizeof(pch::DeclID);
1749 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001750 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001751 // We emit the key length after the data length so that every
1752 // string is preceded by a 16-bit length. This matches the PTH
1753 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001754 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001755 return std::make_pair(KeyLen, DataLen);
1756 }
Mike Stump1eb44332009-09-09 15:08:12 +00001757
1758 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001759 unsigned KeyLen) {
1760 // Record the location of the key data. This is used when generating
1761 // the mapping from persistent IDs to strings.
1762 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001763 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001764 }
Mike Stump1eb44332009-09-09 15:08:12 +00001765
1766 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001767 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001768 if (!isInterestingIdentifier(II)) {
1769 clang::io::Emit32(Out, ID << 1);
1770 return;
1771 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001772
Douglas Gregora92193e2009-04-28 21:18:29 +00001773 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001774 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001775 bool hasMacroDefinition =
1776 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001777 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001778 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001779 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1780 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1781 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1782 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001783 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001784
Douglas Gregor37e26842009-04-21 23:56:24 +00001785 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001786 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001787
Douglas Gregor668c1a42009-04-21 22:25:48 +00001788 // Emit the declaration IDs in reverse order, because the
1789 // IdentifierResolver provides the declarations as they would be
1790 // visible (e.g., the function "stat" would come before the struct
1791 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1792 // adds declarations to the end of the list (so we need to see the
1793 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001794 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001795 IdentifierResolver::end());
1796 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1797 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001798 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001799 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001800 }
1801};
1802} // end anonymous namespace
1803
Douglas Gregorafaf3082009-04-11 00:14:32 +00001804/// \brief Write the identifier table into the PCH file.
1805///
1806/// The identifier table consists of a blob containing string data
1807/// (the actual identifiers themselves) and a separate "offsets" index
1808/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001809void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001810 using namespace llvm;
1811
1812 // Create and write out the blob that contains the identifier
1813 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001814 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001815 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Douglas Gregor92b059e2009-04-28 20:33:11 +00001817 // Look for any identifiers that were named while processing the
1818 // headers, but are otherwise not needed. We add these to the hash
1819 // table to enable checking of the predefines buffer in the case
1820 // where the user adds new macro definitions when building the PCH
1821 // file.
1822 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1823 IDEnd = PP.getIdentifierTable().end();
1824 ID != IDEnd; ++ID)
1825 getIdentifierRef(ID->second);
1826
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001827 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001828 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001829 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1830 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1831 ID != IDEnd; ++ID) {
1832 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001833 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001834 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001835
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001836 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001837 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001838 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001839 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001840 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001841 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001842 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001843 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001844 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001845 }
1846
1847 // Create a blob abbreviation
1848 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1849 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001852 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001853
1854 // Write the identifier table
1855 RecordData Record;
1856 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001857 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001858 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001859 }
1860
1861 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001862 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1863 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1866 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1867
1868 RecordData Record;
1869 Record.push_back(pch::IDENTIFIER_OFFSET);
1870 Record.push_back(IdentifierOffsets.size());
1871 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1872 (const char *)&IdentifierOffsets.front(),
1873 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001874}
1875
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001876//===----------------------------------------------------------------------===//
1877// General Serialization Routines
1878//===----------------------------------------------------------------------===//
1879
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001880/// \brief Write a record containing the given attributes.
1881void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1882 RecordData Record;
1883 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001884 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001885 Record.push_back(Attr->isInherited());
1886 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001887 default:
1888 assert(0 && "Does not support PCH writing for this attribute yet!");
1889 break;
Sean Hunt387475d2010-06-16 23:43:53 +00001890 case attr::Alias:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001891 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1892 break;
1893
Sean Hunt387475d2010-06-16 23:43:53 +00001894 case attr::AlignMac68k:
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001895 break;
1896
Sean Hunt387475d2010-06-16 23:43:53 +00001897 case attr::Aligned:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001898 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1899 break;
1900
Sean Hunt387475d2010-06-16 23:43:53 +00001901 case attr::AlwaysInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001902 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Sean Hunt387475d2010-06-16 23:43:53 +00001904 case attr::AnalyzerNoReturn:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001905 break;
1906
Sean Hunt387475d2010-06-16 23:43:53 +00001907 case attr::Annotate:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001908 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1909 break;
1910
Sean Hunt387475d2010-06-16 23:43:53 +00001911 case attr::AsmLabel:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001912 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1913 break;
1914
Sean Hunt387475d2010-06-16 23:43:53 +00001915 case attr::BaseCheck:
Sean Hunt7725e672009-11-25 04:20:27 +00001916 break;
1917
Sean Hunt387475d2010-06-16 23:43:53 +00001918 case attr::Blocks:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001919 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1920 break;
1921
Sean Hunt387475d2010-06-16 23:43:53 +00001922 case attr::CDecl:
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001923 break;
1924
Sean Hunt387475d2010-06-16 23:43:53 +00001925 case attr::Cleanup:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001926 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1927 break;
1928
Sean Hunt387475d2010-06-16 23:43:53 +00001929 case attr::Const:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001930 break;
1931
Sean Hunt387475d2010-06-16 23:43:53 +00001932 case attr::Constructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001933 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1934 break;
1935
Sean Hunt387475d2010-06-16 23:43:53 +00001936 case attr::DLLExport:
1937 case attr::DLLImport:
1938 case attr::Deprecated:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001939 break;
1940
Sean Hunt387475d2010-06-16 23:43:53 +00001941 case attr::Destructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001942 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1943 break;
1944
Sean Hunt387475d2010-06-16 23:43:53 +00001945 case attr::FastCall:
1946 case attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001947 break;
1948
Sean Hunt387475d2010-06-16 23:43:53 +00001949 case attr::Format: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001950 const FormatAttr *Format = cast<FormatAttr>(Attr);
1951 AddString(Format->getType(), Record);
1952 Record.push_back(Format->getFormatIdx());
1953 Record.push_back(Format->getFirstArg());
1954 break;
1955 }
1956
Sean Hunt387475d2010-06-16 23:43:53 +00001957 case attr::FormatArg: {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001958 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1959 Record.push_back(Format->getFormatIdx());
1960 break;
1961 }
1962
Sean Hunt387475d2010-06-16 23:43:53 +00001963 case attr::Sentinel : {
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001964 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1965 Record.push_back(Sentinel->getSentinel());
1966 Record.push_back(Sentinel->getNullPos());
1967 break;
1968 }
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Sean Hunt387475d2010-06-16 23:43:53 +00001970 case attr::GNUInline:
1971 case attr::Hiding:
1972 case attr::IBAction:
1973 case attr::IBOutlet:
1974 case attr::Malloc:
1975 case attr::NoDebug:
1976 case attr::NoInline:
1977 case attr::NoReturn:
1978 case attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001979 break;
1980
Sean Hunt387475d2010-06-16 23:43:53 +00001981 case attr::IBOutletCollection: {
Ted Kremenek857e9182010-05-19 17:38:06 +00001982 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
1983 AddDeclRef(ICA->getClass(), Record);
1984 break;
1985 }
1986
Sean Hunt387475d2010-06-16 23:43:53 +00001987 case attr::NonNull: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001988 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1989 Record.push_back(NonNull->size());
1990 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1991 break;
1992 }
1993
Sean Hunt387475d2010-06-16 23:43:53 +00001994 case attr::CFReturnsNotRetained:
1995 case attr::CFReturnsRetained:
1996 case attr::NSReturnsNotRetained:
1997 case attr::NSReturnsRetained:
1998 case attr::ObjCException:
1999 case attr::ObjCNSObject:
2000 case attr::Overloadable:
2001 case attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002002 break;
2003
Sean Hunt387475d2010-06-16 23:43:53 +00002004 case attr::MaxFieldAlignment:
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00002005 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002006 break;
2007
Sean Hunt387475d2010-06-16 23:43:53 +00002008 case attr::Packed:
Anders Carlssona860e752009-08-08 18:23:56 +00002009 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Sean Hunt387475d2010-06-16 23:43:53 +00002011 case attr::Pure:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002012 break;
2013
Sean Hunt387475d2010-06-16 23:43:53 +00002014 case attr::Regparm:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002015 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2016 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Sean Hunt387475d2010-06-16 23:43:53 +00002018 case attr::ReqdWorkGroupSize:
Nate Begeman6f3d8382009-06-26 06:32:41 +00002019 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2020 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2021 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2022 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002023
Sean Hunt387475d2010-06-16 23:43:53 +00002024 case attr::Section:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002025 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2026 break;
2027
Sean Hunt387475d2010-06-16 23:43:53 +00002028 case attr::StdCall:
2029 case attr::TransparentUnion:
2030 case attr::Unavailable:
2031 case attr::Unused:
2032 case attr::Used:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002033 break;
2034
Sean Hunt387475d2010-06-16 23:43:53 +00002035 case attr::Visibility:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002036 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00002037 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002038 break;
2039
Sean Hunt387475d2010-06-16 23:43:53 +00002040 case attr::WarnUnusedResult:
2041 case attr::Weak:
2042 case attr::WeakRef:
2043 case attr::WeakImport:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002044 break;
2045 }
2046 }
2047
Douglas Gregorc9490c02009-04-16 22:23:12 +00002048 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002049}
2050
2051void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2052 Record.push_back(Str.size());
2053 Record.insert(Record.end(), Str.begin(), Str.end());
2054}
2055
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002056/// \brief Note that the identifier II occurs at the given offset
2057/// within the identifier table.
2058void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002059 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002060}
2061
Douglas Gregor83941df2009-04-25 17:48:32 +00002062/// \brief Note that the selector Sel occurs at the given offset
2063/// within the method pool/selector table.
2064void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2065 unsigned ID = SelectorIDs[Sel];
2066 assert(ID && "Unknown selector");
2067 SelectorOffsets[ID - 1] = Offset;
2068}
2069
Mike Stump1eb44332009-09-09 15:08:12 +00002070PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2071 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002072 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
2073 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002074
Douglas Gregore650c8c2009-07-07 00:12:59 +00002075void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2076 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002077 using namespace llvm;
2078
Douglas Gregore7785042009-04-20 15:53:59 +00002079 ASTContext &Context = SemaRef.Context;
2080 Preprocessor &PP = SemaRef.PP;
2081
Douglas Gregor2cf26342009-04-09 22:27:44 +00002082 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002083 Stream.Emit((unsigned)'C', 8);
2084 Stream.Emit((unsigned)'P', 8);
2085 Stream.Emit((unsigned)'C', 8);
2086 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002087
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002088 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002089
2090 // The translation unit is the first declaration we'll emit.
2091 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002092 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002093
Douglas Gregor2deaea32009-04-22 18:49:13 +00002094 // Make sure that we emit IdentifierInfos (and any attached
2095 // declarations) for builtins.
2096 {
2097 IdentifierTable &Table = PP.getIdentifierTable();
2098 llvm::SmallVector<const char *, 32> BuiltinNames;
2099 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2100 Context.getLangOptions().NoBuiltin);
2101 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2102 getIdentifierRef(&Table.get(BuiltinNames[I]));
2103 }
2104
Chris Lattner63d65f82009-09-08 18:19:27 +00002105 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002106 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002107 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002108 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002109 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2110 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002111 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002112
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002113 // Build a record containing all of the static unused functions in this file.
2114 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002115 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002116 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002117
Douglas Gregor14c22f22009-04-22 22:18:58 +00002118 // Build a record containing all of the locally-scoped external
2119 // declarations in this header file. Generally, this record will be
2120 // empty.
2121 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002122 // FIXME: This is filling in the PCH file in densemap order which is
2123 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002124 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002125 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2126 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2127 TD != TDEnd; ++TD)
2128 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2129
Douglas Gregorb81c1702009-04-27 20:06:05 +00002130 // Build a record containing all of the ext_vector declarations.
2131 RecordData ExtVectorDecls;
2132 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2133 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2134
Douglas Gregor2cf26342009-04-09 22:27:44 +00002135 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002136 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002137 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002138 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002139 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002140 if (StatCalls && !isysroot)
2141 WriteStatCache(*StatCalls, isysroot);
2142 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002143 // Write the record of special types.
2144 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002146 AddTypeRef(Context.getBuiltinVaListType(), Record);
2147 AddTypeRef(Context.getObjCIdType(), Record);
2148 AddTypeRef(Context.getObjCSelType(), Record);
2149 AddTypeRef(Context.getObjCProtoType(), Record);
2150 AddTypeRef(Context.getObjCClassType(), Record);
2151 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2152 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2153 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002154 AddTypeRef(Context.getjmp_bufType(), Record);
2155 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002156 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2157 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002158 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002159 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002160 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2161 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002162 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002163
Douglas Gregor366809a2009-04-26 03:49:13 +00002164 // Keep writing types and declarations until all types and
2165 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002166 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2167 WriteDeclsBlockAbbrevs();
2168 while (!DeclTypesToEmit.empty()) {
2169 DeclOrType DOT = DeclTypesToEmit.front();
2170 DeclTypesToEmit.pop();
2171 if (DOT.isType())
2172 WriteType(DOT.getType());
2173 else
2174 WriteDecl(Context, DOT.getDecl());
2175 }
2176 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002177
Douglas Gregor813a97b2009-10-17 17:25:45 +00002178 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002179 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002180 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002181
2182 // Write the type offsets array
2183 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2184 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2187 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2188 Record.clear();
2189 Record.push_back(pch::TYPE_OFFSET);
2190 Record.push_back(TypeOffsets.size());
2191 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002192 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002193 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002194
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002195 // Write the declaration offsets array
2196 Abbrev = new BitCodeAbbrev();
2197 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2200 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2201 Record.clear();
2202 Record.push_back(pch::DECL_OFFSET);
2203 Record.push_back(DeclOffsets.size());
2204 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002205 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002206 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002207
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002208 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002209 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002210 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002211
2212 // Write the record containing tentative definitions.
2213 if (!TentativeDefinitions.empty())
2214 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002215
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002216 // Write the record containing unused static functions.
2217 if (!UnusedStaticFuncs.empty())
2218 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002219
Douglas Gregor14c22f22009-04-22 22:18:58 +00002220 // Write the record containing locally-scoped external definitions.
2221 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002222 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002223 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002224
2225 // Write the record containing ext_vector type names.
2226 if (!ExtVectorDecls.empty())
2227 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Douglas Gregor3e1af842009-04-17 22:13:46 +00002229 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002230 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002231 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002232 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002233 Record.push_back(NumLexicalDeclContexts);
2234 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002235 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002236 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002237}
2238
2239void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2240 Record.push_back(Loc.getRawEncoding());
2241}
2242
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002243void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2244 AddSourceLocation(Range.getBegin(), Record);
2245 AddSourceLocation(Range.getEnd(), Record);
2246}
2247
Douglas Gregor2cf26342009-04-09 22:27:44 +00002248void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2249 Record.push_back(Value.getBitWidth());
2250 unsigned N = Value.getNumWords();
2251 const uint64_t* Words = Value.getRawData();
2252 for (unsigned I = 0; I != N; ++I)
2253 Record.push_back(Words[I]);
2254}
2255
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002256void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2257 Record.push_back(Value.isUnsigned());
2258 AddAPInt(Value, Record);
2259}
2260
Douglas Gregor17fc2232009-04-14 21:55:33 +00002261void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2262 AddAPInt(Value.bitcastToAPInt(), Record);
2263}
2264
Douglas Gregor2cf26342009-04-09 22:27:44 +00002265void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002266 Record.push_back(getIdentifierRef(II));
2267}
2268
2269pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2270 if (II == 0)
2271 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002272
2273 pch::IdentID &ID = IdentifierIDs[II];
2274 if (ID == 0)
2275 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002276 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002277}
2278
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002279pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2280 if (MD == 0)
2281 return 0;
2282
2283 pch::IdentID &ID = MacroDefinitions[MD];
2284 if (ID == 0)
2285 ID = MacroDefinitions.size();
2286 return ID;
2287}
2288
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002289void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2290 if (SelRef.getAsOpaquePtr() == 0) {
2291 Record.push_back(0);
2292 return;
2293 }
2294
2295 pch::SelectorID &SID = SelectorIDs[SelRef];
2296 if (SID == 0) {
2297 SID = SelectorIDs.size();
2298 SelVector.push_back(SelRef);
2299 }
2300 Record.push_back(SID);
2301}
2302
Chris Lattnerd2598362010-05-10 00:25:06 +00002303void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2304 AddDeclRef(Temp->getDestructor(), Record);
2305}
2306
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002307void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2308 const TemplateArgumentLocInfo &Arg,
2309 RecordData &Record) {
2310 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002311 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002312 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002313 break;
2314 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002315 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002316 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002317 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002318 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2319 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002320 break;
John McCall833ca992009-10-29 08:12:44 +00002321 case TemplateArgument::Null:
2322 case TemplateArgument::Integral:
2323 case TemplateArgument::Declaration:
2324 case TemplateArgument::Pack:
2325 break;
2326 }
2327}
2328
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002329void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2330 RecordData &Record) {
2331 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002332
2333 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2334 bool InfoHasSameExpr
2335 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2336 Record.push_back(InfoHasSameExpr);
2337 if (InfoHasSameExpr)
2338 return; // Avoid storing the same expr twice.
2339 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002340 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2341 Record);
2342}
2343
John McCalla93c9342009-12-07 02:54:59 +00002344void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2345 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002346 AddTypeRef(QualType(), Record);
2347 return;
2348 }
2349
John McCalla93c9342009-12-07 02:54:59 +00002350 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002351 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002352 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002353 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002354}
2355
Douglas Gregor2cf26342009-04-09 22:27:44 +00002356void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2357 if (T.isNull()) {
2358 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2359 return;
2360 }
2361
Douglas Gregora4923eb2009-11-16 21:35:15 +00002362 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002363 T.removeFastQualifiers();
2364
Douglas Gregora4923eb2009-11-16 21:35:15 +00002365 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002366 pch::TypeID &ID = TypeIDs[T];
2367 if (ID == 0) {
2368 // We haven't seen these qualifiers applied to this type before.
2369 // Assign it a new ID. This is the only time we enqueue a
2370 // qualified type, and it has no CV qualifiers.
2371 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002372 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002373 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002374
John McCall0953e762009-09-24 19:53:00 +00002375 // Encode the type qualifiers in the type reference.
2376 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2377 return;
2378 }
2379
Douglas Gregora4923eb2009-11-16 21:35:15 +00002380 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002381
Douglas Gregor2cf26342009-04-09 22:27:44 +00002382 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002383 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002384 switch (BT->getKind()) {
2385 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2386 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2387 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2388 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2389 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2390 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2391 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2392 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002393 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002394 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2395 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2396 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2397 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2398 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2399 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2400 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002401 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002402 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2403 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2404 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002405 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002406 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2407 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002408 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2409 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002410 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2411 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002412 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002413 case BuiltinType::UndeducedAuto:
2414 assert(0 && "Should not see undeduced auto here");
2415 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002416 }
2417
John McCall0953e762009-09-24 19:53:00 +00002418 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002419 return;
2420 }
2421
John McCall0953e762009-09-24 19:53:00 +00002422 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002423 if (ID == 0) {
2424 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002425 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002426 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002427 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002428 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002429
2430 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002431 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002432}
2433
2434void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2435 if (D == 0) {
2436 Record.push_back(0);
2437 return;
2438 }
2439
Douglas Gregor8038d512009-04-10 17:25:41 +00002440 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002441 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002442 // We haven't seen this declaration before. Give it a new ID and
2443 // enqueue it in the list of declarations to emit.
2444 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002445 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002446 }
2447
2448 Record.push_back(ID);
2449}
2450
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002451pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2452 if (D == 0)
2453 return 0;
2454
2455 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2456 return DeclIDs[D];
2457}
2458
Douglas Gregor2cf26342009-04-09 22:27:44 +00002459void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002460 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002461 Record.push_back(Name.getNameKind());
2462 switch (Name.getNameKind()) {
2463 case DeclarationName::Identifier:
2464 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2465 break;
2466
2467 case DeclarationName::ObjCZeroArgSelector:
2468 case DeclarationName::ObjCOneArgSelector:
2469 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002470 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002471 break;
2472
2473 case DeclarationName::CXXConstructorName:
2474 case DeclarationName::CXXDestructorName:
2475 case DeclarationName::CXXConversionFunctionName:
2476 AddTypeRef(Name.getCXXNameType(), Record);
2477 break;
2478
2479 case DeclarationName::CXXOperatorName:
2480 Record.push_back(Name.getCXXOverloadedOperator());
2481 break;
2482
Sean Hunt3e518bd2009-11-29 07:34:05 +00002483 case DeclarationName::CXXLiteralOperatorName:
2484 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2485 break;
2486
Douglas Gregor2cf26342009-04-09 22:27:44 +00002487 case DeclarationName::CXXUsingDirective:
2488 // No extra data to emit
2489 break;
2490 }
2491}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002492
2493void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2494 RecordData &Record) {
2495 // Nested name specifiers usually aren't too long. I think that 8 would
2496 // typically accomodate the vast majority.
2497 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2498
2499 // Push each of the NNS's onto a stack for serialization in reverse order.
2500 while (NNS) {
2501 NestedNames.push_back(NNS);
2502 NNS = NNS->getPrefix();
2503 }
2504
2505 Record.push_back(NestedNames.size());
2506 while(!NestedNames.empty()) {
2507 NNS = NestedNames.pop_back_val();
2508 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2509 Record.push_back(Kind);
2510 switch (Kind) {
2511 case NestedNameSpecifier::Identifier:
2512 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2513 break;
2514
2515 case NestedNameSpecifier::Namespace:
2516 AddDeclRef(NNS->getAsNamespace(), Record);
2517 break;
2518
2519 case NestedNameSpecifier::TypeSpec:
2520 case NestedNameSpecifier::TypeSpecWithTemplate:
2521 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2522 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2523 break;
2524
2525 case NestedNameSpecifier::Global:
2526 // Don't need to write an associated value.
2527 break;
2528 }
2529 }
2530}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002531
2532void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2533 TemplateName::NameKind Kind = Name.getKind();
2534 Record.push_back(Kind);
2535 switch (Kind) {
2536 case TemplateName::Template:
2537 AddDeclRef(Name.getAsTemplateDecl(), Record);
2538 break;
2539
2540 case TemplateName::OverloadedTemplate: {
2541 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2542 Record.push_back(OvT->size());
2543 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2544 I != E; ++I)
2545 AddDeclRef(*I, Record);
2546 break;
2547 }
2548
2549 case TemplateName::QualifiedTemplate: {
2550 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2551 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2552 Record.push_back(QualT->hasTemplateKeyword());
2553 AddDeclRef(QualT->getTemplateDecl(), Record);
2554 break;
2555 }
2556
2557 case TemplateName::DependentTemplate: {
2558 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2559 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2560 Record.push_back(DepT->isIdentifier());
2561 if (DepT->isIdentifier())
2562 AddIdentifierRef(DepT->getIdentifier(), Record);
2563 else
2564 Record.push_back(DepT->getOperator());
2565 break;
2566 }
2567 }
2568}
2569
2570void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2571 RecordData &Record) {
2572 Record.push_back(Arg.getKind());
2573 switch (Arg.getKind()) {
2574 case TemplateArgument::Null:
2575 break;
2576 case TemplateArgument::Type:
2577 AddTypeRef(Arg.getAsType(), Record);
2578 break;
2579 case TemplateArgument::Declaration:
2580 AddDeclRef(Arg.getAsDecl(), Record);
2581 break;
2582 case TemplateArgument::Integral:
2583 AddAPSInt(*Arg.getAsIntegral(), Record);
2584 AddTypeRef(Arg.getIntegralType(), Record);
2585 break;
2586 case TemplateArgument::Template:
2587 AddTemplateName(Arg.getAsTemplate(), Record);
2588 break;
2589 case TemplateArgument::Expression:
2590 AddStmt(Arg.getAsExpr());
2591 break;
2592 case TemplateArgument::Pack:
2593 Record.push_back(Arg.pack_size());
2594 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2595 I != E; ++I)
2596 AddTemplateArgument(*I, Record);
2597 break;
2598 }
2599}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002600
2601void
2602PCHWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
2603 RecordData &Record) {
2604 assert(TemplateParams && "No TemplateParams!");
2605 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2606 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2607 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2608 Record.push_back(TemplateParams->size());
2609 for (TemplateParameterList::const_iterator
2610 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2611 P != PEnd; ++P)
2612 AddDeclRef(*P, Record);
2613}
2614
2615/// \brief Emit a template argument list.
2616void
2617PCHWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
2618 RecordData &Record) {
2619 assert(TemplateArgs && "No TemplateArgs!");
2620 Record.push_back(TemplateArgs->flat_size());
2621 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2622 AddTemplateArgument(TemplateArgs->get(i), Record);
2623}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00002624
2625
2626void
2627PCHWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
2628 Record.push_back(Set.size());
2629 for (UnresolvedSetImpl::const_iterator
2630 I = Set.begin(), E = Set.end(); I != E; ++I) {
2631 AddDeclRef(I.getDecl(), Record);
2632 Record.push_back(I.getAccess());
2633 }
2634}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00002635
2636void PCHWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
2637 RecordData &Record) {
2638 Record.push_back(Base.isVirtual());
2639 Record.push_back(Base.isBaseOfClass());
2640 Record.push_back(Base.getAccessSpecifierAsWritten());
2641 AddTypeRef(Base.getType(), Record);
2642 AddSourceRange(Base.getSourceRange(), Record);
2643}