blob: 1fb90851b5bb5584b41c33f2ecd6432d005bd48f [file] [log] [blame]
Chris Lattnere127a0d2010-04-20 20:35:58 +00001//===--- PCHWriter.cpp - Precompiled Headers Writer -----------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregore7785042009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump1eb44332009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000033#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000045
Douglas Gregor2cf26342009-04-09 22:27:44 +000046namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000047 class PCHTypeWriter {
Douglas Gregor2cf26342009-04-09 22:27:44 +000048 PCHWriter &Writer;
49 PCHWriter::RecordData &Record;
50
51 public:
52 /// \brief Type code that corresponds to the record generated.
53 pch::TypeCode Code;
54
Mike Stump1eb44332009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
58 void VisitArrayType(const ArrayType *T);
59 void VisitFunctionType(const FunctionType *T);
60 void VisitTagType(const TagType *T);
61
62#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000064#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
Douglas Gregor2cf26342009-04-09 22:27:44 +000072void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
73 Writer.AddTypeRef(T->getElementType(), Record);
74 Code = pch::TYPE_COMPLEX;
75}
76
77void PCHTypeWriter::VisitPointerType(const PointerType *T) {
78 Writer.AddTypeRef(T->getPointeeType(), Record);
79 Code = pch::TYPE_POINTER;
80}
81
82void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000084 Code = pch::TYPE_BLOCK_POINTER;
85}
86
87void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_LVALUE_REFERENCE;
90}
91
92void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_RVALUE_REFERENCE;
95}
96
97void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100 Code = pch::TYPE_MEMBER_POINTER;
101}
102
103void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
104 Writer.AddTypeRef(T->getElementType(), Record);
105 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000106 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
109void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
110 VisitArrayType(T);
111 Writer.AddAPInt(T->getSize(), Record);
112 Code = pch::TYPE_CONSTANT_ARRAY;
113}
114
115void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
116 VisitArrayType(T);
117 Code = pch::TYPE_INCOMPLETE_ARRAY;
118}
119
120void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
121 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000122 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
123 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000124 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125 Code = pch::TYPE_VARIABLE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVectorType(const VectorType *T) {
129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getNumElements());
John Thompson82287d12010-02-05 00:12:22 +0000131 Record.push_back(T->isAltiVec());
132 Record.push_back(T->isPixel());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133 Code = pch::TYPE_VECTOR;
134}
135
136void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
137 VisitVectorType(T);
138 Code = pch::TYPE_EXT_VECTOR;
139}
140
141void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
142 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000143 FunctionType::ExtInfo C = T->getExtInfo();
144 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000145 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000146 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000147 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
150void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
151 VisitFunctionType(T);
152 Code = pch::TYPE_FUNCTION_NO_PROTO;
153}
154
155void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
156 VisitFunctionType(T);
157 Record.push_back(T->getNumArgs());
158 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
159 Writer.AddTypeRef(T->getArgType(I), Record);
160 Record.push_back(T->isVariadic());
161 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000162 Record.push_back(T->hasExceptionSpec());
163 Record.push_back(T->hasAnyExceptionSpec());
164 Record.push_back(T->getNumExceptions());
165 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
166 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Code = pch::TYPE_FUNCTION_PROTO;
168}
169
John McCalled976492009-12-04 22:46:56 +0000170void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_UNRESOLVED_USING;
173}
John McCalled976492009-12-04 22:46:56 +0000174
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
176 Writer.AddDeclRef(T->getDecl(), Record);
177 Code = pch::TYPE_TYPEDEF;
178}
179
180void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000181 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182 Code = pch::TYPE_TYPEOF_EXPR;
183}
184
185void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
186 Writer.AddTypeRef(T->getUnderlyingType(), Record);
187 Code = pch::TYPE_TYPEOF;
188}
189
Anders Carlsson395b4752009-06-24 19:06:50 +0000190void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
191 Writer.AddStmt(T->getUnderlyingExpr());
192 Code = pch::TYPE_DECLTYPE;
193}
194
Douglas Gregor2cf26342009-04-09 22:27:44 +0000195void PCHTypeWriter::VisitTagType(const TagType *T) {
196 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000197 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198 "Cannot serialize in the middle of a type definition");
199}
200
201void PCHTypeWriter::VisitRecordType(const RecordType *T) {
202 VisitTagType(T);
203 Code = pch::TYPE_RECORD;
204}
205
206void PCHTypeWriter::VisitEnumType(const EnumType *T) {
207 VisitTagType(T);
208 Code = pch::TYPE_ENUM;
209}
210
Mike Stump1eb44332009-09-09 15:08:12 +0000211void
John McCall49a832b2009-10-18 09:09:24 +0000212PCHTypeWriter::VisitSubstTemplateTypeParmType(
213 const SubstTemplateTypeParmType *T) {
214 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
215 Writer.AddTypeRef(T->getReplacementType(), Record);
216 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
217}
218
219void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220PCHTypeWriter::VisitTemplateSpecializationType(
221 const TemplateSpecializationType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000222 Writer.AddTemplateName(T->getTemplateName(), Record);
223 Record.push_back(T->getNumArgs());
224 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
225 ArgI != ArgE; ++ArgI)
226 Writer.AddTemplateArgument(*ArgI, Record);
227 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
228}
229
230void
231PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000232 // FIXME: Serialize this type (C++ only)
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000233 assert(false && "Cannot serialize dependent sized array types");
234}
235
236void
237PCHTypeWriter::VisitDependentSizedExtVectorType(
238 const DependentSizedExtVectorType *T) {
239 // FIXME: Serialize this type (C++ only)
240 assert(false && "Cannot serialize dependent sized extended vector types");
241}
242
243void
244PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
245 Record.push_back(T->getDepth());
246 Record.push_back(T->getIndex());
247 Record.push_back(T->isParameterPack());
248 Writer.AddIdentifierRef(T->getName(), Record);
249 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
250}
251
252void
253PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) {
254 // FIXME: Serialize this type (C++ only)
255 assert(false && "Cannot serialize dependent name types");
256}
257
258void
259PCHTypeWriter::VisitDependentTemplateSpecializationType(
260 const DependentTemplateSpecializationType *T) {
261 // FIXME: Serialize this type (C++ only)
262 assert(false && "Cannot serialize dependent template specialization types");
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263}
264
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000265void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
266 Writer.AddTypeRef(T->getNamedType(), Record);
267 Record.push_back(T->getKeyword());
268 // FIXME: Serialize the qualifier (C++ only)
269 assert(T->getQualifier() == 0 && "Cannot serialize qualified name types");
270 Code = pch::TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000271}
272
John McCall3cb0ebd2010-03-10 03:28:59 +0000273void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
274 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000275 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000276 Code = pch::TYPE_INJECTED_CLASS_NAME;
277}
278
Douglas Gregor2cf26342009-04-09 22:27:44 +0000279void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
280 Writer.AddDeclRef(T->getDecl(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000281 Code = pch::TYPE_OBJC_INTERFACE;
282}
283
284void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
285 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000286 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000287 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000288 E = T->qual_end(); I != E; ++I)
289 Writer.AddDeclRef(*I, Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000290 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000291}
292
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000293void
294PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000295 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000296 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000297}
298
John McCalla1ee0c52009-10-16 21:56:05 +0000299namespace {
300
301class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
302 PCHWriter &Writer;
303 PCHWriter::RecordData &Record;
304
305public:
306 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
307 : Writer(Writer), Record(Record) { }
308
John McCall51bd8032009-10-18 01:05:36 +0000309#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000310#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000311 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000312#include "clang/AST/TypeLocNodes.def"
313
John McCall51bd8032009-10-18 01:05:36 +0000314 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
315 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000316};
317
318}
319
John McCall51bd8032009-10-18 01:05:36 +0000320void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
321 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000322}
John McCall51bd8032009-10-18 01:05:36 +0000323void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000324 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
325 if (TL.needsExtraLocalData()) {
326 Record.push_back(TL.getWrittenTypeSpec());
327 Record.push_back(TL.getWrittenSignSpec());
328 Record.push_back(TL.getWrittenWidthSpec());
329 Record.push_back(TL.hasModeAttr());
330 }
John McCalla1ee0c52009-10-16 21:56:05 +0000331}
John McCall51bd8032009-10-18 01:05:36 +0000332void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
333 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000334}
John McCall51bd8032009-10-18 01:05:36 +0000335void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
336 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000337}
John McCall51bd8032009-10-18 01:05:36 +0000338void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000340}
John McCall51bd8032009-10-18 01:05:36 +0000341void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
342 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000343}
John McCall51bd8032009-10-18 01:05:36 +0000344void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
345 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000346}
John McCall51bd8032009-10-18 01:05:36 +0000347void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
348 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000349}
John McCall51bd8032009-10-18 01:05:36 +0000350void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
351 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
352 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
353 Record.push_back(TL.getSizeExpr() ? 1 : 0);
354 if (TL.getSizeExpr())
355 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000356}
John McCall51bd8032009-10-18 01:05:36 +0000357void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
358 VisitArrayTypeLoc(TL);
359}
360void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
361 VisitArrayTypeLoc(TL);
362}
363void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
364 VisitArrayTypeLoc(TL);
365}
366void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
367 DependentSizedArrayTypeLoc TL) {
368 VisitArrayTypeLoc(TL);
369}
370void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
371 DependentSizedExtVectorTypeLoc TL) {
372 Writer.AddSourceLocation(TL.getNameLoc(), Record);
373}
374void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
375 Writer.AddSourceLocation(TL.getNameLoc(), Record);
376}
377void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
378 Writer.AddSourceLocation(TL.getNameLoc(), Record);
379}
380void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
381 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
382 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
383 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
384 Writer.AddDeclRef(TL.getArg(i), Record);
385}
386void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
387 VisitFunctionTypeLoc(TL);
388}
389void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
390 VisitFunctionTypeLoc(TL);
391}
John McCalled976492009-12-04 22:46:56 +0000392void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
393 Writer.AddSourceLocation(TL.getNameLoc(), Record);
394}
John McCall51bd8032009-10-18 01:05:36 +0000395void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
396 Writer.AddSourceLocation(TL.getNameLoc(), Record);
397}
398void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000399 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
400 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
401 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000402}
403void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000404 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
405 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
406 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
407 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000408}
409void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
411}
412void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getNameLoc(), Record);
414}
415void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
416 Writer.AddSourceLocation(TL.getNameLoc(), Record);
417}
John McCall51bd8032009-10-18 01:05:36 +0000418void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
419 Writer.AddSourceLocation(TL.getNameLoc(), Record);
420}
John McCall49a832b2009-10-18 09:09:24 +0000421void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
422 SubstTemplateTypeParmTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getNameLoc(), Record);
424}
John McCall51bd8032009-10-18 01:05:36 +0000425void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
426 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000427 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
428 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
429 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
430 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000431 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
432 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000433}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000434void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000435 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
436 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000437}
John McCall3cb0ebd2010-03-10 03:28:59 +0000438void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
440}
Douglas Gregor4714c122010-03-31 17:34:00 +0000441void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000442 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
443 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
John McCall33500952010-06-11 00:33:02 +0000446void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
447 DependentTemplateSpecializationTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
449 Writer.AddSourceRange(TL.getQualifierRange(), Record);
450 Writer.AddSourceLocation(TL.getNameLoc(), Record);
451 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
452 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
453 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000454 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
455 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000456}
John McCall51bd8032009-10-18 01:05:36 +0000457void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000459}
460void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
461 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000462 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
463 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
464 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
465 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000466}
John McCall54e14c42009-10-22 22:37:11 +0000467void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
468 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000469}
John McCalla1ee0c52009-10-16 21:56:05 +0000470
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000471//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000472// PCHWriter Implementation
473//===----------------------------------------------------------------------===//
474
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000475static void EmitBlockID(unsigned ID, const char *Name,
476 llvm::BitstreamWriter &Stream,
477 PCHWriter::RecordData &Record) {
478 Record.clear();
479 Record.push_back(ID);
480 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
481
482 // Emit the block name if present.
483 if (Name == 0 || Name[0] == 0) return;
484 Record.clear();
485 while (*Name)
486 Record.push_back(*Name++);
487 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
488}
489
490static void EmitRecordID(unsigned ID, const char *Name,
491 llvm::BitstreamWriter &Stream,
492 PCHWriter::RecordData &Record) {
493 Record.clear();
494 Record.push_back(ID);
495 while (*Name)
496 Record.push_back(*Name++);
497 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000498}
499
500static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
501 PCHWriter::RecordData &Record) {
502#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
503 RECORD(STMT_STOP);
504 RECORD(STMT_NULL_PTR);
505 RECORD(STMT_NULL);
506 RECORD(STMT_COMPOUND);
507 RECORD(STMT_CASE);
508 RECORD(STMT_DEFAULT);
509 RECORD(STMT_LABEL);
510 RECORD(STMT_IF);
511 RECORD(STMT_SWITCH);
512 RECORD(STMT_WHILE);
513 RECORD(STMT_DO);
514 RECORD(STMT_FOR);
515 RECORD(STMT_GOTO);
516 RECORD(STMT_INDIRECT_GOTO);
517 RECORD(STMT_CONTINUE);
518 RECORD(STMT_BREAK);
519 RECORD(STMT_RETURN);
520 RECORD(STMT_DECL);
521 RECORD(STMT_ASM);
522 RECORD(EXPR_PREDEFINED);
523 RECORD(EXPR_DECL_REF);
524 RECORD(EXPR_INTEGER_LITERAL);
525 RECORD(EXPR_FLOATING_LITERAL);
526 RECORD(EXPR_IMAGINARY_LITERAL);
527 RECORD(EXPR_STRING_LITERAL);
528 RECORD(EXPR_CHARACTER_LITERAL);
529 RECORD(EXPR_PAREN);
530 RECORD(EXPR_UNARY_OPERATOR);
531 RECORD(EXPR_SIZEOF_ALIGN_OF);
532 RECORD(EXPR_ARRAY_SUBSCRIPT);
533 RECORD(EXPR_CALL);
534 RECORD(EXPR_MEMBER);
535 RECORD(EXPR_BINARY_OPERATOR);
536 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
537 RECORD(EXPR_CONDITIONAL_OPERATOR);
538 RECORD(EXPR_IMPLICIT_CAST);
539 RECORD(EXPR_CSTYLE_CAST);
540 RECORD(EXPR_COMPOUND_LITERAL);
541 RECORD(EXPR_EXT_VECTOR_ELEMENT);
542 RECORD(EXPR_INIT_LIST);
543 RECORD(EXPR_DESIGNATED_INIT);
544 RECORD(EXPR_IMPLICIT_VALUE_INIT);
545 RECORD(EXPR_VA_ARG);
546 RECORD(EXPR_ADDR_LABEL);
547 RECORD(EXPR_STMT);
548 RECORD(EXPR_TYPES_COMPATIBLE);
549 RECORD(EXPR_CHOOSE);
550 RECORD(EXPR_GNU_NULL);
551 RECORD(EXPR_SHUFFLE_VECTOR);
552 RECORD(EXPR_BLOCK);
553 RECORD(EXPR_BLOCK_DECL_REF);
554 RECORD(EXPR_OBJC_STRING_LITERAL);
555 RECORD(EXPR_OBJC_ENCODE);
556 RECORD(EXPR_OBJC_SELECTOR_EXPR);
557 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
558 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
559 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
560 RECORD(EXPR_OBJC_KVC_REF_EXPR);
561 RECORD(EXPR_OBJC_MESSAGE_EXPR);
562 RECORD(EXPR_OBJC_SUPER_EXPR);
563 RECORD(STMT_OBJC_FOR_COLLECTION);
564 RECORD(STMT_OBJC_CATCH);
565 RECORD(STMT_OBJC_FINALLY);
566 RECORD(STMT_OBJC_AT_TRY);
567 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
568 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000569 RECORD(EXPR_CXX_OPERATOR_CALL);
570 RECORD(EXPR_CXX_CONSTRUCT);
571 RECORD(EXPR_CXX_STATIC_CAST);
572 RECORD(EXPR_CXX_DYNAMIC_CAST);
573 RECORD(EXPR_CXX_REINTERPRET_CAST);
574 RECORD(EXPR_CXX_CONST_CAST);
575 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
576 RECORD(EXPR_CXX_BOOL_LITERAL);
577 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000578#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000579}
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000581void PCHWriter::WriteBlockInfoBlock() {
582 RecordData Record;
583 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner2f4efd12009-04-27 00:40:25 +0000585#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000586#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000588 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000589 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000590 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000591 RECORD(TYPE_OFFSET);
592 RECORD(DECL_OFFSET);
593 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000594 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000595 RECORD(IDENTIFIER_OFFSET);
596 RECORD(IDENTIFIER_TABLE);
597 RECORD(EXTERNAL_DEFINITIONS);
598 RECORD(SPECIAL_TYPES);
599 RECORD(STATISTICS);
600 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000601 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000602 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
603 RECORD(SELECTOR_OFFSETS);
604 RECORD(METHOD_POOL);
605 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000606 RECORD(SOURCE_LOCATION_OFFSETS);
607 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000608 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000609 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000610 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000611 RECORD(UNUSED_STATIC_FUNCS);
612 RECORD(MACRO_DEFINITION_OFFSETS);
613
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000614 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000615 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000616 RECORD(SM_SLOC_FILE_ENTRY);
617 RECORD(SM_SLOC_BUFFER_ENTRY);
618 RECORD(SM_SLOC_BUFFER_BLOB);
619 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
620 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000622 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000623 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000624 RECORD(PP_MACRO_OBJECT_LIKE);
625 RECORD(PP_MACRO_FUNCTION_LIKE);
626 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000627 RECORD(PP_MACRO_INSTANTIATION);
628 RECORD(PP_MACRO_DEFINITION);
629
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000630 // Decls and Types block.
631 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 RECORD(TYPE_COMPLEX);
634 RECORD(TYPE_POINTER);
635 RECORD(TYPE_BLOCK_POINTER);
636 RECORD(TYPE_LVALUE_REFERENCE);
637 RECORD(TYPE_RVALUE_REFERENCE);
638 RECORD(TYPE_MEMBER_POINTER);
639 RECORD(TYPE_CONSTANT_ARRAY);
640 RECORD(TYPE_INCOMPLETE_ARRAY);
641 RECORD(TYPE_VARIABLE_ARRAY);
642 RECORD(TYPE_VECTOR);
643 RECORD(TYPE_EXT_VECTOR);
644 RECORD(TYPE_FUNCTION_PROTO);
645 RECORD(TYPE_FUNCTION_NO_PROTO);
646 RECORD(TYPE_TYPEDEF);
647 RECORD(TYPE_TYPEOF_EXPR);
648 RECORD(TYPE_TYPEOF);
649 RECORD(TYPE_RECORD);
650 RECORD(TYPE_ENUM);
651 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000652 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000653 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000654 RECORD(DECL_ATTR);
655 RECORD(DECL_TRANSLATION_UNIT);
656 RECORD(DECL_TYPEDEF);
657 RECORD(DECL_ENUM);
658 RECORD(DECL_RECORD);
659 RECORD(DECL_ENUM_CONSTANT);
660 RECORD(DECL_FUNCTION);
661 RECORD(DECL_OBJC_METHOD);
662 RECORD(DECL_OBJC_INTERFACE);
663 RECORD(DECL_OBJC_PROTOCOL);
664 RECORD(DECL_OBJC_IVAR);
665 RECORD(DECL_OBJC_AT_DEFS_FIELD);
666 RECORD(DECL_OBJC_CLASS);
667 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
668 RECORD(DECL_OBJC_CATEGORY);
669 RECORD(DECL_OBJC_CATEGORY_IMPL);
670 RECORD(DECL_OBJC_IMPLEMENTATION);
671 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
672 RECORD(DECL_OBJC_PROPERTY);
673 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000674 RECORD(DECL_FIELD);
675 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000676 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000677 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000678 RECORD(DECL_FILE_SCOPE_ASM);
679 RECORD(DECL_BLOCK);
680 RECORD(DECL_CONTEXT_LEXICAL);
681 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000682 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000683 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000684#undef RECORD
685#undef BLOCK
686 Stream.ExitBlock();
687}
688
Douglas Gregore650c8c2009-07-07 00:12:59 +0000689/// \brief Adjusts the given filename to only write out the portion of the
690/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000691///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000692/// \param Filename the file name to adjust.
693///
694/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
695/// the returned filename will be adjusted by this system root.
696///
697/// \returns either the original filename (if it needs no adjustment) or the
698/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000699static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000700adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
701 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Douglas Gregore650c8c2009-07-07 00:12:59 +0000703 if (!isysroot)
704 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Douglas Gregore650c8c2009-07-07 00:12:59 +0000706 // Verify that the filename and the system root have the same prefix.
707 unsigned Pos = 0;
708 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
709 if (Filename[Pos] != isysroot[Pos])
710 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Douglas Gregore650c8c2009-07-07 00:12:59 +0000712 // We hit the end of the filename before we hit the end of the system root.
713 if (!Filename[Pos])
714 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Douglas Gregore650c8c2009-07-07 00:12:59 +0000716 // If the file name has a '/' at the current position, skip over the '/'.
717 // We distinguish sysroot-based includes from absolute includes by the
718 // absence of '/' at the beginning of sysroot-based includes.
719 if (Filename[Pos] == '/')
720 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregore650c8c2009-07-07 00:12:59 +0000722 return Filename + Pos;
723}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000724
Douglas Gregorab41e632009-04-27 22:23:34 +0000725/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000726void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000727 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000728
Douglas Gregore650c8c2009-07-07 00:12:59 +0000729 // Metadata
730 const TargetInfo &Target = Context.Target;
731 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
732 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
733 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
734 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
735 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
736 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
737 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
738 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
739 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Douglas Gregore650c8c2009-07-07 00:12:59 +0000741 RecordData Record;
742 Record.push_back(pch::METADATA);
743 Record.push_back(pch::VERSION_MAJOR);
744 Record.push_back(pch::VERSION_MINOR);
745 Record.push_back(CLANG_VERSION_MAJOR);
746 Record.push_back(CLANG_VERSION_MINOR);
747 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000748 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000749 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregorb64c1932009-05-12 01:31:05 +0000751 // Original file name
752 SourceManager &SM = Context.getSourceManager();
753 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
754 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
755 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
756 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
757 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
758
759 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000761 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000762
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000763 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000764 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000765 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000766 RecordData Record;
767 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000768 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000769 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000770
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000771 // Repository branch/version information.
772 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
773 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
774 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
775 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000776 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000777 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000778 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
779 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000780}
781
782/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000783void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
784 RecordData Record;
785 Record.push_back(LangOpts.Trigraphs);
786 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
787 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
788 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
789 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000790 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000791 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
792 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
793 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
794 Record.push_back(LangOpts.C99); // C99 Support
795 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
796 Record.push_back(LangOpts.CPlusPlus); // C++ Support
797 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000798 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000800 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
801 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000802 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000803 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000804 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000805 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000806 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000808 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000809 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
810 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000811 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000812 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000813 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000814
815 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
816 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
817 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
818
Chris Lattnerea5ce472009-04-27 07:35:58 +0000819 // Whether static initializers are protected by locks.
820 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000821 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000822 Record.push_back(LangOpts.Blocks); // block extension to C
823 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
824 // they are unused.
825 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
826 // (modulo the platform support).
827
828 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
829 // signed integer arithmetic overflows.
830
831 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
832 // may be ripped out at any time.
833
834 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000835 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000836 // defined.
837 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
838 // opposed to __DYNAMIC__).
839 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
840
841 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
842 // used (instead of C99 semantics).
843 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000844 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
845 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000846 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
847 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000848 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000849 Record.push_back(LangOpts.getGCMode());
850 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000851 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000852 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000853 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000854 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000855 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000856 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000857}
858
Douglas Gregor14f79002009-04-10 03:52:48 +0000859//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000860// stat cache Serialization
861//===----------------------------------------------------------------------===//
862
863namespace {
864// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000865class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000866public:
867 typedef const char * key_type;
868 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000870 typedef std::pair<int, struct stat> data_type;
871 typedef const data_type& data_type_ref;
872
873 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000874 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
877 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000878 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
879 data_type_ref Data) {
880 unsigned StrLen = strlen(path);
881 clang::io::Emit16(Out, StrLen);
882 unsigned DataLen = 1; // result value
883 if (Data.first == 0)
884 DataLen += 4 + 4 + 2 + 8 + 8;
885 clang::io::Emit8(Out, DataLen);
886 return std::make_pair(StrLen + 1, DataLen);
887 }
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000889 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
890 Out.write(path, KeyLen);
891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000893 void EmitData(llvm::raw_ostream& Out, key_type_ref,
894 data_type_ref Data, unsigned DataLen) {
895 using namespace clang::io;
896 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000898 // Result of stat()
899 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000901 if (Data.first == 0) {
902 Emit32(Out, (uint32_t) Data.second.st_ino);
903 Emit32(Out, (uint32_t) Data.second.st_dev);
904 Emit16(Out, (uint16_t) Data.second.st_mode);
905 Emit64(Out, (uint64_t) Data.second.st_mtime);
906 Emit64(Out, (uint64_t) Data.second.st_size);
907 }
908
909 assert(Out.tell() - Start == DataLen && "Wrong data length");
910 }
911};
912} // end anonymous namespace
913
914/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000915void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
916 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000917 // Build the on-disk hash table containing information about every
918 // stat() call.
919 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
920 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000921 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000922 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000923 Stat != StatEnd; ++Stat, ++NumStatEntries) {
924 const char *Filename = Stat->first();
925 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
926 Generator.insert(Filename, Stat->second);
927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000929 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000930 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000931 uint32_t BucketOffset;
932 {
933 llvm::raw_svector_ostream Out(StatCacheData);
934 // Make sure that no bucket is at offset 0
935 clang::io::Emit32(Out, 0);
936 BucketOffset = Generator.Emit(Out);
937 }
938
939 // Create a blob abbreviation
940 using namespace llvm;
941 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
942 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
946 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
947
948 // Write the stat cache
949 RecordData Record;
950 Record.push_back(pch::STAT_CACHE);
951 Record.push_back(BucketOffset);
952 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000953 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000954}
955
956//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000957// Source Manager Serialization
958//===----------------------------------------------------------------------===//
959
960/// \brief Create an abbreviation for the SLocEntry that refers to a
961/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000962static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000963 using namespace llvm;
964 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
965 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000970 // FileEntry fields.
971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000973 // HeaderFileInfo fields.
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000979 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000980}
981
982/// \brief Create an abbreviation for the SLocEntry that refers to a
983/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000984static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000985 using namespace llvm;
986 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
987 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000993 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000994}
995
996/// \brief Create an abbreviation for the SLocEntry that refers to a
997/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000998static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000999 using namespace llvm;
1000 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1001 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001003 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001004}
1005
1006/// \brief Create an abbreviation for the SLocEntry that refers to an
1007/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001008static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001009 using namespace llvm;
1010 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1011 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001017 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001018}
1019
1020/// \brief Writes the block containing the serialized form of the
1021/// source manager.
1022///
1023/// TODO: We should probably use an on-disk hash table (stored in a
1024/// blob), indexed based on the file name, so that we only create
1025/// entries for files that we actually need. In the common case (no
1026/// errors), we probably won't have to create file entries for any of
1027/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001028void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001029 const Preprocessor &PP,
1030 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001031 RecordData Record;
1032
Chris Lattnerf04ad692009-04-10 17:16:57 +00001033 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001034 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001035
1036 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001037 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1038 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1039 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1040 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001041
Douglas Gregorbd945002009-04-13 16:31:14 +00001042 // Write the line table.
1043 if (SourceMgr.hasLineTable()) {
1044 LineTableInfo &LineTable = SourceMgr.getLineTable();
1045
1046 // Emit the file names
1047 Record.push_back(LineTable.getNumFilenames());
1048 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1049 // Emit the file name
1050 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001051 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001052 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1053 Record.push_back(FilenameLen);
1054 if (FilenameLen)
1055 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Douglas Gregorbd945002009-04-13 16:31:14 +00001058 // Emit the line entries
1059 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1060 L != LEnd; ++L) {
1061 // Emit the file ID
1062 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Douglas Gregorbd945002009-04-13 16:31:14 +00001064 // Emit the line entries
1065 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001066 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001067 LEEnd = L->second.end();
1068 LE != LEEnd; ++LE) {
1069 Record.push_back(LE->FileOffset);
1070 Record.push_back(LE->LineNo);
1071 Record.push_back(LE->FilenameID);
1072 Record.push_back((unsigned)LE->FileKind);
1073 Record.push_back(LE->IncludeOffset);
1074 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001075 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001076 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001077 }
1078
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001079 // Write out the source location entry table. We skip the first
1080 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001081 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001082 RecordData PreloadSLocs;
1083 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001084 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1085 // Get this source location entry.
1086 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001087
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001088 // Record the offset of this source-location entry.
1089 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1090
1091 // Figure out which record code to use.
1092 unsigned Code;
1093 if (SLoc->isFile()) {
1094 if (SLoc->getFile().getContentCache()->Entry)
1095 Code = pch::SM_SLOC_FILE_ENTRY;
1096 else
1097 Code = pch::SM_SLOC_BUFFER_ENTRY;
1098 } else
1099 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1100 Record.clear();
1101 Record.push_back(Code);
1102
1103 Record.push_back(SLoc->getOffset());
1104 if (SLoc->isFile()) {
1105 const SrcMgr::FileInfo &File = SLoc->getFile();
1106 Record.push_back(File.getIncludeLoc().getRawEncoding());
1107 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1108 Record.push_back(File.hasLineDirectives());
1109
1110 const SrcMgr::ContentCache *Content = File.getContentCache();
1111 if (Content->Entry) {
1112 // The source location entry is a file. The blob associated
1113 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Douglas Gregor2d52be52010-03-21 22:49:54 +00001115 // Emit size/modification time for this file.
1116 Record.push_back(Content->Entry->getSize());
1117 Record.push_back(Content->Entry->getModificationTime());
1118
Douglas Gregor12fab312010-03-16 16:35:32 +00001119 // Emit header-search information associated with this file.
1120 HeaderFileInfo HFI;
1121 HeaderSearch &HS = PP.getHeaderSearchInfo();
1122 if (Content->Entry->getUID() < HS.header_file_size())
1123 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1124 Record.push_back(HFI.isImport);
1125 Record.push_back(HFI.DirInfo);
1126 Record.push_back(HFI.NumIncludes);
1127 AddIdentifierRef(HFI.ControllingMacro, Record);
1128
Douglas Gregore650c8c2009-07-07 00:12:59 +00001129 // Turn the file name into an absolute path, if it isn't already.
1130 const char *Filename = Content->Entry->getName();
1131 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001132 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001133 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Douglas Gregore650c8c2009-07-07 00:12:59 +00001135 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001136 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001137
1138 // FIXME: For now, preload all file source locations, so that
1139 // we get the appropriate File entries in the reader. This is
1140 // a temporary measure.
1141 PreloadSLocs.push_back(SLocEntryOffsets.size());
1142 } else {
1143 // The source location entry is a buffer. The blob associated
1144 // with this entry contains the contents of the buffer.
1145
1146 // We add one to the size so that we capture the trailing NULL
1147 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1148 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001149 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001150 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001151 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001152 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1153 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001154 Record.clear();
1155 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1156 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001157 llvm::StringRef(Buffer->getBufferStart(),
1158 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001159
1160 if (strcmp(Name, "<built-in>") == 0)
1161 PreloadSLocs.push_back(SLocEntryOffsets.size());
1162 }
1163 } else {
1164 // The source location entry is an instantiation.
1165 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1166 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1167 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1168 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1169
1170 // Compute the token length for this macro expansion.
1171 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001172 if (I + 1 != N)
1173 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001174 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1175 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1176 }
1177 }
1178
Douglas Gregorc9490c02009-04-16 22:23:12 +00001179 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001180
1181 if (SLocEntryOffsets.empty())
1182 return;
1183
1184 // Write the source-location offsets table into the PCH block. This
1185 // table is used for lazily loading source-location information.
1186 using namespace llvm;
1187 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1188 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1192 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001194 Record.clear();
1195 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1196 Record.push_back(SLocEntryOffsets.size());
1197 Record.push_back(SourceMgr.getNextOffset());
1198 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001199 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001200 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001201
1202 // Write the source location entry preloads array, telling the PCH
1203 // reader which source locations entries it should load eagerly.
1204 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001205}
1206
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001207//===----------------------------------------------------------------------===//
1208// Preprocessor Serialization
1209//===----------------------------------------------------------------------===//
1210
Chris Lattner0b1fb982009-04-10 17:15:23 +00001211/// \brief Writes the block containing the serialized form of the
1212/// preprocessor.
1213///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001214void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001215 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001216
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001217 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1218 if (PP.getCounterValue() != 0) {
1219 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001220 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001221 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001222 }
1223
1224 // Enter the preprocessor block.
1225 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001227 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1228 // FIXME: use diagnostics subsystem for localization etc.
1229 if (PP.SawDateOrTime())
1230 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001232 // Loop over all the macro definitions that are live at the end of the file,
1233 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001234 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001235 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1236 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001237 // FIXME: This emits macros in hash table order, we should do it in a stable
1238 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001239 MacroInfo *MI = I->second;
1240
1241 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1242 // been redefined by the header (in which case they are not isBuiltinMacro).
1243 if (MI->isBuiltinMacro())
1244 continue;
1245
Chris Lattner7356a312009-04-11 21:15:38 +00001246 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001247 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001248 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1249 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001251 unsigned Code;
1252 if (MI->isObjectLike()) {
1253 Code = pch::PP_MACRO_OBJECT_LIKE;
1254 } else {
1255 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001257 Record.push_back(MI->isC99Varargs());
1258 Record.push_back(MI->isGNUVarargs());
1259 Record.push_back(MI->getNumArgs());
1260 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1261 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001262 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001263 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001264
1265 // If we have a detailed preprocessing record, record the macro definition
1266 // ID that corresponds to this macro.
1267 if (PPRec)
1268 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1269
Douglas Gregorc9490c02009-04-16 22:23:12 +00001270 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001271 Record.clear();
1272
Chris Lattnerdf961c22009-04-10 18:08:30 +00001273 // Emit the tokens array.
1274 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1275 // Note that we know that the preprocessor does not have any annotation
1276 // tokens in it because they are created by the parser, and thus can't be
1277 // in a macro definition.
1278 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Chris Lattnerdf961c22009-04-10 18:08:30 +00001280 Record.push_back(Tok.getLocation().getRawEncoding());
1281 Record.push_back(Tok.getLength());
1282
Chris Lattnerdf961c22009-04-10 18:08:30 +00001283 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1284 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001285 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Chris Lattnerdf961c22009-04-10 18:08:30 +00001287 // FIXME: Should translate token kind to a stable encoding.
1288 Record.push_back(Tok.getKind());
1289 // FIXME: Should translate token flags to a stable encoding.
1290 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Douglas Gregorc9490c02009-04-16 22:23:12 +00001292 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001293 Record.clear();
1294 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001295 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001296 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001297
1298 // If the preprocessor has a preprocessing record, emit it.
1299 unsigned NumPreprocessingRecords = 0;
1300 if (PPRec) {
1301 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1302 E != EEnd; ++E) {
1303 Record.clear();
1304
1305 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1306 Record.push_back(NumPreprocessingRecords++);
1307 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1308 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1309 AddIdentifierRef(MI->getName(), Record);
1310 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1311 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1312 continue;
1313 }
1314
1315 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1316 // Record this macro definition's location.
1317 pch::IdentID ID = getMacroDefinitionID(MD);
1318 if (ID != MacroDefinitionOffsets.size()) {
1319 if (ID > MacroDefinitionOffsets.size())
1320 MacroDefinitionOffsets.resize(ID + 1);
1321
1322 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1323 } else
1324 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1325
1326 Record.push_back(NumPreprocessingRecords++);
1327 Record.push_back(ID);
1328 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1329 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1330 AddIdentifierRef(MD->getName(), Record);
1331 AddSourceLocation(MD->getLocation(), Record);
1332 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1333 continue;
1334 }
1335 }
1336 }
1337
Douglas Gregorc9490c02009-04-16 22:23:12 +00001338 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001339
1340 // Write the offsets table for the preprocessing record.
1341 if (NumPreprocessingRecords > 0) {
1342 // Write the offsets table for identifier IDs.
1343 using namespace llvm;
1344 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1345 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1348 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1349 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1350
1351 Record.clear();
1352 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1353 Record.push_back(NumPreprocessingRecords);
1354 Record.push_back(MacroDefinitionOffsets.size());
1355 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1356 (const char *)&MacroDefinitionOffsets.front(),
1357 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1358 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001359}
1360
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001361//===----------------------------------------------------------------------===//
1362// Type Serialization
1363//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001364
Douglas Gregor2cf26342009-04-09 22:27:44 +00001365/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001366void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001367 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001368 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001369 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Douglas Gregor2cf26342009-04-09 22:27:44 +00001371 // Record the offset for this type.
1372 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001373 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001374 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1375 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001376 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001377 }
1378
1379 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Douglas Gregor2cf26342009-04-09 22:27:44 +00001381 // Emit the type's representation.
1382 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001383
Douglas Gregora4923eb2009-11-16 21:35:15 +00001384 if (T.hasLocalNonFastQualifiers()) {
1385 Qualifiers Qs = T.getLocalQualifiers();
1386 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001387 Record.push_back(Qs.getAsOpaqueValue());
1388 W.Code = pch::TYPE_EXT_QUAL;
1389 } else {
1390 switch (T->getTypeClass()) {
1391 // For all of the concrete, non-dependent types, call the
1392 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001393#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001394 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001395#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001396#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001397 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001398 }
1399
1400 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001401 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001402
1403 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001404 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001405}
1406
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001407//===----------------------------------------------------------------------===//
1408// Declaration Serialization
1409//===----------------------------------------------------------------------===//
1410
Douglas Gregor2cf26342009-04-09 22:27:44 +00001411/// \brief Write the block containing all of the declaration IDs
1412/// lexically declared within the given DeclContext.
1413///
1414/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1415/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001416uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001417 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001418 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001419 return 0;
1420
Douglas Gregorc9490c02009-04-16 22:23:12 +00001421 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001422 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001423 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1424 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001425 AddDeclRef(*D, Record);
1426
Douglas Gregor25123082009-04-22 22:34:57 +00001427 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001428 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001429 return Offset;
1430}
1431
1432/// \brief Write the block containing all of the declaration IDs
1433/// visible from the given DeclContext.
1434///
1435/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1436/// bistream, or 0 if no block was written.
1437uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1438 DeclContext *DC) {
1439 if (DC->getPrimaryContext() != DC)
1440 return 0;
1441
Douglas Gregoraff22df2009-04-21 22:32:33 +00001442 // Since there is no name lookup into functions or methods, and we
1443 // perform name lookup for the translation unit via the
1444 // IdentifierInfo chains, don't bother to build a
1445 // visible-declarations table for these entities.
1446 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001447 return 0;
1448
Douglas Gregor2cf26342009-04-09 22:27:44 +00001449 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001450 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001451
1452 // Serialize the contents of the mapping used for lookup. Note that,
1453 // although we have two very different code paths, the serialized
1454 // representation is the same for both cases: a declaration name,
1455 // followed by a size, followed by references to the visible
1456 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001457 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001458 RecordData Record;
1459 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001460 if (!Map)
1461 return 0;
1462
Douglas Gregor2cf26342009-04-09 22:27:44 +00001463 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1464 D != DEnd; ++D) {
1465 AddDeclarationName(D->first, Record);
1466 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1467 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001468 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001469 AddDeclRef(*Result.first, Record);
1470 }
1471
1472 if (Record.size() == 0)
1473 return 0;
1474
Douglas Gregorc9490c02009-04-16 22:23:12 +00001475 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001476 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001477 return Offset;
1478}
1479
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001480//===----------------------------------------------------------------------===//
1481// Global Method Pool and Selector Serialization
1482//===----------------------------------------------------------------------===//
1483
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001484namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001485// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001486class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001487 PCHWriter &Writer;
1488
1489public:
1490 typedef Selector key_type;
1491 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001493 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1494 typedef const data_type& data_type_ref;
1495
1496 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001498 static unsigned ComputeHash(Selector Sel) {
1499 unsigned N = Sel.getNumArgs();
1500 if (N == 0)
1501 ++N;
1502 unsigned R = 5381;
1503 for (unsigned I = 0; I != N; ++I)
1504 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001505 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001506 return R;
1507 }
Mike Stump1eb44332009-09-09 15:08:12 +00001508
1509 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001510 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1511 data_type_ref Methods) {
1512 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1513 clang::io::Emit16(Out, KeyLen);
1514 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001515 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001516 Method = Method->Next)
1517 if (Method->Method)
1518 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001519 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001520 Method = Method->Next)
1521 if (Method->Method)
1522 DataLen += 4;
1523 clang::io::Emit16(Out, DataLen);
1524 return std::make_pair(KeyLen, DataLen);
1525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Douglas Gregor83941df2009-04-25 17:48:32 +00001527 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001528 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001529 assert((Start >> 32) == 0 && "Selector key offset too large");
1530 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001531 unsigned N = Sel.getNumArgs();
1532 clang::io::Emit16(Out, N);
1533 if (N == 0)
1534 N = 1;
1535 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001536 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001537 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1538 }
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001540 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001541 data_type_ref Methods, unsigned DataLen) {
1542 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001543 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001544 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001545 Method = Method->Next)
1546 if (Method->Method)
1547 ++NumInstanceMethods;
1548
1549 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001550 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001551 Method = Method->Next)
1552 if (Method->Method)
1553 ++NumFactoryMethods;
1554
1555 clang::io::Emit16(Out, NumInstanceMethods);
1556 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001557 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001558 Method = Method->Next)
1559 if (Method->Method)
1560 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001561 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001562 Method = Method->Next)
1563 if (Method->Method)
1564 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001565
1566 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001567 }
1568};
1569} // end anonymous namespace
1570
1571/// \brief Write the method pool into the PCH file.
1572///
1573/// The method pool contains both instance and factory methods, stored
1574/// in an on-disk hash table indexed by the selector.
1575void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1576 using namespace llvm;
1577
1578 // Create and write out the blob that contains the instance and
1579 // factor method pools.
1580 bool Empty = true;
1581 {
1582 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001584 // Create the on-disk hash table representation. Start by
1585 // iterating through the instance method pool.
1586 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001587 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001588 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001589 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001590 InstanceEnd = SemaRef.InstanceMethodPool.end();
1591 Instance != InstanceEnd; ++Instance) {
1592 // Check whether there is a factory method with the same
1593 // selector.
1594 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1595 = SemaRef.FactoryMethodPool.find(Instance->first);
1596
1597 if (Factory == SemaRef.FactoryMethodPool.end())
1598 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001599 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600 ObjCMethodList()));
1601 else
1602 Generator.insert(Instance->first,
1603 std::make_pair(Instance->second, Factory->second));
1604
Douglas Gregor83941df2009-04-25 17:48:32 +00001605 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001606 Empty = false;
1607 }
1608
1609 // Now iterate through the factory method pool, to pick up any
1610 // selectors that weren't already in the instance method pool.
1611 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001612 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001613 FactoryEnd = SemaRef.FactoryMethodPool.end();
1614 Factory != FactoryEnd; ++Factory) {
1615 // Check whether there is an instance method with the same
1616 // selector. If so, there is no work to do here.
1617 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1618 = SemaRef.InstanceMethodPool.find(Factory->first);
1619
Douglas Gregor83941df2009-04-25 17:48:32 +00001620 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001621 Generator.insert(Factory->first,
1622 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001623 ++NumSelectorsInMethodPool;
1624 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001625
1626 Empty = false;
1627 }
1628
Douglas Gregor83941df2009-04-25 17:48:32 +00001629 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001630 return;
1631
1632 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001633 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001634 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001635 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001636 {
1637 PCHMethodPoolTrait Trait(*this);
1638 llvm::raw_svector_ostream Out(MethodPool);
1639 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001640 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001641 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001642
1643 // For every selector that we have seen but which was not
1644 // written into the hash table, write the selector itself and
1645 // record it's offset.
1646 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1647 if (SelectorOffsets[I] == 0)
1648 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001649 }
1650
1651 // Create a blob abbreviation
1652 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1653 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001656 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1657 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1658
Douglas Gregor83941df2009-04-25 17:48:32 +00001659 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001660 RecordData Record;
1661 Record.push_back(pch::METHOD_POOL);
1662 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001663 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001664 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001665
1666 // Create a blob abbreviation for the selector table offsets.
1667 Abbrev = new BitCodeAbbrev();
1668 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1669 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1670 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1671 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1672
1673 // Write the selector offsets table.
1674 Record.clear();
1675 Record.push_back(pch::SELECTOR_OFFSETS);
1676 Record.push_back(SelectorOffsets.size());
1677 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1678 (const char *)&SelectorOffsets.front(),
1679 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001680 }
1681}
1682
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001683//===----------------------------------------------------------------------===//
1684// Identifier Table Serialization
1685//===----------------------------------------------------------------------===//
1686
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001687namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001688class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001689 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001690 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001691
Douglas Gregora92193e2009-04-28 21:18:29 +00001692 /// \brief Determines whether this is an "interesting" identifier
1693 /// that needs a full IdentifierInfo structure written into the hash
1694 /// table.
1695 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1696 return II->isPoisoned() ||
1697 II->isExtensionToken() ||
1698 II->hasMacroDefinition() ||
1699 II->getObjCOrBuiltinID() ||
1700 II->getFETokenInfo<void>();
1701 }
1702
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001703public:
1704 typedef const IdentifierInfo* key_type;
1705 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001707 typedef pch::IdentID data_type;
1708 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001709
1710 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001711 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001712
1713 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001714 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001715 }
Mike Stump1eb44332009-09-09 15:08:12 +00001716
1717 std::pair<unsigned,unsigned>
1718 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001719 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001720 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001721 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1722 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001723 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001724 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001725 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001726 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001727 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1728 DEnd = IdentifierResolver::end();
1729 D != DEnd; ++D)
1730 DataLen += sizeof(pch::DeclID);
1731 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001732 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001733 // We emit the key length after the data length so that every
1734 // string is preceded by a 16-bit length. This matches the PTH
1735 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001736 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001737 return std::make_pair(KeyLen, DataLen);
1738 }
Mike Stump1eb44332009-09-09 15:08:12 +00001739
1740 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001741 unsigned KeyLen) {
1742 // Record the location of the key data. This is used when generating
1743 // the mapping from persistent IDs to strings.
1744 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001745 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001746 }
Mike Stump1eb44332009-09-09 15:08:12 +00001747
1748 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001749 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001750 if (!isInterestingIdentifier(II)) {
1751 clang::io::Emit32(Out, ID << 1);
1752 return;
1753 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001754
Douglas Gregora92193e2009-04-28 21:18:29 +00001755 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001756 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001757 bool hasMacroDefinition =
1758 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001759 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001760 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001761 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1762 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1763 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1764 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001765 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001766
Douglas Gregor37e26842009-04-21 23:56:24 +00001767 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001768 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001769
Douglas Gregor668c1a42009-04-21 22:25:48 +00001770 // Emit the declaration IDs in reverse order, because the
1771 // IdentifierResolver provides the declarations as they would be
1772 // visible (e.g., the function "stat" would come before the struct
1773 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1774 // adds declarations to the end of the list (so we need to see the
1775 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001776 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001777 IdentifierResolver::end());
1778 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1779 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001780 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001781 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001782 }
1783};
1784} // end anonymous namespace
1785
Douglas Gregorafaf3082009-04-11 00:14:32 +00001786/// \brief Write the identifier table into the PCH file.
1787///
1788/// The identifier table consists of a blob containing string data
1789/// (the actual identifiers themselves) and a separate "offsets" index
1790/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001791void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001792 using namespace llvm;
1793
1794 // Create and write out the blob that contains the identifier
1795 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001796 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001797 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Douglas Gregor92b059e2009-04-28 20:33:11 +00001799 // Look for any identifiers that were named while processing the
1800 // headers, but are otherwise not needed. We add these to the hash
1801 // table to enable checking of the predefines buffer in the case
1802 // where the user adds new macro definitions when building the PCH
1803 // file.
1804 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1805 IDEnd = PP.getIdentifierTable().end();
1806 ID != IDEnd; ++ID)
1807 getIdentifierRef(ID->second);
1808
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001809 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001810 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001811 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1812 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1813 ID != IDEnd; ++ID) {
1814 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001815 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001816 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001817
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001818 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001819 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001820 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001821 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001822 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001823 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001824 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001825 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001826 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001827 }
1828
1829 // Create a blob abbreviation
1830 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1831 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001834 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001835
1836 // Write the identifier table
1837 RecordData Record;
1838 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001839 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001840 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001841 }
1842
1843 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001844 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1845 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1848 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1849
1850 RecordData Record;
1851 Record.push_back(pch::IDENTIFIER_OFFSET);
1852 Record.push_back(IdentifierOffsets.size());
1853 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1854 (const char *)&IdentifierOffsets.front(),
1855 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001856}
1857
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001858//===----------------------------------------------------------------------===//
1859// General Serialization Routines
1860//===----------------------------------------------------------------------===//
1861
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001862/// \brief Write a record containing the given attributes.
1863void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1864 RecordData Record;
1865 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001866 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001867 Record.push_back(Attr->isInherited());
1868 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001869 default:
1870 assert(0 && "Does not support PCH writing for this attribute yet!");
1871 break;
Sean Hunt387475d2010-06-16 23:43:53 +00001872 case attr::Alias:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001873 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1874 break;
1875
Sean Hunt387475d2010-06-16 23:43:53 +00001876 case attr::AlignMac68k:
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001877 break;
1878
Sean Hunt387475d2010-06-16 23:43:53 +00001879 case attr::Aligned:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001880 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1881 break;
1882
Sean Hunt387475d2010-06-16 23:43:53 +00001883 case attr::AlwaysInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001884 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Sean Hunt387475d2010-06-16 23:43:53 +00001886 case attr::AnalyzerNoReturn:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001887 break;
1888
Sean Hunt387475d2010-06-16 23:43:53 +00001889 case attr::Annotate:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001890 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1891 break;
1892
Sean Hunt387475d2010-06-16 23:43:53 +00001893 case attr::AsmLabel:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001894 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1895 break;
1896
Sean Hunt387475d2010-06-16 23:43:53 +00001897 case attr::BaseCheck:
Sean Hunt7725e672009-11-25 04:20:27 +00001898 break;
1899
Sean Hunt387475d2010-06-16 23:43:53 +00001900 case attr::Blocks:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001901 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1902 break;
1903
Sean Hunt387475d2010-06-16 23:43:53 +00001904 case attr::CDecl:
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001905 break;
1906
Sean Hunt387475d2010-06-16 23:43:53 +00001907 case attr::Cleanup:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001908 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1909 break;
1910
Sean Hunt387475d2010-06-16 23:43:53 +00001911 case attr::Const:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001912 break;
1913
Sean Hunt387475d2010-06-16 23:43:53 +00001914 case attr::Constructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001915 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1916 break;
1917
Sean Hunt387475d2010-06-16 23:43:53 +00001918 case attr::DLLExport:
1919 case attr::DLLImport:
1920 case attr::Deprecated:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001921 break;
1922
Sean Hunt387475d2010-06-16 23:43:53 +00001923 case attr::Destructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001924 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1925 break;
1926
Sean Hunt387475d2010-06-16 23:43:53 +00001927 case attr::FastCall:
1928 case attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001929 break;
1930
Sean Hunt387475d2010-06-16 23:43:53 +00001931 case attr::Format: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001932 const FormatAttr *Format = cast<FormatAttr>(Attr);
1933 AddString(Format->getType(), Record);
1934 Record.push_back(Format->getFormatIdx());
1935 Record.push_back(Format->getFirstArg());
1936 break;
1937 }
1938
Sean Hunt387475d2010-06-16 23:43:53 +00001939 case attr::FormatArg: {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001940 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1941 Record.push_back(Format->getFormatIdx());
1942 break;
1943 }
1944
Sean Hunt387475d2010-06-16 23:43:53 +00001945 case attr::Sentinel : {
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001946 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1947 Record.push_back(Sentinel->getSentinel());
1948 Record.push_back(Sentinel->getNullPos());
1949 break;
1950 }
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Sean Hunt387475d2010-06-16 23:43:53 +00001952 case attr::GNUInline:
1953 case attr::Hiding:
1954 case attr::IBAction:
1955 case attr::IBOutlet:
1956 case attr::Malloc:
1957 case attr::NoDebug:
1958 case attr::NoInline:
1959 case attr::NoReturn:
1960 case attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001961 break;
1962
Sean Hunt387475d2010-06-16 23:43:53 +00001963 case attr::IBOutletCollection: {
Ted Kremenek857e9182010-05-19 17:38:06 +00001964 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
1965 AddDeclRef(ICA->getClass(), Record);
1966 break;
1967 }
1968
Sean Hunt387475d2010-06-16 23:43:53 +00001969 case attr::NonNull: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001970 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1971 Record.push_back(NonNull->size());
1972 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1973 break;
1974 }
1975
Sean Hunt387475d2010-06-16 23:43:53 +00001976 case attr::CFReturnsNotRetained:
1977 case attr::CFReturnsRetained:
1978 case attr::NSReturnsNotRetained:
1979 case attr::NSReturnsRetained:
1980 case attr::ObjCException:
1981 case attr::ObjCNSObject:
1982 case attr::Overloadable:
1983 case attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001984 break;
1985
Sean Hunt387475d2010-06-16 23:43:53 +00001986 case attr::MaxFieldAlignment:
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00001987 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001988 break;
1989
Sean Hunt387475d2010-06-16 23:43:53 +00001990 case attr::Packed:
Anders Carlssona860e752009-08-08 18:23:56 +00001991 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Sean Hunt387475d2010-06-16 23:43:53 +00001993 case attr::Pure:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001994 break;
1995
Sean Hunt387475d2010-06-16 23:43:53 +00001996 case attr::Regparm:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001997 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1998 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Sean Hunt387475d2010-06-16 23:43:53 +00002000 case attr::ReqdWorkGroupSize:
Nate Begeman6f3d8382009-06-26 06:32:41 +00002001 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2002 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2003 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2004 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002005
Sean Hunt387475d2010-06-16 23:43:53 +00002006 case attr::Section:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002007 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2008 break;
2009
Sean Hunt387475d2010-06-16 23:43:53 +00002010 case attr::StdCall:
2011 case attr::TransparentUnion:
2012 case attr::Unavailable:
2013 case attr::Unused:
2014 case attr::Used:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002015 break;
2016
Sean Hunt387475d2010-06-16 23:43:53 +00002017 case attr::Visibility:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002018 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00002019 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002020 break;
2021
Sean Hunt387475d2010-06-16 23:43:53 +00002022 case attr::WarnUnusedResult:
2023 case attr::Weak:
2024 case attr::WeakRef:
2025 case attr::WeakImport:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002026 break;
2027 }
2028 }
2029
Douglas Gregorc9490c02009-04-16 22:23:12 +00002030 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002031}
2032
2033void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2034 Record.push_back(Str.size());
2035 Record.insert(Record.end(), Str.begin(), Str.end());
2036}
2037
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002038/// \brief Note that the identifier II occurs at the given offset
2039/// within the identifier table.
2040void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002041 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002042}
2043
Douglas Gregor83941df2009-04-25 17:48:32 +00002044/// \brief Note that the selector Sel occurs at the given offset
2045/// within the method pool/selector table.
2046void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2047 unsigned ID = SelectorIDs[Sel];
2048 assert(ID && "Unknown selector");
2049 SelectorOffsets[ID - 1] = Offset;
2050}
2051
Mike Stump1eb44332009-09-09 15:08:12 +00002052PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2053 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00002054 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2055 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002056
Douglas Gregore650c8c2009-07-07 00:12:59 +00002057void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2058 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002059 using namespace llvm;
2060
Douglas Gregore7785042009-04-20 15:53:59 +00002061 ASTContext &Context = SemaRef.Context;
2062 Preprocessor &PP = SemaRef.PP;
2063
Douglas Gregor2cf26342009-04-09 22:27:44 +00002064 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002065 Stream.Emit((unsigned)'C', 8);
2066 Stream.Emit((unsigned)'P', 8);
2067 Stream.Emit((unsigned)'C', 8);
2068 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002070 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002071
2072 // The translation unit is the first declaration we'll emit.
2073 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002074 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002075
Douglas Gregor2deaea32009-04-22 18:49:13 +00002076 // Make sure that we emit IdentifierInfos (and any attached
2077 // declarations) for builtins.
2078 {
2079 IdentifierTable &Table = PP.getIdentifierTable();
2080 llvm::SmallVector<const char *, 32> BuiltinNames;
2081 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2082 Context.getLangOptions().NoBuiltin);
2083 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2084 getIdentifierRef(&Table.get(BuiltinNames[I]));
2085 }
2086
Chris Lattner63d65f82009-09-08 18:19:27 +00002087 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002088 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002089 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002090 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002091 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2092 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002093 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002094
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002095 // Build a record containing all of the static unused functions in this file.
2096 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002097 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002098 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002099
Douglas Gregor14c22f22009-04-22 22:18:58 +00002100 // Build a record containing all of the locally-scoped external
2101 // declarations in this header file. Generally, this record will be
2102 // empty.
2103 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002104 // FIXME: This is filling in the PCH file in densemap order which is
2105 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002106 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002107 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2108 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2109 TD != TDEnd; ++TD)
2110 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2111
Douglas Gregorb81c1702009-04-27 20:06:05 +00002112 // Build a record containing all of the ext_vector declarations.
2113 RecordData ExtVectorDecls;
2114 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2115 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2116
Douglas Gregor2cf26342009-04-09 22:27:44 +00002117 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002118 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002119 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002120 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002121 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002122 if (StatCalls && !isysroot)
2123 WriteStatCache(*StatCalls, isysroot);
2124 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002125 // Write the record of special types.
2126 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002128 AddTypeRef(Context.getBuiltinVaListType(), Record);
2129 AddTypeRef(Context.getObjCIdType(), Record);
2130 AddTypeRef(Context.getObjCSelType(), Record);
2131 AddTypeRef(Context.getObjCProtoType(), Record);
2132 AddTypeRef(Context.getObjCClassType(), Record);
2133 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2134 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2135 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002136 AddTypeRef(Context.getjmp_bufType(), Record);
2137 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002138 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2139 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002140 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002141 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002142 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2143 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002144 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Douglas Gregor366809a2009-04-26 03:49:13 +00002146 // Keep writing types and declarations until all types and
2147 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002148 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2149 WriteDeclsBlockAbbrevs();
2150 while (!DeclTypesToEmit.empty()) {
2151 DeclOrType DOT = DeclTypesToEmit.front();
2152 DeclTypesToEmit.pop();
2153 if (DOT.isType())
2154 WriteType(DOT.getType());
2155 else
2156 WriteDecl(Context, DOT.getDecl());
2157 }
2158 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002159
Douglas Gregor813a97b2009-10-17 17:25:45 +00002160 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002161 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002162 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002163
2164 // Write the type offsets array
2165 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2166 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2168 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2169 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2170 Record.clear();
2171 Record.push_back(pch::TYPE_OFFSET);
2172 Record.push_back(TypeOffsets.size());
2173 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002174 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002175 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002176
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002177 // Write the declaration offsets array
2178 Abbrev = new BitCodeAbbrev();
2179 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2182 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2183 Record.clear();
2184 Record.push_back(pch::DECL_OFFSET);
2185 Record.push_back(DeclOffsets.size());
2186 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002187 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002188 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002189
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002190 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002191 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002192 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002193
2194 // Write the record containing tentative definitions.
2195 if (!TentativeDefinitions.empty())
2196 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002197
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002198 // Write the record containing unused static functions.
2199 if (!UnusedStaticFuncs.empty())
2200 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002201
Douglas Gregor14c22f22009-04-22 22:18:58 +00002202 // Write the record containing locally-scoped external definitions.
2203 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002204 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002205 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002206
2207 // Write the record containing ext_vector type names.
2208 if (!ExtVectorDecls.empty())
2209 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Douglas Gregor3e1af842009-04-17 22:13:46 +00002211 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002212 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002213 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002214 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002215 Record.push_back(NumLexicalDeclContexts);
2216 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002217 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002218 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002219}
2220
2221void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2222 Record.push_back(Loc.getRawEncoding());
2223}
2224
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002225void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2226 AddSourceLocation(Range.getBegin(), Record);
2227 AddSourceLocation(Range.getEnd(), Record);
2228}
2229
Douglas Gregor2cf26342009-04-09 22:27:44 +00002230void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2231 Record.push_back(Value.getBitWidth());
2232 unsigned N = Value.getNumWords();
2233 const uint64_t* Words = Value.getRawData();
2234 for (unsigned I = 0; I != N; ++I)
2235 Record.push_back(Words[I]);
2236}
2237
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002238void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2239 Record.push_back(Value.isUnsigned());
2240 AddAPInt(Value, Record);
2241}
2242
Douglas Gregor17fc2232009-04-14 21:55:33 +00002243void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2244 AddAPInt(Value.bitcastToAPInt(), Record);
2245}
2246
Douglas Gregor2cf26342009-04-09 22:27:44 +00002247void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002248 Record.push_back(getIdentifierRef(II));
2249}
2250
2251pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2252 if (II == 0)
2253 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002254
2255 pch::IdentID &ID = IdentifierIDs[II];
2256 if (ID == 0)
2257 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002258 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002259}
2260
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002261pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2262 if (MD == 0)
2263 return 0;
2264
2265 pch::IdentID &ID = MacroDefinitions[MD];
2266 if (ID == 0)
2267 ID = MacroDefinitions.size();
2268 return ID;
2269}
2270
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002271void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2272 if (SelRef.getAsOpaquePtr() == 0) {
2273 Record.push_back(0);
2274 return;
2275 }
2276
2277 pch::SelectorID &SID = SelectorIDs[SelRef];
2278 if (SID == 0) {
2279 SID = SelectorIDs.size();
2280 SelVector.push_back(SelRef);
2281 }
2282 Record.push_back(SID);
2283}
2284
Chris Lattnerd2598362010-05-10 00:25:06 +00002285void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2286 AddDeclRef(Temp->getDestructor(), Record);
2287}
2288
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002289void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2290 const TemplateArgumentLocInfo &Arg,
2291 RecordData &Record) {
2292 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002293 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002294 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002295 break;
2296 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002297 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002298 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002299 case TemplateArgument::Template:
2300 Record.push_back(
2301 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2302 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2303 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2304 break;
John McCall833ca992009-10-29 08:12:44 +00002305 case TemplateArgument::Null:
2306 case TemplateArgument::Integral:
2307 case TemplateArgument::Declaration:
2308 case TemplateArgument::Pack:
2309 break;
2310 }
2311}
2312
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002313void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2314 RecordData &Record) {
2315 AddTemplateArgument(Arg.getArgument(), Record);
2316 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2317 Record);
2318}
2319
John McCalla93c9342009-12-07 02:54:59 +00002320void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2321 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002322 AddTypeRef(QualType(), Record);
2323 return;
2324 }
2325
John McCalla93c9342009-12-07 02:54:59 +00002326 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002327 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002328 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002329 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002330}
2331
Douglas Gregor2cf26342009-04-09 22:27:44 +00002332void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2333 if (T.isNull()) {
2334 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2335 return;
2336 }
2337
Douglas Gregora4923eb2009-11-16 21:35:15 +00002338 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002339 T.removeFastQualifiers();
2340
Douglas Gregora4923eb2009-11-16 21:35:15 +00002341 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002342 pch::TypeID &ID = TypeIDs[T];
2343 if (ID == 0) {
2344 // We haven't seen these qualifiers applied to this type before.
2345 // Assign it a new ID. This is the only time we enqueue a
2346 // qualified type, and it has no CV qualifiers.
2347 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002348 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002349 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002350
John McCall0953e762009-09-24 19:53:00 +00002351 // Encode the type qualifiers in the type reference.
2352 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2353 return;
2354 }
2355
Douglas Gregora4923eb2009-11-16 21:35:15 +00002356 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002357
Douglas Gregor2cf26342009-04-09 22:27:44 +00002358 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002359 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002360 switch (BT->getKind()) {
2361 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2362 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2363 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2364 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2365 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2366 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2367 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2368 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002369 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002370 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2371 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2372 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2373 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2374 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2375 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2376 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002377 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002378 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2379 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2380 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002381 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002382 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2383 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002384 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2385 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002386 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2387 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002388 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002389 case BuiltinType::UndeducedAuto:
2390 assert(0 && "Should not see undeduced auto here");
2391 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002392 }
2393
John McCall0953e762009-09-24 19:53:00 +00002394 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002395 return;
2396 }
2397
John McCall0953e762009-09-24 19:53:00 +00002398 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002399 if (ID == 0) {
2400 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002401 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002402 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002403 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002404 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002405
2406 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002407 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002408}
2409
2410void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2411 if (D == 0) {
2412 Record.push_back(0);
2413 return;
2414 }
2415
Douglas Gregor8038d512009-04-10 17:25:41 +00002416 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002417 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002418 // We haven't seen this declaration before. Give it a new ID and
2419 // enqueue it in the list of declarations to emit.
2420 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002421 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002422 }
2423
2424 Record.push_back(ID);
2425}
2426
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002427pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2428 if (D == 0)
2429 return 0;
2430
2431 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2432 return DeclIDs[D];
2433}
2434
Douglas Gregor2cf26342009-04-09 22:27:44 +00002435void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002436 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002437 Record.push_back(Name.getNameKind());
2438 switch (Name.getNameKind()) {
2439 case DeclarationName::Identifier:
2440 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2441 break;
2442
2443 case DeclarationName::ObjCZeroArgSelector:
2444 case DeclarationName::ObjCOneArgSelector:
2445 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002446 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002447 break;
2448
2449 case DeclarationName::CXXConstructorName:
2450 case DeclarationName::CXXDestructorName:
2451 case DeclarationName::CXXConversionFunctionName:
2452 AddTypeRef(Name.getCXXNameType(), Record);
2453 break;
2454
2455 case DeclarationName::CXXOperatorName:
2456 Record.push_back(Name.getCXXOverloadedOperator());
2457 break;
2458
Sean Hunt3e518bd2009-11-29 07:34:05 +00002459 case DeclarationName::CXXLiteralOperatorName:
2460 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2461 break;
2462
Douglas Gregor2cf26342009-04-09 22:27:44 +00002463 case DeclarationName::CXXUsingDirective:
2464 // No extra data to emit
2465 break;
2466 }
2467}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002468
2469void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2470 RecordData &Record) {
2471 // Nested name specifiers usually aren't too long. I think that 8 would
2472 // typically accomodate the vast majority.
2473 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2474
2475 // Push each of the NNS's onto a stack for serialization in reverse order.
2476 while (NNS) {
2477 NestedNames.push_back(NNS);
2478 NNS = NNS->getPrefix();
2479 }
2480
2481 Record.push_back(NestedNames.size());
2482 while(!NestedNames.empty()) {
2483 NNS = NestedNames.pop_back_val();
2484 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2485 Record.push_back(Kind);
2486 switch (Kind) {
2487 case NestedNameSpecifier::Identifier:
2488 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2489 break;
2490
2491 case NestedNameSpecifier::Namespace:
2492 AddDeclRef(NNS->getAsNamespace(), Record);
2493 break;
2494
2495 case NestedNameSpecifier::TypeSpec:
2496 case NestedNameSpecifier::TypeSpecWithTemplate:
2497 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2498 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2499 break;
2500
2501 case NestedNameSpecifier::Global:
2502 // Don't need to write an associated value.
2503 break;
2504 }
2505 }
2506}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002507
2508void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2509 TemplateName::NameKind Kind = Name.getKind();
2510 Record.push_back(Kind);
2511 switch (Kind) {
2512 case TemplateName::Template:
2513 AddDeclRef(Name.getAsTemplateDecl(), Record);
2514 break;
2515
2516 case TemplateName::OverloadedTemplate: {
2517 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2518 Record.push_back(OvT->size());
2519 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2520 I != E; ++I)
2521 AddDeclRef(*I, Record);
2522 break;
2523 }
2524
2525 case TemplateName::QualifiedTemplate: {
2526 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2527 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2528 Record.push_back(QualT->hasTemplateKeyword());
2529 AddDeclRef(QualT->getTemplateDecl(), Record);
2530 break;
2531 }
2532
2533 case TemplateName::DependentTemplate: {
2534 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2535 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2536 Record.push_back(DepT->isIdentifier());
2537 if (DepT->isIdentifier())
2538 AddIdentifierRef(DepT->getIdentifier(), Record);
2539 else
2540 Record.push_back(DepT->getOperator());
2541 break;
2542 }
2543 }
2544}
2545
2546void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2547 RecordData &Record) {
2548 Record.push_back(Arg.getKind());
2549 switch (Arg.getKind()) {
2550 case TemplateArgument::Null:
2551 break;
2552 case TemplateArgument::Type:
2553 AddTypeRef(Arg.getAsType(), Record);
2554 break;
2555 case TemplateArgument::Declaration:
2556 AddDeclRef(Arg.getAsDecl(), Record);
2557 break;
2558 case TemplateArgument::Integral:
2559 AddAPSInt(*Arg.getAsIntegral(), Record);
2560 AddTypeRef(Arg.getIntegralType(), Record);
2561 break;
2562 case TemplateArgument::Template:
2563 AddTemplateName(Arg.getAsTemplate(), Record);
2564 break;
2565 case TemplateArgument::Expression:
2566 AddStmt(Arg.getAsExpr());
2567 break;
2568 case TemplateArgument::Pack:
2569 Record.push_back(Arg.pack_size());
2570 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2571 I != E; ++I)
2572 AddTemplateArgument(*I, Record);
2573 break;
2574 }
2575}