blob: d947f7250d2a44105f278ddff6af6e6ef97b0ea1 [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) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000233 // FIXME: Serialize this type (C++ only)
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000234 assert(false && "Cannot serialize dependent sized array types");
235}
236
237void
238PCHTypeWriter::VisitDependentSizedExtVectorType(
239 const DependentSizedExtVectorType *T) {
240 // FIXME: Serialize this type (C++ only)
241 assert(false && "Cannot serialize dependent sized extended vector types");
242}
243
244void
245PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
246 Record.push_back(T->getDepth());
247 Record.push_back(T->getIndex());
248 Record.push_back(T->isParameterPack());
249 Writer.AddIdentifierRef(T->getName(), Record);
250 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
251}
252
253void
254PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000255 Record.push_back(T->getKeyword());
256 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
257 Writer.AddIdentifierRef(T->getIdentifier(), Record);
258 Code = pch::TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000259}
260
261void
262PCHTypeWriter::VisitDependentTemplateSpecializationType(
263 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000264 Record.push_back(T->getKeyword());
265 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
266 Writer.AddIdentifierRef(T->getIdentifier(), Record);
267 Record.push_back(T->getNumArgs());
268 for (DependentTemplateSpecializationType::iterator
269 I = T->begin(), E = T->end(); I != E; ++I)
270 Writer.AddTemplateArgument(*I, Record);
271 Code = pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000272}
273
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000274void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000275 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000276 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
277 Writer.AddTypeRef(T->getNamedType(), Record);
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000278 Code = pch::TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000279}
280
John McCall3cb0ebd2010-03-10 03:28:59 +0000281void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
282 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000283 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000284 Code = pch::TYPE_INJECTED_CLASS_NAME;
285}
286
Douglas Gregor2cf26342009-04-09 22:27:44 +0000287void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
288 Writer.AddDeclRef(T->getDecl(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000289 Code = pch::TYPE_OBJC_INTERFACE;
290}
291
292void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
293 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000294 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000295 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000296 E = T->qual_end(); I != E; ++I)
297 Writer.AddDeclRef(*I, Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000298 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000299}
300
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000301void
302PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000303 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000304 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000305}
306
John McCalla1ee0c52009-10-16 21:56:05 +0000307namespace {
308
309class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
310 PCHWriter &Writer;
311 PCHWriter::RecordData &Record;
312
313public:
314 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
315 : Writer(Writer), Record(Record) { }
316
John McCall51bd8032009-10-18 01:05:36 +0000317#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000318#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000319 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000320#include "clang/AST/TypeLocNodes.def"
321
John McCall51bd8032009-10-18 01:05:36 +0000322 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
323 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000324};
325
326}
327
John McCall51bd8032009-10-18 01:05:36 +0000328void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
329 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000330}
John McCall51bd8032009-10-18 01:05:36 +0000331void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000332 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
333 if (TL.needsExtraLocalData()) {
334 Record.push_back(TL.getWrittenTypeSpec());
335 Record.push_back(TL.getWrittenSignSpec());
336 Record.push_back(TL.getWrittenWidthSpec());
337 Record.push_back(TL.hasModeAttr());
338 }
John McCalla1ee0c52009-10-16 21:56:05 +0000339}
John McCall51bd8032009-10-18 01:05:36 +0000340void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
341 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000342}
John McCall51bd8032009-10-18 01:05:36 +0000343void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
344 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000345}
John McCall51bd8032009-10-18 01:05:36 +0000346void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
347 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000348}
John McCall51bd8032009-10-18 01:05:36 +0000349void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
350 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000351}
John McCall51bd8032009-10-18 01:05:36 +0000352void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
353 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000354}
John McCall51bd8032009-10-18 01:05:36 +0000355void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
356 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000357}
John McCall51bd8032009-10-18 01:05:36 +0000358void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
359 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
360 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
361 Record.push_back(TL.getSizeExpr() ? 1 : 0);
362 if (TL.getSizeExpr())
363 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000364}
John McCall51bd8032009-10-18 01:05:36 +0000365void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
366 VisitArrayTypeLoc(TL);
367}
368void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
369 VisitArrayTypeLoc(TL);
370}
371void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
372 VisitArrayTypeLoc(TL);
373}
374void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
375 DependentSizedArrayTypeLoc TL) {
376 VisitArrayTypeLoc(TL);
377}
378void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
379 DependentSizedExtVectorTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
382void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getNameLoc(), Record);
384}
385void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getNameLoc(), Record);
387}
388void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
389 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
390 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
391 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
392 Writer.AddDeclRef(TL.getArg(i), Record);
393}
394void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
395 VisitFunctionTypeLoc(TL);
396}
397void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
398 VisitFunctionTypeLoc(TL);
399}
John McCalled976492009-12-04 22:46:56 +0000400void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getNameLoc(), Record);
402}
John McCall51bd8032009-10-18 01:05:36 +0000403void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getNameLoc(), Record);
405}
406void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000407 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
408 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
409 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000410}
411void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000412 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
413 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
414 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
415 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000416}
417void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getNameLoc(), Record);
419}
420void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getNameLoc(), Record);
422}
423void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getNameLoc(), Record);
425}
John McCall51bd8032009-10-18 01:05:36 +0000426void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
427 Writer.AddSourceLocation(TL.getNameLoc(), Record);
428}
John McCall49a832b2009-10-18 09:09:24 +0000429void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
430 SubstTemplateTypeParmTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getNameLoc(), Record);
432}
John McCall51bd8032009-10-18 01:05:36 +0000433void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
434 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000435 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
436 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
437 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
438 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000439 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
440 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000441}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000442void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000443 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
444 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000445}
John McCall3cb0ebd2010-03-10 03:28:59 +0000446void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getNameLoc(), Record);
448}
Douglas Gregor4714c122010-03-31 17:34:00 +0000449void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000450 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
451 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000452 Writer.AddSourceLocation(TL.getNameLoc(), Record);
453}
John McCall33500952010-06-11 00:33:02 +0000454void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
455 DependentTemplateSpecializationTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
457 Writer.AddSourceRange(TL.getQualifierRange(), Record);
458 Writer.AddSourceLocation(TL.getNameLoc(), Record);
459 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
460 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
461 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000462 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
463 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000464}
John McCall51bd8032009-10-18 01:05:36 +0000465void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000467}
468void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
469 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000470 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
471 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
472 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
473 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000474}
John McCall54e14c42009-10-22 22:37:11 +0000475void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000477}
John McCalla1ee0c52009-10-16 21:56:05 +0000478
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000479//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000480// PCHWriter Implementation
481//===----------------------------------------------------------------------===//
482
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000483static void EmitBlockID(unsigned ID, const char *Name,
484 llvm::BitstreamWriter &Stream,
485 PCHWriter::RecordData &Record) {
486 Record.clear();
487 Record.push_back(ID);
488 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
489
490 // Emit the block name if present.
491 if (Name == 0 || Name[0] == 0) return;
492 Record.clear();
493 while (*Name)
494 Record.push_back(*Name++);
495 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
496}
497
498static void EmitRecordID(unsigned ID, const char *Name,
499 llvm::BitstreamWriter &Stream,
500 PCHWriter::RecordData &Record) {
501 Record.clear();
502 Record.push_back(ID);
503 while (*Name)
504 Record.push_back(*Name++);
505 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000506}
507
508static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
509 PCHWriter::RecordData &Record) {
510#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
511 RECORD(STMT_STOP);
512 RECORD(STMT_NULL_PTR);
513 RECORD(STMT_NULL);
514 RECORD(STMT_COMPOUND);
515 RECORD(STMT_CASE);
516 RECORD(STMT_DEFAULT);
517 RECORD(STMT_LABEL);
518 RECORD(STMT_IF);
519 RECORD(STMT_SWITCH);
520 RECORD(STMT_WHILE);
521 RECORD(STMT_DO);
522 RECORD(STMT_FOR);
523 RECORD(STMT_GOTO);
524 RECORD(STMT_INDIRECT_GOTO);
525 RECORD(STMT_CONTINUE);
526 RECORD(STMT_BREAK);
527 RECORD(STMT_RETURN);
528 RECORD(STMT_DECL);
529 RECORD(STMT_ASM);
530 RECORD(EXPR_PREDEFINED);
531 RECORD(EXPR_DECL_REF);
532 RECORD(EXPR_INTEGER_LITERAL);
533 RECORD(EXPR_FLOATING_LITERAL);
534 RECORD(EXPR_IMAGINARY_LITERAL);
535 RECORD(EXPR_STRING_LITERAL);
536 RECORD(EXPR_CHARACTER_LITERAL);
537 RECORD(EXPR_PAREN);
538 RECORD(EXPR_UNARY_OPERATOR);
539 RECORD(EXPR_SIZEOF_ALIGN_OF);
540 RECORD(EXPR_ARRAY_SUBSCRIPT);
541 RECORD(EXPR_CALL);
542 RECORD(EXPR_MEMBER);
543 RECORD(EXPR_BINARY_OPERATOR);
544 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
545 RECORD(EXPR_CONDITIONAL_OPERATOR);
546 RECORD(EXPR_IMPLICIT_CAST);
547 RECORD(EXPR_CSTYLE_CAST);
548 RECORD(EXPR_COMPOUND_LITERAL);
549 RECORD(EXPR_EXT_VECTOR_ELEMENT);
550 RECORD(EXPR_INIT_LIST);
551 RECORD(EXPR_DESIGNATED_INIT);
552 RECORD(EXPR_IMPLICIT_VALUE_INIT);
553 RECORD(EXPR_VA_ARG);
554 RECORD(EXPR_ADDR_LABEL);
555 RECORD(EXPR_STMT);
556 RECORD(EXPR_TYPES_COMPATIBLE);
557 RECORD(EXPR_CHOOSE);
558 RECORD(EXPR_GNU_NULL);
559 RECORD(EXPR_SHUFFLE_VECTOR);
560 RECORD(EXPR_BLOCK);
561 RECORD(EXPR_BLOCK_DECL_REF);
562 RECORD(EXPR_OBJC_STRING_LITERAL);
563 RECORD(EXPR_OBJC_ENCODE);
564 RECORD(EXPR_OBJC_SELECTOR_EXPR);
565 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
566 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
567 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
568 RECORD(EXPR_OBJC_KVC_REF_EXPR);
569 RECORD(EXPR_OBJC_MESSAGE_EXPR);
570 RECORD(EXPR_OBJC_SUPER_EXPR);
571 RECORD(STMT_OBJC_FOR_COLLECTION);
572 RECORD(STMT_OBJC_CATCH);
573 RECORD(STMT_OBJC_FINALLY);
574 RECORD(STMT_OBJC_AT_TRY);
575 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
576 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000577 RECORD(EXPR_CXX_OPERATOR_CALL);
578 RECORD(EXPR_CXX_CONSTRUCT);
579 RECORD(EXPR_CXX_STATIC_CAST);
580 RECORD(EXPR_CXX_DYNAMIC_CAST);
581 RECORD(EXPR_CXX_REINTERPRET_CAST);
582 RECORD(EXPR_CXX_CONST_CAST);
583 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
584 RECORD(EXPR_CXX_BOOL_LITERAL);
585 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000586#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000587}
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000589void PCHWriter::WriteBlockInfoBlock() {
590 RecordData Record;
591 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Chris Lattner2f4efd12009-04-27 00:40:25 +0000593#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000594#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000596 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000597 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000598 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000599 RECORD(TYPE_OFFSET);
600 RECORD(DECL_OFFSET);
601 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000602 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000603 RECORD(IDENTIFIER_OFFSET);
604 RECORD(IDENTIFIER_TABLE);
605 RECORD(EXTERNAL_DEFINITIONS);
606 RECORD(SPECIAL_TYPES);
607 RECORD(STATISTICS);
608 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000609 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000610 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
611 RECORD(SELECTOR_OFFSETS);
612 RECORD(METHOD_POOL);
613 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000614 RECORD(SOURCE_LOCATION_OFFSETS);
615 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000616 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000617 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000618 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000619 RECORD(UNUSED_STATIC_FUNCS);
620 RECORD(MACRO_DEFINITION_OFFSETS);
621
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000622 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000623 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000624 RECORD(SM_SLOC_FILE_ENTRY);
625 RECORD(SM_SLOC_BUFFER_ENTRY);
626 RECORD(SM_SLOC_BUFFER_BLOB);
627 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
628 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000630 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000631 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 RECORD(PP_MACRO_OBJECT_LIKE);
633 RECORD(PP_MACRO_FUNCTION_LIKE);
634 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000635 RECORD(PP_MACRO_INSTANTIATION);
636 RECORD(PP_MACRO_DEFINITION);
637
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000638 // Decls and Types block.
639 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000640 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000641 RECORD(TYPE_COMPLEX);
642 RECORD(TYPE_POINTER);
643 RECORD(TYPE_BLOCK_POINTER);
644 RECORD(TYPE_LVALUE_REFERENCE);
645 RECORD(TYPE_RVALUE_REFERENCE);
646 RECORD(TYPE_MEMBER_POINTER);
647 RECORD(TYPE_CONSTANT_ARRAY);
648 RECORD(TYPE_INCOMPLETE_ARRAY);
649 RECORD(TYPE_VARIABLE_ARRAY);
650 RECORD(TYPE_VECTOR);
651 RECORD(TYPE_EXT_VECTOR);
652 RECORD(TYPE_FUNCTION_PROTO);
653 RECORD(TYPE_FUNCTION_NO_PROTO);
654 RECORD(TYPE_TYPEDEF);
655 RECORD(TYPE_TYPEOF_EXPR);
656 RECORD(TYPE_TYPEOF);
657 RECORD(TYPE_RECORD);
658 RECORD(TYPE_ENUM);
659 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000660 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000661 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000662 RECORD(DECL_ATTR);
663 RECORD(DECL_TRANSLATION_UNIT);
664 RECORD(DECL_TYPEDEF);
665 RECORD(DECL_ENUM);
666 RECORD(DECL_RECORD);
667 RECORD(DECL_ENUM_CONSTANT);
668 RECORD(DECL_FUNCTION);
669 RECORD(DECL_OBJC_METHOD);
670 RECORD(DECL_OBJC_INTERFACE);
671 RECORD(DECL_OBJC_PROTOCOL);
672 RECORD(DECL_OBJC_IVAR);
673 RECORD(DECL_OBJC_AT_DEFS_FIELD);
674 RECORD(DECL_OBJC_CLASS);
675 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
676 RECORD(DECL_OBJC_CATEGORY);
677 RECORD(DECL_OBJC_CATEGORY_IMPL);
678 RECORD(DECL_OBJC_IMPLEMENTATION);
679 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
680 RECORD(DECL_OBJC_PROPERTY);
681 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000682 RECORD(DECL_FIELD);
683 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000684 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000685 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000686 RECORD(DECL_FILE_SCOPE_ASM);
687 RECORD(DECL_BLOCK);
688 RECORD(DECL_CONTEXT_LEXICAL);
689 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000690 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000691 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000692#undef RECORD
693#undef BLOCK
694 Stream.ExitBlock();
695}
696
Douglas Gregore650c8c2009-07-07 00:12:59 +0000697/// \brief Adjusts the given filename to only write out the portion of the
698/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000699///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000700/// \param Filename the file name to adjust.
701///
702/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
703/// the returned filename will be adjusted by this system root.
704///
705/// \returns either the original filename (if it needs no adjustment) or the
706/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000707static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000708adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
709 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Douglas Gregore650c8c2009-07-07 00:12:59 +0000711 if (!isysroot)
712 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Douglas Gregore650c8c2009-07-07 00:12:59 +0000714 // Verify that the filename and the system root have the same prefix.
715 unsigned Pos = 0;
716 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
717 if (Filename[Pos] != isysroot[Pos])
718 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Douglas Gregore650c8c2009-07-07 00:12:59 +0000720 // We hit the end of the filename before we hit the end of the system root.
721 if (!Filename[Pos])
722 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Douglas Gregore650c8c2009-07-07 00:12:59 +0000724 // If the file name has a '/' at the current position, skip over the '/'.
725 // We distinguish sysroot-based includes from absolute includes by the
726 // absence of '/' at the beginning of sysroot-based includes.
727 if (Filename[Pos] == '/')
728 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Douglas Gregore650c8c2009-07-07 00:12:59 +0000730 return Filename + Pos;
731}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000732
Douglas Gregorab41e632009-04-27 22:23:34 +0000733/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000734void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000735 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000736
Douglas Gregore650c8c2009-07-07 00:12:59 +0000737 // Metadata
738 const TargetInfo &Target = Context.Target;
739 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
740 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
741 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
742 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
743 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
744 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
745 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
746 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
747 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Douglas Gregore650c8c2009-07-07 00:12:59 +0000749 RecordData Record;
750 Record.push_back(pch::METADATA);
751 Record.push_back(pch::VERSION_MAJOR);
752 Record.push_back(pch::VERSION_MINOR);
753 Record.push_back(CLANG_VERSION_MAJOR);
754 Record.push_back(CLANG_VERSION_MINOR);
755 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000756 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000757 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Douglas Gregorb64c1932009-05-12 01:31:05 +0000759 // Original file name
760 SourceManager &SM = Context.getSourceManager();
761 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
762 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
763 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
764 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
765 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
766
767 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000769 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000770
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000771 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000772 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000773 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000774 RecordData Record;
775 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000776 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000777 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000778
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000779 // Repository branch/version information.
780 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
781 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
782 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
783 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000784 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000785 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000786 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
787 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000788}
789
790/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000791void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
792 RecordData Record;
793 Record.push_back(LangOpts.Trigraphs);
794 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
795 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
796 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
797 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000798 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000799 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
800 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
801 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
802 Record.push_back(LangOpts.C99); // C99 Support
803 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
804 Record.push_back(LangOpts.CPlusPlus); // C++ Support
805 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000806 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000808 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
809 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000810 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000811 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000812 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000813 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000814 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000816 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000817 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
818 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000819 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000820 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000821 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000822
823 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
824 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
825 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
826
Chris Lattnerea5ce472009-04-27 07:35:58 +0000827 // Whether static initializers are protected by locks.
828 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000829 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000830 Record.push_back(LangOpts.Blocks); // block extension to C
831 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
832 // they are unused.
833 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
834 // (modulo the platform support).
835
Chris Lattnera4d71452010-06-26 21:25:03 +0000836 Record.push_back(LangOpts.getSignedOverflowBehavior());
837 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000838
839 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000840 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000841 // defined.
842 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
843 // opposed to __DYNAMIC__).
844 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
845
846 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
847 // used (instead of C99 semantics).
848 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000849 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
850 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000851 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
852 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000853 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000854 Record.push_back(LangOpts.getGCMode());
855 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000856 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000857 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000858 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000859 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000860 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000861 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000862}
863
Douglas Gregor14f79002009-04-10 03:52:48 +0000864//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000865// stat cache Serialization
866//===----------------------------------------------------------------------===//
867
868namespace {
869// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000870class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000871public:
872 typedef const char * key_type;
873 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000875 typedef std::pair<int, struct stat> data_type;
876 typedef const data_type& data_type_ref;
877
878 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000879 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
882 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000883 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
884 data_type_ref Data) {
885 unsigned StrLen = strlen(path);
886 clang::io::Emit16(Out, StrLen);
887 unsigned DataLen = 1; // result value
888 if (Data.first == 0)
889 DataLen += 4 + 4 + 2 + 8 + 8;
890 clang::io::Emit8(Out, DataLen);
891 return std::make_pair(StrLen + 1, DataLen);
892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000894 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
895 Out.write(path, KeyLen);
896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000898 void EmitData(llvm::raw_ostream& Out, key_type_ref,
899 data_type_ref Data, unsigned DataLen) {
900 using namespace clang::io;
901 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000903 // Result of stat()
904 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000906 if (Data.first == 0) {
907 Emit32(Out, (uint32_t) Data.second.st_ino);
908 Emit32(Out, (uint32_t) Data.second.st_dev);
909 Emit16(Out, (uint16_t) Data.second.st_mode);
910 Emit64(Out, (uint64_t) Data.second.st_mtime);
911 Emit64(Out, (uint64_t) Data.second.st_size);
912 }
913
914 assert(Out.tell() - Start == DataLen && "Wrong data length");
915 }
916};
917} // end anonymous namespace
918
919/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000920void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
921 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000922 // Build the on-disk hash table containing information about every
923 // stat() call.
924 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
925 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000926 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000927 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000928 Stat != StatEnd; ++Stat, ++NumStatEntries) {
929 const char *Filename = Stat->first();
930 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
931 Generator.insert(Filename, Stat->second);
932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000934 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000935 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000936 uint32_t BucketOffset;
937 {
938 llvm::raw_svector_ostream Out(StatCacheData);
939 // Make sure that no bucket is at offset 0
940 clang::io::Emit32(Out, 0);
941 BucketOffset = Generator.Emit(Out);
942 }
943
944 // Create a blob abbreviation
945 using namespace llvm;
946 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
947 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
951 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
952
953 // Write the stat cache
954 RecordData Record;
955 Record.push_back(pch::STAT_CACHE);
956 Record.push_back(BucketOffset);
957 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000958 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000959}
960
961//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000962// Source Manager Serialization
963//===----------------------------------------------------------------------===//
964
965/// \brief Create an abbreviation for the SLocEntry that refers to a
966/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000967static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000968 using namespace llvm;
969 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
970 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000975 // FileEntry fields.
976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000978 // HeaderFileInfo fields.
979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
981 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000984 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000985}
986
987/// \brief Create an abbreviation for the SLocEntry that refers to a
988/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000989static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000990 using namespace llvm;
991 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
992 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000998 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000999}
1000
1001/// \brief Create an abbreviation for the SLocEntry that refers to a
1002/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001003static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001004 using namespace llvm;
1005 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1006 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001008 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001009}
1010
1011/// \brief Create an abbreviation for the SLocEntry that refers to an
1012/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001013static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001014 using namespace llvm;
1015 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1016 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001022 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001023}
1024
1025/// \brief Writes the block containing the serialized form of the
1026/// source manager.
1027///
1028/// TODO: We should probably use an on-disk hash table (stored in a
1029/// blob), indexed based on the file name, so that we only create
1030/// entries for files that we actually need. In the common case (no
1031/// errors), we probably won't have to create file entries for any of
1032/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001033void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001034 const Preprocessor &PP,
1035 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001036 RecordData Record;
1037
Chris Lattnerf04ad692009-04-10 17:16:57 +00001038 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001039 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001040
1041 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001042 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1043 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1044 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1045 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001046
Douglas Gregorbd945002009-04-13 16:31:14 +00001047 // Write the line table.
1048 if (SourceMgr.hasLineTable()) {
1049 LineTableInfo &LineTable = SourceMgr.getLineTable();
1050
1051 // Emit the file names
1052 Record.push_back(LineTable.getNumFilenames());
1053 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1054 // Emit the file name
1055 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001056 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001057 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1058 Record.push_back(FilenameLen);
1059 if (FilenameLen)
1060 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Douglas Gregorbd945002009-04-13 16:31:14 +00001063 // Emit the line entries
1064 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1065 L != LEnd; ++L) {
1066 // Emit the file ID
1067 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Douglas Gregorbd945002009-04-13 16:31:14 +00001069 // Emit the line entries
1070 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001071 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001072 LEEnd = L->second.end();
1073 LE != LEEnd; ++LE) {
1074 Record.push_back(LE->FileOffset);
1075 Record.push_back(LE->LineNo);
1076 Record.push_back(LE->FilenameID);
1077 Record.push_back((unsigned)LE->FileKind);
1078 Record.push_back(LE->IncludeOffset);
1079 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001080 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001081 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001082 }
1083
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001084 // Write out the source location entry table. We skip the first
1085 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001086 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001087 RecordData PreloadSLocs;
1088 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001089 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1090 // Get this source location entry.
1091 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001092
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001093 // Record the offset of this source-location entry.
1094 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1095
1096 // Figure out which record code to use.
1097 unsigned Code;
1098 if (SLoc->isFile()) {
1099 if (SLoc->getFile().getContentCache()->Entry)
1100 Code = pch::SM_SLOC_FILE_ENTRY;
1101 else
1102 Code = pch::SM_SLOC_BUFFER_ENTRY;
1103 } else
1104 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1105 Record.clear();
1106 Record.push_back(Code);
1107
1108 Record.push_back(SLoc->getOffset());
1109 if (SLoc->isFile()) {
1110 const SrcMgr::FileInfo &File = SLoc->getFile();
1111 Record.push_back(File.getIncludeLoc().getRawEncoding());
1112 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1113 Record.push_back(File.hasLineDirectives());
1114
1115 const SrcMgr::ContentCache *Content = File.getContentCache();
1116 if (Content->Entry) {
1117 // The source location entry is a file. The blob associated
1118 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Douglas Gregor2d52be52010-03-21 22:49:54 +00001120 // Emit size/modification time for this file.
1121 Record.push_back(Content->Entry->getSize());
1122 Record.push_back(Content->Entry->getModificationTime());
1123
Douglas Gregor12fab312010-03-16 16:35:32 +00001124 // Emit header-search information associated with this file.
1125 HeaderFileInfo HFI;
1126 HeaderSearch &HS = PP.getHeaderSearchInfo();
1127 if (Content->Entry->getUID() < HS.header_file_size())
1128 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1129 Record.push_back(HFI.isImport);
1130 Record.push_back(HFI.DirInfo);
1131 Record.push_back(HFI.NumIncludes);
1132 AddIdentifierRef(HFI.ControllingMacro, Record);
1133
Douglas Gregore650c8c2009-07-07 00:12:59 +00001134 // Turn the file name into an absolute path, if it isn't already.
1135 const char *Filename = Content->Entry->getName();
1136 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001137 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001138 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregore650c8c2009-07-07 00:12:59 +00001140 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001141 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001142
1143 // FIXME: For now, preload all file source locations, so that
1144 // we get the appropriate File entries in the reader. This is
1145 // a temporary measure.
1146 PreloadSLocs.push_back(SLocEntryOffsets.size());
1147 } else {
1148 // The source location entry is a buffer. The blob associated
1149 // with this entry contains the contents of the buffer.
1150
1151 // We add one to the size so that we capture the trailing NULL
1152 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1153 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001154 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001155 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001156 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001157 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1158 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001159 Record.clear();
1160 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1161 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001162 llvm::StringRef(Buffer->getBufferStart(),
1163 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001164
1165 if (strcmp(Name, "<built-in>") == 0)
1166 PreloadSLocs.push_back(SLocEntryOffsets.size());
1167 }
1168 } else {
1169 // The source location entry is an instantiation.
1170 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1171 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1172 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1173 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1174
1175 // Compute the token length for this macro expansion.
1176 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001177 if (I + 1 != N)
1178 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001179 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1180 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1181 }
1182 }
1183
Douglas Gregorc9490c02009-04-16 22:23:12 +00001184 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001185
1186 if (SLocEntryOffsets.empty())
1187 return;
1188
1189 // Write the source-location offsets table into the PCH block. This
1190 // table is used for lazily loading source-location information.
1191 using namespace llvm;
1192 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1193 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1197 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001199 Record.clear();
1200 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1201 Record.push_back(SLocEntryOffsets.size());
1202 Record.push_back(SourceMgr.getNextOffset());
1203 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001204 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001205 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001206
1207 // Write the source location entry preloads array, telling the PCH
1208 // reader which source locations entries it should load eagerly.
1209 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001210}
1211
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001212//===----------------------------------------------------------------------===//
1213// Preprocessor Serialization
1214//===----------------------------------------------------------------------===//
1215
Chris Lattner0b1fb982009-04-10 17:15:23 +00001216/// \brief Writes the block containing the serialized form of the
1217/// preprocessor.
1218///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001219void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001220 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001221
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001222 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1223 if (PP.getCounterValue() != 0) {
1224 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001225 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001226 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001227 }
1228
1229 // Enter the preprocessor block.
1230 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001232 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1233 // FIXME: use diagnostics subsystem for localization etc.
1234 if (PP.SawDateOrTime())
1235 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001237 // Loop over all the macro definitions that are live at the end of the file,
1238 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001239 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001240 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1241 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001242 // FIXME: This emits macros in hash table order, we should do it in a stable
1243 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001244 MacroInfo *MI = I->second;
1245
1246 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1247 // been redefined by the header (in which case they are not isBuiltinMacro).
1248 if (MI->isBuiltinMacro())
1249 continue;
1250
Chris Lattner7356a312009-04-11 21:15:38 +00001251 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001252 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001253 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1254 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001256 unsigned Code;
1257 if (MI->isObjectLike()) {
1258 Code = pch::PP_MACRO_OBJECT_LIKE;
1259 } else {
1260 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001262 Record.push_back(MI->isC99Varargs());
1263 Record.push_back(MI->isGNUVarargs());
1264 Record.push_back(MI->getNumArgs());
1265 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1266 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001267 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001268 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001269
1270 // If we have a detailed preprocessing record, record the macro definition
1271 // ID that corresponds to this macro.
1272 if (PPRec)
1273 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1274
Douglas Gregorc9490c02009-04-16 22:23:12 +00001275 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001276 Record.clear();
1277
Chris Lattnerdf961c22009-04-10 18:08:30 +00001278 // Emit the tokens array.
1279 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1280 // Note that we know that the preprocessor does not have any annotation
1281 // tokens in it because they are created by the parser, and thus can't be
1282 // in a macro definition.
1283 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Chris Lattnerdf961c22009-04-10 18:08:30 +00001285 Record.push_back(Tok.getLocation().getRawEncoding());
1286 Record.push_back(Tok.getLength());
1287
Chris Lattnerdf961c22009-04-10 18:08:30 +00001288 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1289 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001290 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Chris Lattnerdf961c22009-04-10 18:08:30 +00001292 // FIXME: Should translate token kind to a stable encoding.
1293 Record.push_back(Tok.getKind());
1294 // FIXME: Should translate token flags to a stable encoding.
1295 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Douglas Gregorc9490c02009-04-16 22:23:12 +00001297 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001298 Record.clear();
1299 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001300 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001301 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001302
1303 // If the preprocessor has a preprocessing record, emit it.
1304 unsigned NumPreprocessingRecords = 0;
1305 if (PPRec) {
1306 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1307 E != EEnd; ++E) {
1308 Record.clear();
1309
1310 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1311 Record.push_back(NumPreprocessingRecords++);
1312 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1313 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1314 AddIdentifierRef(MI->getName(), Record);
1315 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1316 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1317 continue;
1318 }
1319
1320 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1321 // Record this macro definition's location.
1322 pch::IdentID ID = getMacroDefinitionID(MD);
1323 if (ID != MacroDefinitionOffsets.size()) {
1324 if (ID > MacroDefinitionOffsets.size())
1325 MacroDefinitionOffsets.resize(ID + 1);
1326
1327 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1328 } else
1329 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1330
1331 Record.push_back(NumPreprocessingRecords++);
1332 Record.push_back(ID);
1333 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1334 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1335 AddIdentifierRef(MD->getName(), Record);
1336 AddSourceLocation(MD->getLocation(), Record);
1337 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1338 continue;
1339 }
1340 }
1341 }
1342
Douglas Gregorc9490c02009-04-16 22:23:12 +00001343 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001344
1345 // Write the offsets table for the preprocessing record.
1346 if (NumPreprocessingRecords > 0) {
1347 // Write the offsets table for identifier IDs.
1348 using namespace llvm;
1349 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1350 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1354 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1355
1356 Record.clear();
1357 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1358 Record.push_back(NumPreprocessingRecords);
1359 Record.push_back(MacroDefinitionOffsets.size());
1360 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1361 (const char *)&MacroDefinitionOffsets.front(),
1362 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1363 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001364}
1365
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001366//===----------------------------------------------------------------------===//
1367// Type Serialization
1368//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001369
Douglas Gregor2cf26342009-04-09 22:27:44 +00001370/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001371void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001372 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001373 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001374 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Douglas Gregor2cf26342009-04-09 22:27:44 +00001376 // Record the offset for this type.
1377 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001378 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001379 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1380 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001381 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001382 }
1383
1384 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Douglas Gregor2cf26342009-04-09 22:27:44 +00001386 // Emit the type's representation.
1387 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001388
Douglas Gregora4923eb2009-11-16 21:35:15 +00001389 if (T.hasLocalNonFastQualifiers()) {
1390 Qualifiers Qs = T.getLocalQualifiers();
1391 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001392 Record.push_back(Qs.getAsOpaqueValue());
1393 W.Code = pch::TYPE_EXT_QUAL;
1394 } else {
1395 switch (T->getTypeClass()) {
1396 // For all of the concrete, non-dependent types, call the
1397 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001398#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001399 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001400#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001401#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001402 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001403 }
1404
1405 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001406 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001407
1408 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001409 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001410}
1411
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001412//===----------------------------------------------------------------------===//
1413// Declaration Serialization
1414//===----------------------------------------------------------------------===//
1415
Douglas Gregor2cf26342009-04-09 22:27:44 +00001416/// \brief Write the block containing all of the declaration IDs
1417/// lexically declared within the given DeclContext.
1418///
1419/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1420/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001421uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001422 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001423 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001424 return 0;
1425
Douglas Gregorc9490c02009-04-16 22:23:12 +00001426 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001427 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001428 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1429 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001430 AddDeclRef(*D, Record);
1431
Douglas Gregor25123082009-04-22 22:34:57 +00001432 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001433 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001434 return Offset;
1435}
1436
1437/// \brief Write the block containing all of the declaration IDs
1438/// visible from the given DeclContext.
1439///
1440/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1441/// bistream, or 0 if no block was written.
1442uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1443 DeclContext *DC) {
1444 if (DC->getPrimaryContext() != DC)
1445 return 0;
1446
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +00001447 // Since there is no name lookup into functions or methods, don't bother to
1448 // build a visible-declarations table for these entities.
1449 if (DC->isFunctionOrMethod())
1450 return 0;
1451
1452 // If not in C++, we perform name lookup for the translation unit via the
1453 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1454 // FIXME: In C++ we need the visible declarations in order to "see" the
1455 // friend declarations, is there a way to do this without writing the table ?
1456 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor58f06992009-04-18 15:49:20 +00001457 return 0;
1458
Douglas Gregor2cf26342009-04-09 22:27:44 +00001459 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001460 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001461
1462 // Serialize the contents of the mapping used for lookup. Note that,
1463 // although we have two very different code paths, the serialized
1464 // representation is the same for both cases: a declaration name,
1465 // followed by a size, followed by references to the visible
1466 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001467 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001468 RecordData Record;
1469 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001470 if (!Map)
1471 return 0;
1472
Douglas Gregor2cf26342009-04-09 22:27:44 +00001473 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1474 D != DEnd; ++D) {
1475 AddDeclarationName(D->first, Record);
1476 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1477 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001478 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001479 AddDeclRef(*Result.first, Record);
1480 }
1481
1482 if (Record.size() == 0)
1483 return 0;
1484
Douglas Gregorc9490c02009-04-16 22:23:12 +00001485 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001486 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001487 return Offset;
1488}
1489
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001490//===----------------------------------------------------------------------===//
1491// Global Method Pool and Selector Serialization
1492//===----------------------------------------------------------------------===//
1493
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001494namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001495// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001496class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001497 PCHWriter &Writer;
1498
1499public:
1500 typedef Selector key_type;
1501 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001503 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1504 typedef const data_type& data_type_ref;
1505
1506 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001508 static unsigned ComputeHash(Selector Sel) {
1509 unsigned N = Sel.getNumArgs();
1510 if (N == 0)
1511 ++N;
1512 unsigned R = 5381;
1513 for (unsigned I = 0; I != N; ++I)
1514 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001515 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001516 return R;
1517 }
Mike Stump1eb44332009-09-09 15:08:12 +00001518
1519 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001520 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1521 data_type_ref Methods) {
1522 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1523 clang::io::Emit16(Out, KeyLen);
1524 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001525 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001526 Method = Method->Next)
1527 if (Method->Method)
1528 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001529 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001530 Method = Method->Next)
1531 if (Method->Method)
1532 DataLen += 4;
1533 clang::io::Emit16(Out, DataLen);
1534 return std::make_pair(KeyLen, DataLen);
1535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Douglas Gregor83941df2009-04-25 17:48:32 +00001537 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001538 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001539 assert((Start >> 32) == 0 && "Selector key offset too large");
1540 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001541 unsigned N = Sel.getNumArgs();
1542 clang::io::Emit16(Out, N);
1543 if (N == 0)
1544 N = 1;
1545 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001546 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001547 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001550 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001551 data_type_ref Methods, unsigned DataLen) {
1552 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001553 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001554 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001555 Method = Method->Next)
1556 if (Method->Method)
1557 ++NumInstanceMethods;
1558
1559 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001560 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001561 Method = Method->Next)
1562 if (Method->Method)
1563 ++NumFactoryMethods;
1564
1565 clang::io::Emit16(Out, NumInstanceMethods);
1566 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001567 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001568 Method = Method->Next)
1569 if (Method->Method)
1570 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001571 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001572 Method = Method->Next)
1573 if (Method->Method)
1574 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001575
1576 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001577 }
1578};
1579} // end anonymous namespace
1580
1581/// \brief Write the method pool into the PCH file.
1582///
1583/// The method pool contains both instance and factory methods, stored
1584/// in an on-disk hash table indexed by the selector.
1585void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1586 using namespace llvm;
1587
1588 // Create and write out the blob that contains the instance and
1589 // factor method pools.
1590 bool Empty = true;
1591 {
1592 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001594 // Create the on-disk hash table representation. Start by
1595 // iterating through the instance method pool.
1596 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001597 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001598 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001599 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600 InstanceEnd = SemaRef.InstanceMethodPool.end();
1601 Instance != InstanceEnd; ++Instance) {
1602 // Check whether there is a factory method with the same
1603 // selector.
1604 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1605 = SemaRef.FactoryMethodPool.find(Instance->first);
1606
1607 if (Factory == SemaRef.FactoryMethodPool.end())
1608 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001609 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001610 ObjCMethodList()));
1611 else
1612 Generator.insert(Instance->first,
1613 std::make_pair(Instance->second, Factory->second));
1614
Douglas Gregor83941df2009-04-25 17:48:32 +00001615 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001616 Empty = false;
1617 }
1618
1619 // Now iterate through the factory method pool, to pick up any
1620 // selectors that weren't already in the instance method pool.
1621 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001622 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001623 FactoryEnd = SemaRef.FactoryMethodPool.end();
1624 Factory != FactoryEnd; ++Factory) {
1625 // Check whether there is an instance method with the same
1626 // selector. If so, there is no work to do here.
1627 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1628 = SemaRef.InstanceMethodPool.find(Factory->first);
1629
Douglas Gregor83941df2009-04-25 17:48:32 +00001630 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001631 Generator.insert(Factory->first,
1632 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001633 ++NumSelectorsInMethodPool;
1634 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001635
1636 Empty = false;
1637 }
1638
Douglas Gregor83941df2009-04-25 17:48:32 +00001639 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001640 return;
1641
1642 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001643 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001644 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001645 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001646 {
1647 PCHMethodPoolTrait Trait(*this);
1648 llvm::raw_svector_ostream Out(MethodPool);
1649 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001650 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001651 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001652
1653 // For every selector that we have seen but which was not
1654 // written into the hash table, write the selector itself and
1655 // record it's offset.
1656 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1657 if (SelectorOffsets[I] == 0)
1658 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001659 }
1660
1661 // Create a blob abbreviation
1662 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1663 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1664 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1667 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1668
Douglas Gregor83941df2009-04-25 17:48:32 +00001669 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001670 RecordData Record;
1671 Record.push_back(pch::METHOD_POOL);
1672 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001673 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001674 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001675
1676 // Create a blob abbreviation for the selector table offsets.
1677 Abbrev = new BitCodeAbbrev();
1678 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1679 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1680 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1681 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1682
1683 // Write the selector offsets table.
1684 Record.clear();
1685 Record.push_back(pch::SELECTOR_OFFSETS);
1686 Record.push_back(SelectorOffsets.size());
1687 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1688 (const char *)&SelectorOffsets.front(),
1689 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001690 }
1691}
1692
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001693//===----------------------------------------------------------------------===//
1694// Identifier Table Serialization
1695//===----------------------------------------------------------------------===//
1696
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001697namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001698class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001699 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001700 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001701
Douglas Gregora92193e2009-04-28 21:18:29 +00001702 /// \brief Determines whether this is an "interesting" identifier
1703 /// that needs a full IdentifierInfo structure written into the hash
1704 /// table.
1705 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1706 return II->isPoisoned() ||
1707 II->isExtensionToken() ||
1708 II->hasMacroDefinition() ||
1709 II->getObjCOrBuiltinID() ||
1710 II->getFETokenInfo<void>();
1711 }
1712
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001713public:
1714 typedef const IdentifierInfo* key_type;
1715 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001717 typedef pch::IdentID data_type;
1718 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001719
1720 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001721 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001722
1723 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001724 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001725 }
Mike Stump1eb44332009-09-09 15:08:12 +00001726
1727 std::pair<unsigned,unsigned>
1728 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001729 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001730 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001731 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1732 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001733 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001734 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001735 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001736 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001737 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1738 DEnd = IdentifierResolver::end();
1739 D != DEnd; ++D)
1740 DataLen += sizeof(pch::DeclID);
1741 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001742 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001743 // We emit the key length after the data length so that every
1744 // string is preceded by a 16-bit length. This matches the PTH
1745 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001746 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001747 return std::make_pair(KeyLen, DataLen);
1748 }
Mike Stump1eb44332009-09-09 15:08:12 +00001749
1750 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001751 unsigned KeyLen) {
1752 // Record the location of the key data. This is used when generating
1753 // the mapping from persistent IDs to strings.
1754 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001755 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001756 }
Mike Stump1eb44332009-09-09 15:08:12 +00001757
1758 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001759 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001760 if (!isInterestingIdentifier(II)) {
1761 clang::io::Emit32(Out, ID << 1);
1762 return;
1763 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001764
Douglas Gregora92193e2009-04-28 21:18:29 +00001765 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001766 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001767 bool hasMacroDefinition =
1768 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001769 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001770 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001771 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1772 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1773 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1774 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001775 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001776
Douglas Gregor37e26842009-04-21 23:56:24 +00001777 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001778 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001779
Douglas Gregor668c1a42009-04-21 22:25:48 +00001780 // Emit the declaration IDs in reverse order, because the
1781 // IdentifierResolver provides the declarations as they would be
1782 // visible (e.g., the function "stat" would come before the struct
1783 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1784 // adds declarations to the end of the list (so we need to see the
1785 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001786 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001787 IdentifierResolver::end());
1788 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1789 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001790 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001791 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001792 }
1793};
1794} // end anonymous namespace
1795
Douglas Gregorafaf3082009-04-11 00:14:32 +00001796/// \brief Write the identifier table into the PCH file.
1797///
1798/// The identifier table consists of a blob containing string data
1799/// (the actual identifiers themselves) and a separate "offsets" index
1800/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001801void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001802 using namespace llvm;
1803
1804 // Create and write out the blob that contains the identifier
1805 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001806 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001807 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Douglas Gregor92b059e2009-04-28 20:33:11 +00001809 // Look for any identifiers that were named while processing the
1810 // headers, but are otherwise not needed. We add these to the hash
1811 // table to enable checking of the predefines buffer in the case
1812 // where the user adds new macro definitions when building the PCH
1813 // file.
1814 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1815 IDEnd = PP.getIdentifierTable().end();
1816 ID != IDEnd; ++ID)
1817 getIdentifierRef(ID->second);
1818
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001819 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001820 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001821 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1822 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1823 ID != IDEnd; ++ID) {
1824 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001825 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001826 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001827
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001828 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001829 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001830 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001831 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001832 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001833 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001834 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001835 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001836 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001837 }
1838
1839 // Create a blob abbreviation
1840 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1841 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001844 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001845
1846 // Write the identifier table
1847 RecordData Record;
1848 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001849 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001850 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001851 }
1852
1853 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001854 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1855 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1856 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1857 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1858 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1859
1860 RecordData Record;
1861 Record.push_back(pch::IDENTIFIER_OFFSET);
1862 Record.push_back(IdentifierOffsets.size());
1863 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1864 (const char *)&IdentifierOffsets.front(),
1865 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001866}
1867
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001868//===----------------------------------------------------------------------===//
1869// General Serialization Routines
1870//===----------------------------------------------------------------------===//
1871
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001872/// \brief Write a record containing the given attributes.
1873void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1874 RecordData Record;
1875 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001876 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001877 Record.push_back(Attr->isInherited());
1878 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001879 default:
1880 assert(0 && "Does not support PCH writing for this attribute yet!");
1881 break;
Sean Hunt387475d2010-06-16 23:43:53 +00001882 case attr::Alias:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001883 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1884 break;
1885
Sean Hunt387475d2010-06-16 23:43:53 +00001886 case attr::AlignMac68k:
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001887 break;
1888
Sean Hunt387475d2010-06-16 23:43:53 +00001889 case attr::Aligned:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001890 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1891 break;
1892
Sean Hunt387475d2010-06-16 23:43:53 +00001893 case attr::AlwaysInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001894 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Sean Hunt387475d2010-06-16 23:43:53 +00001896 case attr::AnalyzerNoReturn:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001897 break;
1898
Sean Hunt387475d2010-06-16 23:43:53 +00001899 case attr::Annotate:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001900 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1901 break;
1902
Sean Hunt387475d2010-06-16 23:43:53 +00001903 case attr::AsmLabel:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001904 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1905 break;
1906
Sean Hunt387475d2010-06-16 23:43:53 +00001907 case attr::BaseCheck:
Sean Hunt7725e672009-11-25 04:20:27 +00001908 break;
1909
Sean Hunt387475d2010-06-16 23:43:53 +00001910 case attr::Blocks:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001911 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1912 break;
1913
Sean Hunt387475d2010-06-16 23:43:53 +00001914 case attr::CDecl:
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001915 break;
1916
Sean Hunt387475d2010-06-16 23:43:53 +00001917 case attr::Cleanup:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001918 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1919 break;
1920
Sean Hunt387475d2010-06-16 23:43:53 +00001921 case attr::Const:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001922 break;
1923
Sean Hunt387475d2010-06-16 23:43:53 +00001924 case attr::Constructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001925 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1926 break;
1927
Sean Hunt387475d2010-06-16 23:43:53 +00001928 case attr::DLLExport:
1929 case attr::DLLImport:
1930 case attr::Deprecated:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001931 break;
1932
Sean Hunt387475d2010-06-16 23:43:53 +00001933 case attr::Destructor:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001934 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1935 break;
1936
Sean Hunt387475d2010-06-16 23:43:53 +00001937 case attr::FastCall:
1938 case attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001939 break;
1940
Sean Hunt387475d2010-06-16 23:43:53 +00001941 case attr::Format: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001942 const FormatAttr *Format = cast<FormatAttr>(Attr);
1943 AddString(Format->getType(), Record);
1944 Record.push_back(Format->getFormatIdx());
1945 Record.push_back(Format->getFirstArg());
1946 break;
1947 }
1948
Sean Hunt387475d2010-06-16 23:43:53 +00001949 case attr::FormatArg: {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001950 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1951 Record.push_back(Format->getFormatIdx());
1952 break;
1953 }
1954
Sean Hunt387475d2010-06-16 23:43:53 +00001955 case attr::Sentinel : {
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001956 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1957 Record.push_back(Sentinel->getSentinel());
1958 Record.push_back(Sentinel->getNullPos());
1959 break;
1960 }
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Sean Hunt387475d2010-06-16 23:43:53 +00001962 case attr::GNUInline:
1963 case attr::Hiding:
1964 case attr::IBAction:
1965 case attr::IBOutlet:
1966 case attr::Malloc:
1967 case attr::NoDebug:
1968 case attr::NoInline:
1969 case attr::NoReturn:
1970 case attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001971 break;
1972
Sean Hunt387475d2010-06-16 23:43:53 +00001973 case attr::IBOutletCollection: {
Ted Kremenek857e9182010-05-19 17:38:06 +00001974 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
1975 AddDeclRef(ICA->getClass(), Record);
1976 break;
1977 }
1978
Sean Hunt387475d2010-06-16 23:43:53 +00001979 case attr::NonNull: {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001980 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1981 Record.push_back(NonNull->size());
1982 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1983 break;
1984 }
1985
Sean Hunt387475d2010-06-16 23:43:53 +00001986 case attr::CFReturnsNotRetained:
1987 case attr::CFReturnsRetained:
1988 case attr::NSReturnsNotRetained:
1989 case attr::NSReturnsRetained:
1990 case attr::ObjCException:
1991 case attr::ObjCNSObject:
1992 case attr::Overloadable:
1993 case attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001994 break;
1995
Sean Hunt387475d2010-06-16 23:43:53 +00001996 case attr::MaxFieldAlignment:
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00001997 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001998 break;
1999
Sean Hunt387475d2010-06-16 23:43:53 +00002000 case attr::Packed:
Anders Carlssona860e752009-08-08 18:23:56 +00002001 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Sean Hunt387475d2010-06-16 23:43:53 +00002003 case attr::Pure:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002004 break;
2005
Sean Hunt387475d2010-06-16 23:43:53 +00002006 case attr::Regparm:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002007 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2008 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002009
Sean Hunt387475d2010-06-16 23:43:53 +00002010 case attr::ReqdWorkGroupSize:
Nate Begeman6f3d8382009-06-26 06:32:41 +00002011 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2012 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2013 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2014 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002015
Sean Hunt387475d2010-06-16 23:43:53 +00002016 case attr::Section:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002017 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2018 break;
2019
Sean Hunt387475d2010-06-16 23:43:53 +00002020 case attr::StdCall:
2021 case attr::TransparentUnion:
2022 case attr::Unavailable:
2023 case attr::Unused:
2024 case attr::Used:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002025 break;
2026
Sean Hunt387475d2010-06-16 23:43:53 +00002027 case attr::Visibility:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002028 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00002029 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002030 break;
2031
Sean Hunt387475d2010-06-16 23:43:53 +00002032 case attr::WarnUnusedResult:
2033 case attr::Weak:
2034 case attr::WeakRef:
2035 case attr::WeakImport:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002036 break;
2037 }
2038 }
2039
Douglas Gregorc9490c02009-04-16 22:23:12 +00002040 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002041}
2042
2043void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2044 Record.push_back(Str.size());
2045 Record.insert(Record.end(), Str.begin(), Str.end());
2046}
2047
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002048/// \brief Note that the identifier II occurs at the given offset
2049/// within the identifier table.
2050void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002051 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002052}
2053
Douglas Gregor83941df2009-04-25 17:48:32 +00002054/// \brief Note that the selector Sel occurs at the given offset
2055/// within the method pool/selector table.
2056void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2057 unsigned ID = SelectorIDs[Sel];
2058 assert(ID && "Unknown selector");
2059 SelectorOffsets[ID - 1] = Offset;
2060}
2061
Mike Stump1eb44332009-09-09 15:08:12 +00002062PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2063 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002064 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
2065 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002066
Douglas Gregore650c8c2009-07-07 00:12:59 +00002067void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2068 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002069 using namespace llvm;
2070
Douglas Gregore7785042009-04-20 15:53:59 +00002071 ASTContext &Context = SemaRef.Context;
2072 Preprocessor &PP = SemaRef.PP;
2073
Douglas Gregor2cf26342009-04-09 22:27:44 +00002074 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002075 Stream.Emit((unsigned)'C', 8);
2076 Stream.Emit((unsigned)'P', 8);
2077 Stream.Emit((unsigned)'C', 8);
2078 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002079
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002080 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002081
2082 // The translation unit is the first declaration we'll emit.
2083 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002084 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002085
Douglas Gregor2deaea32009-04-22 18:49:13 +00002086 // Make sure that we emit IdentifierInfos (and any attached
2087 // declarations) for builtins.
2088 {
2089 IdentifierTable &Table = PP.getIdentifierTable();
2090 llvm::SmallVector<const char *, 32> BuiltinNames;
2091 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2092 Context.getLangOptions().NoBuiltin);
2093 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2094 getIdentifierRef(&Table.get(BuiltinNames[I]));
2095 }
2096
Chris Lattner63d65f82009-09-08 18:19:27 +00002097 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002098 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002099 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002100 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002101 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2102 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002103 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002104
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002105 // Build a record containing all of the static unused functions in this file.
2106 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002107 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002108 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002109
Douglas Gregor14c22f22009-04-22 22:18:58 +00002110 // Build a record containing all of the locally-scoped external
2111 // declarations in this header file. Generally, this record will be
2112 // empty.
2113 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002114 // FIXME: This is filling in the PCH file in densemap order which is
2115 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002116 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002117 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2118 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2119 TD != TDEnd; ++TD)
2120 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2121
Douglas Gregorb81c1702009-04-27 20:06:05 +00002122 // Build a record containing all of the ext_vector declarations.
2123 RecordData ExtVectorDecls;
2124 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2125 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2126
Douglas Gregor2cf26342009-04-09 22:27:44 +00002127 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002128 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002129 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002130 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002131 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002132 if (StatCalls && !isysroot)
2133 WriteStatCache(*StatCalls, isysroot);
2134 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002135 // Write the record of special types.
2136 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002137
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002138 AddTypeRef(Context.getBuiltinVaListType(), Record);
2139 AddTypeRef(Context.getObjCIdType(), Record);
2140 AddTypeRef(Context.getObjCSelType(), Record);
2141 AddTypeRef(Context.getObjCProtoType(), Record);
2142 AddTypeRef(Context.getObjCClassType(), Record);
2143 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2144 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2145 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002146 AddTypeRef(Context.getjmp_bufType(), Record);
2147 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002148 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2149 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002150 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002151 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002152 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2153 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002154 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002155
Douglas Gregor366809a2009-04-26 03:49:13 +00002156 // Keep writing types and declarations until all types and
2157 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002158 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2159 WriteDeclsBlockAbbrevs();
2160 while (!DeclTypesToEmit.empty()) {
2161 DeclOrType DOT = DeclTypesToEmit.front();
2162 DeclTypesToEmit.pop();
2163 if (DOT.isType())
2164 WriteType(DOT.getType());
2165 else
2166 WriteDecl(Context, DOT.getDecl());
2167 }
2168 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002169
Douglas Gregor813a97b2009-10-17 17:25:45 +00002170 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002171 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002172 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002173
2174 // Write the type offsets array
2175 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2176 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2179 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2180 Record.clear();
2181 Record.push_back(pch::TYPE_OFFSET);
2182 Record.push_back(TypeOffsets.size());
2183 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002184 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002185 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002186
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002187 // Write the declaration offsets array
2188 Abbrev = new BitCodeAbbrev();
2189 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2192 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2193 Record.clear();
2194 Record.push_back(pch::DECL_OFFSET);
2195 Record.push_back(DeclOffsets.size());
2196 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002197 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002198 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002199
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002200 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002201 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002202 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002203
2204 // Write the record containing tentative definitions.
2205 if (!TentativeDefinitions.empty())
2206 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002207
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002208 // Write the record containing unused static functions.
2209 if (!UnusedStaticFuncs.empty())
2210 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002211
Douglas Gregor14c22f22009-04-22 22:18:58 +00002212 // Write the record containing locally-scoped external definitions.
2213 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002214 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002215 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002216
2217 // Write the record containing ext_vector type names.
2218 if (!ExtVectorDecls.empty())
2219 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002220
Douglas Gregor3e1af842009-04-17 22:13:46 +00002221 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002222 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002223 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002224 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002225 Record.push_back(NumLexicalDeclContexts);
2226 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002227 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002228 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002229}
2230
2231void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2232 Record.push_back(Loc.getRawEncoding());
2233}
2234
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002235void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2236 AddSourceLocation(Range.getBegin(), Record);
2237 AddSourceLocation(Range.getEnd(), Record);
2238}
2239
Douglas Gregor2cf26342009-04-09 22:27:44 +00002240void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2241 Record.push_back(Value.getBitWidth());
2242 unsigned N = Value.getNumWords();
2243 const uint64_t* Words = Value.getRawData();
2244 for (unsigned I = 0; I != N; ++I)
2245 Record.push_back(Words[I]);
2246}
2247
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002248void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2249 Record.push_back(Value.isUnsigned());
2250 AddAPInt(Value, Record);
2251}
2252
Douglas Gregor17fc2232009-04-14 21:55:33 +00002253void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2254 AddAPInt(Value.bitcastToAPInt(), Record);
2255}
2256
Douglas Gregor2cf26342009-04-09 22:27:44 +00002257void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002258 Record.push_back(getIdentifierRef(II));
2259}
2260
2261pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2262 if (II == 0)
2263 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002264
2265 pch::IdentID &ID = IdentifierIDs[II];
2266 if (ID == 0)
2267 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002268 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002269}
2270
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002271pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2272 if (MD == 0)
2273 return 0;
2274
2275 pch::IdentID &ID = MacroDefinitions[MD];
2276 if (ID == 0)
2277 ID = MacroDefinitions.size();
2278 return ID;
2279}
2280
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002281void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2282 if (SelRef.getAsOpaquePtr() == 0) {
2283 Record.push_back(0);
2284 return;
2285 }
2286
2287 pch::SelectorID &SID = SelectorIDs[SelRef];
2288 if (SID == 0) {
2289 SID = SelectorIDs.size();
2290 SelVector.push_back(SelRef);
2291 }
2292 Record.push_back(SID);
2293}
2294
Chris Lattnerd2598362010-05-10 00:25:06 +00002295void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2296 AddDeclRef(Temp->getDestructor(), Record);
2297}
2298
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002299void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2300 const TemplateArgumentLocInfo &Arg,
2301 RecordData &Record) {
2302 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002303 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002304 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002305 break;
2306 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002307 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002308 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002309 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002310 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2311 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002312 break;
John McCall833ca992009-10-29 08:12:44 +00002313 case TemplateArgument::Null:
2314 case TemplateArgument::Integral:
2315 case TemplateArgument::Declaration:
2316 case TemplateArgument::Pack:
2317 break;
2318 }
2319}
2320
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002321void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2322 RecordData &Record) {
2323 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002324
2325 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2326 bool InfoHasSameExpr
2327 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2328 Record.push_back(InfoHasSameExpr);
2329 if (InfoHasSameExpr)
2330 return; // Avoid storing the same expr twice.
2331 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002332 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2333 Record);
2334}
2335
John McCalla93c9342009-12-07 02:54:59 +00002336void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2337 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002338 AddTypeRef(QualType(), Record);
2339 return;
2340 }
2341
John McCalla93c9342009-12-07 02:54:59 +00002342 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002343 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002344 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002345 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002346}
2347
Douglas Gregor2cf26342009-04-09 22:27:44 +00002348void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2349 if (T.isNull()) {
2350 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2351 return;
2352 }
2353
Douglas Gregora4923eb2009-11-16 21:35:15 +00002354 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002355 T.removeFastQualifiers();
2356
Douglas Gregora4923eb2009-11-16 21:35:15 +00002357 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002358 pch::TypeID &ID = TypeIDs[T];
2359 if (ID == 0) {
2360 // We haven't seen these qualifiers applied to this type before.
2361 // Assign it a new ID. This is the only time we enqueue a
2362 // qualified type, and it has no CV qualifiers.
2363 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002364 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002365 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002366
John McCall0953e762009-09-24 19:53:00 +00002367 // Encode the type qualifiers in the type reference.
2368 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2369 return;
2370 }
2371
Douglas Gregora4923eb2009-11-16 21:35:15 +00002372 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002373
Douglas Gregor2cf26342009-04-09 22:27:44 +00002374 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002375 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002376 switch (BT->getKind()) {
2377 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2378 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2379 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2380 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2381 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2382 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2383 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2384 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002385 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002386 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2387 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2388 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2389 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2390 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2391 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2392 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002393 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002394 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2395 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2396 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002397 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002398 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2399 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002400 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2401 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002402 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2403 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002404 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002405 case BuiltinType::UndeducedAuto:
2406 assert(0 && "Should not see undeduced auto here");
2407 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002408 }
2409
John McCall0953e762009-09-24 19:53:00 +00002410 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002411 return;
2412 }
2413
John McCall0953e762009-09-24 19:53:00 +00002414 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002415 if (ID == 0) {
2416 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002417 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002418 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002419 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002420 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002421
2422 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002423 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002424}
2425
2426void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2427 if (D == 0) {
2428 Record.push_back(0);
2429 return;
2430 }
2431
Douglas Gregor8038d512009-04-10 17:25:41 +00002432 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002433 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002434 // We haven't seen this declaration before. Give it a new ID and
2435 // enqueue it in the list of declarations to emit.
2436 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002437 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002438 }
2439
2440 Record.push_back(ID);
2441}
2442
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002443pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2444 if (D == 0)
2445 return 0;
2446
2447 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2448 return DeclIDs[D];
2449}
2450
Douglas Gregor2cf26342009-04-09 22:27:44 +00002451void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002452 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002453 Record.push_back(Name.getNameKind());
2454 switch (Name.getNameKind()) {
2455 case DeclarationName::Identifier:
2456 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2457 break;
2458
2459 case DeclarationName::ObjCZeroArgSelector:
2460 case DeclarationName::ObjCOneArgSelector:
2461 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002462 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002463 break;
2464
2465 case DeclarationName::CXXConstructorName:
2466 case DeclarationName::CXXDestructorName:
2467 case DeclarationName::CXXConversionFunctionName:
2468 AddTypeRef(Name.getCXXNameType(), Record);
2469 break;
2470
2471 case DeclarationName::CXXOperatorName:
2472 Record.push_back(Name.getCXXOverloadedOperator());
2473 break;
2474
Sean Hunt3e518bd2009-11-29 07:34:05 +00002475 case DeclarationName::CXXLiteralOperatorName:
2476 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2477 break;
2478
Douglas Gregor2cf26342009-04-09 22:27:44 +00002479 case DeclarationName::CXXUsingDirective:
2480 // No extra data to emit
2481 break;
2482 }
2483}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002484
2485void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2486 RecordData &Record) {
2487 // Nested name specifiers usually aren't too long. I think that 8 would
2488 // typically accomodate the vast majority.
2489 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2490
2491 // Push each of the NNS's onto a stack for serialization in reverse order.
2492 while (NNS) {
2493 NestedNames.push_back(NNS);
2494 NNS = NNS->getPrefix();
2495 }
2496
2497 Record.push_back(NestedNames.size());
2498 while(!NestedNames.empty()) {
2499 NNS = NestedNames.pop_back_val();
2500 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2501 Record.push_back(Kind);
2502 switch (Kind) {
2503 case NestedNameSpecifier::Identifier:
2504 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2505 break;
2506
2507 case NestedNameSpecifier::Namespace:
2508 AddDeclRef(NNS->getAsNamespace(), Record);
2509 break;
2510
2511 case NestedNameSpecifier::TypeSpec:
2512 case NestedNameSpecifier::TypeSpecWithTemplate:
2513 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2514 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2515 break;
2516
2517 case NestedNameSpecifier::Global:
2518 // Don't need to write an associated value.
2519 break;
2520 }
2521 }
2522}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002523
2524void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2525 TemplateName::NameKind Kind = Name.getKind();
2526 Record.push_back(Kind);
2527 switch (Kind) {
2528 case TemplateName::Template:
2529 AddDeclRef(Name.getAsTemplateDecl(), Record);
2530 break;
2531
2532 case TemplateName::OverloadedTemplate: {
2533 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2534 Record.push_back(OvT->size());
2535 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2536 I != E; ++I)
2537 AddDeclRef(*I, Record);
2538 break;
2539 }
2540
2541 case TemplateName::QualifiedTemplate: {
2542 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2543 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2544 Record.push_back(QualT->hasTemplateKeyword());
2545 AddDeclRef(QualT->getTemplateDecl(), Record);
2546 break;
2547 }
2548
2549 case TemplateName::DependentTemplate: {
2550 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2551 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2552 Record.push_back(DepT->isIdentifier());
2553 if (DepT->isIdentifier())
2554 AddIdentifierRef(DepT->getIdentifier(), Record);
2555 else
2556 Record.push_back(DepT->getOperator());
2557 break;
2558 }
2559 }
2560}
2561
2562void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2563 RecordData &Record) {
2564 Record.push_back(Arg.getKind());
2565 switch (Arg.getKind()) {
2566 case TemplateArgument::Null:
2567 break;
2568 case TemplateArgument::Type:
2569 AddTypeRef(Arg.getAsType(), Record);
2570 break;
2571 case TemplateArgument::Declaration:
2572 AddDeclRef(Arg.getAsDecl(), Record);
2573 break;
2574 case TemplateArgument::Integral:
2575 AddAPSInt(*Arg.getAsIntegral(), Record);
2576 AddTypeRef(Arg.getIntegralType(), Record);
2577 break;
2578 case TemplateArgument::Template:
2579 AddTemplateName(Arg.getAsTemplate(), Record);
2580 break;
2581 case TemplateArgument::Expression:
2582 AddStmt(Arg.getAsExpr());
2583 break;
2584 case TemplateArgument::Pack:
2585 Record.push_back(Arg.pack_size());
2586 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2587 I != E; ++I)
2588 AddTemplateArgument(*I, Record);
2589 break;
2590 }
2591}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002592
2593void
2594PCHWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
2595 RecordData &Record) {
2596 assert(TemplateParams && "No TemplateParams!");
2597 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2598 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2599 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2600 Record.push_back(TemplateParams->size());
2601 for (TemplateParameterList::const_iterator
2602 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2603 P != PEnd; ++P)
2604 AddDeclRef(*P, Record);
2605}
2606
2607/// \brief Emit a template argument list.
2608void
2609PCHWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
2610 RecordData &Record) {
2611 assert(TemplateArgs && "No TemplateArgs!");
2612 Record.push_back(TemplateArgs->flat_size());
2613 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2614 AddTemplateArgument(TemplateArgs->get(i), Record);
2615}