blob: 41815314d791441fa6dc9509639d493af493f503 [file] [log] [blame]
Chris Lattnere127a0d2010-04-20 20:35:58 +00001//===--- PCHWriter.cpp - Precompiled Headers Writer -----------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregore7785042009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump1eb44332009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000033#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000045
Douglas Gregor2cf26342009-04-09 22:27:44 +000046namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000047 class PCHTypeWriter {
Douglas Gregor2cf26342009-04-09 22:27:44 +000048 PCHWriter &Writer;
49 PCHWriter::RecordData &Record;
50
51 public:
52 /// \brief Type code that corresponds to the record generated.
53 pch::TypeCode Code;
54
Mike Stump1eb44332009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
58 void VisitArrayType(const ArrayType *T);
59 void VisitFunctionType(const FunctionType *T);
60 void VisitTagType(const TagType *T);
61
62#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000064#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
Douglas Gregor2cf26342009-04-09 22:27:44 +000072void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
73 Writer.AddTypeRef(T->getElementType(), Record);
74 Code = pch::TYPE_COMPLEX;
75}
76
77void PCHTypeWriter::VisitPointerType(const PointerType *T) {
78 Writer.AddTypeRef(T->getPointeeType(), Record);
79 Code = pch::TYPE_POINTER;
80}
81
82void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000084 Code = pch::TYPE_BLOCK_POINTER;
85}
86
87void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_LVALUE_REFERENCE;
90}
91
92void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_RVALUE_REFERENCE;
95}
96
97void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100 Code = pch::TYPE_MEMBER_POINTER;
101}
102
103void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
104 Writer.AddTypeRef(T->getElementType(), Record);
105 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000106 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
109void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
110 VisitArrayType(T);
111 Writer.AddAPInt(T->getSize(), Record);
112 Code = pch::TYPE_CONSTANT_ARRAY;
113}
114
115void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
116 VisitArrayType(T);
117 Code = pch::TYPE_INCOMPLETE_ARRAY;
118}
119
120void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
121 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000122 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
123 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000124 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125 Code = pch::TYPE_VARIABLE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVectorType(const VectorType *T) {
129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getNumElements());
Chris Lattner788b0fd2010-06-23 06:00:24 +0000131 Record.push_back(T->getAltiVecSpecific());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132 Code = pch::TYPE_VECTOR;
133}
134
135void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
136 VisitVectorType(T);
137 Code = pch::TYPE_EXT_VECTOR;
138}
139
140void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
141 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000142 FunctionType::ExtInfo C = T->getExtInfo();
143 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000144 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000145 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000146 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147}
148
149void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
150 VisitFunctionType(T);
151 Code = pch::TYPE_FUNCTION_NO_PROTO;
152}
153
154void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
155 VisitFunctionType(T);
156 Record.push_back(T->getNumArgs());
157 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
158 Writer.AddTypeRef(T->getArgType(I), Record);
159 Record.push_back(T->isVariadic());
160 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000161 Record.push_back(T->hasExceptionSpec());
162 Record.push_back(T->hasAnyExceptionSpec());
163 Record.push_back(T->getNumExceptions());
164 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
165 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 Code = pch::TYPE_FUNCTION_PROTO;
167}
168
John McCalled976492009-12-04 22:46:56 +0000169void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
170 Writer.AddDeclRef(T->getDecl(), Record);
171 Code = pch::TYPE_UNRESOLVED_USING;
172}
John McCalled976492009-12-04 22:46:56 +0000173
Douglas Gregor2cf26342009-04-09 22:27:44 +0000174void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
175 Writer.AddDeclRef(T->getDecl(), Record);
176 Code = pch::TYPE_TYPEDEF;
177}
178
179void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000180 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181 Code = pch::TYPE_TYPEOF_EXPR;
182}
183
184void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
185 Writer.AddTypeRef(T->getUnderlyingType(), Record);
186 Code = pch::TYPE_TYPEOF;
187}
188
Anders Carlsson395b4752009-06-24 19:06:50 +0000189void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
190 Writer.AddStmt(T->getUnderlyingExpr());
191 Code = pch::TYPE_DECLTYPE;
192}
193
Douglas Gregor2cf26342009-04-09 22:27:44 +0000194void PCHTypeWriter::VisitTagType(const TagType *T) {
195 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000196 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197 "Cannot serialize in the middle of a type definition");
198}
199
200void PCHTypeWriter::VisitRecordType(const RecordType *T) {
201 VisitTagType(T);
202 Code = pch::TYPE_RECORD;
203}
204
205void PCHTypeWriter::VisitEnumType(const EnumType *T) {
206 VisitTagType(T);
207 Code = pch::TYPE_ENUM;
208}
209
Mike Stump1eb44332009-09-09 15:08:12 +0000210void
John McCall49a832b2009-10-18 09:09:24 +0000211PCHTypeWriter::VisitSubstTemplateTypeParmType(
212 const SubstTemplateTypeParmType *T) {
213 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
214 Writer.AddTypeRef(T->getReplacementType(), Record);
215 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
216}
217
218void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219PCHTypeWriter::VisitTemplateSpecializationType(
220 const TemplateSpecializationType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000221 Writer.AddTemplateName(T->getTemplateName(), Record);
222 Record.push_back(T->getNumArgs());
223 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
224 ArgI != ArgE; ++ArgI)
225 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000226 QualType Canon = T->getCanonicalTypeInternal();
227 Writer.AddTypeRef(Canon.getTypePtr() != T ? Canon : QualType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000228 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
229}
230
231void
232PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000233 VisitArrayType(T);
234 Writer.AddStmt(T->getSizeExpr());
235 Writer.AddSourceRange(T->getBracketsRange(), Record);
236 Code = pch::TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000237}
238
239void
240PCHTypeWriter::VisitDependentSizedExtVectorType(
241 const DependentSizedExtVectorType *T) {
242 // FIXME: Serialize this type (C++ only)
243 assert(false && "Cannot serialize dependent sized extended vector types");
244}
245
246void
247PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
248 Record.push_back(T->getDepth());
249 Record.push_back(T->getIndex());
250 Record.push_back(T->isParameterPack());
251 Writer.AddIdentifierRef(T->getName(), Record);
252 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
253}
254
255void
256PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000257 Record.push_back(T->getKeyword());
258 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
259 Writer.AddIdentifierRef(T->getIdentifier(), Record);
260 Code = pch::TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000261}
262
263void
264PCHTypeWriter::VisitDependentTemplateSpecializationType(
265 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000266 Record.push_back(T->getKeyword());
267 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
268 Writer.AddIdentifierRef(T->getIdentifier(), Record);
269 Record.push_back(T->getNumArgs());
270 for (DependentTemplateSpecializationType::iterator
271 I = T->begin(), E = T->end(); I != E; ++I)
272 Writer.AddTemplateArgument(*I, Record);
273 Code = pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000274}
275
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000276void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000277 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000278 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
279 Writer.AddTypeRef(T->getNamedType(), Record);
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000280 Code = pch::TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000281}
282
John McCall3cb0ebd2010-03-10 03:28:59 +0000283void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
284 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000285 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000286 Code = pch::TYPE_INJECTED_CLASS_NAME;
287}
288
Douglas Gregor2cf26342009-04-09 22:27:44 +0000289void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
290 Writer.AddDeclRef(T->getDecl(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000291 Code = pch::TYPE_OBJC_INTERFACE;
292}
293
294void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
295 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000296 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000297 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000298 E = T->qual_end(); I != E; ++I)
299 Writer.AddDeclRef(*I, Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000300 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000301}
302
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000303void
304PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000305 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000306 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000307}
308
John McCalla1ee0c52009-10-16 21:56:05 +0000309namespace {
310
311class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
312 PCHWriter &Writer;
313 PCHWriter::RecordData &Record;
314
315public:
316 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
317 : Writer(Writer), Record(Record) { }
318
John McCall51bd8032009-10-18 01:05:36 +0000319#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000320#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000321 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000322#include "clang/AST/TypeLocNodes.def"
323
John McCall51bd8032009-10-18 01:05:36 +0000324 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
325 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000326};
327
328}
329
John McCall51bd8032009-10-18 01:05:36 +0000330void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
331 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000332}
John McCall51bd8032009-10-18 01:05:36 +0000333void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000334 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
335 if (TL.needsExtraLocalData()) {
336 Record.push_back(TL.getWrittenTypeSpec());
337 Record.push_back(TL.getWrittenSignSpec());
338 Record.push_back(TL.getWrittenWidthSpec());
339 Record.push_back(TL.hasModeAttr());
340 }
John McCalla1ee0c52009-10-16 21:56:05 +0000341}
John McCall51bd8032009-10-18 01:05:36 +0000342void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
343 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000344}
John McCall51bd8032009-10-18 01:05:36 +0000345void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
346 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000347}
John McCall51bd8032009-10-18 01:05:36 +0000348void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
349 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000350}
John McCall51bd8032009-10-18 01:05:36 +0000351void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
352 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000353}
John McCall51bd8032009-10-18 01:05:36 +0000354void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
355 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000356}
John McCall51bd8032009-10-18 01:05:36 +0000357void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
358 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000359}
John McCall51bd8032009-10-18 01:05:36 +0000360void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
362 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
363 Record.push_back(TL.getSizeExpr() ? 1 : 0);
364 if (TL.getSizeExpr())
365 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000366}
John McCall51bd8032009-10-18 01:05:36 +0000367void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
368 VisitArrayTypeLoc(TL);
369}
370void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
371 VisitArrayTypeLoc(TL);
372}
373void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
374 VisitArrayTypeLoc(TL);
375}
376void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
377 DependentSizedArrayTypeLoc TL) {
378 VisitArrayTypeLoc(TL);
379}
380void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
381 DependentSizedExtVectorTypeLoc TL) {
382 Writer.AddSourceLocation(TL.getNameLoc(), Record);
383}
384void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
385 Writer.AddSourceLocation(TL.getNameLoc(), Record);
386}
387void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
388 Writer.AddSourceLocation(TL.getNameLoc(), Record);
389}
390void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
392 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
393 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
394 Writer.AddDeclRef(TL.getArg(i), Record);
395}
396void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
397 VisitFunctionTypeLoc(TL);
398}
399void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
400 VisitFunctionTypeLoc(TL);
401}
John McCalled976492009-12-04 22:46:56 +0000402void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
403 Writer.AddSourceLocation(TL.getNameLoc(), Record);
404}
John McCall51bd8032009-10-18 01:05:36 +0000405void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getNameLoc(), Record);
407}
408void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000409 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
410 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
411 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000412}
413void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000414 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
415 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
416 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
417 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000418}
419void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
420 Writer.AddSourceLocation(TL.getNameLoc(), Record);
421}
422void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getNameLoc(), Record);
424}
425void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getNameLoc(), Record);
427}
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getNameLoc(), Record);
430}
John McCall49a832b2009-10-18 09:09:24 +0000431void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
432 SubstTemplateTypeParmTypeLoc TL) {
433 Writer.AddSourceLocation(TL.getNameLoc(), Record);
434}
John McCall51bd8032009-10-18 01:05:36 +0000435void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
436 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000437 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
438 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
439 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
440 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000441 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
442 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000443}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000444void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000445 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
446 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000447}
John McCall3cb0ebd2010-03-10 03:28:59 +0000448void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
449 Writer.AddSourceLocation(TL.getNameLoc(), Record);
450}
Douglas Gregor4714c122010-03-31 17:34:00 +0000451void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000452 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
453 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000454 Writer.AddSourceLocation(TL.getNameLoc(), Record);
455}
John McCall33500952010-06-11 00:33:02 +0000456void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
457 DependentTemplateSpecializationTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
459 Writer.AddSourceRange(TL.getQualifierRange(), Record);
460 Writer.AddSourceLocation(TL.getNameLoc(), Record);
461 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
462 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
463 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000464 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
465 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000466}
John McCall51bd8032009-10-18 01:05:36 +0000467void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
468 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000469}
470void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
471 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000472 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
473 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
474 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
475 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000476}
John McCall54e14c42009-10-22 22:37:11 +0000477void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000479}
John McCalla1ee0c52009-10-16 21:56:05 +0000480
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000481//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000482// PCHWriter Implementation
483//===----------------------------------------------------------------------===//
484
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000485static void EmitBlockID(unsigned ID, const char *Name,
486 llvm::BitstreamWriter &Stream,
487 PCHWriter::RecordData &Record) {
488 Record.clear();
489 Record.push_back(ID);
490 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
491
492 // Emit the block name if present.
493 if (Name == 0 || Name[0] == 0) return;
494 Record.clear();
495 while (*Name)
496 Record.push_back(*Name++);
497 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
498}
499
500static void EmitRecordID(unsigned ID, const char *Name,
501 llvm::BitstreamWriter &Stream,
502 PCHWriter::RecordData &Record) {
503 Record.clear();
504 Record.push_back(ID);
505 while (*Name)
506 Record.push_back(*Name++);
507 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000508}
509
510static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
511 PCHWriter::RecordData &Record) {
512#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
513 RECORD(STMT_STOP);
514 RECORD(STMT_NULL_PTR);
515 RECORD(STMT_NULL);
516 RECORD(STMT_COMPOUND);
517 RECORD(STMT_CASE);
518 RECORD(STMT_DEFAULT);
519 RECORD(STMT_LABEL);
520 RECORD(STMT_IF);
521 RECORD(STMT_SWITCH);
522 RECORD(STMT_WHILE);
523 RECORD(STMT_DO);
524 RECORD(STMT_FOR);
525 RECORD(STMT_GOTO);
526 RECORD(STMT_INDIRECT_GOTO);
527 RECORD(STMT_CONTINUE);
528 RECORD(STMT_BREAK);
529 RECORD(STMT_RETURN);
530 RECORD(STMT_DECL);
531 RECORD(STMT_ASM);
532 RECORD(EXPR_PREDEFINED);
533 RECORD(EXPR_DECL_REF);
534 RECORD(EXPR_INTEGER_LITERAL);
535 RECORD(EXPR_FLOATING_LITERAL);
536 RECORD(EXPR_IMAGINARY_LITERAL);
537 RECORD(EXPR_STRING_LITERAL);
538 RECORD(EXPR_CHARACTER_LITERAL);
539 RECORD(EXPR_PAREN);
540 RECORD(EXPR_UNARY_OPERATOR);
541 RECORD(EXPR_SIZEOF_ALIGN_OF);
542 RECORD(EXPR_ARRAY_SUBSCRIPT);
543 RECORD(EXPR_CALL);
544 RECORD(EXPR_MEMBER);
545 RECORD(EXPR_BINARY_OPERATOR);
546 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
547 RECORD(EXPR_CONDITIONAL_OPERATOR);
548 RECORD(EXPR_IMPLICIT_CAST);
549 RECORD(EXPR_CSTYLE_CAST);
550 RECORD(EXPR_COMPOUND_LITERAL);
551 RECORD(EXPR_EXT_VECTOR_ELEMENT);
552 RECORD(EXPR_INIT_LIST);
553 RECORD(EXPR_DESIGNATED_INIT);
554 RECORD(EXPR_IMPLICIT_VALUE_INIT);
555 RECORD(EXPR_VA_ARG);
556 RECORD(EXPR_ADDR_LABEL);
557 RECORD(EXPR_STMT);
558 RECORD(EXPR_TYPES_COMPATIBLE);
559 RECORD(EXPR_CHOOSE);
560 RECORD(EXPR_GNU_NULL);
561 RECORD(EXPR_SHUFFLE_VECTOR);
562 RECORD(EXPR_BLOCK);
563 RECORD(EXPR_BLOCK_DECL_REF);
564 RECORD(EXPR_OBJC_STRING_LITERAL);
565 RECORD(EXPR_OBJC_ENCODE);
566 RECORD(EXPR_OBJC_SELECTOR_EXPR);
567 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
568 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
569 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
570 RECORD(EXPR_OBJC_KVC_REF_EXPR);
571 RECORD(EXPR_OBJC_MESSAGE_EXPR);
572 RECORD(EXPR_OBJC_SUPER_EXPR);
573 RECORD(STMT_OBJC_FOR_COLLECTION);
574 RECORD(STMT_OBJC_CATCH);
575 RECORD(STMT_OBJC_FINALLY);
576 RECORD(STMT_OBJC_AT_TRY);
577 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
578 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000579 RECORD(EXPR_CXX_OPERATOR_CALL);
580 RECORD(EXPR_CXX_CONSTRUCT);
581 RECORD(EXPR_CXX_STATIC_CAST);
582 RECORD(EXPR_CXX_DYNAMIC_CAST);
583 RECORD(EXPR_CXX_REINTERPRET_CAST);
584 RECORD(EXPR_CXX_CONST_CAST);
585 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
586 RECORD(EXPR_CXX_BOOL_LITERAL);
587 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000588#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000589}
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000591void PCHWriter::WriteBlockInfoBlock() {
592 RecordData Record;
593 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Chris Lattner2f4efd12009-04-27 00:40:25 +0000595#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000596#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000598 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000599 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000600 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000601 RECORD(TYPE_OFFSET);
602 RECORD(DECL_OFFSET);
603 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000604 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000605 RECORD(IDENTIFIER_OFFSET);
606 RECORD(IDENTIFIER_TABLE);
607 RECORD(EXTERNAL_DEFINITIONS);
608 RECORD(SPECIAL_TYPES);
609 RECORD(STATISTICS);
610 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000611 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000612 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
613 RECORD(SELECTOR_OFFSETS);
614 RECORD(METHOD_POOL);
615 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000616 RECORD(SOURCE_LOCATION_OFFSETS);
617 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000618 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000619 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000620 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000621 RECORD(UNUSED_STATIC_FUNCS);
622 RECORD(MACRO_DEFINITION_OFFSETS);
623
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000624 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000625 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000626 RECORD(SM_SLOC_FILE_ENTRY);
627 RECORD(SM_SLOC_BUFFER_ENTRY);
628 RECORD(SM_SLOC_BUFFER_BLOB);
629 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
630 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000633 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000634 RECORD(PP_MACRO_OBJECT_LIKE);
635 RECORD(PP_MACRO_FUNCTION_LIKE);
636 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000637 RECORD(PP_MACRO_INSTANTIATION);
638 RECORD(PP_MACRO_DEFINITION);
639
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000640 // Decls and Types block.
641 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000642 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000643 RECORD(TYPE_COMPLEX);
644 RECORD(TYPE_POINTER);
645 RECORD(TYPE_BLOCK_POINTER);
646 RECORD(TYPE_LVALUE_REFERENCE);
647 RECORD(TYPE_RVALUE_REFERENCE);
648 RECORD(TYPE_MEMBER_POINTER);
649 RECORD(TYPE_CONSTANT_ARRAY);
650 RECORD(TYPE_INCOMPLETE_ARRAY);
651 RECORD(TYPE_VARIABLE_ARRAY);
652 RECORD(TYPE_VECTOR);
653 RECORD(TYPE_EXT_VECTOR);
654 RECORD(TYPE_FUNCTION_PROTO);
655 RECORD(TYPE_FUNCTION_NO_PROTO);
656 RECORD(TYPE_TYPEDEF);
657 RECORD(TYPE_TYPEOF_EXPR);
658 RECORD(TYPE_TYPEOF);
659 RECORD(TYPE_RECORD);
660 RECORD(TYPE_ENUM);
661 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000662 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000663 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000664 RECORD(DECL_ATTR);
665 RECORD(DECL_TRANSLATION_UNIT);
666 RECORD(DECL_TYPEDEF);
667 RECORD(DECL_ENUM);
668 RECORD(DECL_RECORD);
669 RECORD(DECL_ENUM_CONSTANT);
670 RECORD(DECL_FUNCTION);
671 RECORD(DECL_OBJC_METHOD);
672 RECORD(DECL_OBJC_INTERFACE);
673 RECORD(DECL_OBJC_PROTOCOL);
674 RECORD(DECL_OBJC_IVAR);
675 RECORD(DECL_OBJC_AT_DEFS_FIELD);
676 RECORD(DECL_OBJC_CLASS);
677 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
678 RECORD(DECL_OBJC_CATEGORY);
679 RECORD(DECL_OBJC_CATEGORY_IMPL);
680 RECORD(DECL_OBJC_IMPLEMENTATION);
681 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
682 RECORD(DECL_OBJC_PROPERTY);
683 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000684 RECORD(DECL_FIELD);
685 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000686 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000687 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000688 RECORD(DECL_FILE_SCOPE_ASM);
689 RECORD(DECL_BLOCK);
690 RECORD(DECL_CONTEXT_LEXICAL);
691 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000692 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000693 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000694#undef RECORD
695#undef BLOCK
696 Stream.ExitBlock();
697}
698
Douglas Gregore650c8c2009-07-07 00:12:59 +0000699/// \brief Adjusts the given filename to only write out the portion of the
700/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000701///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000702/// \param Filename the file name to adjust.
703///
704/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
705/// the returned filename will be adjusted by this system root.
706///
707/// \returns either the original filename (if it needs no adjustment) or the
708/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000709static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000710adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
711 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Douglas Gregore650c8c2009-07-07 00:12:59 +0000713 if (!isysroot)
714 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Douglas Gregore650c8c2009-07-07 00:12:59 +0000716 // Verify that the filename and the system root have the same prefix.
717 unsigned Pos = 0;
718 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
719 if (Filename[Pos] != isysroot[Pos])
720 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregore650c8c2009-07-07 00:12:59 +0000722 // We hit the end of the filename before we hit the end of the system root.
723 if (!Filename[Pos])
724 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Douglas Gregore650c8c2009-07-07 00:12:59 +0000726 // If the file name has a '/' at the current position, skip over the '/'.
727 // We distinguish sysroot-based includes from absolute includes by the
728 // absence of '/' at the beginning of sysroot-based includes.
729 if (Filename[Pos] == '/')
730 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregore650c8c2009-07-07 00:12:59 +0000732 return Filename + Pos;
733}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000734
Douglas Gregorab41e632009-04-27 22:23:34 +0000735/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000736void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000737 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000738
Douglas Gregore650c8c2009-07-07 00:12:59 +0000739 // Metadata
740 const TargetInfo &Target = Context.Target;
741 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
742 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
743 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
744 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
745 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
746 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
747 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
748 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
749 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregore650c8c2009-07-07 00:12:59 +0000751 RecordData Record;
752 Record.push_back(pch::METADATA);
753 Record.push_back(pch::VERSION_MAJOR);
754 Record.push_back(pch::VERSION_MINOR);
755 Record.push_back(CLANG_VERSION_MAJOR);
756 Record.push_back(CLANG_VERSION_MINOR);
757 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000758 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000759 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Douglas Gregorb64c1932009-05-12 01:31:05 +0000761 // Original file name
762 SourceManager &SM = Context.getSourceManager();
763 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
764 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
765 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
766 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
767 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
768
769 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000771 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000772
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000773 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000774 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000775 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000776 RecordData Record;
777 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000778 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000779 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000780
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000781 // Repository branch/version information.
782 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
783 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
784 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
785 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000786 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000787 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000788 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
789 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000790}
791
792/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000793void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
794 RecordData Record;
795 Record.push_back(LangOpts.Trigraphs);
796 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
797 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
798 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
799 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000800 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000801 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
802 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
803 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
804 Record.push_back(LangOpts.C99); // C99 Support
805 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
806 Record.push_back(LangOpts.CPlusPlus); // C++ Support
807 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000808 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000810 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
811 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000812 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000813 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000814 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000815 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000816 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000818 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000819 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
820 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000821 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000822 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000823 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000824
825 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
826 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
827 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
828
Chris Lattnerea5ce472009-04-27 07:35:58 +0000829 // Whether static initializers are protected by locks.
830 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000831 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000832 Record.push_back(LangOpts.Blocks); // block extension to C
833 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
834 // they are unused.
835 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
836 // (modulo the platform support).
837
Chris Lattnera4d71452010-06-26 21:25:03 +0000838 Record.push_back(LangOpts.getSignedOverflowBehavior());
839 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000840
841 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000842 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000843 // defined.
844 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
845 // opposed to __DYNAMIC__).
846 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
847
848 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
849 // used (instead of C99 semantics).
850 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000851 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
852 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000853 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
854 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000855 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000856 Record.push_back(LangOpts.getGCMode());
857 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000858 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000859 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000860 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000861 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000862 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000863 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000864}
865
Douglas Gregor14f79002009-04-10 03:52:48 +0000866//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000867// stat cache Serialization
868//===----------------------------------------------------------------------===//
869
870namespace {
871// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000872class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000873public:
874 typedef const char * key_type;
875 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000877 typedef std::pair<int, struct stat> data_type;
878 typedef const data_type& data_type_ref;
879
880 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000881 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
884 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000885 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
886 data_type_ref Data) {
887 unsigned StrLen = strlen(path);
888 clang::io::Emit16(Out, StrLen);
889 unsigned DataLen = 1; // result value
890 if (Data.first == 0)
891 DataLen += 4 + 4 + 2 + 8 + 8;
892 clang::io::Emit8(Out, DataLen);
893 return std::make_pair(StrLen + 1, DataLen);
894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000896 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
897 Out.write(path, KeyLen);
898 }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000900 void EmitData(llvm::raw_ostream& Out, key_type_ref,
901 data_type_ref Data, unsigned DataLen) {
902 using namespace clang::io;
903 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000905 // Result of stat()
906 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000908 if (Data.first == 0) {
909 Emit32(Out, (uint32_t) Data.second.st_ino);
910 Emit32(Out, (uint32_t) Data.second.st_dev);
911 Emit16(Out, (uint16_t) Data.second.st_mode);
912 Emit64(Out, (uint64_t) Data.second.st_mtime);
913 Emit64(Out, (uint64_t) Data.second.st_size);
914 }
915
916 assert(Out.tell() - Start == DataLen && "Wrong data length");
917 }
918};
919} // end anonymous namespace
920
921/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000922void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
923 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000924 // Build the on-disk hash table containing information about every
925 // stat() call.
926 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
927 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000928 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000929 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000930 Stat != StatEnd; ++Stat, ++NumStatEntries) {
931 const char *Filename = Stat->first();
932 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
933 Generator.insert(Filename, Stat->second);
934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000936 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000937 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000938 uint32_t BucketOffset;
939 {
940 llvm::raw_svector_ostream Out(StatCacheData);
941 // Make sure that no bucket is at offset 0
942 clang::io::Emit32(Out, 0);
943 BucketOffset = Generator.Emit(Out);
944 }
945
946 // Create a blob abbreviation
947 using namespace llvm;
948 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
949 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
953 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
954
955 // Write the stat cache
956 RecordData Record;
957 Record.push_back(pch::STAT_CACHE);
958 Record.push_back(BucketOffset);
959 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000960 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000961}
962
963//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000964// Source Manager Serialization
965//===----------------------------------------------------------------------===//
966
967/// \brief Create an abbreviation for the SLocEntry that refers to a
968/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000969static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000970 using namespace llvm;
971 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
972 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000977 // FileEntry fields.
978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000980 // HeaderFileInfo fields.
981 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000986 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000987}
988
989/// \brief Create an abbreviation for the SLocEntry that refers to a
990/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000991static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000992 using namespace llvm;
993 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
994 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001000 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001001}
1002
1003/// \brief Create an abbreviation for the SLocEntry that refers to a
1004/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001005static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001006 using namespace llvm;
1007 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1008 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001010 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001011}
1012
1013/// \brief Create an abbreviation for the SLocEntry that refers to an
1014/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001015static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001016 using namespace llvm;
1017 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1018 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001024 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001025}
1026
1027/// \brief Writes the block containing the serialized form of the
1028/// source manager.
1029///
1030/// TODO: We should probably use an on-disk hash table (stored in a
1031/// blob), indexed based on the file name, so that we only create
1032/// entries for files that we actually need. In the common case (no
1033/// errors), we probably won't have to create file entries for any of
1034/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001035void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001036 const Preprocessor &PP,
1037 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001038 RecordData Record;
1039
Chris Lattnerf04ad692009-04-10 17:16:57 +00001040 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001041 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001042
1043 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001044 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1045 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1046 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1047 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001048
Douglas Gregorbd945002009-04-13 16:31:14 +00001049 // Write the line table.
1050 if (SourceMgr.hasLineTable()) {
1051 LineTableInfo &LineTable = SourceMgr.getLineTable();
1052
1053 // Emit the file names
1054 Record.push_back(LineTable.getNumFilenames());
1055 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1056 // Emit the file name
1057 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001058 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001059 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1060 Record.push_back(FilenameLen);
1061 if (FilenameLen)
1062 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Douglas Gregorbd945002009-04-13 16:31:14 +00001065 // Emit the line entries
1066 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1067 L != LEnd; ++L) {
1068 // Emit the file ID
1069 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Douglas Gregorbd945002009-04-13 16:31:14 +00001071 // Emit the line entries
1072 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001073 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001074 LEEnd = L->second.end();
1075 LE != LEEnd; ++LE) {
1076 Record.push_back(LE->FileOffset);
1077 Record.push_back(LE->LineNo);
1078 Record.push_back(LE->FilenameID);
1079 Record.push_back((unsigned)LE->FileKind);
1080 Record.push_back(LE->IncludeOffset);
1081 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001082 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001083 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001084 }
1085
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001086 // Write out the source location entry table. We skip the first
1087 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001088 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001089 RecordData PreloadSLocs;
1090 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001091 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1092 // Get this source location entry.
1093 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001094
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001095 // Record the offset of this source-location entry.
1096 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1097
1098 // Figure out which record code to use.
1099 unsigned Code;
1100 if (SLoc->isFile()) {
1101 if (SLoc->getFile().getContentCache()->Entry)
1102 Code = pch::SM_SLOC_FILE_ENTRY;
1103 else
1104 Code = pch::SM_SLOC_BUFFER_ENTRY;
1105 } else
1106 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1107 Record.clear();
1108 Record.push_back(Code);
1109
1110 Record.push_back(SLoc->getOffset());
1111 if (SLoc->isFile()) {
1112 const SrcMgr::FileInfo &File = SLoc->getFile();
1113 Record.push_back(File.getIncludeLoc().getRawEncoding());
1114 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1115 Record.push_back(File.hasLineDirectives());
1116
1117 const SrcMgr::ContentCache *Content = File.getContentCache();
1118 if (Content->Entry) {
1119 // The source location entry is a file. The blob associated
1120 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Douglas Gregor2d52be52010-03-21 22:49:54 +00001122 // Emit size/modification time for this file.
1123 Record.push_back(Content->Entry->getSize());
1124 Record.push_back(Content->Entry->getModificationTime());
1125
Douglas Gregor12fab312010-03-16 16:35:32 +00001126 // Emit header-search information associated with this file.
1127 HeaderFileInfo HFI;
1128 HeaderSearch &HS = PP.getHeaderSearchInfo();
1129 if (Content->Entry->getUID() < HS.header_file_size())
1130 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1131 Record.push_back(HFI.isImport);
1132 Record.push_back(HFI.DirInfo);
1133 Record.push_back(HFI.NumIncludes);
1134 AddIdentifierRef(HFI.ControllingMacro, Record);
1135
Douglas Gregore650c8c2009-07-07 00:12:59 +00001136 // Turn the file name into an absolute path, if it isn't already.
1137 const char *Filename = Content->Entry->getName();
1138 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001139 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001140 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Douglas Gregore650c8c2009-07-07 00:12:59 +00001142 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001143 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001144
1145 // FIXME: For now, preload all file source locations, so that
1146 // we get the appropriate File entries in the reader. This is
1147 // a temporary measure.
1148 PreloadSLocs.push_back(SLocEntryOffsets.size());
1149 } else {
1150 // The source location entry is a buffer. The blob associated
1151 // with this entry contains the contents of the buffer.
1152
1153 // We add one to the size so that we capture the trailing NULL
1154 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1155 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001156 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001157 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001158 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001159 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1160 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001161 Record.clear();
1162 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1163 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001164 llvm::StringRef(Buffer->getBufferStart(),
1165 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001166
1167 if (strcmp(Name, "<built-in>") == 0)
1168 PreloadSLocs.push_back(SLocEntryOffsets.size());
1169 }
1170 } else {
1171 // The source location entry is an instantiation.
1172 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1173 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1174 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1175 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1176
1177 // Compute the token length for this macro expansion.
1178 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001179 if (I + 1 != N)
1180 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001181 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1182 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1183 }
1184 }
1185
Douglas Gregorc9490c02009-04-16 22:23:12 +00001186 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001187
1188 if (SLocEntryOffsets.empty())
1189 return;
1190
1191 // Write the source-location offsets table into the PCH block. This
1192 // table is used for lazily loading source-location information.
1193 using namespace llvm;
1194 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1195 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1199 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001201 Record.clear();
1202 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1203 Record.push_back(SLocEntryOffsets.size());
1204 Record.push_back(SourceMgr.getNextOffset());
1205 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001206 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001207 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001208
1209 // Write the source location entry preloads array, telling the PCH
1210 // reader which source locations entries it should load eagerly.
1211 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001212}
1213
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001214//===----------------------------------------------------------------------===//
1215// Preprocessor Serialization
1216//===----------------------------------------------------------------------===//
1217
Chris Lattner0b1fb982009-04-10 17:15:23 +00001218/// \brief Writes the block containing the serialized form of the
1219/// preprocessor.
1220///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001221void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001222 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001223
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001224 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1225 if (PP.getCounterValue() != 0) {
1226 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001227 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001228 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001229 }
1230
1231 // Enter the preprocessor block.
1232 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001234 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1235 // FIXME: use diagnostics subsystem for localization etc.
1236 if (PP.SawDateOrTime())
1237 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001239 // Loop over all the macro definitions that are live at the end of the file,
1240 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001241 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001242 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1243 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001244 // FIXME: This emits macros in hash table order, we should do it in a stable
1245 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001246 MacroInfo *MI = I->second;
1247
1248 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1249 // been redefined by the header (in which case they are not isBuiltinMacro).
1250 if (MI->isBuiltinMacro())
1251 continue;
1252
Chris Lattner7356a312009-04-11 21:15:38 +00001253 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001254 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001255 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1256 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001258 unsigned Code;
1259 if (MI->isObjectLike()) {
1260 Code = pch::PP_MACRO_OBJECT_LIKE;
1261 } else {
1262 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001264 Record.push_back(MI->isC99Varargs());
1265 Record.push_back(MI->isGNUVarargs());
1266 Record.push_back(MI->getNumArgs());
1267 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1268 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001269 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001270 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001271
1272 // If we have a detailed preprocessing record, record the macro definition
1273 // ID that corresponds to this macro.
1274 if (PPRec)
1275 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1276
Douglas Gregorc9490c02009-04-16 22:23:12 +00001277 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001278 Record.clear();
1279
Chris Lattnerdf961c22009-04-10 18:08:30 +00001280 // Emit the tokens array.
1281 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1282 // Note that we know that the preprocessor does not have any annotation
1283 // tokens in it because they are created by the parser, and thus can't be
1284 // in a macro definition.
1285 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Chris Lattnerdf961c22009-04-10 18:08:30 +00001287 Record.push_back(Tok.getLocation().getRawEncoding());
1288 Record.push_back(Tok.getLength());
1289
Chris Lattnerdf961c22009-04-10 18:08:30 +00001290 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1291 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001292 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Chris Lattnerdf961c22009-04-10 18:08:30 +00001294 // FIXME: Should translate token kind to a stable encoding.
1295 Record.push_back(Tok.getKind());
1296 // FIXME: Should translate token flags to a stable encoding.
1297 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Douglas Gregorc9490c02009-04-16 22:23:12 +00001299 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001300 Record.clear();
1301 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001302 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001303 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001304
1305 // If the preprocessor has a preprocessing record, emit it.
1306 unsigned NumPreprocessingRecords = 0;
1307 if (PPRec) {
1308 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1309 E != EEnd; ++E) {
1310 Record.clear();
1311
1312 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1313 Record.push_back(NumPreprocessingRecords++);
1314 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1315 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1316 AddIdentifierRef(MI->getName(), Record);
1317 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1318 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1319 continue;
1320 }
1321
1322 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1323 // Record this macro definition's location.
1324 pch::IdentID ID = getMacroDefinitionID(MD);
1325 if (ID != MacroDefinitionOffsets.size()) {
1326 if (ID > MacroDefinitionOffsets.size())
1327 MacroDefinitionOffsets.resize(ID + 1);
1328
1329 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1330 } else
1331 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1332
1333 Record.push_back(NumPreprocessingRecords++);
1334 Record.push_back(ID);
1335 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1336 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1337 AddIdentifierRef(MD->getName(), Record);
1338 AddSourceLocation(MD->getLocation(), Record);
1339 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1340 continue;
1341 }
1342 }
1343 }
1344
Douglas Gregorc9490c02009-04-16 22:23:12 +00001345 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001346
1347 // Write the offsets table for the preprocessing record.
1348 if (NumPreprocessingRecords > 0) {
1349 // Write the offsets table for identifier IDs.
1350 using namespace llvm;
1351 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1352 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1356 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1357
1358 Record.clear();
1359 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1360 Record.push_back(NumPreprocessingRecords);
1361 Record.push_back(MacroDefinitionOffsets.size());
1362 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1363 (const char *)&MacroDefinitionOffsets.front(),
1364 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1365 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001366}
1367
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001368//===----------------------------------------------------------------------===//
1369// Type Serialization
1370//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001371
Douglas Gregor2cf26342009-04-09 22:27:44 +00001372/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001373void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001374 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001375 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001376 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Douglas Gregor2cf26342009-04-09 22:27:44 +00001378 // Record the offset for this type.
1379 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001380 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001381 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1382 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001383 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001384 }
1385
1386 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Douglas Gregor2cf26342009-04-09 22:27:44 +00001388 // Emit the type's representation.
1389 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001390
Douglas Gregora4923eb2009-11-16 21:35:15 +00001391 if (T.hasLocalNonFastQualifiers()) {
1392 Qualifiers Qs = T.getLocalQualifiers();
1393 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001394 Record.push_back(Qs.getAsOpaqueValue());
1395 W.Code = pch::TYPE_EXT_QUAL;
1396 } else {
1397 switch (T->getTypeClass()) {
1398 // For all of the concrete, non-dependent types, call the
1399 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001400#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001401 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001402#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001403#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001404 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001405 }
1406
1407 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001408 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001409
1410 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001411 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001412}
1413
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001414//===----------------------------------------------------------------------===//
1415// Declaration Serialization
1416//===----------------------------------------------------------------------===//
1417
Douglas Gregor2cf26342009-04-09 22:27:44 +00001418/// \brief Write the block containing all of the declaration IDs
1419/// lexically declared within the given DeclContext.
1420///
1421/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1422/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001423uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001424 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001425 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001426 return 0;
1427
Douglas Gregorc9490c02009-04-16 22:23:12 +00001428 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001429 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001430 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1431 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001432 AddDeclRef(*D, Record);
1433
Douglas Gregor25123082009-04-22 22:34:57 +00001434 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001435 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001436 return Offset;
1437}
1438
1439/// \brief Write the block containing all of the declaration IDs
1440/// visible from the given DeclContext.
1441///
1442/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1443/// bistream, or 0 if no block was written.
1444uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1445 DeclContext *DC) {
1446 if (DC->getPrimaryContext() != DC)
1447 return 0;
1448
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +00001449 // Since there is no name lookup into functions or methods, don't bother to
1450 // build a visible-declarations table for these entities.
1451 if (DC->isFunctionOrMethod())
1452 return 0;
1453
1454 // If not in C++, we perform name lookup for the translation unit via the
1455 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1456 // FIXME: In C++ we need the visible declarations in order to "see" the
1457 // friend declarations, is there a way to do this without writing the table ?
1458 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor58f06992009-04-18 15:49:20 +00001459 return 0;
1460
Douglas Gregor2cf26342009-04-09 22:27:44 +00001461 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001462 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001463
1464 // Serialize the contents of the mapping used for lookup. Note that,
1465 // although we have two very different code paths, the serialized
1466 // representation is the same for both cases: a declaration name,
1467 // followed by a size, followed by references to the visible
1468 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001469 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001470 RecordData Record;
1471 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001472 if (!Map)
1473 return 0;
1474
Douglas Gregor2cf26342009-04-09 22:27:44 +00001475 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1476 D != DEnd; ++D) {
1477 AddDeclarationName(D->first, Record);
1478 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1479 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001480 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001481 AddDeclRef(*Result.first, Record);
1482 }
1483
1484 if (Record.size() == 0)
1485 return 0;
1486
Douglas Gregorc9490c02009-04-16 22:23:12 +00001487 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001488 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001489 return Offset;
1490}
1491
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001492//===----------------------------------------------------------------------===//
1493// Global Method Pool and Selector Serialization
1494//===----------------------------------------------------------------------===//
1495
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001496namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001497// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001498class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001499 PCHWriter &Writer;
1500
1501public:
1502 typedef Selector key_type;
1503 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001505 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1506 typedef const data_type& data_type_ref;
1507
1508 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001510 static unsigned ComputeHash(Selector Sel) {
1511 unsigned N = Sel.getNumArgs();
1512 if (N == 0)
1513 ++N;
1514 unsigned R = 5381;
1515 for (unsigned I = 0; I != N; ++I)
1516 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001517 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001518 return R;
1519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
1521 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001522 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1523 data_type_ref Methods) {
1524 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1525 clang::io::Emit16(Out, KeyLen);
1526 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001527 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001528 Method = Method->Next)
1529 if (Method->Method)
1530 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001531 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001532 Method = Method->Next)
1533 if (Method->Method)
1534 DataLen += 4;
1535 clang::io::Emit16(Out, DataLen);
1536 return std::make_pair(KeyLen, DataLen);
1537 }
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Douglas Gregor83941df2009-04-25 17:48:32 +00001539 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001540 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001541 assert((Start >> 32) == 0 && "Selector key offset too large");
1542 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001543 unsigned N = Sel.getNumArgs();
1544 clang::io::Emit16(Out, N);
1545 if (N == 0)
1546 N = 1;
1547 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001548 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001549 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1550 }
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001552 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001553 data_type_ref Methods, unsigned DataLen) {
1554 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001555 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001556 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001557 Method = Method->Next)
1558 if (Method->Method)
1559 ++NumInstanceMethods;
1560
1561 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001562 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001563 Method = Method->Next)
1564 if (Method->Method)
1565 ++NumFactoryMethods;
1566
1567 clang::io::Emit16(Out, NumInstanceMethods);
1568 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001569 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001570 Method = Method->Next)
1571 if (Method->Method)
1572 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001573 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001574 Method = Method->Next)
1575 if (Method->Method)
1576 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001577
1578 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001579 }
1580};
1581} // end anonymous namespace
1582
1583/// \brief Write the method pool into the PCH file.
1584///
1585/// The method pool contains both instance and factory methods, stored
1586/// in an on-disk hash table indexed by the selector.
1587void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1588 using namespace llvm;
1589
1590 // Create and write out the blob that contains the instance and
1591 // factor method pools.
1592 bool Empty = true;
1593 {
1594 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001596 // Create the on-disk hash table representation. Start by
1597 // iterating through the instance method pool.
1598 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001599 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001601 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001602 InstanceEnd = SemaRef.InstanceMethodPool.end();
1603 Instance != InstanceEnd; ++Instance) {
1604 // Check whether there is a factory method with the same
1605 // selector.
1606 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1607 = SemaRef.FactoryMethodPool.find(Instance->first);
1608
1609 if (Factory == SemaRef.FactoryMethodPool.end())
1610 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001611 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001612 ObjCMethodList()));
1613 else
1614 Generator.insert(Instance->first,
1615 std::make_pair(Instance->second, Factory->second));
1616
Douglas Gregor83941df2009-04-25 17:48:32 +00001617 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001618 Empty = false;
1619 }
1620
1621 // Now iterate through the factory method pool, to pick up any
1622 // selectors that weren't already in the instance method pool.
1623 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001624 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001625 FactoryEnd = SemaRef.FactoryMethodPool.end();
1626 Factory != FactoryEnd; ++Factory) {
1627 // Check whether there is an instance method with the same
1628 // selector. If so, there is no work to do here.
1629 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1630 = SemaRef.InstanceMethodPool.find(Factory->first);
1631
Douglas Gregor83941df2009-04-25 17:48:32 +00001632 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001633 Generator.insert(Factory->first,
1634 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001635 ++NumSelectorsInMethodPool;
1636 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001637
1638 Empty = false;
1639 }
1640
Douglas Gregor83941df2009-04-25 17:48:32 +00001641 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001642 return;
1643
1644 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001645 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001646 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001647 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001648 {
1649 PCHMethodPoolTrait Trait(*this);
1650 llvm::raw_svector_ostream Out(MethodPool);
1651 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001652 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001653 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001654
1655 // For every selector that we have seen but which was not
1656 // written into the hash table, write the selector itself and
1657 // record it's offset.
1658 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1659 if (SelectorOffsets[I] == 0)
1660 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001661 }
1662
1663 // Create a blob abbreviation
1664 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1665 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001667 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001668 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1669 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1670
Douglas Gregor83941df2009-04-25 17:48:32 +00001671 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001672 RecordData Record;
1673 Record.push_back(pch::METHOD_POOL);
1674 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001675 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001676 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001677
1678 // Create a blob abbreviation for the selector table offsets.
1679 Abbrev = new BitCodeAbbrev();
1680 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1681 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1682 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1683 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1684
1685 // Write the selector offsets table.
1686 Record.clear();
1687 Record.push_back(pch::SELECTOR_OFFSETS);
1688 Record.push_back(SelectorOffsets.size());
1689 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1690 (const char *)&SelectorOffsets.front(),
1691 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001692 }
1693}
1694
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001695//===----------------------------------------------------------------------===//
1696// Identifier Table Serialization
1697//===----------------------------------------------------------------------===//
1698
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001699namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001700class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001701 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001702 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001703
Douglas Gregora92193e2009-04-28 21:18:29 +00001704 /// \brief Determines whether this is an "interesting" identifier
1705 /// that needs a full IdentifierInfo structure written into the hash
1706 /// table.
1707 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1708 return II->isPoisoned() ||
1709 II->isExtensionToken() ||
1710 II->hasMacroDefinition() ||
1711 II->getObjCOrBuiltinID() ||
1712 II->getFETokenInfo<void>();
1713 }
1714
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001715public:
1716 typedef const IdentifierInfo* key_type;
1717 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001719 typedef pch::IdentID data_type;
1720 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001721
1722 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001723 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001724
1725 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001726 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001727 }
Mike Stump1eb44332009-09-09 15:08:12 +00001728
1729 std::pair<unsigned,unsigned>
1730 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001731 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001732 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001733 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1734 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001735 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001736 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001737 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001738 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001739 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1740 DEnd = IdentifierResolver::end();
1741 D != DEnd; ++D)
1742 DataLen += sizeof(pch::DeclID);
1743 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001744 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001745 // We emit the key length after the data length so that every
1746 // string is preceded by a 16-bit length. This matches the PTH
1747 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001748 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001749 return std::make_pair(KeyLen, DataLen);
1750 }
Mike Stump1eb44332009-09-09 15:08:12 +00001751
1752 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001753 unsigned KeyLen) {
1754 // Record the location of the key data. This is used when generating
1755 // the mapping from persistent IDs to strings.
1756 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001757 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001758 }
Mike Stump1eb44332009-09-09 15:08:12 +00001759
1760 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001761 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001762 if (!isInterestingIdentifier(II)) {
1763 clang::io::Emit32(Out, ID << 1);
1764 return;
1765 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001766
Douglas Gregora92193e2009-04-28 21:18:29 +00001767 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001768 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001769 bool hasMacroDefinition =
1770 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001771 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001772 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001773 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1774 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1775 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1776 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001777 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001778
Douglas Gregor37e26842009-04-21 23:56:24 +00001779 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001780 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001781
Douglas Gregor668c1a42009-04-21 22:25:48 +00001782 // Emit the declaration IDs in reverse order, because the
1783 // IdentifierResolver provides the declarations as they would be
1784 // visible (e.g., the function "stat" would come before the struct
1785 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1786 // adds declarations to the end of the list (so we need to see the
1787 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001788 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001789 IdentifierResolver::end());
1790 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1791 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001792 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001793 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001794 }
1795};
1796} // end anonymous namespace
1797
Douglas Gregorafaf3082009-04-11 00:14:32 +00001798/// \brief Write the identifier table into the PCH file.
1799///
1800/// The identifier table consists of a blob containing string data
1801/// (the actual identifiers themselves) and a separate "offsets" index
1802/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001803void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001804 using namespace llvm;
1805
1806 // Create and write out the blob that contains the identifier
1807 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001808 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001809 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Douglas Gregor92b059e2009-04-28 20:33:11 +00001811 // Look for any identifiers that were named while processing the
1812 // headers, but are otherwise not needed. We add these to the hash
1813 // table to enable checking of the predefines buffer in the case
1814 // where the user adds new macro definitions when building the PCH
1815 // file.
1816 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1817 IDEnd = PP.getIdentifierTable().end();
1818 ID != IDEnd; ++ID)
1819 getIdentifierRef(ID->second);
1820
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001821 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001822 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001823 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1824 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1825 ID != IDEnd; ++ID) {
1826 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001827 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001828 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001829
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001830 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001831 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001832 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001833 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001834 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001835 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001836 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001837 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001838 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001839 }
1840
1841 // Create a blob abbreviation
1842 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1843 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001846 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001847
1848 // Write the identifier table
1849 RecordData Record;
1850 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001851 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001852 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001853 }
1854
1855 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001856 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1857 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1858 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1859 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1860 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1861
1862 RecordData Record;
1863 Record.push_back(pch::IDENTIFIER_OFFSET);
1864 Record.push_back(IdentifierOffsets.size());
1865 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1866 (const char *)&IdentifierOffsets.front(),
1867 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001868}
1869
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001870//===----------------------------------------------------------------------===//
1871// General Serialization Routines
1872//===----------------------------------------------------------------------===//
1873
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001874/// \brief Write a record containing the given attributes.
1875void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1876 RecordData Record;
1877 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001878 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001879 Record.push_back(Attr->isInherited());
1880 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001881 default:
1882 assert(0 && "Does not support PCH writing for this attribute yet!");
1883 break;
Sean Hunt387475d2010-06-16 23:43:53 +00001884 case attr::Alias:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001885 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1886 break;
1887
Sean Hunt387475d2010-06-16 23:43:53 +00001888 case attr::AlignMac68k:
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001889 break;
1890
Sean Hunt387475d2010-06-16 23:43:53 +00001891 case attr::Aligned:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001892 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1893 break;
1894
Sean Hunt387475d2010-06-16 23:43:53 +00001895 case attr::AlwaysInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001896 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Sean Hunt387475d2010-06-16 23:43:53 +00001898 case attr::AnalyzerNoReturn:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001899 break;
1900
Sean Hunt387475d2010-06-16 23:43:53 +00001901 case attr::Annotate:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001902 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1903 break;
1904
Sean Hunt387475d2010-06-16 23:43:53 +00001905 case attr::AsmLabel:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001906 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1907 break;
1908
Sean Hunt387475d2010-06-16 23:43:53 +00001909 case attr::BaseCheck:
Sean Hunt7725e672009-11-25 04:20:27 +00001910 break;
1911
Sean Hunt387475d2010-06-16 23:43:53 +00001912 case attr::Blocks:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001913 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1914 break;
1915
Sean Hunt387475d2010-06-16 23:43:53 +00001916 case attr::CDecl:
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001917 break;
1918
Sean Hunt387475d2010-06-16 23:43:53 +00001919 case attr::Cleanup:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001920 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1921 break;
1922
Sean Hunt387475d2010-06-16 23:43:53 +00001923 case attr::Const:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001924 break;
1925
Sean Hunt387475d2010-06-16 23:43:53 +00001926 case attr::Constructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001927 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1928 break;
1929
Sean Hunt387475d2010-06-16 23:43:53 +00001930 case attr::DLLExport:
1931 case attr::DLLImport:
1932 case attr::Deprecated:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001933 break;
1934
Sean Hunt387475d2010-06-16 23:43:53 +00001935 case attr::Destructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001936 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1937 break;
1938
Sean Hunt387475d2010-06-16 23:43:53 +00001939 case attr::FastCall:
1940 case attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001941 break;
1942
Sean Hunt387475d2010-06-16 23:43:53 +00001943 case attr::Format: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001944 const FormatAttr *Format = cast<FormatAttr>(Attr);
1945 AddString(Format->getType(), Record);
1946 Record.push_back(Format->getFormatIdx());
1947 Record.push_back(Format->getFirstArg());
1948 break;
1949 }
1950
Sean Hunt387475d2010-06-16 23:43:53 +00001951 case attr::FormatArg: {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001952 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1953 Record.push_back(Format->getFormatIdx());
1954 break;
1955 }
1956
Sean Hunt387475d2010-06-16 23:43:53 +00001957 case attr::Sentinel : {
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001958 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1959 Record.push_back(Sentinel->getSentinel());
1960 Record.push_back(Sentinel->getNullPos());
1961 break;
1962 }
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Sean Hunt387475d2010-06-16 23:43:53 +00001964 case attr::GNUInline:
1965 case attr::Hiding:
1966 case attr::IBAction:
1967 case attr::IBOutlet:
1968 case attr::Malloc:
1969 case attr::NoDebug:
1970 case attr::NoInline:
1971 case attr::NoReturn:
1972 case attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001973 break;
1974
Sean Hunt387475d2010-06-16 23:43:53 +00001975 case attr::IBOutletCollection: {
Ted Kremenek857e9182010-05-19 17:38:06 +00001976 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
1977 AddDeclRef(ICA->getClass(), Record);
1978 break;
1979 }
1980
Sean Hunt387475d2010-06-16 23:43:53 +00001981 case attr::NonNull: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001982 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1983 Record.push_back(NonNull->size());
1984 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1985 break;
1986 }
1987
Sean Hunt387475d2010-06-16 23:43:53 +00001988 case attr::CFReturnsNotRetained:
1989 case attr::CFReturnsRetained:
1990 case attr::NSReturnsNotRetained:
1991 case attr::NSReturnsRetained:
1992 case attr::ObjCException:
1993 case attr::ObjCNSObject:
1994 case attr::Overloadable:
1995 case attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001996 break;
1997
Sean Hunt387475d2010-06-16 23:43:53 +00001998 case attr::MaxFieldAlignment:
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00001999 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002000 break;
2001
Sean Hunt387475d2010-06-16 23:43:53 +00002002 case attr::Packed:
Anders Carlssona860e752009-08-08 18:23:56 +00002003 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Sean Hunt387475d2010-06-16 23:43:53 +00002005 case attr::Pure:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002006 break;
2007
Sean Hunt387475d2010-06-16 23:43:53 +00002008 case attr::Regparm:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002009 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2010 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Sean Hunt387475d2010-06-16 23:43:53 +00002012 case attr::ReqdWorkGroupSize:
Nate Begeman6f3d8382009-06-26 06:32:41 +00002013 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2014 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2015 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2016 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002017
Sean Hunt387475d2010-06-16 23:43:53 +00002018 case attr::Section:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002019 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2020 break;
2021
Sean Hunt387475d2010-06-16 23:43:53 +00002022 case attr::StdCall:
2023 case attr::TransparentUnion:
2024 case attr::Unavailable:
2025 case attr::Unused:
2026 case attr::Used:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002027 break;
2028
Sean Hunt387475d2010-06-16 23:43:53 +00002029 case attr::Visibility:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002030 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00002031 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002032 break;
2033
Sean Hunt387475d2010-06-16 23:43:53 +00002034 case attr::WarnUnusedResult:
2035 case attr::Weak:
2036 case attr::WeakRef:
2037 case attr::WeakImport:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002038 break;
2039 }
2040 }
2041
Douglas Gregorc9490c02009-04-16 22:23:12 +00002042 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002043}
2044
2045void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2046 Record.push_back(Str.size());
2047 Record.insert(Record.end(), Str.begin(), Str.end());
2048}
2049
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002050/// \brief Note that the identifier II occurs at the given offset
2051/// within the identifier table.
2052void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002053 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002054}
2055
Douglas Gregor83941df2009-04-25 17:48:32 +00002056/// \brief Note that the selector Sel occurs at the given offset
2057/// within the method pool/selector table.
2058void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2059 unsigned ID = SelectorIDs[Sel];
2060 assert(ID && "Unknown selector");
2061 SelectorOffsets[ID - 1] = Offset;
2062}
2063
Mike Stump1eb44332009-09-09 15:08:12 +00002064PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2065 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002066 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
2067 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002068
Douglas Gregore650c8c2009-07-07 00:12:59 +00002069void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2070 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002071 using namespace llvm;
2072
Douglas Gregore7785042009-04-20 15:53:59 +00002073 ASTContext &Context = SemaRef.Context;
2074 Preprocessor &PP = SemaRef.PP;
2075
Douglas Gregor2cf26342009-04-09 22:27:44 +00002076 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002077 Stream.Emit((unsigned)'C', 8);
2078 Stream.Emit((unsigned)'P', 8);
2079 Stream.Emit((unsigned)'C', 8);
2080 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002082 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002083
2084 // The translation unit is the first declaration we'll emit.
2085 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002086 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002087
Douglas Gregor2deaea32009-04-22 18:49:13 +00002088 // Make sure that we emit IdentifierInfos (and any attached
2089 // declarations) for builtins.
2090 {
2091 IdentifierTable &Table = PP.getIdentifierTable();
2092 llvm::SmallVector<const char *, 32> BuiltinNames;
2093 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2094 Context.getLangOptions().NoBuiltin);
2095 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2096 getIdentifierRef(&Table.get(BuiltinNames[I]));
2097 }
2098
Chris Lattner63d65f82009-09-08 18:19:27 +00002099 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002100 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002101 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002102 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002103 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2104 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002105 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002106
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002107 // Build a record containing all of the static unused functions in this file.
2108 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002109 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002110 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002111
Douglas Gregor14c22f22009-04-22 22:18:58 +00002112 // Build a record containing all of the locally-scoped external
2113 // declarations in this header file. Generally, this record will be
2114 // empty.
2115 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002116 // FIXME: This is filling in the PCH file in densemap order which is
2117 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002118 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002119 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2120 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2121 TD != TDEnd; ++TD)
2122 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2123
Douglas Gregorb81c1702009-04-27 20:06:05 +00002124 // Build a record containing all of the ext_vector declarations.
2125 RecordData ExtVectorDecls;
2126 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2127 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2128
Douglas Gregor2cf26342009-04-09 22:27:44 +00002129 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002130 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002131 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002132 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002133 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002134 if (StatCalls && !isysroot)
2135 WriteStatCache(*StatCalls, isysroot);
2136 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002137 // Write the record of special types.
2138 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002139
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002140 AddTypeRef(Context.getBuiltinVaListType(), Record);
2141 AddTypeRef(Context.getObjCIdType(), Record);
2142 AddTypeRef(Context.getObjCSelType(), Record);
2143 AddTypeRef(Context.getObjCProtoType(), Record);
2144 AddTypeRef(Context.getObjCClassType(), Record);
2145 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2146 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2147 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002148 AddTypeRef(Context.getjmp_bufType(), Record);
2149 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002150 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2151 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002152 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002153 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002154 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2155 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002156 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002157
Douglas Gregor366809a2009-04-26 03:49:13 +00002158 // Keep writing types and declarations until all types and
2159 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002160 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2161 WriteDeclsBlockAbbrevs();
2162 while (!DeclTypesToEmit.empty()) {
2163 DeclOrType DOT = DeclTypesToEmit.front();
2164 DeclTypesToEmit.pop();
2165 if (DOT.isType())
2166 WriteType(DOT.getType());
2167 else
2168 WriteDecl(Context, DOT.getDecl());
2169 }
2170 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002171
Douglas Gregor813a97b2009-10-17 17:25:45 +00002172 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002173 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002174 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002175
2176 // Write the type offsets array
2177 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2178 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2181 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2182 Record.clear();
2183 Record.push_back(pch::TYPE_OFFSET);
2184 Record.push_back(TypeOffsets.size());
2185 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002186 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002187 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002188
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002189 // Write the declaration offsets array
2190 Abbrev = new BitCodeAbbrev();
2191 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2194 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2195 Record.clear();
2196 Record.push_back(pch::DECL_OFFSET);
2197 Record.push_back(DeclOffsets.size());
2198 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002199 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002200 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002201
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002202 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002203 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002204 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002205
2206 // Write the record containing tentative definitions.
2207 if (!TentativeDefinitions.empty())
2208 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002209
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002210 // Write the record containing unused static functions.
2211 if (!UnusedStaticFuncs.empty())
2212 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002213
Douglas Gregor14c22f22009-04-22 22:18:58 +00002214 // Write the record containing locally-scoped external definitions.
2215 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002216 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002217 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002218
2219 // Write the record containing ext_vector type names.
2220 if (!ExtVectorDecls.empty())
2221 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Douglas Gregor3e1af842009-04-17 22:13:46 +00002223 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002224 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002225 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002226 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002227 Record.push_back(NumLexicalDeclContexts);
2228 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002229 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002230 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002231}
2232
2233void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2234 Record.push_back(Loc.getRawEncoding());
2235}
2236
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002237void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2238 AddSourceLocation(Range.getBegin(), Record);
2239 AddSourceLocation(Range.getEnd(), Record);
2240}
2241
Douglas Gregor2cf26342009-04-09 22:27:44 +00002242void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2243 Record.push_back(Value.getBitWidth());
2244 unsigned N = Value.getNumWords();
2245 const uint64_t* Words = Value.getRawData();
2246 for (unsigned I = 0; I != N; ++I)
2247 Record.push_back(Words[I]);
2248}
2249
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002250void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2251 Record.push_back(Value.isUnsigned());
2252 AddAPInt(Value, Record);
2253}
2254
Douglas Gregor17fc2232009-04-14 21:55:33 +00002255void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2256 AddAPInt(Value.bitcastToAPInt(), Record);
2257}
2258
Douglas Gregor2cf26342009-04-09 22:27:44 +00002259void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002260 Record.push_back(getIdentifierRef(II));
2261}
2262
2263pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2264 if (II == 0)
2265 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002266
2267 pch::IdentID &ID = IdentifierIDs[II];
2268 if (ID == 0)
2269 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002270 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002271}
2272
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002273pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2274 if (MD == 0)
2275 return 0;
2276
2277 pch::IdentID &ID = MacroDefinitions[MD];
2278 if (ID == 0)
2279 ID = MacroDefinitions.size();
2280 return ID;
2281}
2282
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002283void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2284 if (SelRef.getAsOpaquePtr() == 0) {
2285 Record.push_back(0);
2286 return;
2287 }
2288
2289 pch::SelectorID &SID = SelectorIDs[SelRef];
2290 if (SID == 0) {
2291 SID = SelectorIDs.size();
2292 SelVector.push_back(SelRef);
2293 }
2294 Record.push_back(SID);
2295}
2296
Chris Lattnerd2598362010-05-10 00:25:06 +00002297void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2298 AddDeclRef(Temp->getDestructor(), Record);
2299}
2300
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002301void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2302 const TemplateArgumentLocInfo &Arg,
2303 RecordData &Record) {
2304 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002305 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002306 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002307 break;
2308 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002309 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002310 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002311 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002312 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2313 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002314 break;
John McCall833ca992009-10-29 08:12:44 +00002315 case TemplateArgument::Null:
2316 case TemplateArgument::Integral:
2317 case TemplateArgument::Declaration:
2318 case TemplateArgument::Pack:
2319 break;
2320 }
2321}
2322
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002323void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2324 RecordData &Record) {
2325 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002326
2327 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2328 bool InfoHasSameExpr
2329 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2330 Record.push_back(InfoHasSameExpr);
2331 if (InfoHasSameExpr)
2332 return; // Avoid storing the same expr twice.
2333 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002334 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2335 Record);
2336}
2337
John McCalla93c9342009-12-07 02:54:59 +00002338void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2339 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002340 AddTypeRef(QualType(), Record);
2341 return;
2342 }
2343
John McCalla93c9342009-12-07 02:54:59 +00002344 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002345 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002346 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002347 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002348}
2349
Douglas Gregor2cf26342009-04-09 22:27:44 +00002350void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2351 if (T.isNull()) {
2352 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2353 return;
2354 }
2355
Douglas Gregora4923eb2009-11-16 21:35:15 +00002356 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002357 T.removeFastQualifiers();
2358
Douglas Gregora4923eb2009-11-16 21:35:15 +00002359 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002360 pch::TypeID &ID = TypeIDs[T];
2361 if (ID == 0) {
2362 // We haven't seen these qualifiers applied to this type before.
2363 // Assign it a new ID. This is the only time we enqueue a
2364 // qualified type, and it has no CV qualifiers.
2365 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002366 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002367 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002368
John McCall0953e762009-09-24 19:53:00 +00002369 // Encode the type qualifiers in the type reference.
2370 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2371 return;
2372 }
2373
Douglas Gregora4923eb2009-11-16 21:35:15 +00002374 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002375
Douglas Gregor2cf26342009-04-09 22:27:44 +00002376 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002377 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002378 switch (BT->getKind()) {
2379 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2380 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2381 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2382 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2383 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2384 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2385 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2386 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002387 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002388 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2389 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2390 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2391 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2392 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2393 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2394 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002395 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002396 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2397 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2398 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002399 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002400 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2401 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002402 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2403 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002404 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2405 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002406 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002407 case BuiltinType::UndeducedAuto:
2408 assert(0 && "Should not see undeduced auto here");
2409 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002410 }
2411
John McCall0953e762009-09-24 19:53:00 +00002412 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002413 return;
2414 }
2415
John McCall0953e762009-09-24 19:53:00 +00002416 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002417 if (ID == 0) {
2418 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002419 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002420 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002421 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002422 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002423
2424 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002425 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002426}
2427
2428void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2429 if (D == 0) {
2430 Record.push_back(0);
2431 return;
2432 }
2433
Douglas Gregor8038d512009-04-10 17:25:41 +00002434 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002435 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002436 // We haven't seen this declaration before. Give it a new ID and
2437 // enqueue it in the list of declarations to emit.
2438 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002439 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002440 }
2441
2442 Record.push_back(ID);
2443}
2444
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002445pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2446 if (D == 0)
2447 return 0;
2448
2449 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2450 return DeclIDs[D];
2451}
2452
Douglas Gregor2cf26342009-04-09 22:27:44 +00002453void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002454 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002455 Record.push_back(Name.getNameKind());
2456 switch (Name.getNameKind()) {
2457 case DeclarationName::Identifier:
2458 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2459 break;
2460
2461 case DeclarationName::ObjCZeroArgSelector:
2462 case DeclarationName::ObjCOneArgSelector:
2463 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002464 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002465 break;
2466
2467 case DeclarationName::CXXConstructorName:
2468 case DeclarationName::CXXDestructorName:
2469 case DeclarationName::CXXConversionFunctionName:
2470 AddTypeRef(Name.getCXXNameType(), Record);
2471 break;
2472
2473 case DeclarationName::CXXOperatorName:
2474 Record.push_back(Name.getCXXOverloadedOperator());
2475 break;
2476
Sean Hunt3e518bd2009-11-29 07:34:05 +00002477 case DeclarationName::CXXLiteralOperatorName:
2478 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2479 break;
2480
Douglas Gregor2cf26342009-04-09 22:27:44 +00002481 case DeclarationName::CXXUsingDirective:
2482 // No extra data to emit
2483 break;
2484 }
2485}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002486
2487void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2488 RecordData &Record) {
2489 // Nested name specifiers usually aren't too long. I think that 8 would
2490 // typically accomodate the vast majority.
2491 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2492
2493 // Push each of the NNS's onto a stack for serialization in reverse order.
2494 while (NNS) {
2495 NestedNames.push_back(NNS);
2496 NNS = NNS->getPrefix();
2497 }
2498
2499 Record.push_back(NestedNames.size());
2500 while(!NestedNames.empty()) {
2501 NNS = NestedNames.pop_back_val();
2502 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2503 Record.push_back(Kind);
2504 switch (Kind) {
2505 case NestedNameSpecifier::Identifier:
2506 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2507 break;
2508
2509 case NestedNameSpecifier::Namespace:
2510 AddDeclRef(NNS->getAsNamespace(), Record);
2511 break;
2512
2513 case NestedNameSpecifier::TypeSpec:
2514 case NestedNameSpecifier::TypeSpecWithTemplate:
2515 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2516 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2517 break;
2518
2519 case NestedNameSpecifier::Global:
2520 // Don't need to write an associated value.
2521 break;
2522 }
2523 }
2524}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002525
2526void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2527 TemplateName::NameKind Kind = Name.getKind();
2528 Record.push_back(Kind);
2529 switch (Kind) {
2530 case TemplateName::Template:
2531 AddDeclRef(Name.getAsTemplateDecl(), Record);
2532 break;
2533
2534 case TemplateName::OverloadedTemplate: {
2535 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2536 Record.push_back(OvT->size());
2537 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2538 I != E; ++I)
2539 AddDeclRef(*I, Record);
2540 break;
2541 }
2542
2543 case TemplateName::QualifiedTemplate: {
2544 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2545 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2546 Record.push_back(QualT->hasTemplateKeyword());
2547 AddDeclRef(QualT->getTemplateDecl(), Record);
2548 break;
2549 }
2550
2551 case TemplateName::DependentTemplate: {
2552 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2553 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2554 Record.push_back(DepT->isIdentifier());
2555 if (DepT->isIdentifier())
2556 AddIdentifierRef(DepT->getIdentifier(), Record);
2557 else
2558 Record.push_back(DepT->getOperator());
2559 break;
2560 }
2561 }
2562}
2563
2564void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2565 RecordData &Record) {
2566 Record.push_back(Arg.getKind());
2567 switch (Arg.getKind()) {
2568 case TemplateArgument::Null:
2569 break;
2570 case TemplateArgument::Type:
2571 AddTypeRef(Arg.getAsType(), Record);
2572 break;
2573 case TemplateArgument::Declaration:
2574 AddDeclRef(Arg.getAsDecl(), Record);
2575 break;
2576 case TemplateArgument::Integral:
2577 AddAPSInt(*Arg.getAsIntegral(), Record);
2578 AddTypeRef(Arg.getIntegralType(), Record);
2579 break;
2580 case TemplateArgument::Template:
2581 AddTemplateName(Arg.getAsTemplate(), Record);
2582 break;
2583 case TemplateArgument::Expression:
2584 AddStmt(Arg.getAsExpr());
2585 break;
2586 case TemplateArgument::Pack:
2587 Record.push_back(Arg.pack_size());
2588 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2589 I != E; ++I)
2590 AddTemplateArgument(*I, Record);
2591 break;
2592 }
2593}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002594
2595void
2596PCHWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
2597 RecordData &Record) {
2598 assert(TemplateParams && "No TemplateParams!");
2599 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2600 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2601 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2602 Record.push_back(TemplateParams->size());
2603 for (TemplateParameterList::const_iterator
2604 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2605 P != PEnd; ++P)
2606 AddDeclRef(*P, Record);
2607}
2608
2609/// \brief Emit a template argument list.
2610void
2611PCHWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
2612 RecordData &Record) {
2613 assert(TemplateArgs && "No TemplateArgs!");
2614 Record.push_back(TemplateArgs->flat_size());
2615 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2616 AddTemplateArgument(TemplateArgs->get(i), Record);
2617}