blob: a55684a29a9cbb054a34dee145aab03b940dbc4d [file] [log] [blame]
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001//===--- PCHWriter.cpp - Precompiled Headers Writer -----------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000033#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000045
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000047 class PCHTypeWriter {
Douglas Gregoref84c4b2009-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 Stump11289f42009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-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 Gregoref84c4b2009-04-09 22:27:44 +000064#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
Douglas Gregoref84c4b2009-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 Stump11289f42009-09-09 15:08:12 +000083 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-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 Stump11289f42009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-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 McCall8ccfcb52009-09-24 19:53:00 +0000106 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-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 Gregor04318252009-07-06 15:59:29 +0000122 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
123 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000124 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-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 Lattner37141f42010-06-23 06:00:24 +0000131 Record.push_back(T->getAltiVecSpecific());
Douglas Gregoref84c4b2009-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 Espindolac50c27c2010-03-30 20:24:48 +0000142 FunctionType::ExtInfo C = T->getExtInfo();
143 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000144 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000145 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000146 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-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 Redl5068f77ac2009-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 Gregoref84c4b2009-04-09 22:27:44 +0000166 Code = pch::TYPE_FUNCTION_PROTO;
167}
168
John McCallb96ec562009-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 McCallb96ec562009-12-04 22:46:56 +0000173
Douglas Gregoref84c4b2009-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 Gregor8f45df52009-04-16 22:23:12 +0000180 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-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 Carlsson81df7b82009-06-24 19:06:50 +0000189void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
190 Writer.AddStmt(T->getUnderlyingExpr());
191 Code = pch::TYPE_DECLTYPE;
192}
193
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000194void PCHTypeWriter::VisitTagType(const TagType *T) {
195 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000196 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-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 Stump11289f42009-09-09 15:08:12 +0000210void
John McCallcebee162009-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 Gregoref84c4b2009-04-09 22:27:44 +0000219PCHTypeWriter::VisitTemplateSpecializationType(
220 const TemplateSpecializationType *T) {
Argyrios Kyrtzidis106caf922010-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);
226 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
227}
228
229void
230PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000231 // FIXME: Serialize this type (C++ only)
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000232 assert(false && "Cannot serialize dependent sized array types");
233}
234
235void
236PCHTypeWriter::VisitDependentSizedExtVectorType(
237 const DependentSizedExtVectorType *T) {
238 // FIXME: Serialize this type (C++ only)
239 assert(false && "Cannot serialize dependent sized extended vector types");
240}
241
242void
243PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
244 Record.push_back(T->getDepth());
245 Record.push_back(T->getIndex());
246 Record.push_back(T->isParameterPack());
247 Writer.AddIdentifierRef(T->getName(), Record);
248 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
249}
250
251void
252PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) {
253 // FIXME: Serialize this type (C++ only)
254 assert(false && "Cannot serialize dependent name types");
255}
256
257void
258PCHTypeWriter::VisitDependentTemplateSpecializationType(
259 const DependentTemplateSpecializationType *T) {
260 // FIXME: Serialize this type (C++ only)
261 assert(false && "Cannot serialize dependent template specialization types");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262}
263
Abramo Bagnara6150c882010-05-11 21:36:43 +0000264void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
265 Writer.AddTypeRef(T->getNamedType(), Record);
266 Record.push_back(T->getKeyword());
267 // FIXME: Serialize the qualifier (C++ only)
268 assert(T->getQualifier() == 0 && "Cannot serialize qualified name types");
269 Code = pch::TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000270}
271
John McCalle78aac42010-03-10 03:28:59 +0000272void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
273 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000274 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCalle78aac42010-03-10 03:28:59 +0000275 Code = pch::TYPE_INJECTED_CLASS_NAME;
276}
277
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000278void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
279 Writer.AddDeclRef(T->getDecl(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000280 Code = pch::TYPE_OBJC_INTERFACE;
281}
282
283void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
284 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000285 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000286 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000287 E = T->qual_end(); I != E; ++I)
288 Writer.AddDeclRef(*I, Record);
John McCall8b07ec22010-05-15 11:32:37 +0000289 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000290}
291
Steve Narofffb4330f2009-06-17 22:40:22 +0000292void
293PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000294 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000295 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000296}
297
John McCall8f115c62009-10-16 21:56:05 +0000298namespace {
299
300class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
301 PCHWriter &Writer;
302 PCHWriter::RecordData &Record;
303
304public:
305 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
306 : Writer(Writer), Record(Record) { }
307
John McCall17001972009-10-18 01:05:36 +0000308#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000309#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000310 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000311#include "clang/AST/TypeLocNodes.def"
312
John McCall17001972009-10-18 01:05:36 +0000313 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
314 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000315};
316
317}
318
John McCall17001972009-10-18 01:05:36 +0000319void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
320 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000321}
John McCall17001972009-10-18 01:05:36 +0000322void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000323 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
324 if (TL.needsExtraLocalData()) {
325 Record.push_back(TL.getWrittenTypeSpec());
326 Record.push_back(TL.getWrittenSignSpec());
327 Record.push_back(TL.getWrittenWidthSpec());
328 Record.push_back(TL.hasModeAttr());
329 }
John McCall8f115c62009-10-16 21:56:05 +0000330}
John McCall17001972009-10-18 01:05:36 +0000331void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
332 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000333}
John McCall17001972009-10-18 01:05:36 +0000334void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
335 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000336}
John McCall17001972009-10-18 01:05:36 +0000337void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
338 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000339}
John McCall17001972009-10-18 01:05:36 +0000340void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
341 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000342}
John McCall17001972009-10-18 01:05:36 +0000343void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
344 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000345}
John McCall17001972009-10-18 01:05:36 +0000346void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
347 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000348}
John McCall17001972009-10-18 01:05:36 +0000349void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
350 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
351 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
352 Record.push_back(TL.getSizeExpr() ? 1 : 0);
353 if (TL.getSizeExpr())
354 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000355}
John McCall17001972009-10-18 01:05:36 +0000356void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
357 VisitArrayTypeLoc(TL);
358}
359void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
360 VisitArrayTypeLoc(TL);
361}
362void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
363 VisitArrayTypeLoc(TL);
364}
365void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
366 DependentSizedArrayTypeLoc TL) {
367 VisitArrayTypeLoc(TL);
368}
369void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
370 DependentSizedExtVectorTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getNameLoc(), Record);
372}
373void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getNameLoc(), Record);
375}
376void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
381 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
382 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
383 Writer.AddDeclRef(TL.getArg(i), Record);
384}
385void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
386 VisitFunctionTypeLoc(TL);
387}
388void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
389 VisitFunctionTypeLoc(TL);
390}
John McCallb96ec562009-12-04 22:46:56 +0000391void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
392 Writer.AddSourceLocation(TL.getNameLoc(), Record);
393}
John McCall17001972009-10-18 01:05:36 +0000394void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
395 Writer.AddSourceLocation(TL.getNameLoc(), Record);
396}
397void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000398 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
399 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
400 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000401}
402void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000403 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
404 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
405 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
406 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000407}
408void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getNameLoc(), Record);
410}
411void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getNameLoc(), Record);
413}
414void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getNameLoc(), Record);
416}
John McCall17001972009-10-18 01:05:36 +0000417void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getNameLoc(), Record);
419}
John McCallcebee162009-10-18 09:09:24 +0000420void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
421 SubstTemplateTypeParmTypeLoc TL) {
422 Writer.AddSourceLocation(TL.getNameLoc(), Record);
423}
John McCall17001972009-10-18 01:05:36 +0000424void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
425 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000426 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
427 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
428 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
429 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000430 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
431 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000432}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000433void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000434 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
435 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000436}
John McCalle78aac42010-03-10 03:28:59 +0000437void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
439}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000440void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000441 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
442 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
John McCallc392f372010-06-11 00:33:02 +0000445void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
446 DependentTemplateSpecializationTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
448 Writer.AddSourceRange(TL.getQualifierRange(), Record);
449 Writer.AddSourceLocation(TL.getNameLoc(), Record);
450 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
451 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
452 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000453 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
454 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000455}
John McCall17001972009-10-18 01:05:36 +0000456void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000458}
459void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
460 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000461 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
462 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
463 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
464 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000465}
John McCallfc93cf92009-10-22 22:37:11 +0000466void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
467 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000468}
John McCall8f115c62009-10-16 21:56:05 +0000469
Chris Lattner19cea4e2009-04-22 05:57:30 +0000470//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000471// PCHWriter Implementation
472//===----------------------------------------------------------------------===//
473
Chris Lattner28fa4e62009-04-26 22:26:21 +0000474static void EmitBlockID(unsigned ID, const char *Name,
475 llvm::BitstreamWriter &Stream,
476 PCHWriter::RecordData &Record) {
477 Record.clear();
478 Record.push_back(ID);
479 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
480
481 // Emit the block name if present.
482 if (Name == 0 || Name[0] == 0) return;
483 Record.clear();
484 while (*Name)
485 Record.push_back(*Name++);
486 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
487}
488
489static void EmitRecordID(unsigned ID, const char *Name,
490 llvm::BitstreamWriter &Stream,
491 PCHWriter::RecordData &Record) {
492 Record.clear();
493 Record.push_back(ID);
494 while (*Name)
495 Record.push_back(*Name++);
496 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000497}
498
499static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
500 PCHWriter::RecordData &Record) {
501#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
502 RECORD(STMT_STOP);
503 RECORD(STMT_NULL_PTR);
504 RECORD(STMT_NULL);
505 RECORD(STMT_COMPOUND);
506 RECORD(STMT_CASE);
507 RECORD(STMT_DEFAULT);
508 RECORD(STMT_LABEL);
509 RECORD(STMT_IF);
510 RECORD(STMT_SWITCH);
511 RECORD(STMT_WHILE);
512 RECORD(STMT_DO);
513 RECORD(STMT_FOR);
514 RECORD(STMT_GOTO);
515 RECORD(STMT_INDIRECT_GOTO);
516 RECORD(STMT_CONTINUE);
517 RECORD(STMT_BREAK);
518 RECORD(STMT_RETURN);
519 RECORD(STMT_DECL);
520 RECORD(STMT_ASM);
521 RECORD(EXPR_PREDEFINED);
522 RECORD(EXPR_DECL_REF);
523 RECORD(EXPR_INTEGER_LITERAL);
524 RECORD(EXPR_FLOATING_LITERAL);
525 RECORD(EXPR_IMAGINARY_LITERAL);
526 RECORD(EXPR_STRING_LITERAL);
527 RECORD(EXPR_CHARACTER_LITERAL);
528 RECORD(EXPR_PAREN);
529 RECORD(EXPR_UNARY_OPERATOR);
530 RECORD(EXPR_SIZEOF_ALIGN_OF);
531 RECORD(EXPR_ARRAY_SUBSCRIPT);
532 RECORD(EXPR_CALL);
533 RECORD(EXPR_MEMBER);
534 RECORD(EXPR_BINARY_OPERATOR);
535 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
536 RECORD(EXPR_CONDITIONAL_OPERATOR);
537 RECORD(EXPR_IMPLICIT_CAST);
538 RECORD(EXPR_CSTYLE_CAST);
539 RECORD(EXPR_COMPOUND_LITERAL);
540 RECORD(EXPR_EXT_VECTOR_ELEMENT);
541 RECORD(EXPR_INIT_LIST);
542 RECORD(EXPR_DESIGNATED_INIT);
543 RECORD(EXPR_IMPLICIT_VALUE_INIT);
544 RECORD(EXPR_VA_ARG);
545 RECORD(EXPR_ADDR_LABEL);
546 RECORD(EXPR_STMT);
547 RECORD(EXPR_TYPES_COMPATIBLE);
548 RECORD(EXPR_CHOOSE);
549 RECORD(EXPR_GNU_NULL);
550 RECORD(EXPR_SHUFFLE_VECTOR);
551 RECORD(EXPR_BLOCK);
552 RECORD(EXPR_BLOCK_DECL_REF);
553 RECORD(EXPR_OBJC_STRING_LITERAL);
554 RECORD(EXPR_OBJC_ENCODE);
555 RECORD(EXPR_OBJC_SELECTOR_EXPR);
556 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
557 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
558 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
559 RECORD(EXPR_OBJC_KVC_REF_EXPR);
560 RECORD(EXPR_OBJC_MESSAGE_EXPR);
561 RECORD(EXPR_OBJC_SUPER_EXPR);
562 RECORD(STMT_OBJC_FOR_COLLECTION);
563 RECORD(STMT_OBJC_CATCH);
564 RECORD(STMT_OBJC_FINALLY);
565 RECORD(STMT_OBJC_AT_TRY);
566 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
567 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000568 RECORD(EXPR_CXX_OPERATOR_CALL);
569 RECORD(EXPR_CXX_CONSTRUCT);
570 RECORD(EXPR_CXX_STATIC_CAST);
571 RECORD(EXPR_CXX_DYNAMIC_CAST);
572 RECORD(EXPR_CXX_REINTERPRET_CAST);
573 RECORD(EXPR_CXX_CONST_CAST);
574 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
575 RECORD(EXPR_CXX_BOOL_LITERAL);
576 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000577#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000578}
Mike Stump11289f42009-09-09 15:08:12 +0000579
Chris Lattner28fa4e62009-04-26 22:26:21 +0000580void PCHWriter::WriteBlockInfoBlock() {
581 RecordData Record;
582 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000583
Chris Lattner64031982009-04-27 00:40:25 +0000584#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000585#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000586
Chris Lattner28fa4e62009-04-26 22:26:21 +0000587 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000588 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000589 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000590 RECORD(TYPE_OFFSET);
591 RECORD(DECL_OFFSET);
592 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000593 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000594 RECORD(IDENTIFIER_OFFSET);
595 RECORD(IDENTIFIER_TABLE);
596 RECORD(EXTERNAL_DEFINITIONS);
597 RECORD(SPECIAL_TYPES);
598 RECORD(STATISTICS);
599 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattner90073802010-02-12 00:07:30 +0000600 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000601 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
602 RECORD(SELECTOR_OFFSETS);
603 RECORD(METHOD_POOL);
604 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000605 RECORD(SOURCE_LOCATION_OFFSETS);
606 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000607 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000608 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000609 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000610 RECORD(UNUSED_STATIC_FUNCS);
611 RECORD(MACRO_DEFINITION_OFFSETS);
612
Chris Lattner28fa4e62009-04-26 22:26:21 +0000613 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000614 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000615 RECORD(SM_SLOC_FILE_ENTRY);
616 RECORD(SM_SLOC_BUFFER_ENTRY);
617 RECORD(SM_SLOC_BUFFER_BLOB);
618 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
619 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattner28fa4e62009-04-26 22:26:21 +0000621 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000622 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000623 RECORD(PP_MACRO_OBJECT_LIKE);
624 RECORD(PP_MACRO_FUNCTION_LIKE);
625 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000626 RECORD(PP_MACRO_INSTANTIATION);
627 RECORD(PP_MACRO_DEFINITION);
628
Douglas Gregor12bfa382009-10-17 00:13:19 +0000629 // Decls and Types block.
630 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000631 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000632 RECORD(TYPE_COMPLEX);
633 RECORD(TYPE_POINTER);
634 RECORD(TYPE_BLOCK_POINTER);
635 RECORD(TYPE_LVALUE_REFERENCE);
636 RECORD(TYPE_RVALUE_REFERENCE);
637 RECORD(TYPE_MEMBER_POINTER);
638 RECORD(TYPE_CONSTANT_ARRAY);
639 RECORD(TYPE_INCOMPLETE_ARRAY);
640 RECORD(TYPE_VARIABLE_ARRAY);
641 RECORD(TYPE_VECTOR);
642 RECORD(TYPE_EXT_VECTOR);
643 RECORD(TYPE_FUNCTION_PROTO);
644 RECORD(TYPE_FUNCTION_NO_PROTO);
645 RECORD(TYPE_TYPEDEF);
646 RECORD(TYPE_TYPEOF_EXPR);
647 RECORD(TYPE_TYPEOF);
648 RECORD(TYPE_RECORD);
649 RECORD(TYPE_ENUM);
650 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000651 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000652 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000653 RECORD(DECL_ATTR);
654 RECORD(DECL_TRANSLATION_UNIT);
655 RECORD(DECL_TYPEDEF);
656 RECORD(DECL_ENUM);
657 RECORD(DECL_RECORD);
658 RECORD(DECL_ENUM_CONSTANT);
659 RECORD(DECL_FUNCTION);
660 RECORD(DECL_OBJC_METHOD);
661 RECORD(DECL_OBJC_INTERFACE);
662 RECORD(DECL_OBJC_PROTOCOL);
663 RECORD(DECL_OBJC_IVAR);
664 RECORD(DECL_OBJC_AT_DEFS_FIELD);
665 RECORD(DECL_OBJC_CLASS);
666 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
667 RECORD(DECL_OBJC_CATEGORY);
668 RECORD(DECL_OBJC_CATEGORY_IMPL);
669 RECORD(DECL_OBJC_IMPLEMENTATION);
670 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
671 RECORD(DECL_OBJC_PROPERTY);
672 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000673 RECORD(DECL_FIELD);
674 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000675 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000676 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000677 RECORD(DECL_FILE_SCOPE_ASM);
678 RECORD(DECL_BLOCK);
679 RECORD(DECL_CONTEXT_LEXICAL);
680 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000681 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000682 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000683#undef RECORD
684#undef BLOCK
685 Stream.ExitBlock();
686}
687
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000688/// \brief Adjusts the given filename to only write out the portion of the
689/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000690///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000691/// \param Filename the file name to adjust.
692///
693/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
694/// the returned filename will be adjusted by this system root.
695///
696/// \returns either the original filename (if it needs no adjustment) or the
697/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000698static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000699adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
700 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000701
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000702 if (!isysroot)
703 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000704
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000705 // Verify that the filename and the system root have the same prefix.
706 unsigned Pos = 0;
707 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
708 if (Filename[Pos] != isysroot[Pos])
709 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000710
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000711 // We hit the end of the filename before we hit the end of the system root.
712 if (!Filename[Pos])
713 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000714
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000715 // If the file name has a '/' at the current position, skip over the '/'.
716 // We distinguish sysroot-based includes from absolute includes by the
717 // absence of '/' at the beginning of sysroot-based includes.
718 if (Filename[Pos] == '/')
719 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000720
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000721 return Filename + Pos;
722}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000723
Douglas Gregor7b71e632009-04-27 22:23:34 +0000724/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000725void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000726 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000727
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000728 // Metadata
729 const TargetInfo &Target = Context.Target;
730 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
731 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
732 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
733 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
734 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
735 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
736 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
737 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
738 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000739
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000740 RecordData Record;
741 Record.push_back(pch::METADATA);
742 Record.push_back(pch::VERSION_MAJOR);
743 Record.push_back(pch::VERSION_MINOR);
744 Record.push_back(CLANG_VERSION_MAJOR);
745 Record.push_back(CLANG_VERSION_MINOR);
746 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000747 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000748 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000749
Douglas Gregor45fe0362009-05-12 01:31:05 +0000750 // Original file name
751 SourceManager &SM = Context.getSourceManager();
752 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
753 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
754 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
755 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
756 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
757
758 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000759
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000760 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000761
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000762 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000763 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000764 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000765 RecordData Record;
766 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000767 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000768 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000769
Ted Kremenek18e066f2010-01-22 22:12:47 +0000770 // Repository branch/version information.
771 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
772 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
773 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
774 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000775 Record.clear();
Ted Kremenek17437132010-01-22 20:59:36 +0000776 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000777 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
778 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000779}
780
781/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000782void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
783 RecordData Record;
784 Record.push_back(LangOpts.Trigraphs);
785 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
786 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
787 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
788 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000789 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000790 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
791 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
792 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
793 Record.push_back(LangOpts.C99); // C99 Support
794 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
795 Record.push_back(LangOpts.CPlusPlus); // C++ Support
796 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000797 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000798
Douglas Gregor55abb232009-04-10 20:39:37 +0000799 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
800 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000801 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000802 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000803 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000804 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000805 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000806
Douglas Gregor55abb232009-04-10 20:39:37 +0000807 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000808 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
809 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000810 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000811 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000812 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000813
814 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
815 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
816 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
817
Chris Lattner258172e2009-04-27 07:35:58 +0000818 // Whether static initializers are protected by locks.
819 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000820 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000821 Record.push_back(LangOpts.Blocks); // block extension to C
822 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
823 // they are unused.
824 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
825 // (modulo the platform support).
826
827 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
828 // signed integer arithmetic overflows.
829
830 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
831 // may be ripped out at any time.
832
833 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000834 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000835 // defined.
836 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
837 // opposed to __DYNAMIC__).
838 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
839
840 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
841 // used (instead of C99 semantics).
842 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000843 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
844 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000845 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
846 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000847 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000848 Record.push_back(LangOpts.getGCMode());
849 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000850 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000851 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000852 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000853 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000854 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000855 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000856}
857
Douglas Gregora7f71a92009-04-10 03:52:48 +0000858//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000859// stat cache Serialization
860//===----------------------------------------------------------------------===//
861
862namespace {
863// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000864class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000865public:
866 typedef const char * key_type;
867 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000868
Douglas Gregorc5046832009-04-27 18:38:38 +0000869 typedef std::pair<int, struct stat> data_type;
870 typedef const data_type& data_type_ref;
871
872 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000873 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875
876 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000877 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
878 data_type_ref Data) {
879 unsigned StrLen = strlen(path);
880 clang::io::Emit16(Out, StrLen);
881 unsigned DataLen = 1; // result value
882 if (Data.first == 0)
883 DataLen += 4 + 4 + 2 + 8 + 8;
884 clang::io::Emit8(Out, DataLen);
885 return std::make_pair(StrLen + 1, DataLen);
886 }
Mike Stump11289f42009-09-09 15:08:12 +0000887
Douglas Gregorc5046832009-04-27 18:38:38 +0000888 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
889 Out.write(path, KeyLen);
890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Douglas Gregorc5046832009-04-27 18:38:38 +0000892 void EmitData(llvm::raw_ostream& Out, key_type_ref,
893 data_type_ref Data, unsigned DataLen) {
894 using namespace clang::io;
895 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000896
Douglas Gregorc5046832009-04-27 18:38:38 +0000897 // Result of stat()
898 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000899
Douglas Gregorc5046832009-04-27 18:38:38 +0000900 if (Data.first == 0) {
901 Emit32(Out, (uint32_t) Data.second.st_ino);
902 Emit32(Out, (uint32_t) Data.second.st_dev);
903 Emit16(Out, (uint16_t) Data.second.st_mode);
904 Emit64(Out, (uint64_t) Data.second.st_mtime);
905 Emit64(Out, (uint64_t) Data.second.st_size);
906 }
907
908 assert(Out.tell() - Start == DataLen && "Wrong data length");
909 }
910};
911} // end anonymous namespace
912
913/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000914void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
915 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000916 // Build the on-disk hash table containing information about every
917 // stat() call.
918 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
919 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000920 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000921 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000922 Stat != StatEnd; ++Stat, ++NumStatEntries) {
923 const char *Filename = Stat->first();
924 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
925 Generator.insert(Filename, Stat->second);
926 }
Mike Stump11289f42009-09-09 15:08:12 +0000927
Douglas Gregorc5046832009-04-27 18:38:38 +0000928 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000929 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000930 uint32_t BucketOffset;
931 {
932 llvm::raw_svector_ostream Out(StatCacheData);
933 // Make sure that no bucket is at offset 0
934 clang::io::Emit32(Out, 0);
935 BucketOffset = Generator.Emit(Out);
936 }
937
938 // Create a blob abbreviation
939 using namespace llvm;
940 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
941 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
945 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
946
947 // Write the stat cache
948 RecordData Record;
949 Record.push_back(pch::STAT_CACHE);
950 Record.push_back(BucketOffset);
951 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000952 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000953}
954
955//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000956// Source Manager Serialization
957//===----------------------------------------------------------------------===//
958
959/// \brief Create an abbreviation for the SLocEntry that refers to a
960/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000961static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000962 using namespace llvm;
963 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
964 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +0000969 // FileEntry fields.
970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000972 // HeaderFileInfo fields.
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +0000977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000978 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000979}
980
981/// \brief Create an abbreviation for the SLocEntry that refers to a
982/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000983static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000984 using namespace llvm;
985 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
986 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
987 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000992 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000993}
994
995/// \brief Create an abbreviation for the SLocEntry that refers to a
996/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000997static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000998 using namespace llvm;
999 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1000 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001002 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001003}
1004
1005/// \brief Create an abbreviation for the SLocEntry that refers to an
1006/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001007static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001008 using namespace llvm;
1009 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1010 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001016 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001017}
1018
1019/// \brief Writes the block containing the serialized form of the
1020/// source manager.
1021///
1022/// TODO: We should probably use an on-disk hash table (stored in a
1023/// blob), indexed based on the file name, so that we only create
1024/// entries for files that we actually need. In the common case (no
1025/// errors), we probably won't have to create file entries for any of
1026/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001027void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001028 const Preprocessor &PP,
1029 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001030 RecordData Record;
1031
Chris Lattner0910e3b2009-04-10 17:16:57 +00001032 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001033 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001034
1035 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001036 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1037 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1038 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1039 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001040
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001041 // Write the line table.
1042 if (SourceMgr.hasLineTable()) {
1043 LineTableInfo &LineTable = SourceMgr.getLineTable();
1044
1045 // Emit the file names
1046 Record.push_back(LineTable.getNumFilenames());
1047 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1048 // Emit the file name
1049 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001050 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001051 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1052 Record.push_back(FilenameLen);
1053 if (FilenameLen)
1054 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1055 }
Mike Stump11289f42009-09-09 15:08:12 +00001056
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001057 // Emit the line entries
1058 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1059 L != LEnd; ++L) {
1060 // Emit the file ID
1061 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001062
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001063 // Emit the line entries
1064 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001065 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001066 LEEnd = L->second.end();
1067 LE != LEEnd; ++LE) {
1068 Record.push_back(LE->FileOffset);
1069 Record.push_back(LE->LineNo);
1070 Record.push_back(LE->FilenameID);
1071 Record.push_back((unsigned)LE->FileKind);
1072 Record.push_back(LE->IncludeOffset);
1073 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001074 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +00001075 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001076 }
1077
Douglas Gregor258ae542009-04-27 06:38:32 +00001078 // Write out the source location entry table. We skip the first
1079 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001080 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001081 RecordData PreloadSLocs;
1082 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001083 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1084 // Get this source location entry.
1085 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001086
Douglas Gregor258ae542009-04-27 06:38:32 +00001087 // Record the offset of this source-location entry.
1088 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1089
1090 // Figure out which record code to use.
1091 unsigned Code;
1092 if (SLoc->isFile()) {
1093 if (SLoc->getFile().getContentCache()->Entry)
1094 Code = pch::SM_SLOC_FILE_ENTRY;
1095 else
1096 Code = pch::SM_SLOC_BUFFER_ENTRY;
1097 } else
1098 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1099 Record.clear();
1100 Record.push_back(Code);
1101
1102 Record.push_back(SLoc->getOffset());
1103 if (SLoc->isFile()) {
1104 const SrcMgr::FileInfo &File = SLoc->getFile();
1105 Record.push_back(File.getIncludeLoc().getRawEncoding());
1106 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1107 Record.push_back(File.hasLineDirectives());
1108
1109 const SrcMgr::ContentCache *Content = File.getContentCache();
1110 if (Content->Entry) {
1111 // The source location entry is a file. The blob associated
1112 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001113
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001114 // Emit size/modification time for this file.
1115 Record.push_back(Content->Entry->getSize());
1116 Record.push_back(Content->Entry->getModificationTime());
1117
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001118 // Emit header-search information associated with this file.
1119 HeaderFileInfo HFI;
1120 HeaderSearch &HS = PP.getHeaderSearchInfo();
1121 if (Content->Entry->getUID() < HS.header_file_size())
1122 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1123 Record.push_back(HFI.isImport);
1124 Record.push_back(HFI.DirInfo);
1125 Record.push_back(HFI.NumIncludes);
1126 AddIdentifierRef(HFI.ControllingMacro, Record);
1127
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001128 // Turn the file name into an absolute path, if it isn't already.
1129 const char *Filename = Content->Entry->getName();
1130 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001131 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001132 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001133
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001134 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001135 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001136
1137 // FIXME: For now, preload all file source locations, so that
1138 // we get the appropriate File entries in the reader. This is
1139 // a temporary measure.
1140 PreloadSLocs.push_back(SLocEntryOffsets.size());
1141 } else {
1142 // The source location entry is a buffer. The blob associated
1143 // with this entry contains the contents of the buffer.
1144
1145 // We add one to the size so that we capture the trailing NULL
1146 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1147 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001148 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001149 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001150 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001151 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1152 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001153 Record.clear();
1154 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1155 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001156 llvm::StringRef(Buffer->getBufferStart(),
1157 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001158
1159 if (strcmp(Name, "<built-in>") == 0)
1160 PreloadSLocs.push_back(SLocEntryOffsets.size());
1161 }
1162 } else {
1163 // The source location entry is an instantiation.
1164 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1165 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1166 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1167 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1168
1169 // Compute the token length for this macro expansion.
1170 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001171 if (I + 1 != N)
1172 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001173 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1174 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1175 }
1176 }
1177
Douglas Gregor8f45df52009-04-16 22:23:12 +00001178 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001179
1180 if (SLocEntryOffsets.empty())
1181 return;
1182
1183 // Write the source-location offsets table into the PCH block. This
1184 // table is used for lazily loading source-location information.
1185 using namespace llvm;
1186 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1187 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1191 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001192
Douglas Gregor258ae542009-04-27 06:38:32 +00001193 Record.clear();
1194 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1195 Record.push_back(SLocEntryOffsets.size());
1196 Record.push_back(SourceMgr.getNextOffset());
1197 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001198 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001199 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001200
1201 // Write the source location entry preloads array, telling the PCH
1202 // reader which source locations entries it should load eagerly.
1203 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001204}
1205
Douglas Gregorc5046832009-04-27 18:38:38 +00001206//===----------------------------------------------------------------------===//
1207// Preprocessor Serialization
1208//===----------------------------------------------------------------------===//
1209
Chris Lattnereeffaef2009-04-10 17:15:23 +00001210/// \brief Writes the block containing the serialized form of the
1211/// preprocessor.
1212///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001213void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001214 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001215
Chris Lattner0af3ba12009-04-13 01:29:17 +00001216 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1217 if (PP.getCounterValue() != 0) {
1218 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001219 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001220 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001221 }
1222
1223 // Enter the preprocessor block.
1224 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001225
Douglas Gregoreda6a892009-04-26 00:07:37 +00001226 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1227 // FIXME: use diagnostics subsystem for localization etc.
1228 if (PP.SawDateOrTime())
1229 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001230
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001231 // Loop over all the macro definitions that are live at the end of the file,
1232 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001233 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001234 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1235 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001236 // FIXME: This emits macros in hash table order, we should do it in a stable
1237 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001238 MacroInfo *MI = I->second;
1239
1240 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1241 // been redefined by the header (in which case they are not isBuiltinMacro).
1242 if (MI->isBuiltinMacro())
1243 continue;
1244
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001245 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001246 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001247 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1248 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001249
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001250 unsigned Code;
1251 if (MI->isObjectLike()) {
1252 Code = pch::PP_MACRO_OBJECT_LIKE;
1253 } else {
1254 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001255
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001256 Record.push_back(MI->isC99Varargs());
1257 Record.push_back(MI->isGNUVarargs());
1258 Record.push_back(MI->getNumArgs());
1259 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1260 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001261 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001262 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001263
1264 // If we have a detailed preprocessing record, record the macro definition
1265 // ID that corresponds to this macro.
1266 if (PPRec)
1267 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1268
Douglas Gregor8f45df52009-04-16 22:23:12 +00001269 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001270 Record.clear();
1271
Chris Lattner2199f5b2009-04-10 18:08:30 +00001272 // Emit the tokens array.
1273 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1274 // Note that we know that the preprocessor does not have any annotation
1275 // tokens in it because they are created by the parser, and thus can't be
1276 // in a macro definition.
1277 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001278
Chris Lattner2199f5b2009-04-10 18:08:30 +00001279 Record.push_back(Tok.getLocation().getRawEncoding());
1280 Record.push_back(Tok.getLength());
1281
Chris Lattner2199f5b2009-04-10 18:08:30 +00001282 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1283 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001284 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001285
Chris Lattner2199f5b2009-04-10 18:08:30 +00001286 // FIXME: Should translate token kind to a stable encoding.
1287 Record.push_back(Tok.getKind());
1288 // FIXME: Should translate token flags to a stable encoding.
1289 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001290
Douglas Gregor8f45df52009-04-16 22:23:12 +00001291 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001292 Record.clear();
1293 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001294 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001295 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001296
1297 // If the preprocessor has a preprocessing record, emit it.
1298 unsigned NumPreprocessingRecords = 0;
1299 if (PPRec) {
1300 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1301 E != EEnd; ++E) {
1302 Record.clear();
1303
1304 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1305 Record.push_back(NumPreprocessingRecords++);
1306 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1307 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1308 AddIdentifierRef(MI->getName(), Record);
1309 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1310 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1311 continue;
1312 }
1313
1314 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1315 // Record this macro definition's location.
1316 pch::IdentID ID = getMacroDefinitionID(MD);
1317 if (ID != MacroDefinitionOffsets.size()) {
1318 if (ID > MacroDefinitionOffsets.size())
1319 MacroDefinitionOffsets.resize(ID + 1);
1320
1321 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1322 } else
1323 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1324
1325 Record.push_back(NumPreprocessingRecords++);
1326 Record.push_back(ID);
1327 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1328 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1329 AddIdentifierRef(MD->getName(), Record);
1330 AddSourceLocation(MD->getLocation(), Record);
1331 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1332 continue;
1333 }
1334 }
1335 }
1336
Douglas Gregor8f45df52009-04-16 22:23:12 +00001337 Stream.ExitBlock();
Douglas Gregoraae92242010-03-19 21:51:54 +00001338
1339 // Write the offsets table for the preprocessing record.
1340 if (NumPreprocessingRecords > 0) {
1341 // Write the offsets table for identifier IDs.
1342 using namespace llvm;
1343 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1344 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1348 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1349
1350 Record.clear();
1351 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1352 Record.push_back(NumPreprocessingRecords);
1353 Record.push_back(MacroDefinitionOffsets.size());
1354 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1355 (const char *)&MacroDefinitionOffsets.front(),
1356 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1357 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001358}
1359
Douglas Gregorc5046832009-04-27 18:38:38 +00001360//===----------------------------------------------------------------------===//
1361// Type Serialization
1362//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001363
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001364/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001365void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001366 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001367 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001368 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001369
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001370 // Record the offset for this type.
1371 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001372 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001373 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1374 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001375 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001376 }
1377
1378 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001379
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001380 // Emit the type's representation.
1381 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001382
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001383 if (T.hasLocalNonFastQualifiers()) {
1384 Qualifiers Qs = T.getLocalQualifiers();
1385 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001386 Record.push_back(Qs.getAsOpaqueValue());
1387 W.Code = pch::TYPE_EXT_QUAL;
1388 } else {
1389 switch (T->getTypeClass()) {
1390 // For all of the concrete, non-dependent types, call the
1391 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001392#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001393 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001394#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001395#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001396 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001397 }
1398
1399 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001400 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001401
1402 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001403 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001404}
1405
Douglas Gregorc5046832009-04-27 18:38:38 +00001406//===----------------------------------------------------------------------===//
1407// Declaration Serialization
1408//===----------------------------------------------------------------------===//
1409
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001410/// \brief Write the block containing all of the declaration IDs
1411/// lexically declared within the given DeclContext.
1412///
1413/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1414/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001415uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001416 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001417 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001418 return 0;
1419
Douglas Gregor8f45df52009-04-16 22:23:12 +00001420 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001421 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001422 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1423 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001424 AddDeclRef(*D, Record);
1425
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001426 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001427 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001428 return Offset;
1429}
1430
1431/// \brief Write the block containing all of the declaration IDs
1432/// visible from the given DeclContext.
1433///
1434/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1435/// bistream, or 0 if no block was written.
1436uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1437 DeclContext *DC) {
1438 if (DC->getPrimaryContext() != DC)
1439 return 0;
1440
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001441 // Since there is no name lookup into functions or methods, and we
1442 // perform name lookup for the translation unit via the
1443 // IdentifierInfo chains, don't bother to build a
1444 // visible-declarations table for these entities.
1445 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001446 return 0;
1447
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001448 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001449 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001450
1451 // Serialize the contents of the mapping used for lookup. Note that,
1452 // although we have two very different code paths, the serialized
1453 // representation is the same for both cases: a declaration name,
1454 // followed by a size, followed by references to the visible
1455 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001456 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001457 RecordData Record;
1458 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001459 if (!Map)
1460 return 0;
1461
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001462 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1463 D != DEnd; ++D) {
1464 AddDeclarationName(D->first, Record);
1465 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1466 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001467 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001468 AddDeclRef(*Result.first, Record);
1469 }
1470
1471 if (Record.size() == 0)
1472 return 0;
1473
Douglas Gregor8f45df52009-04-16 22:23:12 +00001474 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001475 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001476 return Offset;
1477}
1478
Douglas Gregorc5046832009-04-27 18:38:38 +00001479//===----------------------------------------------------------------------===//
1480// Global Method Pool and Selector Serialization
1481//===----------------------------------------------------------------------===//
1482
Douglas Gregore84a9da2009-04-20 20:36:09 +00001483namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001484// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001485class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001486 PCHWriter &Writer;
1487
1488public:
1489 typedef Selector key_type;
1490 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001491
Douglas Gregorc78d3462009-04-24 21:10:55 +00001492 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1493 typedef const data_type& data_type_ref;
1494
1495 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001496
Douglas Gregorc78d3462009-04-24 21:10:55 +00001497 static unsigned ComputeHash(Selector Sel) {
1498 unsigned N = Sel.getNumArgs();
1499 if (N == 0)
1500 ++N;
1501 unsigned R = 5381;
1502 for (unsigned I = 0; I != N; ++I)
1503 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001504 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001505 return R;
1506 }
Mike Stump11289f42009-09-09 15:08:12 +00001507
1508 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001509 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1510 data_type_ref Methods) {
1511 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1512 clang::io::Emit16(Out, KeyLen);
1513 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001514 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001515 Method = Method->Next)
1516 if (Method->Method)
1517 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001518 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001519 Method = Method->Next)
1520 if (Method->Method)
1521 DataLen += 4;
1522 clang::io::Emit16(Out, DataLen);
1523 return std::make_pair(KeyLen, DataLen);
1524 }
Mike Stump11289f42009-09-09 15:08:12 +00001525
Douglas Gregor95c13f52009-04-25 17:48:32 +00001526 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001527 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001528 assert((Start >> 32) == 0 && "Selector key offset too large");
1529 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001530 unsigned N = Sel.getNumArgs();
1531 clang::io::Emit16(Out, N);
1532 if (N == 0)
1533 N = 1;
1534 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001535 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001536 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1537 }
Mike Stump11289f42009-09-09 15:08:12 +00001538
Douglas Gregorc78d3462009-04-24 21:10:55 +00001539 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001540 data_type_ref Methods, unsigned DataLen) {
1541 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001542 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001543 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001544 Method = Method->Next)
1545 if (Method->Method)
1546 ++NumInstanceMethods;
1547
1548 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001549 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001550 Method = Method->Next)
1551 if (Method->Method)
1552 ++NumFactoryMethods;
1553
1554 clang::io::Emit16(Out, NumInstanceMethods);
1555 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001556 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001557 Method = Method->Next)
1558 if (Method->Method)
1559 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001560 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001561 Method = Method->Next)
1562 if (Method->Method)
1563 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001564
1565 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001566 }
1567};
1568} // end anonymous namespace
1569
1570/// \brief Write the method pool into the PCH file.
1571///
1572/// The method pool contains both instance and factory methods, stored
1573/// in an on-disk hash table indexed by the selector.
1574void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1575 using namespace llvm;
1576
1577 // Create and write out the blob that contains the instance and
1578 // factor method pools.
1579 bool Empty = true;
1580 {
1581 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001582
Douglas Gregorc78d3462009-04-24 21:10:55 +00001583 // Create the on-disk hash table representation. Start by
1584 // iterating through the instance method pool.
1585 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001586 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001587 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001588 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001589 InstanceEnd = SemaRef.InstanceMethodPool.end();
1590 Instance != InstanceEnd; ++Instance) {
1591 // Check whether there is a factory method with the same
1592 // selector.
1593 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1594 = SemaRef.FactoryMethodPool.find(Instance->first);
1595
1596 if (Factory == SemaRef.FactoryMethodPool.end())
1597 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001598 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001599 ObjCMethodList()));
1600 else
1601 Generator.insert(Instance->first,
1602 std::make_pair(Instance->second, Factory->second));
1603
Douglas Gregor95c13f52009-04-25 17:48:32 +00001604 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001605 Empty = false;
1606 }
1607
1608 // Now iterate through the factory method pool, to pick up any
1609 // selectors that weren't already in the instance method pool.
1610 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001611 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001612 FactoryEnd = SemaRef.FactoryMethodPool.end();
1613 Factory != FactoryEnd; ++Factory) {
1614 // Check whether there is an instance method with the same
1615 // selector. If so, there is no work to do here.
1616 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1617 = SemaRef.InstanceMethodPool.find(Factory->first);
1618
Douglas Gregor95c13f52009-04-25 17:48:32 +00001619 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001620 Generator.insert(Factory->first,
1621 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001622 ++NumSelectorsInMethodPool;
1623 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001624
1625 Empty = false;
1626 }
1627
Douglas Gregor95c13f52009-04-25 17:48:32 +00001628 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001629 return;
1630
1631 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001632 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001633 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001634 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001635 {
1636 PCHMethodPoolTrait Trait(*this);
1637 llvm::raw_svector_ostream Out(MethodPool);
1638 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001639 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001640 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001641
1642 // For every selector that we have seen but which was not
1643 // written into the hash table, write the selector itself and
1644 // record it's offset.
1645 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1646 if (SelectorOffsets[I] == 0)
1647 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001648 }
1649
1650 // Create a blob abbreviation
1651 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1652 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1653 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1656 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1657
Douglas Gregor95c13f52009-04-25 17:48:32 +00001658 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001659 RecordData Record;
1660 Record.push_back(pch::METHOD_POOL);
1661 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001662 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001663 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001664
1665 // Create a blob abbreviation for the selector table offsets.
1666 Abbrev = new BitCodeAbbrev();
1667 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1668 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1669 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1670 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1671
1672 // Write the selector offsets table.
1673 Record.clear();
1674 Record.push_back(pch::SELECTOR_OFFSETS);
1675 Record.push_back(SelectorOffsets.size());
1676 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1677 (const char *)&SelectorOffsets.front(),
1678 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001679 }
1680}
1681
Douglas Gregorc5046832009-04-27 18:38:38 +00001682//===----------------------------------------------------------------------===//
1683// Identifier Table Serialization
1684//===----------------------------------------------------------------------===//
1685
Douglas Gregorc78d3462009-04-24 21:10:55 +00001686namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001687class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001688 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001689 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001690
Douglas Gregor1d583f22009-04-28 21:18:29 +00001691 /// \brief Determines whether this is an "interesting" identifier
1692 /// that needs a full IdentifierInfo structure written into the hash
1693 /// table.
1694 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1695 return II->isPoisoned() ||
1696 II->isExtensionToken() ||
1697 II->hasMacroDefinition() ||
1698 II->getObjCOrBuiltinID() ||
1699 II->getFETokenInfo<void>();
1700 }
1701
Douglas Gregore84a9da2009-04-20 20:36:09 +00001702public:
1703 typedef const IdentifierInfo* key_type;
1704 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001705
Douglas Gregore84a9da2009-04-20 20:36:09 +00001706 typedef pch::IdentID data_type;
1707 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001708
1709 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001710 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001711
1712 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001713 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001714 }
Mike Stump11289f42009-09-09 15:08:12 +00001715
1716 std::pair<unsigned,unsigned>
1717 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001718 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001719 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001720 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1721 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001722 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001723 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001724 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001725 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001726 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1727 DEnd = IdentifierResolver::end();
1728 D != DEnd; ++D)
1729 DataLen += sizeof(pch::DeclID);
1730 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001731 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001732 // We emit the key length after the data length so that every
1733 // string is preceded by a 16-bit length. This matches the PTH
1734 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001735 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001736 return std::make_pair(KeyLen, DataLen);
1737 }
Mike Stump11289f42009-09-09 15:08:12 +00001738
1739 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001740 unsigned KeyLen) {
1741 // Record the location of the key data. This is used when generating
1742 // the mapping from persistent IDs to strings.
1743 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001744 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001745 }
Mike Stump11289f42009-09-09 15:08:12 +00001746
1747 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001748 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001749 if (!isInterestingIdentifier(II)) {
1750 clang::io::Emit32(Out, ID << 1);
1751 return;
1752 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001753
Douglas Gregor1d583f22009-04-28 21:18:29 +00001754 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001755 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001756 bool hasMacroDefinition =
1757 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001758 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001759 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001760 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1761 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1762 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1763 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001764 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001765
Douglas Gregorc3366a52009-04-21 23:56:24 +00001766 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001767 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001768
Douglas Gregora868bbd2009-04-21 22:25:48 +00001769 // Emit the declaration IDs in reverse order, because the
1770 // IdentifierResolver provides the declarations as they would be
1771 // visible (e.g., the function "stat" would come before the struct
1772 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1773 // adds declarations to the end of the list (so we need to see the
1774 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001775 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001776 IdentifierResolver::end());
1777 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1778 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001779 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001780 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001781 }
1782};
1783} // end anonymous namespace
1784
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001785/// \brief Write the identifier table into the PCH file.
1786///
1787/// The identifier table consists of a blob containing string data
1788/// (the actual identifiers themselves) and a separate "offsets" index
1789/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001790void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001791 using namespace llvm;
1792
1793 // Create and write out the blob that contains the identifier
1794 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001795 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001796 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001797
Douglas Gregore6648fb2009-04-28 20:33:11 +00001798 // Look for any identifiers that were named while processing the
1799 // headers, but are otherwise not needed. We add these to the hash
1800 // table to enable checking of the predefines buffer in the case
1801 // where the user adds new macro definitions when building the PCH
1802 // file.
1803 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1804 IDEnd = PP.getIdentifierTable().end();
1805 ID != IDEnd; ++ID)
1806 getIdentifierRef(ID->second);
1807
Douglas Gregore84a9da2009-04-20 20:36:09 +00001808 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001809 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001810 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1811 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1812 ID != IDEnd; ++ID) {
1813 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001814 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001815 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001816
Douglas Gregore84a9da2009-04-20 20:36:09 +00001817 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001818 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001819 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001820 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001821 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001822 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001823 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001824 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001825 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001826 }
1827
1828 // Create a blob abbreviation
1829 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1830 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001833 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001834
1835 // Write the identifier table
1836 RecordData Record;
1837 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001838 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001839 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001840 }
1841
1842 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001843 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1844 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1847 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1848
1849 RecordData Record;
1850 Record.push_back(pch::IDENTIFIER_OFFSET);
1851 Record.push_back(IdentifierOffsets.size());
1852 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1853 (const char *)&IdentifierOffsets.front(),
1854 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001855}
1856
Douglas Gregorc5046832009-04-27 18:38:38 +00001857//===----------------------------------------------------------------------===//
1858// General Serialization Routines
1859//===----------------------------------------------------------------------===//
1860
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001861/// \brief Write a record containing the given attributes.
1862void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1863 RecordData Record;
1864 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001865 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001866 Record.push_back(Attr->isInherited());
1867 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001868 default:
1869 assert(0 && "Does not support PCH writing for this attribute yet!");
1870 break;
Alexis Hunt344393e2010-06-16 23:43:53 +00001871 case attr::Alias:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001872 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1873 break;
1874
Alexis Hunt344393e2010-06-16 23:43:53 +00001875 case attr::AlignMac68k:
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00001876 break;
1877
Alexis Hunt344393e2010-06-16 23:43:53 +00001878 case attr::Aligned:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001879 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1880 break;
1881
Alexis Hunt344393e2010-06-16 23:43:53 +00001882 case attr::AlwaysInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001883 break;
Mike Stump11289f42009-09-09 15:08:12 +00001884
Alexis Hunt344393e2010-06-16 23:43:53 +00001885 case attr::AnalyzerNoReturn:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001886 break;
1887
Alexis Hunt344393e2010-06-16 23:43:53 +00001888 case attr::Annotate:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001889 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1890 break;
1891
Alexis Hunt344393e2010-06-16 23:43:53 +00001892 case attr::AsmLabel:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001893 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1894 break;
1895
Alexis Hunt344393e2010-06-16 23:43:53 +00001896 case attr::BaseCheck:
Alexis Hunt54a02542009-11-25 04:20:27 +00001897 break;
1898
Alexis Hunt344393e2010-06-16 23:43:53 +00001899 case attr::Blocks:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001900 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1901 break;
1902
Alexis Hunt344393e2010-06-16 23:43:53 +00001903 case attr::CDecl:
Eli Friedmane4310c82009-11-09 18:38:53 +00001904 break;
1905
Alexis Hunt344393e2010-06-16 23:43:53 +00001906 case attr::Cleanup:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001907 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1908 break;
1909
Alexis Hunt344393e2010-06-16 23:43:53 +00001910 case attr::Const:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001911 break;
1912
Alexis Hunt344393e2010-06-16 23:43:53 +00001913 case attr::Constructor:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001914 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1915 break;
1916
Alexis Hunt344393e2010-06-16 23:43:53 +00001917 case attr::DLLExport:
1918 case attr::DLLImport:
1919 case attr::Deprecated:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001920 break;
1921
Alexis Hunt344393e2010-06-16 23:43:53 +00001922 case attr::Destructor:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001923 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1924 break;
1925
Alexis Hunt344393e2010-06-16 23:43:53 +00001926 case attr::FastCall:
1927 case attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001928 break;
1929
Alexis Hunt344393e2010-06-16 23:43:53 +00001930 case attr::Format: {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001931 const FormatAttr *Format = cast<FormatAttr>(Attr);
1932 AddString(Format->getType(), Record);
1933 Record.push_back(Format->getFormatIdx());
1934 Record.push_back(Format->getFirstArg());
1935 break;
1936 }
1937
Alexis Hunt344393e2010-06-16 23:43:53 +00001938 case attr::FormatArg: {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001939 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1940 Record.push_back(Format->getFormatIdx());
1941 break;
1942 }
1943
Alexis Hunt344393e2010-06-16 23:43:53 +00001944 case attr::Sentinel : {
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001945 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1946 Record.push_back(Sentinel->getSentinel());
1947 Record.push_back(Sentinel->getNullPos());
1948 break;
1949 }
Mike Stump11289f42009-09-09 15:08:12 +00001950
Alexis Hunt344393e2010-06-16 23:43:53 +00001951 case attr::GNUInline:
1952 case attr::Hiding:
1953 case attr::IBAction:
1954 case attr::IBOutlet:
1955 case attr::Malloc:
1956 case attr::NoDebug:
1957 case attr::NoInline:
1958 case attr::NoReturn:
1959 case attr::NoThrow:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001960 break;
1961
Alexis Hunt344393e2010-06-16 23:43:53 +00001962 case attr::IBOutletCollection: {
Ted Kremenek26bde772010-05-19 17:38:06 +00001963 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
1964 AddDeclRef(ICA->getClass(), Record);
1965 break;
1966 }
1967
Alexis Hunt344393e2010-06-16 23:43:53 +00001968 case attr::NonNull: {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001969 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1970 Record.push_back(NonNull->size());
1971 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1972 break;
1973 }
1974
Alexis Hunt344393e2010-06-16 23:43:53 +00001975 case attr::CFReturnsNotRetained:
1976 case attr::CFReturnsRetained:
1977 case attr::NSReturnsNotRetained:
1978 case attr::NSReturnsRetained:
1979 case attr::ObjCException:
1980 case attr::ObjCNSObject:
1981 case attr::Overloadable:
1982 case attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001983 break;
1984
Alexis Hunt344393e2010-06-16 23:43:53 +00001985 case attr::MaxFieldAlignment:
Daniel Dunbar40130442010-05-27 01:12:46 +00001986 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001987 break;
1988
Alexis Hunt344393e2010-06-16 23:43:53 +00001989 case attr::Packed:
Anders Carlsson68e0b682009-08-08 18:23:56 +00001990 break;
Mike Stump11289f42009-09-09 15:08:12 +00001991
Alexis Hunt344393e2010-06-16 23:43:53 +00001992 case attr::Pure:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001993 break;
1994
Alexis Hunt344393e2010-06-16 23:43:53 +00001995 case attr::Regparm:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001996 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1997 break;
Mike Stump11289f42009-09-09 15:08:12 +00001998
Alexis Hunt344393e2010-06-16 23:43:53 +00001999 case attr::ReqdWorkGroupSize:
Nate Begemanf2758702009-06-26 06:32:41 +00002000 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2001 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2002 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2003 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002004
Alexis Hunt344393e2010-06-16 23:43:53 +00002005 case attr::Section:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002006 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2007 break;
2008
Alexis Hunt344393e2010-06-16 23:43:53 +00002009 case attr::StdCall:
2010 case attr::TransparentUnion:
2011 case attr::Unavailable:
2012 case attr::Unused:
2013 case attr::Used:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002014 break;
2015
Alexis Hunt344393e2010-06-16 23:43:53 +00002016 case attr::Visibility:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002017 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00002018 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002019 break;
2020
Alexis Hunt344393e2010-06-16 23:43:53 +00002021 case attr::WarnUnusedResult:
2022 case attr::Weak:
2023 case attr::WeakRef:
2024 case attr::WeakImport:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002025 break;
2026 }
2027 }
2028
Douglas Gregor8f45df52009-04-16 22:23:12 +00002029 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002030}
2031
2032void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2033 Record.push_back(Str.size());
2034 Record.insert(Record.end(), Str.begin(), Str.end());
2035}
2036
Douglas Gregore84a9da2009-04-20 20:36:09 +00002037/// \brief Note that the identifier II occurs at the given offset
2038/// within the identifier table.
2039void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00002040 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002041}
2042
Douglas Gregor95c13f52009-04-25 17:48:32 +00002043/// \brief Note that the selector Sel occurs at the given offset
2044/// within the method pool/selector table.
2045void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2046 unsigned ID = SelectorIDs[Sel];
2047 assert(ID && "Unknown selector");
2048 SelectorOffsets[ID - 1] = Offset;
2049}
2050
Mike Stump11289f42009-09-09 15:08:12 +00002051PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2052 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002053 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2054 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002055
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002056void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2057 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00002058 using namespace llvm;
2059
Douglas Gregor162dd022009-04-20 15:53:59 +00002060 ASTContext &Context = SemaRef.Context;
2061 Preprocessor &PP = SemaRef.PP;
2062
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002063 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002064 Stream.Emit((unsigned)'C', 8);
2065 Stream.Emit((unsigned)'P', 8);
2066 Stream.Emit((unsigned)'C', 8);
2067 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002068
Chris Lattner28fa4e62009-04-26 22:26:21 +00002069 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002070
2071 // The translation unit is the first declaration we'll emit.
2072 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002073 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002074
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002075 // Make sure that we emit IdentifierInfos (and any attached
2076 // declarations) for builtins.
2077 {
2078 IdentifierTable &Table = PP.getIdentifierTable();
2079 llvm::SmallVector<const char *, 32> BuiltinNames;
2080 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2081 Context.getLangOptions().NoBuiltin);
2082 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2083 getIdentifierRef(&Table.get(BuiltinNames[I]));
2084 }
2085
Chris Lattner0c797362009-09-08 18:19:27 +00002086 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002087 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002088 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002089 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002090 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2091 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002092 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002093
Tanya Lattner90073802010-02-12 00:07:30 +00002094 // Build a record containing all of the static unused functions in this file.
2095 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002096 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattner90073802010-02-12 00:07:30 +00002097 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002098
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002099 // Build a record containing all of the locally-scoped external
2100 // declarations in this header file. Generally, this record will be
2101 // empty.
2102 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00002103 // FIXME: This is filling in the PCH file in densemap order which is
2104 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002105 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002106 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2107 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2108 TD != TDEnd; ++TD)
2109 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2110
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002111 // Build a record containing all of the ext_vector declarations.
2112 RecordData ExtVectorDecls;
2113 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2114 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2115
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002116 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002117 RecordData Record;
Douglas Gregoraae92242010-03-19 21:51:54 +00002118 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002119 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00002120 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002121 if (StatCalls && !isysroot)
2122 WriteStatCache(*StatCalls, isysroot);
2123 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002124 // Write the record of special types.
2125 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002126
Steve Naroffc277ad12009-07-18 15:33:26 +00002127 AddTypeRef(Context.getBuiltinVaListType(), Record);
2128 AddTypeRef(Context.getObjCIdType(), Record);
2129 AddTypeRef(Context.getObjCSelType(), Record);
2130 AddTypeRef(Context.getObjCProtoType(), Record);
2131 AddTypeRef(Context.getObjCClassType(), Record);
2132 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2133 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2134 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002135 AddTypeRef(Context.getjmp_bufType(), Record);
2136 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002137 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2138 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002139 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002140 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002141 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2142 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002143 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002144
Douglas Gregor1970d882009-04-26 03:49:13 +00002145 // Keep writing types and declarations until all types and
2146 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002147 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2148 WriteDeclsBlockAbbrevs();
2149 while (!DeclTypesToEmit.empty()) {
2150 DeclOrType DOT = DeclTypesToEmit.front();
2151 DeclTypesToEmit.pop();
2152 if (DOT.isType())
2153 WriteType(DOT.getType());
2154 else
2155 WriteDecl(Context, DOT.getDecl());
2156 }
2157 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002158
Douglas Gregor45053152009-10-17 17:25:45 +00002159 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002160 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002161 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002162
2163 // Write the type offsets array
2164 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2165 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2168 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2169 Record.clear();
2170 Record.push_back(pch::TYPE_OFFSET);
2171 Record.push_back(TypeOffsets.size());
2172 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002173 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002174 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002175
Douglas Gregor745ed142009-04-25 18:35:21 +00002176 // Write the declaration offsets array
2177 Abbrev = new BitCodeAbbrev();
2178 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2181 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2182 Record.clear();
2183 Record.push_back(pch::DECL_OFFSET);
2184 Record.push_back(DeclOffsets.size());
2185 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002186 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002187 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002188
Douglas Gregord4df8652009-04-22 22:02:47 +00002189 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002190 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002191 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002192
2193 // Write the record containing tentative definitions.
2194 if (!TentativeDefinitions.empty())
2195 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002196
Tanya Lattner90073802010-02-12 00:07:30 +00002197 // Write the record containing unused static functions.
2198 if (!UnusedStaticFuncs.empty())
2199 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002200
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002201 // Write the record containing locally-scoped external definitions.
2202 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002203 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002204 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002205
2206 // Write the record containing ext_vector type names.
2207 if (!ExtVectorDecls.empty())
2208 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002209
Douglas Gregor08f01292009-04-17 22:13:46 +00002210 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002211 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002212 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002213 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002214 Record.push_back(NumLexicalDeclContexts);
2215 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002216 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002217 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002218}
2219
2220void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2221 Record.push_back(Loc.getRawEncoding());
2222}
2223
Chris Lattnerca025db2010-05-07 21:43:38 +00002224void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2225 AddSourceLocation(Range.getBegin(), Record);
2226 AddSourceLocation(Range.getEnd(), Record);
2227}
2228
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002229void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2230 Record.push_back(Value.getBitWidth());
2231 unsigned N = Value.getNumWords();
2232 const uint64_t* Words = Value.getRawData();
2233 for (unsigned I = 0; I != N; ++I)
2234 Record.push_back(Words[I]);
2235}
2236
Douglas Gregor1daeb692009-04-13 18:14:40 +00002237void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2238 Record.push_back(Value.isUnsigned());
2239 AddAPInt(Value, Record);
2240}
2241
Douglas Gregore0a3a512009-04-14 21:55:33 +00002242void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2243 AddAPInt(Value.bitcastToAPInt(), Record);
2244}
2245
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002246void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002247 Record.push_back(getIdentifierRef(II));
2248}
2249
2250pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2251 if (II == 0)
2252 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002253
2254 pch::IdentID &ID = IdentifierIDs[II];
2255 if (ID == 0)
2256 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002257 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002258}
2259
Douglas Gregoraae92242010-03-19 21:51:54 +00002260pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2261 if (MD == 0)
2262 return 0;
2263
2264 pch::IdentID &ID = MacroDefinitions[MD];
2265 if (ID == 0)
2266 ID = MacroDefinitions.size();
2267 return ID;
2268}
2269
Steve Naroff2ddea052009-04-23 10:39:46 +00002270void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2271 if (SelRef.getAsOpaquePtr() == 0) {
2272 Record.push_back(0);
2273 return;
2274 }
2275
2276 pch::SelectorID &SID = SelectorIDs[SelRef];
2277 if (SID == 0) {
2278 SID = SelectorIDs.size();
2279 SelVector.push_back(SelRef);
2280 }
2281 Record.push_back(SID);
2282}
2283
Chris Lattnercba86142010-05-10 00:25:06 +00002284void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2285 AddDeclRef(Temp->getDestructor(), Record);
2286}
2287
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002288void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2289 const TemplateArgumentLocInfo &Arg,
2290 RecordData &Record) {
2291 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002292 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002293 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002294 break;
2295 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002296 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002297 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002298 case TemplateArgument::Template:
2299 Record.push_back(
2300 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2301 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2302 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2303 break;
John McCall0ad16662009-10-29 08:12:44 +00002304 case TemplateArgument::Null:
2305 case TemplateArgument::Integral:
2306 case TemplateArgument::Declaration:
2307 case TemplateArgument::Pack:
2308 break;
2309 }
2310}
2311
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002312void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2313 RecordData &Record) {
2314 AddTemplateArgument(Arg.getArgument(), Record);
2315 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2316 Record);
2317}
2318
John McCallbcd03502009-12-07 02:54:59 +00002319void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2320 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002321 AddTypeRef(QualType(), Record);
2322 return;
2323 }
2324
John McCallbcd03502009-12-07 02:54:59 +00002325 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002326 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002327 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002328 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002329}
2330
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002331void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2332 if (T.isNull()) {
2333 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2334 return;
2335 }
2336
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002337 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002338 T.removeFastQualifiers();
2339
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002340 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002341 pch::TypeID &ID = TypeIDs[T];
2342 if (ID == 0) {
2343 // We haven't seen these qualifiers applied to this type before.
2344 // Assign it a new ID. This is the only time we enqueue a
2345 // qualified type, and it has no CV qualifiers.
2346 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002347 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002348 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002349
John McCall8ccfcb52009-09-24 19:53:00 +00002350 // Encode the type qualifiers in the type reference.
2351 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2352 return;
2353 }
2354
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002355 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002356
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002357 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002358 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002359 switch (BT->getKind()) {
2360 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2361 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2362 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2363 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2364 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2365 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2366 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2367 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002368 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002369 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2370 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2371 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2372 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2373 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2374 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2375 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002376 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002377 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2378 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2379 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002380 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002381 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2382 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002383 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2384 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002385 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2386 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002387 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002388 case BuiltinType::UndeducedAuto:
2389 assert(0 && "Should not see undeduced auto here");
2390 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002391 }
2392
John McCall8ccfcb52009-09-24 19:53:00 +00002393 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002394 return;
2395 }
2396
John McCall8ccfcb52009-09-24 19:53:00 +00002397 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002398 if (ID == 0) {
2399 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002400 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002401 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002402 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002403 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002404
2405 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002406 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002407}
2408
2409void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2410 if (D == 0) {
2411 Record.push_back(0);
2412 return;
2413 }
2414
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002415 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002416 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002417 // We haven't seen this declaration before. Give it a new ID and
2418 // enqueue it in the list of declarations to emit.
2419 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002420 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002421 }
2422
2423 Record.push_back(ID);
2424}
2425
Douglas Gregore84a9da2009-04-20 20:36:09 +00002426pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2427 if (D == 0)
2428 return 0;
2429
2430 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2431 return DeclIDs[D];
2432}
2433
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002434void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002435 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002436 Record.push_back(Name.getNameKind());
2437 switch (Name.getNameKind()) {
2438 case DeclarationName::Identifier:
2439 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2440 break;
2441
2442 case DeclarationName::ObjCZeroArgSelector:
2443 case DeclarationName::ObjCOneArgSelector:
2444 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002445 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002446 break;
2447
2448 case DeclarationName::CXXConstructorName:
2449 case DeclarationName::CXXDestructorName:
2450 case DeclarationName::CXXConversionFunctionName:
2451 AddTypeRef(Name.getCXXNameType(), Record);
2452 break;
2453
2454 case DeclarationName::CXXOperatorName:
2455 Record.push_back(Name.getCXXOverloadedOperator());
2456 break;
2457
Alexis Hunt3d221f22009-11-29 07:34:05 +00002458 case DeclarationName::CXXLiteralOperatorName:
2459 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2460 break;
2461
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002462 case DeclarationName::CXXUsingDirective:
2463 // No extra data to emit
2464 break;
2465 }
2466}
Chris Lattnerca025db2010-05-07 21:43:38 +00002467
2468void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2469 RecordData &Record) {
2470 // Nested name specifiers usually aren't too long. I think that 8 would
2471 // typically accomodate the vast majority.
2472 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2473
2474 // Push each of the NNS's onto a stack for serialization in reverse order.
2475 while (NNS) {
2476 NestedNames.push_back(NNS);
2477 NNS = NNS->getPrefix();
2478 }
2479
2480 Record.push_back(NestedNames.size());
2481 while(!NestedNames.empty()) {
2482 NNS = NestedNames.pop_back_val();
2483 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2484 Record.push_back(Kind);
2485 switch (Kind) {
2486 case NestedNameSpecifier::Identifier:
2487 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2488 break;
2489
2490 case NestedNameSpecifier::Namespace:
2491 AddDeclRef(NNS->getAsNamespace(), Record);
2492 break;
2493
2494 case NestedNameSpecifier::TypeSpec:
2495 case NestedNameSpecifier::TypeSpecWithTemplate:
2496 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2497 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2498 break;
2499
2500 case NestedNameSpecifier::Global:
2501 // Don't need to write an associated value.
2502 break;
2503 }
2504 }
2505}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002506
2507void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2508 TemplateName::NameKind Kind = Name.getKind();
2509 Record.push_back(Kind);
2510 switch (Kind) {
2511 case TemplateName::Template:
2512 AddDeclRef(Name.getAsTemplateDecl(), Record);
2513 break;
2514
2515 case TemplateName::OverloadedTemplate: {
2516 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2517 Record.push_back(OvT->size());
2518 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2519 I != E; ++I)
2520 AddDeclRef(*I, Record);
2521 break;
2522 }
2523
2524 case TemplateName::QualifiedTemplate: {
2525 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2526 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2527 Record.push_back(QualT->hasTemplateKeyword());
2528 AddDeclRef(QualT->getTemplateDecl(), Record);
2529 break;
2530 }
2531
2532 case TemplateName::DependentTemplate: {
2533 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2534 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2535 Record.push_back(DepT->isIdentifier());
2536 if (DepT->isIdentifier())
2537 AddIdentifierRef(DepT->getIdentifier(), Record);
2538 else
2539 Record.push_back(DepT->getOperator());
2540 break;
2541 }
2542 }
2543}
2544
2545void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2546 RecordData &Record) {
2547 Record.push_back(Arg.getKind());
2548 switch (Arg.getKind()) {
2549 case TemplateArgument::Null:
2550 break;
2551 case TemplateArgument::Type:
2552 AddTypeRef(Arg.getAsType(), Record);
2553 break;
2554 case TemplateArgument::Declaration:
2555 AddDeclRef(Arg.getAsDecl(), Record);
2556 break;
2557 case TemplateArgument::Integral:
2558 AddAPSInt(*Arg.getAsIntegral(), Record);
2559 AddTypeRef(Arg.getIntegralType(), Record);
2560 break;
2561 case TemplateArgument::Template:
2562 AddTemplateName(Arg.getAsTemplate(), Record);
2563 break;
2564 case TemplateArgument::Expression:
2565 AddStmt(Arg.getAsExpr());
2566 break;
2567 case TemplateArgument::Pack:
2568 Record.push_back(Arg.pack_size());
2569 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2570 I != E; ++I)
2571 AddTemplateArgument(*I, Record);
2572 break;
2573 }
2574}