blob: b97aecbd8ca8c3d8c040caed6b18b235aede17dd [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"
Sebastian Redl77f46032010-07-09 21:00:24 +000023#include "clang/Frontend/PCHReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000025#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000026#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000027#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000029#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000031#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000032#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000034#include "llvm/ADT/APFloat.h"
35#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000036#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000037#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000038#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000039#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000040#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000041using namespace clang;
42
43//===----------------------------------------------------------------------===//
44// Type serialization
45//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000046
Douglas Gregor2cf26342009-04-09 22:27:44 +000047namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000048 class PCHTypeWriter {
Douglas Gregor2cf26342009-04-09 22:27:44 +000049 PCHWriter &Writer;
50 PCHWriter::RecordData &Record;
51
52 public:
53 /// \brief Type code that corresponds to the record generated.
54 pch::TypeCode Code;
55
Mike Stump1eb44332009-09-09 15:08:12 +000056 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000057 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000058
59 void VisitArrayType(const ArrayType *T);
60 void VisitFunctionType(const FunctionType *T);
61 void VisitTagType(const TagType *T);
62
63#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
64#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000065#include "clang/AST/TypeNodes.def"
66 };
67}
68
Douglas Gregor2cf26342009-04-09 22:27:44 +000069void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
Douglas Gregor2cf26342009-04-09 22:27:44 +000073void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
74 Writer.AddTypeRef(T->getElementType(), Record);
75 Code = pch::TYPE_COMPLEX;
76}
77
78void PCHTypeWriter::VisitPointerType(const PointerType *T) {
79 Writer.AddTypeRef(T->getPointeeType(), Record);
80 Code = pch::TYPE_POINTER;
81}
82
83void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000084 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000085 Code = pch::TYPE_BLOCK_POINTER;
86}
87
88void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_LVALUE_REFERENCE;
91}
92
93void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_RVALUE_REFERENCE;
96}
97
98void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000099 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101 Code = pch::TYPE_MEMBER_POINTER;
102}
103
104void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
105 Writer.AddTypeRef(T->getElementType(), Record);
106 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000107 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
110void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
111 VisitArrayType(T);
112 Writer.AddAPInt(T->getSize(), Record);
113 Code = pch::TYPE_CONSTANT_ARRAY;
114}
115
116void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
117 VisitArrayType(T);
118 Code = pch::TYPE_INCOMPLETE_ARRAY;
119}
120
121void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
122 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000123 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
124 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000125 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126 Code = pch::TYPE_VARIABLE_ARRAY;
127}
128
129void PCHTypeWriter::VisitVectorType(const VectorType *T) {
130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getNumElements());
Chris Lattner788b0fd2010-06-23 06:00:24 +0000132 Record.push_back(T->getAltiVecSpecific());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133 Code = pch::TYPE_VECTOR;
134}
135
136void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
137 VisitVectorType(T);
138 Code = pch::TYPE_EXT_VECTOR;
139}
140
141void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
142 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000143 FunctionType::ExtInfo C = T->getExtInfo();
144 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000145 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000146 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000147 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
150void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
151 VisitFunctionType(T);
152 Code = pch::TYPE_FUNCTION_NO_PROTO;
153}
154
155void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
156 VisitFunctionType(T);
157 Record.push_back(T->getNumArgs());
158 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
159 Writer.AddTypeRef(T->getArgType(I), Record);
160 Record.push_back(T->isVariadic());
161 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000162 Record.push_back(T->hasExceptionSpec());
163 Record.push_back(T->hasAnyExceptionSpec());
164 Record.push_back(T->getNumExceptions());
165 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
166 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Code = pch::TYPE_FUNCTION_PROTO;
168}
169
John McCalled976492009-12-04 22:46:56 +0000170void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_UNRESOLVED_USING;
173}
John McCalled976492009-12-04 22:46:56 +0000174
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
176 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000177 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
178 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179 Code = pch::TYPE_TYPEDEF;
180}
181
182void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000183 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184 Code = pch::TYPE_TYPEOF_EXPR;
185}
186
187void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
188 Writer.AddTypeRef(T->getUnderlyingType(), Record);
189 Code = pch::TYPE_TYPEOF;
190}
191
Anders Carlsson395b4752009-06-24 19:06:50 +0000192void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
193 Writer.AddStmt(T->getUnderlyingExpr());
194 Code = pch::TYPE_DECLTYPE;
195}
196
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197void PCHTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000198 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000200 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201 "Cannot serialize in the middle of a type definition");
202}
203
204void PCHTypeWriter::VisitRecordType(const RecordType *T) {
205 VisitTagType(T);
206 Code = pch::TYPE_RECORD;
207}
208
209void PCHTypeWriter::VisitEnumType(const EnumType *T) {
210 VisitTagType(T);
211 Code = pch::TYPE_ENUM;
212}
213
Mike Stump1eb44332009-09-09 15:08:12 +0000214void
John McCall49a832b2009-10-18 09:09:24 +0000215PCHTypeWriter::VisitSubstTemplateTypeParmType(
216 const SubstTemplateTypeParmType *T) {
217 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
218 Writer.AddTypeRef(T->getReplacementType(), Record);
219 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
220}
221
222void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000223PCHTypeWriter::VisitTemplateSpecializationType(
224 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000225 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000226 Writer.AddTemplateName(T->getTemplateName(), Record);
227 Record.push_back(T->getNumArgs());
228 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
229 ArgI != ArgE; ++ArgI)
230 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000231 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
232 : T->getCanonicalTypeInternal(),
233 Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000234 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
235}
236
237void
238PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000239 VisitArrayType(T);
240 Writer.AddStmt(T->getSizeExpr());
241 Writer.AddSourceRange(T->getBracketsRange(), Record);
242 Code = pch::TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000243}
244
245void
246PCHTypeWriter::VisitDependentSizedExtVectorType(
247 const DependentSizedExtVectorType *T) {
248 // FIXME: Serialize this type (C++ only)
249 assert(false && "Cannot serialize dependent sized extended vector types");
250}
251
252void
253PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
254 Record.push_back(T->getDepth());
255 Record.push_back(T->getIndex());
256 Record.push_back(T->isParameterPack());
257 Writer.AddIdentifierRef(T->getName(), Record);
258 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
259}
260
261void
262PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000263 Record.push_back(T->getKeyword());
264 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
265 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000266 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
267 : T->getCanonicalTypeInternal(),
268 Record);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000269 Code = pch::TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000270}
271
272void
273PCHTypeWriter::VisitDependentTemplateSpecializationType(
274 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000275 Record.push_back(T->getKeyword());
276 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
277 Writer.AddIdentifierRef(T->getIdentifier(), Record);
278 Record.push_back(T->getNumArgs());
279 for (DependentTemplateSpecializationType::iterator
280 I = T->begin(), E = T->end(); I != E; ++I)
281 Writer.AddTemplateArgument(*I, Record);
282 Code = pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000283}
284
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000285void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000286 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000287 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
288 Writer.AddTypeRef(T->getNamedType(), Record);
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000289 Code = pch::TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000290}
291
John McCall3cb0ebd2010-03-10 03:28:59 +0000292void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
293 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000294 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000295 Code = pch::TYPE_INJECTED_CLASS_NAME;
296}
297
Douglas Gregor2cf26342009-04-09 22:27:44 +0000298void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
299 Writer.AddDeclRef(T->getDecl(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000300 Code = pch::TYPE_OBJC_INTERFACE;
301}
302
303void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
304 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000305 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000306 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000307 E = T->qual_end(); I != E; ++I)
308 Writer.AddDeclRef(*I, Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000309 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000310}
311
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000312void
313PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000314 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000315 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000316}
317
John McCalla1ee0c52009-10-16 21:56:05 +0000318namespace {
319
320class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
321 PCHWriter &Writer;
322 PCHWriter::RecordData &Record;
323
324public:
325 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
326 : Writer(Writer), Record(Record) { }
327
John McCall51bd8032009-10-18 01:05:36 +0000328#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000329#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000330 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000331#include "clang/AST/TypeLocNodes.def"
332
John McCall51bd8032009-10-18 01:05:36 +0000333 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
334 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000335};
336
337}
338
John McCall51bd8032009-10-18 01:05:36 +0000339void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
340 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000341}
John McCall51bd8032009-10-18 01:05:36 +0000342void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000343 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
344 if (TL.needsExtraLocalData()) {
345 Record.push_back(TL.getWrittenTypeSpec());
346 Record.push_back(TL.getWrittenSignSpec());
347 Record.push_back(TL.getWrittenWidthSpec());
348 Record.push_back(TL.hasModeAttr());
349 }
John McCalla1ee0c52009-10-16 21:56:05 +0000350}
John McCall51bd8032009-10-18 01:05:36 +0000351void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
352 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000353}
John McCall51bd8032009-10-18 01:05:36 +0000354void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
355 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000356}
John McCall51bd8032009-10-18 01:05:36 +0000357void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
358 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000359}
John McCall51bd8032009-10-18 01:05:36 +0000360void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000362}
John McCall51bd8032009-10-18 01:05:36 +0000363void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000365}
John McCall51bd8032009-10-18 01:05:36 +0000366void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000368}
John McCall51bd8032009-10-18 01:05:36 +0000369void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
370 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
371 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
372 Record.push_back(TL.getSizeExpr() ? 1 : 0);
373 if (TL.getSizeExpr())
374 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000375}
John McCall51bd8032009-10-18 01:05:36 +0000376void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
377 VisitArrayTypeLoc(TL);
378}
379void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
380 VisitArrayTypeLoc(TL);
381}
382void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
383 VisitArrayTypeLoc(TL);
384}
385void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
386 DependentSizedArrayTypeLoc TL) {
387 VisitArrayTypeLoc(TL);
388}
389void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
390 DependentSizedExtVectorTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getNameLoc(), Record);
392}
393void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
394 Writer.AddSourceLocation(TL.getNameLoc(), Record);
395}
396void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
397 Writer.AddSourceLocation(TL.getNameLoc(), Record);
398}
399void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
400 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
401 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
402 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
403 Writer.AddDeclRef(TL.getArg(i), Record);
404}
405void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
406 VisitFunctionTypeLoc(TL);
407}
408void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
409 VisitFunctionTypeLoc(TL);
410}
John McCalled976492009-12-04 22:46:56 +0000411void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getNameLoc(), Record);
413}
John McCall51bd8032009-10-18 01:05:36 +0000414void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getNameLoc(), Record);
416}
417void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000418 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
419 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
420 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000421}
422void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000423 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
424 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
425 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
426 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000427}
428void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getNameLoc(), Record);
430}
431void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getNameLoc(), Record);
433}
434void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getNameLoc(), Record);
436}
John McCall51bd8032009-10-18 01:05:36 +0000437void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
439}
John McCall49a832b2009-10-18 09:09:24 +0000440void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
441 SubstTemplateTypeParmTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
445 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000446 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
447 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
448 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
449 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000450 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
451 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000452}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000453void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000454 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
455 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000456}
John McCall3cb0ebd2010-03-10 03:28:59 +0000457void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getNameLoc(), Record);
459}
Douglas Gregor4714c122010-03-31 17:34:00 +0000460void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000461 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
462 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
464}
John McCall33500952010-06-11 00:33:02 +0000465void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
466 DependentTemplateSpecializationTypeLoc TL) {
467 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
468 Writer.AddSourceRange(TL.getQualifierRange(), Record);
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
471 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
472 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000473 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
474 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000475}
John McCall51bd8032009-10-18 01:05:36 +0000476void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000478}
479void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
480 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000481 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
482 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
483 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
484 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000485}
John McCall54e14c42009-10-22 22:37:11 +0000486void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000488}
John McCalla1ee0c52009-10-16 21:56:05 +0000489
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000490//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000491// PCHWriter Implementation
492//===----------------------------------------------------------------------===//
493
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000494static void EmitBlockID(unsigned ID, const char *Name,
495 llvm::BitstreamWriter &Stream,
496 PCHWriter::RecordData &Record) {
497 Record.clear();
498 Record.push_back(ID);
499 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
500
501 // Emit the block name if present.
502 if (Name == 0 || Name[0] == 0) return;
503 Record.clear();
504 while (*Name)
505 Record.push_back(*Name++);
506 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
507}
508
509static void EmitRecordID(unsigned ID, const char *Name,
510 llvm::BitstreamWriter &Stream,
511 PCHWriter::RecordData &Record) {
512 Record.clear();
513 Record.push_back(ID);
514 while (*Name)
515 Record.push_back(*Name++);
516 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000517}
518
519static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
520 PCHWriter::RecordData &Record) {
521#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
522 RECORD(STMT_STOP);
523 RECORD(STMT_NULL_PTR);
524 RECORD(STMT_NULL);
525 RECORD(STMT_COMPOUND);
526 RECORD(STMT_CASE);
527 RECORD(STMT_DEFAULT);
528 RECORD(STMT_LABEL);
529 RECORD(STMT_IF);
530 RECORD(STMT_SWITCH);
531 RECORD(STMT_WHILE);
532 RECORD(STMT_DO);
533 RECORD(STMT_FOR);
534 RECORD(STMT_GOTO);
535 RECORD(STMT_INDIRECT_GOTO);
536 RECORD(STMT_CONTINUE);
537 RECORD(STMT_BREAK);
538 RECORD(STMT_RETURN);
539 RECORD(STMT_DECL);
540 RECORD(STMT_ASM);
541 RECORD(EXPR_PREDEFINED);
542 RECORD(EXPR_DECL_REF);
543 RECORD(EXPR_INTEGER_LITERAL);
544 RECORD(EXPR_FLOATING_LITERAL);
545 RECORD(EXPR_IMAGINARY_LITERAL);
546 RECORD(EXPR_STRING_LITERAL);
547 RECORD(EXPR_CHARACTER_LITERAL);
548 RECORD(EXPR_PAREN);
549 RECORD(EXPR_UNARY_OPERATOR);
550 RECORD(EXPR_SIZEOF_ALIGN_OF);
551 RECORD(EXPR_ARRAY_SUBSCRIPT);
552 RECORD(EXPR_CALL);
553 RECORD(EXPR_MEMBER);
554 RECORD(EXPR_BINARY_OPERATOR);
555 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
556 RECORD(EXPR_CONDITIONAL_OPERATOR);
557 RECORD(EXPR_IMPLICIT_CAST);
558 RECORD(EXPR_CSTYLE_CAST);
559 RECORD(EXPR_COMPOUND_LITERAL);
560 RECORD(EXPR_EXT_VECTOR_ELEMENT);
561 RECORD(EXPR_INIT_LIST);
562 RECORD(EXPR_DESIGNATED_INIT);
563 RECORD(EXPR_IMPLICIT_VALUE_INIT);
564 RECORD(EXPR_VA_ARG);
565 RECORD(EXPR_ADDR_LABEL);
566 RECORD(EXPR_STMT);
567 RECORD(EXPR_TYPES_COMPATIBLE);
568 RECORD(EXPR_CHOOSE);
569 RECORD(EXPR_GNU_NULL);
570 RECORD(EXPR_SHUFFLE_VECTOR);
571 RECORD(EXPR_BLOCK);
572 RECORD(EXPR_BLOCK_DECL_REF);
573 RECORD(EXPR_OBJC_STRING_LITERAL);
574 RECORD(EXPR_OBJC_ENCODE);
575 RECORD(EXPR_OBJC_SELECTOR_EXPR);
576 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
577 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
578 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
579 RECORD(EXPR_OBJC_KVC_REF_EXPR);
580 RECORD(EXPR_OBJC_MESSAGE_EXPR);
581 RECORD(EXPR_OBJC_SUPER_EXPR);
582 RECORD(STMT_OBJC_FOR_COLLECTION);
583 RECORD(STMT_OBJC_CATCH);
584 RECORD(STMT_OBJC_FINALLY);
585 RECORD(STMT_OBJC_AT_TRY);
586 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
587 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000588 RECORD(EXPR_CXX_OPERATOR_CALL);
589 RECORD(EXPR_CXX_CONSTRUCT);
590 RECORD(EXPR_CXX_STATIC_CAST);
591 RECORD(EXPR_CXX_DYNAMIC_CAST);
592 RECORD(EXPR_CXX_REINTERPRET_CAST);
593 RECORD(EXPR_CXX_CONST_CAST);
594 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
595 RECORD(EXPR_CXX_BOOL_LITERAL);
596 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000597#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000598}
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000600void PCHWriter::WriteBlockInfoBlock() {
601 RecordData Record;
602 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Chris Lattner2f4efd12009-04-27 00:40:25 +0000604#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000605#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000607 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000608 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000609 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000610 RECORD(TYPE_OFFSET);
611 RECORD(DECL_OFFSET);
612 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000613 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000614 RECORD(IDENTIFIER_OFFSET);
615 RECORD(IDENTIFIER_TABLE);
616 RECORD(EXTERNAL_DEFINITIONS);
617 RECORD(SPECIAL_TYPES);
618 RECORD(STATISTICS);
619 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000620 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000621 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
622 RECORD(SELECTOR_OFFSETS);
623 RECORD(METHOD_POOL);
624 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000625 RECORD(SOURCE_LOCATION_OFFSETS);
626 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000627 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000628 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000629 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000630 RECORD(UNUSED_STATIC_FUNCS);
631 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000632 RECORD(CHAINED_METADATA);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000633
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000634 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000635 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000636 RECORD(SM_SLOC_FILE_ENTRY);
637 RECORD(SM_SLOC_BUFFER_ENTRY);
638 RECORD(SM_SLOC_BUFFER_BLOB);
639 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
640 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000642 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000643 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000644 RECORD(PP_MACRO_OBJECT_LIKE);
645 RECORD(PP_MACRO_FUNCTION_LIKE);
646 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000647 RECORD(PP_MACRO_INSTANTIATION);
648 RECORD(PP_MACRO_DEFINITION);
649
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000650 // Decls and Types block.
651 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000652 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000653 RECORD(TYPE_COMPLEX);
654 RECORD(TYPE_POINTER);
655 RECORD(TYPE_BLOCK_POINTER);
656 RECORD(TYPE_LVALUE_REFERENCE);
657 RECORD(TYPE_RVALUE_REFERENCE);
658 RECORD(TYPE_MEMBER_POINTER);
659 RECORD(TYPE_CONSTANT_ARRAY);
660 RECORD(TYPE_INCOMPLETE_ARRAY);
661 RECORD(TYPE_VARIABLE_ARRAY);
662 RECORD(TYPE_VECTOR);
663 RECORD(TYPE_EXT_VECTOR);
664 RECORD(TYPE_FUNCTION_PROTO);
665 RECORD(TYPE_FUNCTION_NO_PROTO);
666 RECORD(TYPE_TYPEDEF);
667 RECORD(TYPE_TYPEOF_EXPR);
668 RECORD(TYPE_TYPEOF);
669 RECORD(TYPE_RECORD);
670 RECORD(TYPE_ENUM);
671 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000672 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000673 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000674 RECORD(DECL_ATTR);
675 RECORD(DECL_TRANSLATION_UNIT);
676 RECORD(DECL_TYPEDEF);
677 RECORD(DECL_ENUM);
678 RECORD(DECL_RECORD);
679 RECORD(DECL_ENUM_CONSTANT);
680 RECORD(DECL_FUNCTION);
681 RECORD(DECL_OBJC_METHOD);
682 RECORD(DECL_OBJC_INTERFACE);
683 RECORD(DECL_OBJC_PROTOCOL);
684 RECORD(DECL_OBJC_IVAR);
685 RECORD(DECL_OBJC_AT_DEFS_FIELD);
686 RECORD(DECL_OBJC_CLASS);
687 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
688 RECORD(DECL_OBJC_CATEGORY);
689 RECORD(DECL_OBJC_CATEGORY_IMPL);
690 RECORD(DECL_OBJC_IMPLEMENTATION);
691 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
692 RECORD(DECL_OBJC_PROPERTY);
693 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000694 RECORD(DECL_FIELD);
695 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000696 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000697 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000698 RECORD(DECL_FILE_SCOPE_ASM);
699 RECORD(DECL_BLOCK);
700 RECORD(DECL_CONTEXT_LEXICAL);
701 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000702 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000703 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000704#undef RECORD
705#undef BLOCK
706 Stream.ExitBlock();
707}
708
Douglas Gregore650c8c2009-07-07 00:12:59 +0000709/// \brief Adjusts the given filename to only write out the portion of the
710/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000711///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000712/// \param Filename the file name to adjust.
713///
714/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
715/// the returned filename will be adjusted by this system root.
716///
717/// \returns either the original filename (if it needs no adjustment) or the
718/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000719static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000720adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
721 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Douglas Gregore650c8c2009-07-07 00:12:59 +0000723 if (!isysroot)
724 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Douglas Gregore650c8c2009-07-07 00:12:59 +0000726 // Verify that the filename and the system root have the same prefix.
727 unsigned Pos = 0;
728 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
729 if (Filename[Pos] != isysroot[Pos])
730 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregore650c8c2009-07-07 00:12:59 +0000732 // We hit the end of the filename before we hit the end of the system root.
733 if (!Filename[Pos])
734 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Douglas Gregore650c8c2009-07-07 00:12:59 +0000736 // If the file name has a '/' at the current position, skip over the '/'.
737 // We distinguish sysroot-based includes from absolute includes by the
738 // absence of '/' at the beginning of sysroot-based includes.
739 if (Filename[Pos] == '/')
740 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Douglas Gregore650c8c2009-07-07 00:12:59 +0000742 return Filename + Pos;
743}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000744
Douglas Gregorab41e632009-04-27 22:23:34 +0000745/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Sebastian Redl30c514c2010-07-14 23:45:08 +0000746void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000747 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000748
Douglas Gregore650c8c2009-07-07 00:12:59 +0000749 // Metadata
750 const TargetInfo &Target = Context.Target;
751 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000752 MetaAbbrev->Add(BitCodeAbbrevOp(
753 Chain ? pch::CHAINED_METADATA : pch::METADATA));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000754 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
755 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
756 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
757 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
758 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000759 // Target triple or chained PCH name
760 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000761 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Douglas Gregore650c8c2009-07-07 00:12:59 +0000763 RecordData Record;
Sebastian Redl77f46032010-07-09 21:00:24 +0000764 Record.push_back(Chain ? pch::CHAINED_METADATA : pch::METADATA);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000765 Record.push_back(pch::VERSION_MAJOR);
766 Record.push_back(pch::VERSION_MINOR);
767 Record.push_back(CLANG_VERSION_MAJOR);
768 Record.push_back(CLANG_VERSION_MINOR);
769 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000770 // FIXME: This writes the absolute path for chained headers.
771 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
772 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Douglas Gregorb64c1932009-05-12 01:31:05 +0000774 // Original file name
775 SourceManager &SM = Context.getSourceManager();
776 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
777 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
778 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
779 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
780 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
781
782 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000784 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000785
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000786 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000787 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000788 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000789 RecordData Record;
790 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000791 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000792 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000793
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000794 // Repository branch/version information.
795 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
796 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
797 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
798 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000799 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000800 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000801 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
802 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000803}
804
805/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000806void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
807 RecordData Record;
808 Record.push_back(LangOpts.Trigraphs);
809 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
810 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
811 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
812 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000813 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000814 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
815 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
816 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
817 Record.push_back(LangOpts.C99); // C99 Support
818 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
819 Record.push_back(LangOpts.CPlusPlus); // C++ Support
820 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000821 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000823 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
824 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000825 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000826 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000827 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000828 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000829 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000831 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000832 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
833 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000834 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000835 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000836 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000837
838 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
839 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
840 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
841
Chris Lattnerea5ce472009-04-27 07:35:58 +0000842 // Whether static initializers are protected by locks.
843 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000844 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000845 Record.push_back(LangOpts.Blocks); // block extension to C
846 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
847 // they are unused.
848 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
849 // (modulo the platform support).
850
Chris Lattnera4d71452010-06-26 21:25:03 +0000851 Record.push_back(LangOpts.getSignedOverflowBehavior());
852 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000853
854 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000855 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000856 // defined.
857 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
858 // opposed to __DYNAMIC__).
859 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
860
861 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
862 // used (instead of C99 semantics).
863 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000864 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
865 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000866 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
867 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000868 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000869 Record.push_back(LangOpts.getGCMode());
870 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000871 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000872 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000873 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000874 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000875 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000876 Record.push_back(LangOpts.SpellChecking);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000877 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000878}
879
Douglas Gregor14f79002009-04-10 03:52:48 +0000880//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000881// stat cache Serialization
882//===----------------------------------------------------------------------===//
883
884namespace {
885// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000886class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000887public:
888 typedef const char * key_type;
889 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000891 typedef std::pair<int, struct stat> data_type;
892 typedef const data_type& data_type_ref;
893
894 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000895 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
898 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000899 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
900 data_type_ref Data) {
901 unsigned StrLen = strlen(path);
902 clang::io::Emit16(Out, StrLen);
903 unsigned DataLen = 1; // result value
904 if (Data.first == 0)
905 DataLen += 4 + 4 + 2 + 8 + 8;
906 clang::io::Emit8(Out, DataLen);
907 return std::make_pair(StrLen + 1, DataLen);
908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000910 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
911 Out.write(path, KeyLen);
912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000914 void EmitData(llvm::raw_ostream& Out, key_type_ref,
915 data_type_ref Data, unsigned DataLen) {
916 using namespace clang::io;
917 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000919 // Result of stat()
920 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000922 if (Data.first == 0) {
923 Emit32(Out, (uint32_t) Data.second.st_ino);
924 Emit32(Out, (uint32_t) Data.second.st_dev);
925 Emit16(Out, (uint16_t) Data.second.st_mode);
926 Emit64(Out, (uint64_t) Data.second.st_mtime);
927 Emit64(Out, (uint64_t) Data.second.st_size);
928 }
929
930 assert(Out.tell() - Start == DataLen && "Wrong data length");
931 }
932};
933} // end anonymous namespace
934
935/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregordd41ed52010-07-12 23:48:14 +0000936void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000937 // Build the on-disk hash table containing information about every
938 // stat() call.
939 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
940 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000941 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000942 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000943 Stat != StatEnd; ++Stat, ++NumStatEntries) {
944 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000945 Generator.insert(Filename, Stat->second);
946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000948 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000949 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000950 uint32_t BucketOffset;
951 {
952 llvm::raw_svector_ostream Out(StatCacheData);
953 // Make sure that no bucket is at offset 0
954 clang::io::Emit32(Out, 0);
955 BucketOffset = Generator.Emit(Out);
956 }
957
958 // Create a blob abbreviation
959 using namespace llvm;
960 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
961 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
965 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
966
967 // Write the stat cache
968 RecordData Record;
969 Record.push_back(pch::STAT_CACHE);
970 Record.push_back(BucketOffset);
971 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000972 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000973}
974
975//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000976// Source Manager Serialization
977//===----------------------------------------------------------------------===//
978
979/// \brief Create an abbreviation for the SLocEntry that refers to a
980/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000981static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000982 using namespace llvm;
983 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
984 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
986 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
987 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000989 // FileEntry fields.
990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000992 // HeaderFileInfo fields.
993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000998 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000999}
1000
1001/// \brief Create an abbreviation for the SLocEntry that refers to a
1002/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001003static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001004 using namespace llvm;
1005 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1006 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001012 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001013}
1014
1015/// \brief Create an abbreviation for the SLocEntry that refers to a
1016/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001017static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001018 using namespace llvm;
1019 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1020 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001022 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001023}
1024
1025/// \brief Create an abbreviation for the SLocEntry that refers to an
1026/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001027static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001028 using namespace llvm;
1029 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1030 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001036 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001037}
1038
1039/// \brief Writes the block containing the serialized form of the
1040/// source manager.
1041///
1042/// TODO: We should probably use an on-disk hash table (stored in a
1043/// blob), indexed based on the file name, so that we only create
1044/// entries for files that we actually need. In the common case (no
1045/// errors), we probably won't have to create file entries for any of
1046/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001047void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001048 const Preprocessor &PP,
1049 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001050 RecordData Record;
1051
Chris Lattnerf04ad692009-04-10 17:16:57 +00001052 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001053 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001054
1055 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001056 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1057 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1058 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1059 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001060
Douglas Gregorbd945002009-04-13 16:31:14 +00001061 // Write the line table.
1062 if (SourceMgr.hasLineTable()) {
1063 LineTableInfo &LineTable = SourceMgr.getLineTable();
1064
1065 // Emit the file names
1066 Record.push_back(LineTable.getNumFilenames());
1067 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1068 // Emit the file name
1069 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001070 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001071 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1072 Record.push_back(FilenameLen);
1073 if (FilenameLen)
1074 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1075 }
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Douglas Gregorbd945002009-04-13 16:31:14 +00001077 // Emit the line entries
1078 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1079 L != LEnd; ++L) {
1080 // Emit the file ID
1081 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Douglas Gregorbd945002009-04-13 16:31:14 +00001083 // Emit the line entries
1084 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001085 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001086 LEEnd = L->second.end();
1087 LE != LEEnd; ++LE) {
1088 Record.push_back(LE->FileOffset);
1089 Record.push_back(LE->LineNo);
1090 Record.push_back(LE->FilenameID);
1091 Record.push_back((unsigned)LE->FileKind);
1092 Record.push_back(LE->IncludeOffset);
1093 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001094 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001095 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001096 }
1097
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001098 // Write out the source location entry table. We skip the first
1099 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001100 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001101 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001102 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1103 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1104 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1105 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001106 // Get this source location entry.
1107 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001108
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001109 // Record the offset of this source-location entry.
1110 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1111
1112 // Figure out which record code to use.
1113 unsigned Code;
1114 if (SLoc->isFile()) {
1115 if (SLoc->getFile().getContentCache()->Entry)
1116 Code = pch::SM_SLOC_FILE_ENTRY;
1117 else
1118 Code = pch::SM_SLOC_BUFFER_ENTRY;
1119 } else
1120 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1121 Record.clear();
1122 Record.push_back(Code);
1123
1124 Record.push_back(SLoc->getOffset());
1125 if (SLoc->isFile()) {
1126 const SrcMgr::FileInfo &File = SLoc->getFile();
1127 Record.push_back(File.getIncludeLoc().getRawEncoding());
1128 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1129 Record.push_back(File.hasLineDirectives());
1130
1131 const SrcMgr::ContentCache *Content = File.getContentCache();
1132 if (Content->Entry) {
1133 // The source location entry is a file. The blob associated
1134 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Douglas Gregor2d52be52010-03-21 22:49:54 +00001136 // Emit size/modification time for this file.
1137 Record.push_back(Content->Entry->getSize());
1138 Record.push_back(Content->Entry->getModificationTime());
1139
Douglas Gregor12fab312010-03-16 16:35:32 +00001140 // Emit header-search information associated with this file.
1141 HeaderFileInfo HFI;
1142 HeaderSearch &HS = PP.getHeaderSearchInfo();
1143 if (Content->Entry->getUID() < HS.header_file_size())
1144 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1145 Record.push_back(HFI.isImport);
1146 Record.push_back(HFI.DirInfo);
1147 Record.push_back(HFI.NumIncludes);
1148 AddIdentifierRef(HFI.ControllingMacro, Record);
1149
Douglas Gregore650c8c2009-07-07 00:12:59 +00001150 // Turn the file name into an absolute path, if it isn't already.
1151 const char *Filename = Content->Entry->getName();
1152 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001153 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001154 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Douglas Gregore650c8c2009-07-07 00:12:59 +00001156 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001157 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001158
1159 // FIXME: For now, preload all file source locations, so that
1160 // we get the appropriate File entries in the reader. This is
1161 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001162 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001163 } else {
1164 // The source location entry is a buffer. The blob associated
1165 // with this entry contains the contents of the buffer.
1166
1167 // We add one to the size so that we capture the trailing NULL
1168 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1169 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001170 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001171 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001172 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001173 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1174 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001175 Record.clear();
1176 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1177 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001178 llvm::StringRef(Buffer->getBufferStart(),
1179 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001180
1181 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001182 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001183 }
1184 } else {
1185 // The source location entry is an instantiation.
1186 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1187 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1188 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1189 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1190
1191 // Compute the token length for this macro expansion.
1192 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001193 if (I + 1 != N)
1194 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001195 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1196 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1197 }
1198 }
1199
Douglas Gregorc9490c02009-04-16 22:23:12 +00001200 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001201
1202 if (SLocEntryOffsets.empty())
1203 return;
1204
1205 // Write the source-location offsets table into the PCH block. This
1206 // table is used for lazily loading source-location information.
1207 using namespace llvm;
1208 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1209 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1213 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001215 Record.clear();
1216 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1217 Record.push_back(SLocEntryOffsets.size());
1218 Record.push_back(SourceMgr.getNextOffset());
1219 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001220 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001221 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001222
1223 // Write the source location entry preloads array, telling the PCH
1224 // reader which source locations entries it should load eagerly.
1225 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001226}
1227
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001228//===----------------------------------------------------------------------===//
1229// Preprocessor Serialization
1230//===----------------------------------------------------------------------===//
1231
Chris Lattner0b1fb982009-04-10 17:15:23 +00001232/// \brief Writes the block containing the serialized form of the
1233/// preprocessor.
1234///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001235void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001236 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001237
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001238 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1239 if (PP.getCounterValue() != 0) {
1240 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001241 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001242 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001243 }
1244
1245 // Enter the preprocessor block.
1246 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001248 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1249 // FIXME: use diagnostics subsystem for localization etc.
1250 if (PP.SawDateOrTime())
1251 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001253 // Loop over all the macro definitions that are live at the end of the file,
1254 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001255 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001256 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1257 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001258 // FIXME: This emits macros in hash table order, we should do it in a stable
1259 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001260 MacroInfo *MI = I->second;
1261
1262 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1263 // been redefined by the header (in which case they are not isBuiltinMacro).
1264 if (MI->isBuiltinMacro())
1265 continue;
1266
Chris Lattner7356a312009-04-11 21:15:38 +00001267 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001268 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001269 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1270 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001272 unsigned Code;
1273 if (MI->isObjectLike()) {
1274 Code = pch::PP_MACRO_OBJECT_LIKE;
1275 } else {
1276 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001278 Record.push_back(MI->isC99Varargs());
1279 Record.push_back(MI->isGNUVarargs());
1280 Record.push_back(MI->getNumArgs());
1281 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1282 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001283 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001284 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001285
1286 // If we have a detailed preprocessing record, record the macro definition
1287 // ID that corresponds to this macro.
1288 if (PPRec)
1289 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1290
Douglas Gregorc9490c02009-04-16 22:23:12 +00001291 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001292 Record.clear();
1293
Chris Lattnerdf961c22009-04-10 18:08:30 +00001294 // Emit the tokens array.
1295 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1296 // Note that we know that the preprocessor does not have any annotation
1297 // tokens in it because they are created by the parser, and thus can't be
1298 // in a macro definition.
1299 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Chris Lattnerdf961c22009-04-10 18:08:30 +00001301 Record.push_back(Tok.getLocation().getRawEncoding());
1302 Record.push_back(Tok.getLength());
1303
Chris Lattnerdf961c22009-04-10 18:08:30 +00001304 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1305 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001306 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Chris Lattnerdf961c22009-04-10 18:08:30 +00001308 // FIXME: Should translate token kind to a stable encoding.
1309 Record.push_back(Tok.getKind());
1310 // FIXME: Should translate token flags to a stable encoding.
1311 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Douglas Gregorc9490c02009-04-16 22:23:12 +00001313 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001314 Record.clear();
1315 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001316 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001317 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001318
1319 // If the preprocessor has a preprocessing record, emit it.
1320 unsigned NumPreprocessingRecords = 0;
1321 if (PPRec) {
1322 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1323 E != EEnd; ++E) {
1324 Record.clear();
1325
1326 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1327 Record.push_back(NumPreprocessingRecords++);
1328 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1329 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1330 AddIdentifierRef(MI->getName(), Record);
1331 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1332 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1333 continue;
1334 }
1335
1336 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1337 // Record this macro definition's location.
1338 pch::IdentID ID = getMacroDefinitionID(MD);
1339 if (ID != MacroDefinitionOffsets.size()) {
1340 if (ID > MacroDefinitionOffsets.size())
1341 MacroDefinitionOffsets.resize(ID + 1);
1342
1343 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1344 } else
1345 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1346
1347 Record.push_back(NumPreprocessingRecords++);
1348 Record.push_back(ID);
1349 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1350 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1351 AddIdentifierRef(MD->getName(), Record);
1352 AddSourceLocation(MD->getLocation(), Record);
1353 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1354 continue;
1355 }
1356 }
1357 }
1358
Douglas Gregorc9490c02009-04-16 22:23:12 +00001359 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001360
1361 // Write the offsets table for the preprocessing record.
1362 if (NumPreprocessingRecords > 0) {
1363 // Write the offsets table for identifier IDs.
1364 using namespace llvm;
1365 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1366 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1370 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1371
1372 Record.clear();
1373 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1374 Record.push_back(NumPreprocessingRecords);
1375 Record.push_back(MacroDefinitionOffsets.size());
1376 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1377 (const char *)&MacroDefinitionOffsets.front(),
1378 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1379 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001380}
1381
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001382//===----------------------------------------------------------------------===//
1383// Type Serialization
1384//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001385
Douglas Gregor2cf26342009-04-09 22:27:44 +00001386/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001387void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001388 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001389 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001390 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Douglas Gregor2cf26342009-04-09 22:27:44 +00001392 // Record the offset for this type.
1393 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001394 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001395 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1396 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001397 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001398 }
1399
1400 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Douglas Gregor2cf26342009-04-09 22:27:44 +00001402 // Emit the type's representation.
1403 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001404
Douglas Gregora4923eb2009-11-16 21:35:15 +00001405 if (T.hasLocalNonFastQualifiers()) {
1406 Qualifiers Qs = T.getLocalQualifiers();
1407 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001408 Record.push_back(Qs.getAsOpaqueValue());
1409 W.Code = pch::TYPE_EXT_QUAL;
1410 } else {
1411 switch (T->getTypeClass()) {
1412 // For all of the concrete, non-dependent types, call the
1413 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001414#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001415 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001416#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001417#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001418 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001419 }
1420
1421 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001422 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001423
1424 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001425 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001426}
1427
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001428//===----------------------------------------------------------------------===//
1429// Declaration Serialization
1430//===----------------------------------------------------------------------===//
1431
Douglas Gregor2cf26342009-04-09 22:27:44 +00001432/// \brief Write the block containing all of the declaration IDs
1433/// lexically declared within the given DeclContext.
1434///
1435/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1436/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001437uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001438 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001439 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001440 return 0;
1441
Douglas Gregorc9490c02009-04-16 22:23:12 +00001442 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001443 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001444 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1445 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001446 AddDeclRef(*D, Record);
1447
Douglas Gregor25123082009-04-22 22:34:57 +00001448 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001449 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001450 return Offset;
1451}
1452
1453/// \brief Write the block containing all of the declaration IDs
1454/// visible from the given DeclContext.
1455///
1456/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1457/// bistream, or 0 if no block was written.
1458uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1459 DeclContext *DC) {
1460 if (DC->getPrimaryContext() != DC)
1461 return 0;
1462
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +00001463 // Since there is no name lookup into functions or methods, don't bother to
1464 // build a visible-declarations table for these entities.
1465 if (DC->isFunctionOrMethod())
1466 return 0;
1467
1468 // If not in C++, we perform name lookup for the translation unit via the
1469 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1470 // FIXME: In C++ we need the visible declarations in order to "see" the
1471 // friend declarations, is there a way to do this without writing the table ?
1472 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor58f06992009-04-18 15:49:20 +00001473 return 0;
1474
Douglas Gregor2cf26342009-04-09 22:27:44 +00001475 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001476 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001477
1478 // Serialize the contents of the mapping used for lookup. Note that,
1479 // although we have two very different code paths, the serialized
1480 // representation is the same for both cases: a declaration name,
1481 // followed by a size, followed by references to the visible
1482 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001483 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001484 RecordData Record;
1485 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001486 if (!Map)
1487 return 0;
1488
Douglas Gregor2cf26342009-04-09 22:27:44 +00001489 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1490 D != DEnd; ++D) {
1491 AddDeclarationName(D->first, Record);
1492 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1493 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001494 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001495 AddDeclRef(*Result.first, Record);
1496 }
1497
1498 if (Record.size() == 0)
1499 return 0;
1500
Douglas Gregorc9490c02009-04-16 22:23:12 +00001501 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001502 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001503 return Offset;
1504}
1505
Sebastian Redl1476ed42010-07-16 16:36:56 +00001506void PCHWriter::WriteTypeDeclOffsets() {
1507 using namespace llvm;
1508 RecordData Record;
1509
1510 // Write the type offsets array
1511 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1512 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1515 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1516 Record.clear();
1517 Record.push_back(pch::TYPE_OFFSET);
1518 Record.push_back(TypeOffsets.size());
1519 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1520 (const char *)&TypeOffsets.front(),
1521 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1522
1523 // Write the declaration offsets array
1524 Abbrev = new BitCodeAbbrev();
1525 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1526 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1528 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1529 Record.clear();
1530 Record.push_back(pch::DECL_OFFSET);
1531 Record.push_back(DeclOffsets.size());
1532 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1533 (const char *)&DeclOffsets.front(),
1534 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1535}
1536
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001537//===----------------------------------------------------------------------===//
1538// Global Method Pool and Selector Serialization
1539//===----------------------------------------------------------------------===//
1540
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001541namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001542// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001543class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001544 PCHWriter &Writer;
1545
1546public:
1547 typedef Selector key_type;
1548 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001550 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1551 typedef const data_type& data_type_ref;
1552
1553 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001555 static unsigned ComputeHash(Selector Sel) {
1556 unsigned N = Sel.getNumArgs();
1557 if (N == 0)
1558 ++N;
1559 unsigned R = 5381;
1560 for (unsigned I = 0; I != N; ++I)
1561 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001562 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001563 return R;
1564 }
Mike Stump1eb44332009-09-09 15:08:12 +00001565
1566 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001567 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1568 data_type_ref Methods) {
1569 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1570 clang::io::Emit16(Out, KeyLen);
1571 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001572 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001573 Method = Method->Next)
1574 if (Method->Method)
1575 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001576 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001577 Method = Method->Next)
1578 if (Method->Method)
1579 DataLen += 4;
1580 clang::io::Emit16(Out, DataLen);
1581 return std::make_pair(KeyLen, DataLen);
1582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Douglas Gregor83941df2009-04-25 17:48:32 +00001584 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001585 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001586 assert((Start >> 32) == 0 && "Selector key offset too large");
1587 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001588 unsigned N = Sel.getNumArgs();
1589 clang::io::Emit16(Out, N);
1590 if (N == 0)
1591 N = 1;
1592 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001593 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001594 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1595 }
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001597 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001598 data_type_ref Methods, unsigned DataLen) {
1599 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001601 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001602 Method = Method->Next)
1603 if (Method->Method)
1604 ++NumInstanceMethods;
1605
1606 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001607 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001608 Method = Method->Next)
1609 if (Method->Method)
1610 ++NumFactoryMethods;
1611
1612 clang::io::Emit16(Out, NumInstanceMethods);
1613 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001614 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001615 Method = Method->Next)
1616 if (Method->Method)
1617 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001618 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001619 Method = Method->Next)
1620 if (Method->Method)
1621 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001622
1623 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001624 }
1625};
1626} // end anonymous namespace
1627
1628/// \brief Write the method pool into the PCH file.
1629///
1630/// The method pool contains both instance and factory methods, stored
1631/// in an on-disk hash table indexed by the selector.
1632void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1633 using namespace llvm;
1634
1635 // Create and write out the blob that contains the instance and
1636 // factor method pools.
1637 bool Empty = true;
1638 {
1639 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001641 // Create the on-disk hash table representation. Start by
1642 // iterating through the instance method pool.
1643 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001644 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001645 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001646 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001647 InstanceEnd = SemaRef.InstanceMethodPool.end();
1648 Instance != InstanceEnd; ++Instance) {
1649 // Check whether there is a factory method with the same
1650 // selector.
1651 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1652 = SemaRef.FactoryMethodPool.find(Instance->first);
1653
1654 if (Factory == SemaRef.FactoryMethodPool.end())
1655 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001656 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001657 ObjCMethodList()));
1658 else
1659 Generator.insert(Instance->first,
1660 std::make_pair(Instance->second, Factory->second));
1661
Douglas Gregor83941df2009-04-25 17:48:32 +00001662 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001663 Empty = false;
1664 }
1665
1666 // Now iterate through the factory method pool, to pick up any
1667 // selectors that weren't already in the instance method pool.
1668 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001669 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001670 FactoryEnd = SemaRef.FactoryMethodPool.end();
1671 Factory != FactoryEnd; ++Factory) {
1672 // Check whether there is an instance method with the same
1673 // selector. If so, there is no work to do here.
1674 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1675 = SemaRef.InstanceMethodPool.find(Factory->first);
1676
Douglas Gregor83941df2009-04-25 17:48:32 +00001677 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001678 Generator.insert(Factory->first,
1679 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001680 ++NumSelectorsInMethodPool;
1681 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001682
1683 Empty = false;
1684 }
1685
Douglas Gregor83941df2009-04-25 17:48:32 +00001686 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001687 return;
1688
1689 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001690 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001691 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001692 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001693 {
1694 PCHMethodPoolTrait Trait(*this);
1695 llvm::raw_svector_ostream Out(MethodPool);
1696 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001697 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001698 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001699
1700 // For every selector that we have seen but which was not
1701 // written into the hash table, write the selector itself and
1702 // record it's offset.
1703 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1704 if (SelectorOffsets[I] == 0)
1705 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001706 }
1707
1708 // Create a blob abbreviation
1709 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1710 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1714 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1715
Douglas Gregor83941df2009-04-25 17:48:32 +00001716 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001717 RecordData Record;
1718 Record.push_back(pch::METHOD_POOL);
1719 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001720 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001721 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001722
1723 // Create a blob abbreviation for the selector table offsets.
1724 Abbrev = new BitCodeAbbrev();
1725 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1728 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1729
1730 // Write the selector offsets table.
1731 Record.clear();
1732 Record.push_back(pch::SELECTOR_OFFSETS);
1733 Record.push_back(SelectorOffsets.size());
1734 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1735 (const char *)&SelectorOffsets.front(),
1736 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001737 }
1738}
1739
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001740//===----------------------------------------------------------------------===//
1741// Identifier Table Serialization
1742//===----------------------------------------------------------------------===//
1743
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001744namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001745class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001746 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001747 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001748
Douglas Gregora92193e2009-04-28 21:18:29 +00001749 /// \brief Determines whether this is an "interesting" identifier
1750 /// that needs a full IdentifierInfo structure written into the hash
1751 /// table.
1752 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1753 return II->isPoisoned() ||
1754 II->isExtensionToken() ||
1755 II->hasMacroDefinition() ||
1756 II->getObjCOrBuiltinID() ||
1757 II->getFETokenInfo<void>();
1758 }
1759
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001760public:
1761 typedef const IdentifierInfo* key_type;
1762 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001764 typedef pch::IdentID data_type;
1765 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001766
1767 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001768 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001769
1770 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001771 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001772 }
Mike Stump1eb44332009-09-09 15:08:12 +00001773
1774 std::pair<unsigned,unsigned>
1775 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001776 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001777 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001778 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1779 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001780 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001781 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001782 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001783 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001784 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1785 DEnd = IdentifierResolver::end();
1786 D != DEnd; ++D)
1787 DataLen += sizeof(pch::DeclID);
1788 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001789 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001790 // We emit the key length after the data length so that every
1791 // string is preceded by a 16-bit length. This matches the PTH
1792 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001793 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001794 return std::make_pair(KeyLen, DataLen);
1795 }
Mike Stump1eb44332009-09-09 15:08:12 +00001796
1797 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001798 unsigned KeyLen) {
1799 // Record the location of the key data. This is used when generating
1800 // the mapping from persistent IDs to strings.
1801 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001802 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001803 }
Mike Stump1eb44332009-09-09 15:08:12 +00001804
1805 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001806 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001807 if (!isInterestingIdentifier(II)) {
1808 clang::io::Emit32(Out, ID << 1);
1809 return;
1810 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001811
Douglas Gregora92193e2009-04-28 21:18:29 +00001812 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001813 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001814 bool hasMacroDefinition =
1815 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001816 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001817 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001818 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1819 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1820 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1821 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001822 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001823
Douglas Gregor37e26842009-04-21 23:56:24 +00001824 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001825 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001826
Douglas Gregor668c1a42009-04-21 22:25:48 +00001827 // Emit the declaration IDs in reverse order, because the
1828 // IdentifierResolver provides the declarations as they would be
1829 // visible (e.g., the function "stat" would come before the struct
1830 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1831 // adds declarations to the end of the list (so we need to see the
1832 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001833 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001834 IdentifierResolver::end());
1835 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1836 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001837 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001838 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001839 }
1840};
1841} // end anonymous namespace
1842
Douglas Gregorafaf3082009-04-11 00:14:32 +00001843/// \brief Write the identifier table into the PCH file.
1844///
1845/// The identifier table consists of a blob containing string data
1846/// (the actual identifiers themselves) and a separate "offsets" index
1847/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001848void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001849 using namespace llvm;
1850
1851 // Create and write out the blob that contains the identifier
1852 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001853 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001854 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001855
Douglas Gregor92b059e2009-04-28 20:33:11 +00001856 // Look for any identifiers that were named while processing the
1857 // headers, but are otherwise not needed. We add these to the hash
1858 // table to enable checking of the predefines buffer in the case
1859 // where the user adds new macro definitions when building the PCH
1860 // file.
1861 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1862 IDEnd = PP.getIdentifierTable().end();
1863 ID != IDEnd; ++ID)
1864 getIdentifierRef(ID->second);
1865
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001866 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001867 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001868 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1869 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1870 ID != IDEnd; ++ID) {
1871 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001872 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001873 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001874
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001875 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001876 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001877 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001878 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001879 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001880 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001881 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001882 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001883 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001884 }
1885
1886 // Create a blob abbreviation
1887 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1888 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001889 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001891 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001892
1893 // Write the identifier table
1894 RecordData Record;
1895 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001896 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001897 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001898 }
1899
1900 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001901 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1902 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1905 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1906
1907 RecordData Record;
1908 Record.push_back(pch::IDENTIFIER_OFFSET);
1909 Record.push_back(IdentifierOffsets.size());
1910 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1911 (const char *)&IdentifierOffsets.front(),
1912 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001913}
1914
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001915//===----------------------------------------------------------------------===//
1916// General Serialization Routines
1917//===----------------------------------------------------------------------===//
1918
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001919/// \brief Write a record containing the given attributes.
1920void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1921 RecordData Record;
1922 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001923 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001924 Record.push_back(Attr->isInherited());
1925 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001926 default:
1927 assert(0 && "Does not support PCH writing for this attribute yet!");
1928 break;
Sean Hunt387475d2010-06-16 23:43:53 +00001929 case attr::Alias:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001930 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1931 break;
1932
Sean Hunt387475d2010-06-16 23:43:53 +00001933 case attr::AlignMac68k:
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001934 break;
1935
Sean Hunt387475d2010-06-16 23:43:53 +00001936 case attr::Aligned:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001937 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1938 break;
1939
Sean Hunt387475d2010-06-16 23:43:53 +00001940 case attr::AlwaysInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001941 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Sean Hunt387475d2010-06-16 23:43:53 +00001943 case attr::AnalyzerNoReturn:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001944 break;
1945
Sean Hunt387475d2010-06-16 23:43:53 +00001946 case attr::Annotate:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001947 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1948 break;
1949
Sean Hunt387475d2010-06-16 23:43:53 +00001950 case attr::AsmLabel:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001951 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1952 break;
1953
Sean Hunt387475d2010-06-16 23:43:53 +00001954 case attr::BaseCheck:
Sean Hunt7725e672009-11-25 04:20:27 +00001955 break;
1956
Sean Hunt387475d2010-06-16 23:43:53 +00001957 case attr::Blocks:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001958 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1959 break;
1960
Sean Hunt387475d2010-06-16 23:43:53 +00001961 case attr::CDecl:
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001962 break;
1963
Sean Hunt387475d2010-06-16 23:43:53 +00001964 case attr::Cleanup:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001965 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1966 break;
1967
Sean Hunt387475d2010-06-16 23:43:53 +00001968 case attr::Const:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001969 break;
1970
Sean Hunt387475d2010-06-16 23:43:53 +00001971 case attr::Constructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001972 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1973 break;
1974
Sean Hunt387475d2010-06-16 23:43:53 +00001975 case attr::DLLExport:
1976 case attr::DLLImport:
1977 case attr::Deprecated:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001978 break;
1979
Sean Hunt387475d2010-06-16 23:43:53 +00001980 case attr::Destructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001981 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1982 break;
1983
Sean Hunt387475d2010-06-16 23:43:53 +00001984 case attr::FastCall:
1985 case attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001986 break;
1987
Sean Hunt387475d2010-06-16 23:43:53 +00001988 case attr::Format: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001989 const FormatAttr *Format = cast<FormatAttr>(Attr);
1990 AddString(Format->getType(), Record);
1991 Record.push_back(Format->getFormatIdx());
1992 Record.push_back(Format->getFirstArg());
1993 break;
1994 }
1995
Sean Hunt387475d2010-06-16 23:43:53 +00001996 case attr::FormatArg: {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001997 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1998 Record.push_back(Format->getFormatIdx());
1999 break;
2000 }
2001
Sean Hunt387475d2010-06-16 23:43:53 +00002002 case attr::Sentinel : {
Fariborz Jahanian5b530052009-05-13 18:09:35 +00002003 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
2004 Record.push_back(Sentinel->getSentinel());
2005 Record.push_back(Sentinel->getNullPos());
2006 break;
2007 }
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Sean Hunt387475d2010-06-16 23:43:53 +00002009 case attr::GNUInline:
2010 case attr::Hiding:
2011 case attr::IBAction:
2012 case attr::IBOutlet:
2013 case attr::Malloc:
2014 case attr::NoDebug:
2015 case attr::NoInline:
2016 case attr::NoReturn:
2017 case attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002018 break;
2019
Sean Hunt387475d2010-06-16 23:43:53 +00002020 case attr::IBOutletCollection: {
Ted Kremenek857e9182010-05-19 17:38:06 +00002021 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
2022 AddDeclRef(ICA->getClass(), Record);
2023 break;
2024 }
2025
Sean Hunt387475d2010-06-16 23:43:53 +00002026 case attr::NonNull: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002027 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2028 Record.push_back(NonNull->size());
2029 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2030 break;
2031 }
2032
Sean Hunt387475d2010-06-16 23:43:53 +00002033 case attr::CFReturnsNotRetained:
2034 case attr::CFReturnsRetained:
2035 case attr::NSReturnsNotRetained:
2036 case attr::NSReturnsRetained:
2037 case attr::ObjCException:
2038 case attr::ObjCNSObject:
2039 case attr::Overloadable:
2040 case attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002041 break;
2042
Sean Hunt387475d2010-06-16 23:43:53 +00002043 case attr::MaxFieldAlignment:
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00002044 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002045 break;
2046
Sean Hunt387475d2010-06-16 23:43:53 +00002047 case attr::Packed:
Anders Carlssona860e752009-08-08 18:23:56 +00002048 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Sean Hunt387475d2010-06-16 23:43:53 +00002050 case attr::Pure:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002051 break;
2052
Sean Hunt387475d2010-06-16 23:43:53 +00002053 case attr::Regparm:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002054 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2055 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002056
Sean Hunt387475d2010-06-16 23:43:53 +00002057 case attr::ReqdWorkGroupSize:
Nate Begeman6f3d8382009-06-26 06:32:41 +00002058 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2059 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2060 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2061 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002062
Sean Hunt387475d2010-06-16 23:43:53 +00002063 case attr::Section:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002064 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2065 break;
2066
Sean Hunt387475d2010-06-16 23:43:53 +00002067 case attr::StdCall:
2068 case attr::TransparentUnion:
2069 case attr::Unavailable:
2070 case attr::Unused:
2071 case attr::Used:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002072 break;
2073
Sean Hunt387475d2010-06-16 23:43:53 +00002074 case attr::Visibility:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002075 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00002076 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002077 break;
2078
Sean Hunt387475d2010-06-16 23:43:53 +00002079 case attr::WarnUnusedResult:
2080 case attr::Weak:
2081 case attr::WeakRef:
2082 case attr::WeakImport:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002083 break;
2084 }
2085 }
2086
Douglas Gregorc9490c02009-04-16 22:23:12 +00002087 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002088}
2089
2090void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2091 Record.push_back(Str.size());
2092 Record.insert(Record.end(), Str.begin(), Str.end());
2093}
2094
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002095/// \brief Note that the identifier II occurs at the given offset
2096/// within the identifier table.
2097void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002098 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002099}
2100
Douglas Gregor83941df2009-04-25 17:48:32 +00002101/// \brief Note that the selector Sel occurs at the given offset
2102/// within the method pool/selector table.
2103void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2104 unsigned ID = SelectorIDs[Sel];
2105 assert(ID && "Unknown selector");
2106 SelectorOffsets[ID - 1] = Offset;
2107}
2108
Sebastian Redl30c514c2010-07-14 23:45:08 +00002109PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream, PCHReader *Chain)
Sebastian Redl1476ed42010-07-16 16:36:56 +00002110 : Stream(Stream), Chain(Chain), FirstDeclID(1),
2111 FirstTypeID(pch::NUM_PREDEF_TYPE_IDS),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002112 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
Sebastian Redl30c514c2010-07-14 23:45:08 +00002113 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002114 if (Chain) {
Sebastian Redl30c514c2010-07-14 23:45:08 +00002115 Chain->setDeserializationListener(this);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002116 FirstDeclID += Chain->getTotalNumDecls();
2117 FirstTypeID += Chain->getTotalNumTypes();
2118 }
2119 NextTypeID = FirstTypeID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00002120}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002121
Douglas Gregore650c8c2009-07-07 00:12:59 +00002122void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002123 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002124 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002125 Stream.Emit((unsigned)'C', 8);
2126 Stream.Emit((unsigned)'P', 8);
2127 Stream.Emit((unsigned)'C', 8);
2128 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002130 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002131
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002132 if (Chain)
Sebastian Redl30c514c2010-07-14 23:45:08 +00002133 WritePCHChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002134 else
2135 WritePCHCore(SemaRef, StatCalls, isysroot);
2136}
2137
2138void PCHWriter::WritePCHCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2139 const char *isysroot) {
2140 using namespace llvm;
2141
2142 ASTContext &Context = SemaRef.Context;
2143 Preprocessor &PP = SemaRef.PP;
2144
Douglas Gregor2cf26342009-04-09 22:27:44 +00002145 // The translation unit is the first declaration we'll emit.
2146 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002147 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002148
Douglas Gregor2deaea32009-04-22 18:49:13 +00002149 // Make sure that we emit IdentifierInfos (and any attached
2150 // declarations) for builtins.
2151 {
2152 IdentifierTable &Table = PP.getIdentifierTable();
2153 llvm::SmallVector<const char *, 32> BuiltinNames;
2154 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2155 Context.getLangOptions().NoBuiltin);
2156 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2157 getIdentifierRef(&Table.get(BuiltinNames[I]));
2158 }
2159
Chris Lattner63d65f82009-09-08 18:19:27 +00002160 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002161 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002162 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002163 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002164 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2165 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002166 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002167
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002168 // Build a record containing all of the static unused functions in this file.
2169 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002170 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002171 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002172
Douglas Gregor14c22f22009-04-22 22:18:58 +00002173 // Build a record containing all of the locally-scoped external
2174 // declarations in this header file. Generally, this record will be
2175 // empty.
2176 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002177 // FIXME: This is filling in the PCH file in densemap order which is
2178 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002179 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002180 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2181 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2182 TD != TDEnd; ++TD)
2183 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2184
Douglas Gregorb81c1702009-04-27 20:06:05 +00002185 // Build a record containing all of the ext_vector declarations.
2186 RecordData ExtVectorDecls;
2187 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2188 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2189
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002190 // Build a record containing all of the VTable uses information.
2191 RecordData VTableUses;
2192 VTableUses.push_back(SemaRef.VTableUses.size());
2193 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2194 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2195 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2196 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2197 }
2198
2199 // Build a record containing all of dynamic classes declarations.
2200 RecordData DynamicClasses;
2201 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2202 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2203
Douglas Gregor2cf26342009-04-09 22:27:44 +00002204 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002205 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002206 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002207 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002208 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002209 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002210 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002211 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002212 // Write the record of special types.
2213 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002214
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002215 AddTypeRef(Context.getBuiltinVaListType(), Record);
2216 AddTypeRef(Context.getObjCIdType(), Record);
2217 AddTypeRef(Context.getObjCSelType(), Record);
2218 AddTypeRef(Context.getObjCProtoType(), Record);
2219 AddTypeRef(Context.getObjCClassType(), Record);
2220 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2221 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2222 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002223 AddTypeRef(Context.getjmp_bufType(), Record);
2224 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002225 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2226 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002227 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002228 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002229 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2230 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002231 Record.push_back(Context.isInt128Installed());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002232 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002233
Douglas Gregor366809a2009-04-26 03:49:13 +00002234 // Keep writing types and declarations until all types and
2235 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002236 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2237 WriteDeclsBlockAbbrevs();
2238 while (!DeclTypesToEmit.empty()) {
2239 DeclOrType DOT = DeclTypesToEmit.front();
2240 DeclTypesToEmit.pop();
2241 if (DOT.isType())
2242 WriteType(DOT.getType());
2243 else
2244 WriteDecl(Context, DOT.getDecl());
2245 }
2246 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002247
Douglas Gregor813a97b2009-10-17 17:25:45 +00002248 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002249 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002250 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002251
Sebastian Redl1476ed42010-07-16 16:36:56 +00002252 WriteTypeDeclOffsets();
Douglas Gregorad1de002009-04-18 05:55:16 +00002253
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002254 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002255 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002256 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002257
2258 // Write the record containing tentative definitions.
2259 if (!TentativeDefinitions.empty())
2260 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002261
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002262 // Write the record containing unused static functions.
2263 if (!UnusedStaticFuncs.empty())
2264 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002265
Douglas Gregor14c22f22009-04-22 22:18:58 +00002266 // Write the record containing locally-scoped external definitions.
2267 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002268 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002269 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002270
2271 // Write the record containing ext_vector type names.
2272 if (!ExtVectorDecls.empty())
2273 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002274
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002275 // Write the record containing VTable uses information.
2276 if (!VTableUses.empty())
2277 Stream.EmitRecord(pch::VTABLE_USES, VTableUses);
2278
2279 // Write the record containing dynamic classes declarations.
2280 if (!DynamicClasses.empty())
2281 Stream.EmitRecord(pch::DYNAMIC_CLASSES, DynamicClasses);
2282
Douglas Gregor3e1af842009-04-17 22:13:46 +00002283 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002284 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002285 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002286 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002287 Record.push_back(NumLexicalDeclContexts);
2288 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002289 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002290 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002291}
2292
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002293void PCHWriter::WritePCHChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002294 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002295 using namespace llvm;
2296
2297 ASTContext &Context = SemaRef.Context;
2298 Preprocessor &PP = SemaRef.PP;
2299 (void)PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002300
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002301 RecordData Record;
2302 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002303 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002304 if (StatCalls && !isysroot)
2305 WriteStatCache(*StatCalls);
2306 // FIXME: Source manager block should only write new stuff, which could be
2307 // done by tracking the largest ID in the chain
2308 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002309
2310 // The special types are in the chained PCH.
2311
2312 // We don't start with the translation unit, but with its decls that
2313 // don't come from the other PCH.
2314 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
2315 // FIXME: We don't want to iterate over everything here, because it needlessly
2316 // deserializes the entire original PCH. Instead we only want to iterate over
2317 // the stuff that's already there.
2318 // All in good time, though.
2319 for (DeclContext::decl_iterator I = TU->decls_begin(), E = TU->decls_end();
2320 I != E; ++I) {
2321 if ((*I)->getPCHLevel() == 0) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002322 DeclTypesToEmit.push(*I);
2323 }
2324 }
2325
2326 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2327 WriteDeclsBlockAbbrevs();
2328 while (!DeclTypesToEmit.empty()) {
2329 DeclOrType DOT = DeclTypesToEmit.front();
2330 DeclTypesToEmit.pop();
2331 if (DOT.isType())
2332 WriteType(DOT.getType());
2333 else
2334 WriteDecl(Context, DOT.getDecl());
2335 }
2336 Stream.ExitBlock();
2337
2338 // FIXME: Preprocessor
2339 // FIXME: Method pool
2340 // FIXME: Identifier table
Sebastian Redl1476ed42010-07-16 16:36:56 +00002341 WriteTypeDeclOffsets();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002342 // FIXME: External unnamed definitions
2343 // FIXME: Tentative definitions
2344 // FIXME: Unused static functions
2345 // FIXME: Locally-scoped external definitions
2346 // FIXME: ext_vector type names
2347 // FIXME: Dynamic classes declarations
2348 // FIXME: Statistics
2349 Stream.ExitBlock();
2350}
2351
Douglas Gregor2cf26342009-04-09 22:27:44 +00002352void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2353 Record.push_back(Loc.getRawEncoding());
2354}
2355
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002356void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2357 AddSourceLocation(Range.getBegin(), Record);
2358 AddSourceLocation(Range.getEnd(), Record);
2359}
2360
Douglas Gregor2cf26342009-04-09 22:27:44 +00002361void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2362 Record.push_back(Value.getBitWidth());
2363 unsigned N = Value.getNumWords();
2364 const uint64_t* Words = Value.getRawData();
2365 for (unsigned I = 0; I != N; ++I)
2366 Record.push_back(Words[I]);
2367}
2368
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002369void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2370 Record.push_back(Value.isUnsigned());
2371 AddAPInt(Value, Record);
2372}
2373
Douglas Gregor17fc2232009-04-14 21:55:33 +00002374void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2375 AddAPInt(Value.bitcastToAPInt(), Record);
2376}
2377
Douglas Gregor2cf26342009-04-09 22:27:44 +00002378void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002379 Record.push_back(getIdentifierRef(II));
2380}
2381
2382pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2383 if (II == 0)
2384 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002385
2386 pch::IdentID &ID = IdentifierIDs[II];
2387 if (ID == 0)
2388 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002389 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002390}
2391
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002392pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2393 if (MD == 0)
2394 return 0;
2395
2396 pch::IdentID &ID = MacroDefinitions[MD];
2397 if (ID == 0)
2398 ID = MacroDefinitions.size();
2399 return ID;
2400}
2401
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002402void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2403 if (SelRef.getAsOpaquePtr() == 0) {
2404 Record.push_back(0);
2405 return;
2406 }
2407
2408 pch::SelectorID &SID = SelectorIDs[SelRef];
2409 if (SID == 0) {
2410 SID = SelectorIDs.size();
2411 SelVector.push_back(SelRef);
2412 }
2413 Record.push_back(SID);
2414}
2415
Chris Lattnerd2598362010-05-10 00:25:06 +00002416void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2417 AddDeclRef(Temp->getDestructor(), Record);
2418}
2419
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002420void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2421 const TemplateArgumentLocInfo &Arg,
2422 RecordData &Record) {
2423 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002424 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002425 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002426 break;
2427 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002428 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002429 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002430 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002431 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2432 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002433 break;
John McCall833ca992009-10-29 08:12:44 +00002434 case TemplateArgument::Null:
2435 case TemplateArgument::Integral:
2436 case TemplateArgument::Declaration:
2437 case TemplateArgument::Pack:
2438 break;
2439 }
2440}
2441
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002442void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2443 RecordData &Record) {
2444 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002445
2446 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2447 bool InfoHasSameExpr
2448 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2449 Record.push_back(InfoHasSameExpr);
2450 if (InfoHasSameExpr)
2451 return; // Avoid storing the same expr twice.
2452 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002453 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2454 Record);
2455}
2456
John McCalla93c9342009-12-07 02:54:59 +00002457void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2458 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002459 AddTypeRef(QualType(), Record);
2460 return;
2461 }
2462
John McCalla93c9342009-12-07 02:54:59 +00002463 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002464 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002465 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002466 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002467}
2468
Douglas Gregor2cf26342009-04-09 22:27:44 +00002469void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2470 if (T.isNull()) {
2471 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2472 return;
2473 }
2474
Douglas Gregora4923eb2009-11-16 21:35:15 +00002475 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002476 T.removeFastQualifiers();
2477
Douglas Gregora4923eb2009-11-16 21:35:15 +00002478 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002479 pch::TypeID &ID = TypeIDs[T];
2480 if (ID == 0) {
2481 // We haven't seen these qualifiers applied to this type before.
2482 // Assign it a new ID. This is the only time we enqueue a
2483 // qualified type, and it has no CV qualifiers.
2484 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002485 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002486 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002487
John McCall0953e762009-09-24 19:53:00 +00002488 // Encode the type qualifiers in the type reference.
2489 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2490 return;
2491 }
2492
Douglas Gregora4923eb2009-11-16 21:35:15 +00002493 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002494
Douglas Gregor2cf26342009-04-09 22:27:44 +00002495 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002496 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002497 switch (BT->getKind()) {
2498 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2499 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2500 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2501 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2502 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2503 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2504 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2505 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002506 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002507 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2508 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2509 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2510 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2511 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2512 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2513 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002514 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002515 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2516 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2517 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002518 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002519 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2520 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002521 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2522 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002523 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2524 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002525 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002526 case BuiltinType::UndeducedAuto:
2527 assert(0 && "Should not see undeduced auto here");
2528 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002529 }
2530
John McCall0953e762009-09-24 19:53:00 +00002531 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002532 return;
2533 }
2534
John McCall0953e762009-09-24 19:53:00 +00002535 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002536 if (ID == 0) {
2537 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002538 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002539 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002540 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002541 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002542
2543 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002544 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002545}
2546
2547void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2548 if (D == 0) {
2549 Record.push_back(0);
2550 return;
2551 }
2552
Douglas Gregor8038d512009-04-10 17:25:41 +00002553 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002554 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002555 // We haven't seen this declaration before. Give it a new ID and
2556 // enqueue it in the list of declarations to emit.
2557 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002558 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002559 }
2560
2561 Record.push_back(ID);
2562}
2563
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002564pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2565 if (D == 0)
2566 return 0;
2567
2568 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2569 return DeclIDs[D];
2570}
2571
Douglas Gregor2cf26342009-04-09 22:27:44 +00002572void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002573 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002574 Record.push_back(Name.getNameKind());
2575 switch (Name.getNameKind()) {
2576 case DeclarationName::Identifier:
2577 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2578 break;
2579
2580 case DeclarationName::ObjCZeroArgSelector:
2581 case DeclarationName::ObjCOneArgSelector:
2582 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002583 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002584 break;
2585
2586 case DeclarationName::CXXConstructorName:
2587 case DeclarationName::CXXDestructorName:
2588 case DeclarationName::CXXConversionFunctionName:
2589 AddTypeRef(Name.getCXXNameType(), Record);
2590 break;
2591
2592 case DeclarationName::CXXOperatorName:
2593 Record.push_back(Name.getCXXOverloadedOperator());
2594 break;
2595
Sean Hunt3e518bd2009-11-29 07:34:05 +00002596 case DeclarationName::CXXLiteralOperatorName:
2597 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2598 break;
2599
Douglas Gregor2cf26342009-04-09 22:27:44 +00002600 case DeclarationName::CXXUsingDirective:
2601 // No extra data to emit
2602 break;
2603 }
2604}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002605
2606void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2607 RecordData &Record) {
2608 // Nested name specifiers usually aren't too long. I think that 8 would
2609 // typically accomodate the vast majority.
2610 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2611
2612 // Push each of the NNS's onto a stack for serialization in reverse order.
2613 while (NNS) {
2614 NestedNames.push_back(NNS);
2615 NNS = NNS->getPrefix();
2616 }
2617
2618 Record.push_back(NestedNames.size());
2619 while(!NestedNames.empty()) {
2620 NNS = NestedNames.pop_back_val();
2621 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2622 Record.push_back(Kind);
2623 switch (Kind) {
2624 case NestedNameSpecifier::Identifier:
2625 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2626 break;
2627
2628 case NestedNameSpecifier::Namespace:
2629 AddDeclRef(NNS->getAsNamespace(), Record);
2630 break;
2631
2632 case NestedNameSpecifier::TypeSpec:
2633 case NestedNameSpecifier::TypeSpecWithTemplate:
2634 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2635 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2636 break;
2637
2638 case NestedNameSpecifier::Global:
2639 // Don't need to write an associated value.
2640 break;
2641 }
2642 }
2643}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002644
2645void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2646 TemplateName::NameKind Kind = Name.getKind();
2647 Record.push_back(Kind);
2648 switch (Kind) {
2649 case TemplateName::Template:
2650 AddDeclRef(Name.getAsTemplateDecl(), Record);
2651 break;
2652
2653 case TemplateName::OverloadedTemplate: {
2654 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2655 Record.push_back(OvT->size());
2656 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2657 I != E; ++I)
2658 AddDeclRef(*I, Record);
2659 break;
2660 }
2661
2662 case TemplateName::QualifiedTemplate: {
2663 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2664 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2665 Record.push_back(QualT->hasTemplateKeyword());
2666 AddDeclRef(QualT->getTemplateDecl(), Record);
2667 break;
2668 }
2669
2670 case TemplateName::DependentTemplate: {
2671 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2672 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2673 Record.push_back(DepT->isIdentifier());
2674 if (DepT->isIdentifier())
2675 AddIdentifierRef(DepT->getIdentifier(), Record);
2676 else
2677 Record.push_back(DepT->getOperator());
2678 break;
2679 }
2680 }
2681}
2682
2683void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2684 RecordData &Record) {
2685 Record.push_back(Arg.getKind());
2686 switch (Arg.getKind()) {
2687 case TemplateArgument::Null:
2688 break;
2689 case TemplateArgument::Type:
2690 AddTypeRef(Arg.getAsType(), Record);
2691 break;
2692 case TemplateArgument::Declaration:
2693 AddDeclRef(Arg.getAsDecl(), Record);
2694 break;
2695 case TemplateArgument::Integral:
2696 AddAPSInt(*Arg.getAsIntegral(), Record);
2697 AddTypeRef(Arg.getIntegralType(), Record);
2698 break;
2699 case TemplateArgument::Template:
2700 AddTemplateName(Arg.getAsTemplate(), Record);
2701 break;
2702 case TemplateArgument::Expression:
2703 AddStmt(Arg.getAsExpr());
2704 break;
2705 case TemplateArgument::Pack:
2706 Record.push_back(Arg.pack_size());
2707 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2708 I != E; ++I)
2709 AddTemplateArgument(*I, Record);
2710 break;
2711 }
2712}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002713
2714void
2715PCHWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
2716 RecordData &Record) {
2717 assert(TemplateParams && "No TemplateParams!");
2718 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2719 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2720 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2721 Record.push_back(TemplateParams->size());
2722 for (TemplateParameterList::const_iterator
2723 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2724 P != PEnd; ++P)
2725 AddDeclRef(*P, Record);
2726}
2727
2728/// \brief Emit a template argument list.
2729void
2730PCHWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
2731 RecordData &Record) {
2732 assert(TemplateArgs && "No TemplateArgs!");
2733 Record.push_back(TemplateArgs->flat_size());
2734 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2735 AddTemplateArgument(TemplateArgs->get(i), Record);
2736}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00002737
2738
2739void
2740PCHWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
2741 Record.push_back(Set.size());
2742 for (UnresolvedSetImpl::const_iterator
2743 I = Set.begin(), E = Set.end(); I != E; ++I) {
2744 AddDeclRef(I.getDecl(), Record);
2745 Record.push_back(I.getAccess());
2746 }
2747}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00002748
2749void PCHWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
2750 RecordData &Record) {
2751 Record.push_back(Base.isVirtual());
2752 Record.push_back(Base.isBaseOfClass());
2753 Record.push_back(Base.getAccessSpecifierAsWritten());
2754 AddTypeRef(Base.getType(), Record);
2755 AddSourceRange(Base.getSourceRange(), Record);
2756}
Sebastian Redl30c514c2010-07-14 23:45:08 +00002757
2758void PCHWriter::TypeRead(pch::TypeID ID, QualType T) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002759 TypeIDs[T] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00002760}
2761
2762void PCHWriter::DeclRead(pch::DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002763 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00002764}
2765