blob: f475db5c5bc15cb08a12c1f3fda22328c05342ff [file] [log] [blame]
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001//===--- PCHWriter.cpp - Precompiled Headers Writer -----------------------===//
Douglas Gregoref84c4b2009-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 Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl4d3af3e2010-07-09 21:00:24 +000023#include "clang/Frontend/PCHReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000025#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000026#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000027#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000029#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000030#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000031#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000032#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000034#include "llvm/ADT/APFloat.h"
35#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000036#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000037#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000038#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000039#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000040#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000041using namespace clang;
42
43//===----------------------------------------------------------------------===//
44// Type serialization
45//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000046
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000048 class PCHTypeWriter {
Douglas Gregoref84c4b2009-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 Stump11289f42009-09-09 15:08:12 +000056 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000057 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-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 Gregoref84c4b2009-04-09 22:27:44 +000065#include "clang/AST/TypeNodes.def"
66 };
67}
68
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
Douglas Gregoref84c4b2009-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 Stump11289f42009-09-09 15:08:12 +000084 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-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 Stump11289f42009-09-09 15:08:12 +000099 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-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 McCall8ccfcb52009-09-24 19:53:00 +0000107 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-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 Gregor04318252009-07-06 15:59:29 +0000123 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
124 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000125 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-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 Lattner37141f42010-06-23 06:00:24 +0000132 Record.push_back(T->getAltiVecSpecific());
Douglas Gregoref84c4b2009-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 Espindolac50c27c2010-03-30 20:24:48 +0000143 FunctionType::ExtInfo C = T->getExtInfo();
144 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000145 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000146 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000147 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-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 Redl5068f77ac2009-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 Gregoref84c4b2009-04-09 22:27:44 +0000167 Code = pch::TYPE_FUNCTION_PROTO;
168}
169
John McCallb96ec562009-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 McCallb96ec562009-12-04 22:46:56 +0000174
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
176 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000177 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
178 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179 Code = pch::TYPE_TYPEDEF;
180}
181
182void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000183 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-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 Carlsson81df7b82009-06-24 19:06:50 +0000192void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
193 Writer.AddStmt(T->getUnderlyingExpr());
194 Code = pch::TYPE_DECLTYPE;
195}
196
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197void PCHTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000198 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000200 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-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 Stump11289f42009-09-09 15:08:12 +0000214void
John McCallcebee162009-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 Gregoref84c4b2009-04-09 22:27:44 +0000223PCHTypeWriter::VisitTemplateSpecializationType(
224 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000225 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-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 Kyrtzidis45a83f92010-07-02 11:55:11 +0000231 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
232 : T->getCanonicalTypeInternal(),
233 Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000234 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
235}
236
237void
238PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-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 Kyrtzidis106caf922010-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 Kyrtzidisbfcacee2010-06-24 08:57:31 +0000263 Record.push_back(T->getKeyword());
264 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
265 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000266 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
267 : T->getCanonicalTypeInternal(),
268 Record);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000269 Code = pch::TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000270}
271
272void
273PCHTypeWriter::VisitDependentTemplateSpecializationType(
274 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-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 Gregoref84c4b2009-04-09 22:27:44 +0000283}
284
Abramo Bagnara6150c882010-05-11 21:36:43 +0000285void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000286 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000287 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
288 Writer.AddTypeRef(T->getNamedType(), Record);
Abramo Bagnara6150c882010-05-11 21:36:43 +0000289 Code = pch::TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000290}
291
John McCalle78aac42010-03-10 03:28:59 +0000292void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
293 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000294 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCalle78aac42010-03-10 03:28:59 +0000295 Code = pch::TYPE_INJECTED_CLASS_NAME;
296}
297
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000298void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
299 Writer.AddDeclRef(T->getDecl(), Record);
John McCall8b07ec22010-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 Gregoref84c4b2009-04-09 22:27:44 +0000305 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000306 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000307 E = T->qual_end(); I != E; ++I)
308 Writer.AddDeclRef(*I, Record);
John McCall8b07ec22010-05-15 11:32:37 +0000309 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000310}
311
Steve Narofffb4330f2009-06-17 22:40:22 +0000312void
313PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000314 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000315 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000316}
317
John McCall8f115c62009-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 McCall17001972009-10-18 01:05:36 +0000328#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000329#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000330 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000331#include "clang/AST/TypeLocNodes.def"
332
John McCall17001972009-10-18 01:05:36 +0000333 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
334 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000335};
336
337}
338
John McCall17001972009-10-18 01:05:36 +0000339void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
340 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000341}
John McCall17001972009-10-18 01:05:36 +0000342void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-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 McCall8f115c62009-10-16 21:56:05 +0000350}
John McCall17001972009-10-18 01:05:36 +0000351void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
352 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000353}
John McCall17001972009-10-18 01:05:36 +0000354void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
355 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000356}
John McCall17001972009-10-18 01:05:36 +0000357void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
358 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000359}
John McCall17001972009-10-18 01:05:36 +0000360void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000362}
John McCall17001972009-10-18 01:05:36 +0000363void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000365}
John McCall17001972009-10-18 01:05:36 +0000366void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000368}
John McCall17001972009-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 McCall8f115c62009-10-16 21:56:05 +0000375}
John McCall17001972009-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 McCallb96ec562009-12-04 22:46:56 +0000411void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getNameLoc(), Record);
413}
John McCall17001972009-10-18 01:05:36 +0000414void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getNameLoc(), Record);
416}
417void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000418 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
419 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
420 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000421}
422void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-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 McCall17001972009-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 McCall17001972009-10-18 01:05:36 +0000437void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
439}
John McCallcebee162009-10-18 09:09:24 +0000440void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
441 SubstTemplateTypeParmTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
443}
John McCall17001972009-10-18 01:05:36 +0000444void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
445 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-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 Kyrtzidisae85e242010-06-22 09:54:59 +0000450 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
451 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000452}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000453void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000454 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
455 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000456}
John McCalle78aac42010-03-10 03:28:59 +0000457void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getNameLoc(), Record);
459}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000460void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000461 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
462 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
464}
John McCallc392f372010-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 Kyrtzidisae85e242010-06-22 09:54:59 +0000473 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
474 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000475}
John McCall17001972009-10-18 01:05:36 +0000476void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000478}
479void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
480 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-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 McCall8f115c62009-10-16 21:56:05 +0000485}
John McCallfc93cf92009-10-22 22:37:11 +0000486void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000488}
John McCall8f115c62009-10-16 21:56:05 +0000489
Chris Lattner19cea4e2009-04-22 05:57:30 +0000490//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000491// PCHWriter Implementation
492//===----------------------------------------------------------------------===//
493
Chris Lattner28fa4e62009-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 Lattnerccac3a62009-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 Weinige83b3ac2010-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 Lattnerccac3a62009-04-27 00:49:53 +0000597#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000598}
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattner28fa4e62009-04-26 22:26:21 +0000600void PCHWriter::WriteBlockInfoBlock() {
601 RecordData Record;
602 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000603
Chris Lattner64031982009-04-27 00:40:25 +0000604#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000605#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000606
Chris Lattner28fa4e62009-04-26 22:26:21 +0000607 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000608 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000609 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000610 RECORD(TYPE_OFFSET);
611 RECORD(DECL_OFFSET);
612 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000613 RECORD(METADATA);
Chris Lattner28fa4e62009-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 Lattner90073802010-02-12 00:07:30 +0000620 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattner28fa4e62009-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 Gregor258ae542009-04-27 06:38:32 +0000625 RECORD(SOURCE_LOCATION_OFFSETS);
626 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000627 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000628 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000629 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000630 RECORD(UNUSED_STATIC_FUNCS);
631 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000632 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000633 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregoraae92242010-03-19 21:51:54 +0000634
Chris Lattner28fa4e62009-04-26 22:26:21 +0000635 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000636 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000637 RECORD(SM_SLOC_FILE_ENTRY);
638 RECORD(SM_SLOC_BUFFER_ENTRY);
639 RECORD(SM_SLOC_BUFFER_BLOB);
640 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
641 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000642
Chris Lattner28fa4e62009-04-26 22:26:21 +0000643 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000644 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000645 RECORD(PP_MACRO_OBJECT_LIKE);
646 RECORD(PP_MACRO_FUNCTION_LIKE);
647 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000648 RECORD(PP_MACRO_INSTANTIATION);
649 RECORD(PP_MACRO_DEFINITION);
650
Douglas Gregor12bfa382009-10-17 00:13:19 +0000651 // Decls and Types block.
652 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000653 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000654 RECORD(TYPE_COMPLEX);
655 RECORD(TYPE_POINTER);
656 RECORD(TYPE_BLOCK_POINTER);
657 RECORD(TYPE_LVALUE_REFERENCE);
658 RECORD(TYPE_RVALUE_REFERENCE);
659 RECORD(TYPE_MEMBER_POINTER);
660 RECORD(TYPE_CONSTANT_ARRAY);
661 RECORD(TYPE_INCOMPLETE_ARRAY);
662 RECORD(TYPE_VARIABLE_ARRAY);
663 RECORD(TYPE_VECTOR);
664 RECORD(TYPE_EXT_VECTOR);
665 RECORD(TYPE_FUNCTION_PROTO);
666 RECORD(TYPE_FUNCTION_NO_PROTO);
667 RECORD(TYPE_TYPEDEF);
668 RECORD(TYPE_TYPEOF_EXPR);
669 RECORD(TYPE_TYPEOF);
670 RECORD(TYPE_RECORD);
671 RECORD(TYPE_ENUM);
672 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000673 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000674 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000675 RECORD(DECL_ATTR);
676 RECORD(DECL_TRANSLATION_UNIT);
677 RECORD(DECL_TYPEDEF);
678 RECORD(DECL_ENUM);
679 RECORD(DECL_RECORD);
680 RECORD(DECL_ENUM_CONSTANT);
681 RECORD(DECL_FUNCTION);
682 RECORD(DECL_OBJC_METHOD);
683 RECORD(DECL_OBJC_INTERFACE);
684 RECORD(DECL_OBJC_PROTOCOL);
685 RECORD(DECL_OBJC_IVAR);
686 RECORD(DECL_OBJC_AT_DEFS_FIELD);
687 RECORD(DECL_OBJC_CLASS);
688 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
689 RECORD(DECL_OBJC_CATEGORY);
690 RECORD(DECL_OBJC_CATEGORY_IMPL);
691 RECORD(DECL_OBJC_IMPLEMENTATION);
692 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
693 RECORD(DECL_OBJC_PROPERTY);
694 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000695 RECORD(DECL_FIELD);
696 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000697 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000698 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000699 RECORD(DECL_FILE_SCOPE_ASM);
700 RECORD(DECL_BLOCK);
701 RECORD(DECL_CONTEXT_LEXICAL);
702 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000703 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000704 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000705#undef RECORD
706#undef BLOCK
707 Stream.ExitBlock();
708}
709
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000710/// \brief Adjusts the given filename to only write out the portion of the
711/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000712///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000713/// \param Filename the file name to adjust.
714///
715/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
716/// the returned filename will be adjusted by this system root.
717///
718/// \returns either the original filename (if it needs no adjustment) or the
719/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000720static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000721adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
722 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000723
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000724 if (!isysroot)
725 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000726
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000727 // Verify that the filename and the system root have the same prefix.
728 unsigned Pos = 0;
729 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
730 if (Filename[Pos] != isysroot[Pos])
731 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000732
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000733 // We hit the end of the filename before we hit the end of the system root.
734 if (!Filename[Pos])
735 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000736
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000737 // If the file name has a '/' at the current position, skip over the '/'.
738 // We distinguish sysroot-based includes from absolute includes by the
739 // absence of '/' at the beginning of sysroot-based includes.
740 if (Filename[Pos] == '/')
741 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000742
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000743 return Filename + Pos;
744}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000745
Douglas Gregor7b71e632009-04-27 22:23:34 +0000746/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Sebastian Redl85b2a6a2010-07-14 23:45:08 +0000747void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000748 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000749
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000750 // Metadata
751 const TargetInfo &Target = Context.Target;
752 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000753 MetaAbbrev->Add(BitCodeAbbrevOp(
754 Chain ? pch::CHAINED_METADATA : pch::METADATA));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000755 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
756 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
757 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
758 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
759 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000760 // Target triple or chained PCH name
761 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000762 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000764 RecordData Record;
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000765 Record.push_back(Chain ? pch::CHAINED_METADATA : pch::METADATA);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000766 Record.push_back(pch::VERSION_MAJOR);
767 Record.push_back(pch::VERSION_MINOR);
768 Record.push_back(CLANG_VERSION_MAJOR);
769 Record.push_back(CLANG_VERSION_MINOR);
770 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000771 // FIXME: This writes the absolute path for chained headers.
772 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
773 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000774
Douglas Gregor45fe0362009-05-12 01:31:05 +0000775 // Original file name
776 SourceManager &SM = Context.getSourceManager();
777 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
778 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
779 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
780 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
781 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
782
783 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000784
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000785 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000786
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000787 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000788 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000789 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000790 RecordData Record;
791 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000792 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000793 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000794
Ted Kremenek18e066f2010-01-22 22:12:47 +0000795 // Repository branch/version information.
796 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
797 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
798 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
799 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000800 Record.clear();
Ted Kremenek17437132010-01-22 20:59:36 +0000801 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000802 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
803 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000804}
805
806/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000807void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
808 RecordData Record;
809 Record.push_back(LangOpts.Trigraphs);
810 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
811 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
812 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
813 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000814 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000815 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
816 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
817 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
818 Record.push_back(LangOpts.C99); // C99 Support
819 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
820 Record.push_back(LangOpts.CPlusPlus); // C++ Support
821 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000822 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000823
Douglas Gregor55abb232009-04-10 20:39:37 +0000824 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
825 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000826 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000827 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000828 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000829 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000830 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000831
Douglas Gregor55abb232009-04-10 20:39:37 +0000832 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000833 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
834 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000835 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000836 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000837 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000838
839 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
840 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
841 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
842
Chris Lattner258172e2009-04-27 07:35:58 +0000843 // Whether static initializers are protected by locks.
844 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000845 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000846 Record.push_back(LangOpts.Blocks); // block extension to C
847 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
848 // they are unused.
849 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
850 // (modulo the platform support).
851
Chris Lattner51924e512010-06-26 21:25:03 +0000852 Record.push_back(LangOpts.getSignedOverflowBehavior());
853 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000854
855 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000856 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000857 // defined.
858 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
859 // opposed to __DYNAMIC__).
860 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
861
862 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
863 // used (instead of C99 semantics).
864 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000865 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
866 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000867 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
868 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000869 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000870 Record.push_back(LangOpts.getGCMode());
871 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000872 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000873 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000874 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000875 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000876 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000877 Record.push_back(LangOpts.SpellChecking);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000878 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000879}
880
Douglas Gregora7f71a92009-04-10 03:52:48 +0000881//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000882// stat cache Serialization
883//===----------------------------------------------------------------------===//
884
885namespace {
886// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000887class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000888public:
889 typedef const char * key_type;
890 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000891
Douglas Gregorc5046832009-04-27 18:38:38 +0000892 typedef std::pair<int, struct stat> data_type;
893 typedef const data_type& data_type_ref;
894
895 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000896 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000897 }
Mike Stump11289f42009-09-09 15:08:12 +0000898
899 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000900 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
901 data_type_ref Data) {
902 unsigned StrLen = strlen(path);
903 clang::io::Emit16(Out, StrLen);
904 unsigned DataLen = 1; // result value
905 if (Data.first == 0)
906 DataLen += 4 + 4 + 2 + 8 + 8;
907 clang::io::Emit8(Out, DataLen);
908 return std::make_pair(StrLen + 1, DataLen);
909 }
Mike Stump11289f42009-09-09 15:08:12 +0000910
Douglas Gregorc5046832009-04-27 18:38:38 +0000911 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
912 Out.write(path, KeyLen);
913 }
Mike Stump11289f42009-09-09 15:08:12 +0000914
Douglas Gregorc5046832009-04-27 18:38:38 +0000915 void EmitData(llvm::raw_ostream& Out, key_type_ref,
916 data_type_ref Data, unsigned DataLen) {
917 using namespace clang::io;
918 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000919
Douglas Gregorc5046832009-04-27 18:38:38 +0000920 // Result of stat()
921 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000922
Douglas Gregorc5046832009-04-27 18:38:38 +0000923 if (Data.first == 0) {
924 Emit32(Out, (uint32_t) Data.second.st_ino);
925 Emit32(Out, (uint32_t) Data.second.st_dev);
926 Emit16(Out, (uint16_t) Data.second.st_mode);
927 Emit64(Out, (uint64_t) Data.second.st_mtime);
928 Emit64(Out, (uint64_t) Data.second.st_size);
929 }
930
931 assert(Out.tell() - Start == DataLen && "Wrong data length");
932 }
933};
934} // end anonymous namespace
935
936/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor11cfd942010-07-12 23:48:14 +0000937void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000938 // Build the on-disk hash table containing information about every
939 // stat() call.
940 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
941 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000942 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000943 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000944 Stat != StatEnd; ++Stat, ++NumStatEntries) {
945 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000946 Generator.insert(Filename, Stat->second);
947 }
Mike Stump11289f42009-09-09 15:08:12 +0000948
Douglas Gregorc5046832009-04-27 18:38:38 +0000949 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000950 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000951 uint32_t BucketOffset;
952 {
953 llvm::raw_svector_ostream Out(StatCacheData);
954 // Make sure that no bucket is at offset 0
955 clang::io::Emit32(Out, 0);
956 BucketOffset = Generator.Emit(Out);
957 }
958
959 // Create a blob abbreviation
960 using namespace llvm;
961 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
962 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
966 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
967
968 // Write the stat cache
969 RecordData Record;
970 Record.push_back(pch::STAT_CACHE);
971 Record.push_back(BucketOffset);
972 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000973 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000974}
975
976//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000977// Source Manager Serialization
978//===----------------------------------------------------------------------===//
979
980/// \brief Create an abbreviation for the SLocEntry that refers to a
981/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000982static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000983 using namespace llvm;
984 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
985 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
986 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
987 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +0000990 // FileEntry fields.
991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000993 // HeaderFileInfo fields.
994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +0000998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000999 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001000}
1001
1002/// \brief Create an abbreviation for the SLocEntry that refers to a
1003/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001004static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001005 using namespace llvm;
1006 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1007 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001013 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001014}
1015
1016/// \brief Create an abbreviation for the SLocEntry that refers to a
1017/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001018static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001019 using namespace llvm;
1020 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1021 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001023 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001024}
1025
1026/// \brief Create an abbreviation for the SLocEntry that refers to an
1027/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001028static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001029 using namespace llvm;
1030 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1031 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001037 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001038}
1039
1040/// \brief Writes the block containing the serialized form of the
1041/// source manager.
1042///
1043/// TODO: We should probably use an on-disk hash table (stored in a
1044/// blob), indexed based on the file name, so that we only create
1045/// entries for files that we actually need. In the common case (no
1046/// errors), we probably won't have to create file entries for any of
1047/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001048void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001049 const Preprocessor &PP,
1050 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001051 RecordData Record;
1052
Chris Lattner0910e3b2009-04-10 17:16:57 +00001053 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001054 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001055
1056 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001057 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1058 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1059 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1060 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001061
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001062 // Write the line table.
1063 if (SourceMgr.hasLineTable()) {
1064 LineTableInfo &LineTable = SourceMgr.getLineTable();
1065
1066 // Emit the file names
1067 Record.push_back(LineTable.getNumFilenames());
1068 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1069 // Emit the file name
1070 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001071 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001072 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1073 Record.push_back(FilenameLen);
1074 if (FilenameLen)
1075 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001078 // Emit the line entries
1079 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1080 L != LEnd; ++L) {
1081 // Emit the file ID
1082 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001083
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001084 // Emit the line entries
1085 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001086 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001087 LEEnd = L->second.end();
1088 LE != LEEnd; ++LE) {
1089 Record.push_back(LE->FileOffset);
1090 Record.push_back(LE->LineNo);
1091 Record.push_back(LE->FilenameID);
1092 Record.push_back((unsigned)LE->FileKind);
1093 Record.push_back(LE->IncludeOffset);
1094 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001095 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +00001096 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001097 }
1098
Douglas Gregor258ae542009-04-27 06:38:32 +00001099 // Write out the source location entry table. We skip the first
1100 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001101 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001102 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001103 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1104 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1105 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1106 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001107 // Get this source location entry.
1108 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001109
Douglas Gregor258ae542009-04-27 06:38:32 +00001110 // Record the offset of this source-location entry.
1111 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1112
1113 // Figure out which record code to use.
1114 unsigned Code;
1115 if (SLoc->isFile()) {
1116 if (SLoc->getFile().getContentCache()->Entry)
1117 Code = pch::SM_SLOC_FILE_ENTRY;
1118 else
1119 Code = pch::SM_SLOC_BUFFER_ENTRY;
1120 } else
1121 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1122 Record.clear();
1123 Record.push_back(Code);
1124
1125 Record.push_back(SLoc->getOffset());
1126 if (SLoc->isFile()) {
1127 const SrcMgr::FileInfo &File = SLoc->getFile();
1128 Record.push_back(File.getIncludeLoc().getRawEncoding());
1129 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1130 Record.push_back(File.hasLineDirectives());
1131
1132 const SrcMgr::ContentCache *Content = File.getContentCache();
1133 if (Content->Entry) {
1134 // The source location entry is a file. The blob associated
1135 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001136
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001137 // Emit size/modification time for this file.
1138 Record.push_back(Content->Entry->getSize());
1139 Record.push_back(Content->Entry->getModificationTime());
1140
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001141 // Emit header-search information associated with this file.
1142 HeaderFileInfo HFI;
1143 HeaderSearch &HS = PP.getHeaderSearchInfo();
1144 if (Content->Entry->getUID() < HS.header_file_size())
1145 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1146 Record.push_back(HFI.isImport);
1147 Record.push_back(HFI.DirInfo);
1148 Record.push_back(HFI.NumIncludes);
1149 AddIdentifierRef(HFI.ControllingMacro, Record);
1150
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001151 // Turn the file name into an absolute path, if it isn't already.
1152 const char *Filename = Content->Entry->getName();
1153 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001154 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001155 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001156
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001157 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001158 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001159
1160 // FIXME: For now, preload all file source locations, so that
1161 // we get the appropriate File entries in the reader. This is
1162 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001163 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001164 } else {
1165 // The source location entry is a buffer. The blob associated
1166 // with this entry contains the contents of the buffer.
1167
1168 // We add one to the size so that we capture the trailing NULL
1169 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1170 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001171 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001172 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001173 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001174 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1175 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001176 Record.clear();
1177 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1178 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001179 llvm::StringRef(Buffer->getBufferStart(),
1180 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001181
1182 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001183 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001184 }
1185 } else {
1186 // The source location entry is an instantiation.
1187 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1188 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1189 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1190 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1191
1192 // Compute the token length for this macro expansion.
1193 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001194 if (I + 1 != N)
1195 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001196 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1197 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1198 }
1199 }
1200
Douglas Gregor8f45df52009-04-16 22:23:12 +00001201 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001202
1203 if (SLocEntryOffsets.empty())
1204 return;
1205
1206 // Write the source-location offsets table into the PCH block. This
1207 // table is used for lazily loading source-location information.
1208 using namespace llvm;
1209 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1210 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1214 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001215
Douglas Gregor258ae542009-04-27 06:38:32 +00001216 Record.clear();
1217 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1218 Record.push_back(SLocEntryOffsets.size());
1219 Record.push_back(SourceMgr.getNextOffset());
1220 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001221 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001222 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001223
1224 // Write the source location entry preloads array, telling the PCH
1225 // reader which source locations entries it should load eagerly.
1226 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001227}
1228
Douglas Gregorc5046832009-04-27 18:38:38 +00001229//===----------------------------------------------------------------------===//
1230// Preprocessor Serialization
1231//===----------------------------------------------------------------------===//
1232
Chris Lattnereeffaef2009-04-10 17:15:23 +00001233/// \brief Writes the block containing the serialized form of the
1234/// preprocessor.
1235///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001236void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001237 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001238
Chris Lattner0af3ba12009-04-13 01:29:17 +00001239 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1240 if (PP.getCounterValue() != 0) {
1241 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001242 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001243 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001244 }
1245
1246 // Enter the preprocessor block.
1247 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001248
Douglas Gregoreda6a892009-04-26 00:07:37 +00001249 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1250 // FIXME: use diagnostics subsystem for localization etc.
1251 if (PP.SawDateOrTime())
1252 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001253
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001254 // Loop over all the macro definitions that are live at the end of the file,
1255 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001256 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001257 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1258 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001259 // FIXME: This emits macros in hash table order, we should do it in a stable
1260 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001261 MacroInfo *MI = I->second;
1262
1263 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1264 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl98912122010-07-27 23:01:28 +00001265 // Also skip macros from a PCH file if we're chaining.
1266 if (MI->isBuiltinMacro() || (Chain && MI->isFromPCH()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001267 continue;
1268
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001269 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001270 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001271 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1272 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001273
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001274 unsigned Code;
1275 if (MI->isObjectLike()) {
1276 Code = pch::PP_MACRO_OBJECT_LIKE;
1277 } else {
1278 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001279
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001280 Record.push_back(MI->isC99Varargs());
1281 Record.push_back(MI->isGNUVarargs());
1282 Record.push_back(MI->getNumArgs());
1283 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1284 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001285 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001286 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001287
1288 // If we have a detailed preprocessing record, record the macro definition
1289 // ID that corresponds to this macro.
1290 if (PPRec)
1291 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1292
Douglas Gregor8f45df52009-04-16 22:23:12 +00001293 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001294 Record.clear();
1295
Chris Lattner2199f5b2009-04-10 18:08:30 +00001296 // Emit the tokens array.
1297 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1298 // Note that we know that the preprocessor does not have any annotation
1299 // tokens in it because they are created by the parser, and thus can't be
1300 // in a macro definition.
1301 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001302
Chris Lattner2199f5b2009-04-10 18:08:30 +00001303 Record.push_back(Tok.getLocation().getRawEncoding());
1304 Record.push_back(Tok.getLength());
1305
Chris Lattner2199f5b2009-04-10 18:08:30 +00001306 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1307 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001308 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001309
Chris Lattner2199f5b2009-04-10 18:08:30 +00001310 // FIXME: Should translate token kind to a stable encoding.
1311 Record.push_back(Tok.getKind());
1312 // FIXME: Should translate token flags to a stable encoding.
1313 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001314
Douglas Gregor8f45df52009-04-16 22:23:12 +00001315 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001316 Record.clear();
1317 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001318 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001319 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001320
1321 // If the preprocessor has a preprocessing record, emit it.
1322 unsigned NumPreprocessingRecords = 0;
1323 if (PPRec) {
1324 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1325 E != EEnd; ++E) {
1326 Record.clear();
1327
1328 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1329 Record.push_back(NumPreprocessingRecords++);
1330 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1331 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1332 AddIdentifierRef(MI->getName(), Record);
1333 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1334 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1335 continue;
1336 }
1337
1338 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1339 // Record this macro definition's location.
1340 pch::IdentID ID = getMacroDefinitionID(MD);
1341 if (ID != MacroDefinitionOffsets.size()) {
1342 if (ID > MacroDefinitionOffsets.size())
1343 MacroDefinitionOffsets.resize(ID + 1);
1344
1345 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1346 } else
1347 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1348
1349 Record.push_back(NumPreprocessingRecords++);
1350 Record.push_back(ID);
1351 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1352 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1353 AddIdentifierRef(MD->getName(), Record);
1354 AddSourceLocation(MD->getLocation(), Record);
1355 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1356 continue;
1357 }
1358 }
1359 }
1360
Douglas Gregor8f45df52009-04-16 22:23:12 +00001361 Stream.ExitBlock();
Douglas Gregoraae92242010-03-19 21:51:54 +00001362
1363 // Write the offsets table for the preprocessing record.
1364 if (NumPreprocessingRecords > 0) {
1365 // Write the offsets table for identifier IDs.
1366 using namespace llvm;
1367 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1368 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1372 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1373
1374 Record.clear();
1375 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1376 Record.push_back(NumPreprocessingRecords);
1377 Record.push_back(MacroDefinitionOffsets.size());
1378 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1379 (const char *)&MacroDefinitionOffsets.front(),
1380 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1381 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001382}
1383
Douglas Gregorc5046832009-04-27 18:38:38 +00001384//===----------------------------------------------------------------------===//
1385// Type Serialization
1386//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001387
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001388/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001389void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001390 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001391 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001392 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001393
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001394 // Record the offset for this type.
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001395 unsigned Index = ID - FirstTypeID;
1396 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001397 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001398 else if (TypeOffsets.size() < Index) {
1399 TypeOffsets.resize(Index + 1);
1400 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001401 }
1402
1403 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001404
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001405 // Emit the type's representation.
1406 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001407
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001408 if (T.hasLocalNonFastQualifiers()) {
1409 Qualifiers Qs = T.getLocalQualifiers();
1410 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001411 Record.push_back(Qs.getAsOpaqueValue());
1412 W.Code = pch::TYPE_EXT_QUAL;
1413 } else {
1414 switch (T->getTypeClass()) {
1415 // For all of the concrete, non-dependent types, call the
1416 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001417#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001418 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001419#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001420#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001421 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001422 }
1423
1424 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001425 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001426
1427 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001428 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001429}
1430
Douglas Gregorc5046832009-04-27 18:38:38 +00001431//===----------------------------------------------------------------------===//
1432// Declaration Serialization
1433//===----------------------------------------------------------------------===//
1434
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001435/// \brief Write the block containing all of the declaration IDs
1436/// lexically declared within the given DeclContext.
1437///
1438/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1439/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001440uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001441 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001442 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001443 return 0;
1444
Douglas Gregor8f45df52009-04-16 22:23:12 +00001445 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001446 RecordData Record;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001447 Record.push_back(pch::DECL_CONTEXT_LEXICAL);
1448 llvm::SmallVector<pch::DeclID, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001449 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1450 D != DEnd; ++D)
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001451 Decls.push_back(GetDeclRef(*D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001452
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001453 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001454 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
1455 reinterpret_cast<char*>(Decls.data()), Decls.size() * sizeof(pch::DeclID));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001456 return Offset;
1457}
1458
1459/// \brief Write the block containing all of the declaration IDs
1460/// visible from the given DeclContext.
1461///
1462/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1463/// bistream, or 0 if no block was written.
1464uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1465 DeclContext *DC) {
1466 if (DC->getPrimaryContext() != DC)
1467 return 0;
1468
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001469 // Since there is no name lookup into functions or methods, don't bother to
1470 // build a visible-declarations table for these entities.
1471 if (DC->isFunctionOrMethod())
1472 return 0;
1473
1474 // If not in C++, we perform name lookup for the translation unit via the
1475 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1476 // FIXME: In C++ we need the visible declarations in order to "see" the
1477 // friend declarations, is there a way to do this without writing the table ?
1478 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor13d190f2009-04-18 15:49:20 +00001479 return 0;
1480
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001481 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001482 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001483
1484 // Serialize the contents of the mapping used for lookup. Note that,
1485 // although we have two very different code paths, the serialized
1486 // representation is the same for both cases: a declaration name,
1487 // followed by a size, followed by references to the visible
1488 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001489 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001490 RecordData Record;
1491 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001492 if (!Map)
1493 return 0;
1494
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001495 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1496 D != DEnd; ++D) {
1497 AddDeclarationName(D->first, Record);
1498 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1499 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001500 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001501 AddDeclRef(*Result.first, Record);
1502 }
1503
1504 if (Record.size() == 0)
1505 return 0;
1506
Douglas Gregor8f45df52009-04-16 22:23:12 +00001507 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001508 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001509 return Offset;
1510}
1511
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001512void PCHWriter::WriteTypeDeclOffsets() {
1513 using namespace llvm;
1514 RecordData Record;
1515
1516 // Write the type offsets array
1517 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1518 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1521 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1522 Record.clear();
1523 Record.push_back(pch::TYPE_OFFSET);
1524 Record.push_back(TypeOffsets.size());
1525 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1526 (const char *)&TypeOffsets.front(),
1527 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1528
1529 // Write the declaration offsets array
1530 Abbrev = new BitCodeAbbrev();
1531 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1534 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1535 Record.clear();
1536 Record.push_back(pch::DECL_OFFSET);
1537 Record.push_back(DeclOffsets.size());
1538 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1539 (const char *)&DeclOffsets.front(),
1540 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1541}
1542
Douglas Gregorc5046832009-04-27 18:38:38 +00001543//===----------------------------------------------------------------------===//
1544// Global Method Pool and Selector Serialization
1545//===----------------------------------------------------------------------===//
1546
Douglas Gregore84a9da2009-04-20 20:36:09 +00001547namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001548// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001549class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001550 PCHWriter &Writer;
1551
1552public:
1553 typedef Selector key_type;
1554 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001555
Douglas Gregorc78d3462009-04-24 21:10:55 +00001556 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1557 typedef const data_type& data_type_ref;
1558
1559 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001560
Douglas Gregorc78d3462009-04-24 21:10:55 +00001561 static unsigned ComputeHash(Selector Sel) {
1562 unsigned N = Sel.getNumArgs();
1563 if (N == 0)
1564 ++N;
1565 unsigned R = 5381;
1566 for (unsigned I = 0; I != N; ++I)
1567 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001568 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001569 return R;
1570 }
Mike Stump11289f42009-09-09 15:08:12 +00001571
1572 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001573 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1574 data_type_ref Methods) {
1575 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1576 clang::io::Emit16(Out, KeyLen);
1577 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001578 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001579 Method = Method->Next)
1580 if (Method->Method)
1581 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001582 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001583 Method = Method->Next)
1584 if (Method->Method)
1585 DataLen += 4;
1586 clang::io::Emit16(Out, DataLen);
1587 return std::make_pair(KeyLen, DataLen);
1588 }
Mike Stump11289f42009-09-09 15:08:12 +00001589
Douglas Gregor95c13f52009-04-25 17:48:32 +00001590 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001591 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001592 assert((Start >> 32) == 0 && "Selector key offset too large");
1593 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001594 unsigned N = Sel.getNumArgs();
1595 clang::io::Emit16(Out, N);
1596 if (N == 0)
1597 N = 1;
1598 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001599 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001600 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1601 }
Mike Stump11289f42009-09-09 15:08:12 +00001602
Douglas Gregorc78d3462009-04-24 21:10:55 +00001603 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001604 data_type_ref Methods, unsigned DataLen) {
1605 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001606 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001607 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001608 Method = Method->Next)
1609 if (Method->Method)
1610 ++NumInstanceMethods;
1611
1612 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001613 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001614 Method = Method->Next)
1615 if (Method->Method)
1616 ++NumFactoryMethods;
1617
1618 clang::io::Emit16(Out, NumInstanceMethods);
1619 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001620 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001621 Method = Method->Next)
1622 if (Method->Method)
1623 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001624 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001625 Method = Method->Next)
1626 if (Method->Method)
1627 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001628
1629 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001630 }
1631};
1632} // end anonymous namespace
1633
1634/// \brief Write the method pool into the PCH file.
1635///
1636/// The method pool contains both instance and factory methods, stored
1637/// in an on-disk hash table indexed by the selector.
1638void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1639 using namespace llvm;
1640
1641 // Create and write out the blob that contains the instance and
1642 // factor method pools.
1643 bool Empty = true;
1644 {
1645 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001646
Douglas Gregorc78d3462009-04-24 21:10:55 +00001647 // Create the on-disk hash table representation. Start by
1648 // iterating through the instance method pool.
1649 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001650 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001651 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001652 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001653 InstanceEnd = SemaRef.InstanceMethodPool.end();
1654 Instance != InstanceEnd; ++Instance) {
1655 // Check whether there is a factory method with the same
1656 // selector.
1657 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1658 = SemaRef.FactoryMethodPool.find(Instance->first);
1659
1660 if (Factory == SemaRef.FactoryMethodPool.end())
1661 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001662 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001663 ObjCMethodList()));
1664 else
1665 Generator.insert(Instance->first,
1666 std::make_pair(Instance->second, Factory->second));
1667
Douglas Gregor95c13f52009-04-25 17:48:32 +00001668 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001669 Empty = false;
1670 }
1671
1672 // Now iterate through the factory method pool, to pick up any
1673 // selectors that weren't already in the instance method pool.
1674 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001675 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001676 FactoryEnd = SemaRef.FactoryMethodPool.end();
1677 Factory != FactoryEnd; ++Factory) {
1678 // Check whether there is an instance method with the same
1679 // selector. If so, there is no work to do here.
1680 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1681 = SemaRef.InstanceMethodPool.find(Factory->first);
1682
Douglas Gregor95c13f52009-04-25 17:48:32 +00001683 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001684 Generator.insert(Factory->first,
1685 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001686 ++NumSelectorsInMethodPool;
1687 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001688
1689 Empty = false;
1690 }
1691
Douglas Gregor95c13f52009-04-25 17:48:32 +00001692 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001693 return;
1694
1695 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001696 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001697 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001698 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001699 {
1700 PCHMethodPoolTrait Trait(*this);
1701 llvm::raw_svector_ostream Out(MethodPool);
1702 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001703 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001704 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001705
1706 // For every selector that we have seen but which was not
1707 // written into the hash table, write the selector itself and
1708 // record it's offset.
1709 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1710 if (SelectorOffsets[I] == 0)
1711 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001712 }
1713
1714 // Create a blob abbreviation
1715 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1716 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1717 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1720 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1721
Douglas Gregor95c13f52009-04-25 17:48:32 +00001722 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001723 RecordData Record;
1724 Record.push_back(pch::METHOD_POOL);
1725 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001726 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001727 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001728
1729 // Create a blob abbreviation for the selector table offsets.
1730 Abbrev = new BitCodeAbbrev();
1731 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1734 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1735
1736 // Write the selector offsets table.
1737 Record.clear();
1738 Record.push_back(pch::SELECTOR_OFFSETS);
1739 Record.push_back(SelectorOffsets.size());
1740 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1741 (const char *)&SelectorOffsets.front(),
1742 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001743 }
1744}
1745
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001746/// \brief Write the selectors referenced in @selector expression into PCH file.
1747void PCHWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
1748 using namespace llvm;
1749 if (SemaRef.ReferencedSelectors.empty())
1750 return;
1751
1752 RecordData Record;
1753
1754 Record.push_back(SemaRef.ReferencedSelectors.size());
1755 for (DenseMap<Selector, SourceLocation>::iterator S =
1756 SemaRef.ReferencedSelectors.begin(),
1757 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1758 Selector Sel = (*S).first;
1759 SourceLocation Loc = (*S).second;
1760 AddSelectorRef(Sel, Record);
1761 AddSourceLocation(Loc, Record);
1762 }
1763 Stream.EmitRecord(pch::REFERENCED_SELECTOR_POOL, Record);
1764}
1765
Douglas Gregorc5046832009-04-27 18:38:38 +00001766//===----------------------------------------------------------------------===//
1767// Identifier Table Serialization
1768//===----------------------------------------------------------------------===//
1769
Douglas Gregorc78d3462009-04-24 21:10:55 +00001770namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001771class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001772 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001773 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001774
Douglas Gregor1d583f22009-04-28 21:18:29 +00001775 /// \brief Determines whether this is an "interesting" identifier
1776 /// that needs a full IdentifierInfo structure written into the hash
1777 /// table.
1778 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1779 return II->isPoisoned() ||
1780 II->isExtensionToken() ||
1781 II->hasMacroDefinition() ||
1782 II->getObjCOrBuiltinID() ||
1783 II->getFETokenInfo<void>();
1784 }
1785
Douglas Gregore84a9da2009-04-20 20:36:09 +00001786public:
1787 typedef const IdentifierInfo* key_type;
1788 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001789
Douglas Gregore84a9da2009-04-20 20:36:09 +00001790 typedef pch::IdentID data_type;
1791 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001792
1793 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001794 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001795
1796 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001797 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001798 }
Mike Stump11289f42009-09-09 15:08:12 +00001799
1800 std::pair<unsigned,unsigned>
1801 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001802 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001803 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001804 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1805 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001806 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001807 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001808 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001809 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001810 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1811 DEnd = IdentifierResolver::end();
1812 D != DEnd; ++D)
1813 DataLen += sizeof(pch::DeclID);
1814 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001815 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001816 // We emit the key length after the data length so that every
1817 // string is preceded by a 16-bit length. This matches the PTH
1818 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001819 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001820 return std::make_pair(KeyLen, DataLen);
1821 }
Mike Stump11289f42009-09-09 15:08:12 +00001822
1823 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001824 unsigned KeyLen) {
1825 // Record the location of the key data. This is used when generating
1826 // the mapping from persistent IDs to strings.
1827 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001828 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001829 }
Mike Stump11289f42009-09-09 15:08:12 +00001830
1831 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001832 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001833 if (!isInterestingIdentifier(II)) {
1834 clang::io::Emit32(Out, ID << 1);
1835 return;
1836 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001837
Douglas Gregor1d583f22009-04-28 21:18:29 +00001838 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001839 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001840 bool hasMacroDefinition =
1841 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001842 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001843 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001844 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1845 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1846 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1847 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001848 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001849
Douglas Gregorc3366a52009-04-21 23:56:24 +00001850 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001851 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001852
Douglas Gregora868bbd2009-04-21 22:25:48 +00001853 // Emit the declaration IDs in reverse order, because the
1854 // IdentifierResolver provides the declarations as they would be
1855 // visible (e.g., the function "stat" would come before the struct
1856 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1857 // adds declarations to the end of the list (so we need to see the
1858 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001859 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001860 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001861 IdentifierResolver::end());
1862 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1863 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001864 D != DEnd; ++D)
Sebastian Redlff4a2952010-07-23 23:49:55 +00001865 if (!Writer.hasChain() || (*D)->getPCHLevel() == 0)
1866 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001867 }
1868};
1869} // end anonymous namespace
1870
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001871/// \brief Write the identifier table into the PCH file.
1872///
1873/// The identifier table consists of a blob containing string data
1874/// (the actual identifiers themselves) and a separate "offsets" index
1875/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001876void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001877 using namespace llvm;
1878
1879 // Create and write out the blob that contains the identifier
1880 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001881 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001882 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001883
Douglas Gregore6648fb2009-04-28 20:33:11 +00001884 // Look for any identifiers that were named while processing the
1885 // headers, but are otherwise not needed. We add these to the hash
1886 // table to enable checking of the predefines buffer in the case
1887 // where the user adds new macro definitions when building the PCH
1888 // file.
1889 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1890 IDEnd = PP.getIdentifierTable().end();
1891 ID != IDEnd; ++ID)
1892 getIdentifierRef(ID->second);
1893
Sebastian Redlff4a2952010-07-23 23:49:55 +00001894 // Create the on-disk hash table representation. We only store offsets
1895 // for identifiers that appear here for the first time.
1896 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001897 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1898 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1899 ID != IDEnd; ++ID) {
1900 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redlff4a2952010-07-23 23:49:55 +00001901 // FIXME: Right now, we only write identifiers that are new to this file.
1902 // We need to write older identifiers that changed too, though.
1903 if (ID->second >= FirstIdentID)
1904 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001905 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001906
Douglas Gregore84a9da2009-04-20 20:36:09 +00001907 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001908 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001909 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001910 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001911 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001912 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001913 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001914 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001915 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001916 }
1917
1918 // Create a blob abbreviation
1919 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1920 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001923 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001924
1925 // Write the identifier table
1926 RecordData Record;
1927 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001928 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001929 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001930 }
1931
1932 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001933 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1934 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1937 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1938
1939 RecordData Record;
1940 Record.push_back(pch::IDENTIFIER_OFFSET);
1941 Record.push_back(IdentifierOffsets.size());
1942 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1943 (const char *)&IdentifierOffsets.front(),
1944 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001945}
1946
Douglas Gregorc5046832009-04-27 18:38:38 +00001947//===----------------------------------------------------------------------===//
1948// General Serialization Routines
1949//===----------------------------------------------------------------------===//
1950
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001951/// \brief Write a record containing the given attributes.
1952void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1953 RecordData Record;
1954 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001955 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001956 Record.push_back(Attr->isInherited());
1957 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001958 default:
1959 assert(0 && "Does not support PCH writing for this attribute yet!");
1960 break;
Alexis Hunt344393e2010-06-16 23:43:53 +00001961 case attr::Alias:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001962 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1963 break;
1964
Alexis Hunt344393e2010-06-16 23:43:53 +00001965 case attr::AlignMac68k:
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00001966 break;
1967
Alexis Hunt344393e2010-06-16 23:43:53 +00001968 case attr::Aligned:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001969 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1970 break;
1971
Alexis Hunt344393e2010-06-16 23:43:53 +00001972 case attr::AlwaysInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001973 break;
Mike Stump11289f42009-09-09 15:08:12 +00001974
Alexis Hunt344393e2010-06-16 23:43:53 +00001975 case attr::AnalyzerNoReturn:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001976 break;
1977
Alexis Hunt344393e2010-06-16 23:43:53 +00001978 case attr::Annotate:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001979 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1980 break;
1981
Alexis Hunt344393e2010-06-16 23:43:53 +00001982 case attr::AsmLabel:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001983 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1984 break;
1985
Alexis Hunt344393e2010-06-16 23:43:53 +00001986 case attr::BaseCheck:
Alexis Hunt54a02542009-11-25 04:20:27 +00001987 break;
1988
Alexis Hunt344393e2010-06-16 23:43:53 +00001989 case attr::Blocks:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001990 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1991 break;
1992
Alexis Hunt344393e2010-06-16 23:43:53 +00001993 case attr::CDecl:
Eli Friedmane4310c82009-11-09 18:38:53 +00001994 break;
1995
Alexis Hunt344393e2010-06-16 23:43:53 +00001996 case attr::Cleanup:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001997 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1998 break;
1999
Alexis Hunt344393e2010-06-16 23:43:53 +00002000 case attr::Const:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002001 break;
2002
Alexis Hunt344393e2010-06-16 23:43:53 +00002003 case attr::Constructor:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002004 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2005 break;
2006
Alexis Hunt344393e2010-06-16 23:43:53 +00002007 case attr::DLLExport:
2008 case attr::DLLImport:
2009 case attr::Deprecated:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002010 break;
2011
Alexis Hunt344393e2010-06-16 23:43:53 +00002012 case attr::Destructor:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002013 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2014 break;
2015
Alexis Hunt344393e2010-06-16 23:43:53 +00002016 case attr::FastCall:
2017 case attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002018 break;
2019
Alexis Hunt344393e2010-06-16 23:43:53 +00002020 case attr::Format: {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002021 const FormatAttr *Format = cast<FormatAttr>(Attr);
2022 AddString(Format->getType(), Record);
2023 Record.push_back(Format->getFormatIdx());
2024 Record.push_back(Format->getFirstArg());
2025 break;
2026 }
2027
Alexis Hunt344393e2010-06-16 23:43:53 +00002028 case attr::FormatArg: {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00002029 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
2030 Record.push_back(Format->getFormatIdx());
2031 break;
2032 }
2033
Alexis Hunt344393e2010-06-16 23:43:53 +00002034 case attr::Sentinel : {
Fariborz Jahanian027b8862009-05-13 18:09:35 +00002035 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
2036 Record.push_back(Sentinel->getSentinel());
2037 Record.push_back(Sentinel->getNullPos());
2038 break;
2039 }
Mike Stump11289f42009-09-09 15:08:12 +00002040
Alexis Hunt344393e2010-06-16 23:43:53 +00002041 case attr::GNUInline:
2042 case attr::Hiding:
2043 case attr::IBAction:
2044 case attr::IBOutlet:
2045 case attr::Malloc:
2046 case attr::NoDebug:
2047 case attr::NoInline:
2048 case attr::NoReturn:
2049 case attr::NoThrow:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002050 break;
2051
Alexis Hunt344393e2010-06-16 23:43:53 +00002052 case attr::IBOutletCollection: {
Ted Kremenek26bde772010-05-19 17:38:06 +00002053 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
2054 AddDeclRef(ICA->getClass(), Record);
2055 break;
2056 }
2057
Alexis Hunt344393e2010-06-16 23:43:53 +00002058 case attr::NonNull: {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002059 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2060 Record.push_back(NonNull->size());
2061 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2062 break;
2063 }
2064
Alexis Hunt344393e2010-06-16 23:43:53 +00002065 case attr::CFReturnsNotRetained:
2066 case attr::CFReturnsRetained:
2067 case attr::NSReturnsNotRetained:
2068 case attr::NSReturnsRetained:
2069 case attr::ObjCException:
2070 case attr::ObjCNSObject:
2071 case attr::Overloadable:
2072 case attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002073 break;
2074
Alexis Hunt344393e2010-06-16 23:43:53 +00002075 case attr::MaxFieldAlignment:
Daniel Dunbar40130442010-05-27 01:12:46 +00002076 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002077 break;
2078
Alexis Hunt344393e2010-06-16 23:43:53 +00002079 case attr::Packed:
Anders Carlsson68e0b682009-08-08 18:23:56 +00002080 break;
Mike Stump11289f42009-09-09 15:08:12 +00002081
Alexis Hunt344393e2010-06-16 23:43:53 +00002082 case attr::Pure:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002083 break;
2084
Alexis Hunt344393e2010-06-16 23:43:53 +00002085 case attr::Regparm:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002086 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2087 break;
Mike Stump11289f42009-09-09 15:08:12 +00002088
Alexis Hunt344393e2010-06-16 23:43:53 +00002089 case attr::ReqdWorkGroupSize:
Nate Begemanf2758702009-06-26 06:32:41 +00002090 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2091 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2092 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2093 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002094
Alexis Hunt344393e2010-06-16 23:43:53 +00002095 case attr::Section:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002096 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2097 break;
2098
Alexis Hunt344393e2010-06-16 23:43:53 +00002099 case attr::StdCall:
2100 case attr::TransparentUnion:
2101 case attr::Unavailable:
2102 case attr::Unused:
2103 case attr::Used:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002104 break;
2105
Alexis Hunt344393e2010-06-16 23:43:53 +00002106 case attr::Visibility:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002107 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00002108 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002109 break;
2110
Alexis Hunt344393e2010-06-16 23:43:53 +00002111 case attr::WarnUnusedResult:
2112 case attr::Weak:
2113 case attr::WeakRef:
2114 case attr::WeakImport:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002115 break;
2116 }
2117 }
2118
Douglas Gregor8f45df52009-04-16 22:23:12 +00002119 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002120}
2121
2122void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2123 Record.push_back(Str.size());
2124 Record.insert(Record.end(), Str.begin(), Str.end());
2125}
2126
Douglas Gregore84a9da2009-04-20 20:36:09 +00002127/// \brief Note that the identifier II occurs at the given offset
2128/// within the identifier table.
2129void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00002130 pch::IdentID ID = IdentifierIDs[II];
2131 // Only store offsets new to this PCH file. Other identifier names are looked
2132 // up earlier in the chain and thus don't need an offset.
2133 if (ID >= FirstIdentID)
2134 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002135}
2136
Douglas Gregor95c13f52009-04-25 17:48:32 +00002137/// \brief Note that the selector Sel occurs at the given offset
2138/// within the method pool/selector table.
2139void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2140 unsigned ID = SelectorIDs[Sel];
2141 assert(ID && "Unknown selector");
2142 SelectorOffsets[ID - 1] = Offset;
2143}
2144
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002145PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream, PCHReader *Chain)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002146 : Stream(Stream), Chain(Chain), FirstDeclID(1),
Sebastian Redlff4a2952010-07-23 23:49:55 +00002147 FirstTypeID(pch::NUM_PREDEF_TYPE_IDS), FirstIdentID(1),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002148 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002149 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002150 if (Chain) {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002151 Chain->setDeserializationListener(this);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002152 FirstDeclID += Chain->getTotalNumDecls();
2153 FirstTypeID += Chain->getTotalNumTypes();
Sebastian Redlff4a2952010-07-23 23:49:55 +00002154 FirstIdentID += Chain->getTotalNumIdentifiers();
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002155 }
Sebastian Redlff4a2952010-07-23 23:49:55 +00002156 NextDeclID = FirstDeclID;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002157 NextTypeID = FirstTypeID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002158 NextIdentID = FirstIdentID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002159}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002160
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002161void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002162 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002163 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002164 Stream.Emit((unsigned)'C', 8);
2165 Stream.Emit((unsigned)'P', 8);
2166 Stream.Emit((unsigned)'C', 8);
2167 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002168
Chris Lattner28fa4e62009-04-26 22:26:21 +00002169 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002170
Sebastian Redl143413f2010-07-12 22:02:52 +00002171 if (Chain)
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002172 WritePCHChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002173 else
2174 WritePCHCore(SemaRef, StatCalls, isysroot);
2175}
2176
2177void PCHWriter::WritePCHCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2178 const char *isysroot) {
2179 using namespace llvm;
2180
2181 ASTContext &Context = SemaRef.Context;
2182 Preprocessor &PP = SemaRef.PP;
2183
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002184 // The translation unit is the first declaration we'll emit.
2185 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002186 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002187 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002188
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002189 // Make sure that we emit IdentifierInfos (and any attached
2190 // declarations) for builtins.
2191 {
2192 IdentifierTable &Table = PP.getIdentifierTable();
2193 llvm::SmallVector<const char *, 32> BuiltinNames;
2194 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2195 Context.getLangOptions().NoBuiltin);
2196 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2197 getIdentifierRef(&Table.get(BuiltinNames[I]));
2198 }
2199
Chris Lattner0c797362009-09-08 18:19:27 +00002200 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002201 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002202 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002203 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002204 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2205 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002206 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002207
Tanya Lattner90073802010-02-12 00:07:30 +00002208 // Build a record containing all of the static unused functions in this file.
2209 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002210 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattner90073802010-02-12 00:07:30 +00002211 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002212
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002213 // Build a record containing all of the locally-scoped external
2214 // declarations in this header file. Generally, this record will be
2215 // empty.
2216 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00002217 // FIXME: This is filling in the PCH file in densemap order which is
2218 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002219 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002220 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2221 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2222 TD != TDEnd; ++TD)
2223 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2224
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002225 // Build a record containing all of the ext_vector declarations.
2226 RecordData ExtVectorDecls;
2227 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2228 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2229
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002230 // Build a record containing all of the VTable uses information.
2231 RecordData VTableUses;
2232 VTableUses.push_back(SemaRef.VTableUses.size());
2233 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2234 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2235 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2236 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2237 }
2238
2239 // Build a record containing all of dynamic classes declarations.
2240 RecordData DynamicClasses;
2241 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2242 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2243
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002244 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002245 RecordData Record;
Douglas Gregoraae92242010-03-19 21:51:54 +00002246 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002247 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002248 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002249 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002250 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002251 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002252 // Write the record of special types.
2253 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002254
Steve Naroffc277ad12009-07-18 15:33:26 +00002255 AddTypeRef(Context.getBuiltinVaListType(), Record);
2256 AddTypeRef(Context.getObjCIdType(), Record);
2257 AddTypeRef(Context.getObjCSelType(), Record);
2258 AddTypeRef(Context.getObjCProtoType(), Record);
2259 AddTypeRef(Context.getObjCClassType(), Record);
2260 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2261 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2262 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002263 AddTypeRef(Context.getjmp_bufType(), Record);
2264 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002265 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2266 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002267 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002268 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002269 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2270 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002271 Record.push_back(Context.isInt128Installed());
Steve Naroffc277ad12009-07-18 15:33:26 +00002272 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002273
Douglas Gregor1970d882009-04-26 03:49:13 +00002274 // Keep writing types and declarations until all types and
2275 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002276 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2277 WriteDeclsBlockAbbrevs();
2278 while (!DeclTypesToEmit.empty()) {
2279 DeclOrType DOT = DeclTypesToEmit.front();
2280 DeclTypesToEmit.pop();
2281 if (DOT.isType())
2282 WriteType(DOT.getType());
2283 else
2284 WriteDecl(Context, DOT.getDecl());
2285 }
2286 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002287
Douglas Gregor45053152009-10-17 17:25:45 +00002288 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002289 WriteMethodPool(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002290 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002291 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002292
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002293 WriteTypeDeclOffsets();
Douglas Gregor652d82a2009-04-18 05:55:16 +00002294
Douglas Gregord4df8652009-04-22 22:02:47 +00002295 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002296 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002297 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002298
2299 // Write the record containing tentative definitions.
2300 if (!TentativeDefinitions.empty())
2301 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002302
Tanya Lattner90073802010-02-12 00:07:30 +00002303 // Write the record containing unused static functions.
2304 if (!UnusedStaticFuncs.empty())
2305 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002306
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002307 // Write the record containing locally-scoped external definitions.
2308 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002309 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002310 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002311
2312 // Write the record containing ext_vector type names.
2313 if (!ExtVectorDecls.empty())
2314 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002315
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002316 // Write the record containing VTable uses information.
2317 if (!VTableUses.empty())
2318 Stream.EmitRecord(pch::VTABLE_USES, VTableUses);
2319
2320 // Write the record containing dynamic classes declarations.
2321 if (!DynamicClasses.empty())
2322 Stream.EmitRecord(pch::DYNAMIC_CLASSES, DynamicClasses);
2323
Douglas Gregor08f01292009-04-17 22:13:46 +00002324 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002325 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002326 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002327 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002328 Record.push_back(NumLexicalDeclContexts);
2329 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002330 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002331 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002332}
2333
Sebastian Redl143413f2010-07-12 22:02:52 +00002334void PCHWriter::WritePCHChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002335 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002336 using namespace llvm;
2337
2338 ASTContext &Context = SemaRef.Context;
2339 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002340
Sebastian Redl143413f2010-07-12 22:02:52 +00002341 RecordData Record;
2342 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002343 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002344 if (StatCalls && !isysroot)
2345 WriteStatCache(*StatCalls);
2346 // FIXME: Source manager block should only write new stuff, which could be
2347 // done by tracking the largest ID in the chain
2348 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002349
2350 // The special types are in the chained PCH.
2351
2352 // We don't start with the translation unit, but with its decls that
2353 // don't come from the other PCH.
2354 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Sebastian Redlff4a2952010-07-23 23:49:55 +00002355 // The TU was loaded before we managed to register ourselves as a listener.
2356 // Thus we need to add it manually.
2357 DeclIDs[TU] = 1;
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002358 llvm::SmallVector<pch::DeclID, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002359 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2360 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002361 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002362 if ((*I)->getPCHLevel() == 0)
2363 NewGlobalDecls.push_back(GetDeclRef(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002364 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002365 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002366 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
2367 Abv->Add(llvm::BitCodeAbbrevOp(pch::TU_UPDATE_LEXICAL));
2368 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2369 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2370 Record.clear();
2371 Record.push_back(pch::TU_UPDATE_LEXICAL);
2372 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2373 reinterpret_cast<const char*>(NewGlobalDecls.data()),
2374 NewGlobalDecls.size() * sizeof(pch::DeclID));
Sebastian Redl143413f2010-07-12 22:02:52 +00002375
Sebastian Redl98912122010-07-27 23:01:28 +00002376 // Build a record containing all of the new tentative definitions in this
2377 // file, in TentativeDefinitions order.
2378 RecordData TentativeDefinitions;
2379 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2380 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2381 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2382 }
2383
2384 // Build a record containing all of the static unused functions in this file.
2385 RecordData UnusedStaticFuncs;
2386 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i) {
2387 if (SemaRef.UnusedStaticFuncs[i]->getPCHLevel() == 0)
2388 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
2389 }
2390
2391 // Build a record containing all of the locally-scoped external
2392 // declarations in this header file. Generally, this record will be
2393 // empty.
2394 RecordData LocallyScopedExternalDecls;
2395 // FIXME: This is filling in the PCH file in densemap order which is
2396 // nondeterminstic!
2397 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2398 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2399 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2400 TD != TDEnd; ++TD) {
2401 if (TD->second->getPCHLevel() == 0)
2402 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2403 }
2404
2405 // Build a record containing all of the ext_vector declarations.
2406 RecordData ExtVectorDecls;
2407 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2408 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2409 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2410 }
2411
Sebastian Redl143413f2010-07-12 22:02:52 +00002412 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2413 WriteDeclsBlockAbbrevs();
2414 while (!DeclTypesToEmit.empty()) {
2415 DeclOrType DOT = DeclTypesToEmit.front();
2416 DeclTypesToEmit.pop();
2417 if (DOT.isType())
2418 WriteType(DOT.getType());
2419 else
2420 WriteDecl(Context, DOT.getDecl());
2421 }
2422 Stream.ExitBlock();
2423
Sebastian Redl98912122010-07-27 23:01:28 +00002424 WritePreprocessor(PP);
Sebastian Redl143413f2010-07-12 22:02:52 +00002425 // FIXME: Method pool
Sebastian Redlff4a2952010-07-23 23:49:55 +00002426 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002427 WriteTypeDeclOffsets();
Sebastian Redl98912122010-07-27 23:01:28 +00002428
2429 // Write the record containing external, unnamed definitions.
2430 if (!ExternalDefinitions.empty())
2431 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
2432
2433 // Write the record containing tentative definitions.
2434 if (!TentativeDefinitions.empty())
2435 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
2436
2437 // Write the record containing unused static functions.
2438 if (!UnusedStaticFuncs.empty())
2439 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
2440
2441 // Write the record containing locally-scoped external definitions.
2442 if (!LocallyScopedExternalDecls.empty())
2443 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2444 LocallyScopedExternalDecls);
2445
2446 // Write the record containing ext_vector type names.
2447 if (!ExtVectorDecls.empty())
2448 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
2449
2450 // FIXME: Vtable uses
Sebastian Redl143413f2010-07-12 22:02:52 +00002451 // FIXME: Dynamic classes declarations
Sebastian Redl98912122010-07-27 23:01:28 +00002452
2453 Record.clear();
2454 Record.push_back(NumStatements);
2455 Record.push_back(NumMacros);
2456 Record.push_back(NumLexicalDeclContexts);
2457 Record.push_back(NumVisibleDeclContexts);
2458 Stream.EmitRecord(pch::STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002459 Stream.ExitBlock();
2460}
2461
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002462void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2463 Record.push_back(Loc.getRawEncoding());
2464}
2465
Chris Lattnerca025db2010-05-07 21:43:38 +00002466void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2467 AddSourceLocation(Range.getBegin(), Record);
2468 AddSourceLocation(Range.getEnd(), Record);
2469}
2470
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002471void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2472 Record.push_back(Value.getBitWidth());
2473 unsigned N = Value.getNumWords();
2474 const uint64_t* Words = Value.getRawData();
2475 for (unsigned I = 0; I != N; ++I)
2476 Record.push_back(Words[I]);
2477}
2478
Douglas Gregor1daeb692009-04-13 18:14:40 +00002479void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2480 Record.push_back(Value.isUnsigned());
2481 AddAPInt(Value, Record);
2482}
2483
Douglas Gregore0a3a512009-04-14 21:55:33 +00002484void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2485 AddAPInt(Value.bitcastToAPInt(), Record);
2486}
2487
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002488void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002489 Record.push_back(getIdentifierRef(II));
2490}
2491
2492pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2493 if (II == 0)
2494 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002495
2496 pch::IdentID &ID = IdentifierIDs[II];
2497 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002498 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002499 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002500}
2501
Douglas Gregoraae92242010-03-19 21:51:54 +00002502pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2503 if (MD == 0)
2504 return 0;
2505
2506 pch::IdentID &ID = MacroDefinitions[MD];
2507 if (ID == 0)
2508 ID = MacroDefinitions.size();
2509 return ID;
2510}
2511
Steve Naroff2ddea052009-04-23 10:39:46 +00002512void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2513 if (SelRef.getAsOpaquePtr() == 0) {
2514 Record.push_back(0);
2515 return;
2516 }
2517
2518 pch::SelectorID &SID = SelectorIDs[SelRef];
2519 if (SID == 0) {
2520 SID = SelectorIDs.size();
2521 SelVector.push_back(SelRef);
2522 }
2523 Record.push_back(SID);
2524}
2525
Chris Lattnercba86142010-05-10 00:25:06 +00002526void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2527 AddDeclRef(Temp->getDestructor(), Record);
2528}
2529
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002530void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2531 const TemplateArgumentLocInfo &Arg,
2532 RecordData &Record) {
2533 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002534 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002535 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002536 break;
2537 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002538 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002539 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002540 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002541 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2542 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002543 break;
John McCall0ad16662009-10-29 08:12:44 +00002544 case TemplateArgument::Null:
2545 case TemplateArgument::Integral:
2546 case TemplateArgument::Declaration:
2547 case TemplateArgument::Pack:
2548 break;
2549 }
2550}
2551
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002552void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2553 RecordData &Record) {
2554 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002555
2556 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2557 bool InfoHasSameExpr
2558 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2559 Record.push_back(InfoHasSameExpr);
2560 if (InfoHasSameExpr)
2561 return; // Avoid storing the same expr twice.
2562 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002563 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2564 Record);
2565}
2566
John McCallbcd03502009-12-07 02:54:59 +00002567void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2568 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002569 AddTypeRef(QualType(), Record);
2570 return;
2571 }
2572
John McCallbcd03502009-12-07 02:54:59 +00002573 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002574 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002575 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002576 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002577}
2578
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002579void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2580 if (T.isNull()) {
2581 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2582 return;
2583 }
2584
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002585 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002586 T.removeFastQualifiers();
2587
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002588 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002589 pch::TypeID &ID = TypeIDs[T];
2590 if (ID == 0) {
2591 // We haven't seen these qualifiers applied to this type before.
2592 // Assign it a new ID. This is the only time we enqueue a
2593 // qualified type, and it has no CV qualifiers.
2594 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002595 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002596 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002597
John McCall8ccfcb52009-09-24 19:53:00 +00002598 // Encode the type qualifiers in the type reference.
2599 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2600 return;
2601 }
2602
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002603 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002604
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002605 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002606 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002607 switch (BT->getKind()) {
2608 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2609 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2610 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2611 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2612 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2613 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2614 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2615 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002616 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002617 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2618 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2619 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2620 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2621 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2622 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2623 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002624 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002625 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2626 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2627 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002628 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002629 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2630 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002631 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2632 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002633 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2634 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002635 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002636 case BuiltinType::UndeducedAuto:
2637 assert(0 && "Should not see undeduced auto here");
2638 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002639 }
2640
John McCall8ccfcb52009-09-24 19:53:00 +00002641 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002642 return;
2643 }
2644
John McCall8ccfcb52009-09-24 19:53:00 +00002645 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002646 if (ID == 0) {
2647 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002648 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002649 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002650 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002651 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002652
2653 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002654 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002655}
2656
2657void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002658 Record.push_back(GetDeclRef(D));
2659}
2660
2661pch::DeclID PCHWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002662 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002663 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002664 }
2665
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002666 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002667 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002668 // We haven't seen this declaration before. Give it a new ID and
2669 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002670 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002671 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002672 }
2673
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002674 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002675}
2676
Douglas Gregore84a9da2009-04-20 20:36:09 +00002677pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2678 if (D == 0)
2679 return 0;
2680
2681 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2682 return DeclIDs[D];
2683}
2684
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002685void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002686 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002687 Record.push_back(Name.getNameKind());
2688 switch (Name.getNameKind()) {
2689 case DeclarationName::Identifier:
2690 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2691 break;
2692
2693 case DeclarationName::ObjCZeroArgSelector:
2694 case DeclarationName::ObjCOneArgSelector:
2695 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002696 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002697 break;
2698
2699 case DeclarationName::CXXConstructorName:
2700 case DeclarationName::CXXDestructorName:
2701 case DeclarationName::CXXConversionFunctionName:
2702 AddTypeRef(Name.getCXXNameType(), Record);
2703 break;
2704
2705 case DeclarationName::CXXOperatorName:
2706 Record.push_back(Name.getCXXOverloadedOperator());
2707 break;
2708
Alexis Hunt3d221f22009-11-29 07:34:05 +00002709 case DeclarationName::CXXLiteralOperatorName:
2710 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2711 break;
2712
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002713 case DeclarationName::CXXUsingDirective:
2714 // No extra data to emit
2715 break;
2716 }
2717}
Chris Lattnerca025db2010-05-07 21:43:38 +00002718
2719void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2720 RecordData &Record) {
2721 // Nested name specifiers usually aren't too long. I think that 8 would
2722 // typically accomodate the vast majority.
2723 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2724
2725 // Push each of the NNS's onto a stack for serialization in reverse order.
2726 while (NNS) {
2727 NestedNames.push_back(NNS);
2728 NNS = NNS->getPrefix();
2729 }
2730
2731 Record.push_back(NestedNames.size());
2732 while(!NestedNames.empty()) {
2733 NNS = NestedNames.pop_back_val();
2734 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2735 Record.push_back(Kind);
2736 switch (Kind) {
2737 case NestedNameSpecifier::Identifier:
2738 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2739 break;
2740
2741 case NestedNameSpecifier::Namespace:
2742 AddDeclRef(NNS->getAsNamespace(), Record);
2743 break;
2744
2745 case NestedNameSpecifier::TypeSpec:
2746 case NestedNameSpecifier::TypeSpecWithTemplate:
2747 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2748 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2749 break;
2750
2751 case NestedNameSpecifier::Global:
2752 // Don't need to write an associated value.
2753 break;
2754 }
2755 }
2756}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002757
2758void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2759 TemplateName::NameKind Kind = Name.getKind();
2760 Record.push_back(Kind);
2761 switch (Kind) {
2762 case TemplateName::Template:
2763 AddDeclRef(Name.getAsTemplateDecl(), Record);
2764 break;
2765
2766 case TemplateName::OverloadedTemplate: {
2767 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2768 Record.push_back(OvT->size());
2769 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2770 I != E; ++I)
2771 AddDeclRef(*I, Record);
2772 break;
2773 }
2774
2775 case TemplateName::QualifiedTemplate: {
2776 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2777 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2778 Record.push_back(QualT->hasTemplateKeyword());
2779 AddDeclRef(QualT->getTemplateDecl(), Record);
2780 break;
2781 }
2782
2783 case TemplateName::DependentTemplate: {
2784 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2785 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2786 Record.push_back(DepT->isIdentifier());
2787 if (DepT->isIdentifier())
2788 AddIdentifierRef(DepT->getIdentifier(), Record);
2789 else
2790 Record.push_back(DepT->getOperator());
2791 break;
2792 }
2793 }
2794}
2795
2796void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2797 RecordData &Record) {
2798 Record.push_back(Arg.getKind());
2799 switch (Arg.getKind()) {
2800 case TemplateArgument::Null:
2801 break;
2802 case TemplateArgument::Type:
2803 AddTypeRef(Arg.getAsType(), Record);
2804 break;
2805 case TemplateArgument::Declaration:
2806 AddDeclRef(Arg.getAsDecl(), Record);
2807 break;
2808 case TemplateArgument::Integral:
2809 AddAPSInt(*Arg.getAsIntegral(), Record);
2810 AddTypeRef(Arg.getIntegralType(), Record);
2811 break;
2812 case TemplateArgument::Template:
2813 AddTemplateName(Arg.getAsTemplate(), Record);
2814 break;
2815 case TemplateArgument::Expression:
2816 AddStmt(Arg.getAsExpr());
2817 break;
2818 case TemplateArgument::Pack:
2819 Record.push_back(Arg.pack_size());
2820 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2821 I != E; ++I)
2822 AddTemplateArgument(*I, Record);
2823 break;
2824 }
2825}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002826
2827void
2828PCHWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
2829 RecordData &Record) {
2830 assert(TemplateParams && "No TemplateParams!");
2831 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2832 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2833 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2834 Record.push_back(TemplateParams->size());
2835 for (TemplateParameterList::const_iterator
2836 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2837 P != PEnd; ++P)
2838 AddDeclRef(*P, Record);
2839}
2840
2841/// \brief Emit a template argument list.
2842void
2843PCHWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
2844 RecordData &Record) {
2845 assert(TemplateArgs && "No TemplateArgs!");
2846 Record.push_back(TemplateArgs->flat_size());
2847 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2848 AddTemplateArgument(TemplateArgs->get(i), Record);
2849}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00002850
2851
2852void
2853PCHWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
2854 Record.push_back(Set.size());
2855 for (UnresolvedSetImpl::const_iterator
2856 I = Set.begin(), E = Set.end(); I != E; ++I) {
2857 AddDeclRef(I.getDecl(), Record);
2858 Record.push_back(I.getAccess());
2859 }
2860}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00002861
2862void PCHWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
2863 RecordData &Record) {
2864 Record.push_back(Base.isVirtual());
2865 Record.push_back(Base.isBaseOfClass());
2866 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00002867 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00002868 AddSourceRange(Base.getSourceRange(), Record);
2869}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002870
Sebastian Redlff4a2952010-07-23 23:49:55 +00002871void PCHWriter::IdentifierRead(pch::IdentID ID, IdentifierInfo *II) {
2872 IdentifierIDs[II] = ID;
2873}
2874
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002875void PCHWriter::TypeRead(pch::TypeID ID, QualType T) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002876 TypeIDs[T] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002877}
2878
2879void PCHWriter::DeclRead(pch::DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002880 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002881}