blob: c256b4103a7dc4c6451c5ac005f319aaf4b9cccd [file] [log] [blame]
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
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"
24#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000031#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000037#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000038#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42// Type serialization
43//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000044
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000046 class PCHTypeWriter {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047 PCHWriter &Writer;
48 PCHWriter::RecordData &Record;
49
50 public:
51 /// \brief Type code that corresponds to the record generated.
52 pch::TypeCode Code;
53
Mike Stump11289f42009-09-09 15:08:12 +000054 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000055 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
57 void VisitArrayType(const ArrayType *T);
58 void VisitFunctionType(const FunctionType *T);
59 void VisitTagType(const TagType *T);
60
61#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
62#define ABSTRACT_TYPE(Class, Base)
63#define DEPENDENT_TYPE(Class, Base)
64#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());
John Thompson22334602010-02-05 00:12:22 +0000131 Record.push_back(T->isAltiVec());
132 Record.push_back(T->isPixel());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133 Code = pch::TYPE_VECTOR;
134}
135
136void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
137 VisitVectorType(T);
138 Code = pch::TYPE_EXT_VECTOR;
139}
140
141void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
142 Writer.AddTypeRef(T->getResultType(), Record);
Douglas Gregordc728752009-12-22 18:11:50 +0000143 Record.push_back(T->getNoReturnAttr());
Douglas Gregor8c940862010-01-18 17:14:39 +0000144 // FIXME: need to stabilize encoding of calling convention...
145 Record.push_back(T->getCallConv());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146}
147
148void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
149 VisitFunctionType(T);
150 Code = pch::TYPE_FUNCTION_NO_PROTO;
151}
152
153void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
154 VisitFunctionType(T);
155 Record.push_back(T->getNumArgs());
156 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
157 Writer.AddTypeRef(T->getArgType(I), Record);
158 Record.push_back(T->isVariadic());
159 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000160 Record.push_back(T->hasExceptionSpec());
161 Record.push_back(T->hasAnyExceptionSpec());
162 Record.push_back(T->getNumExceptions());
163 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
164 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165 Code = pch::TYPE_FUNCTION_PROTO;
166}
167
John McCallb96ec562009-12-04 22:46:56 +0000168#if 0
169// For when we want it....
170void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_UNRESOLVED_USING;
173}
174#endif
175
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
177 Writer.AddDeclRef(T->getDecl(), Record);
178 Code = pch::TYPE_TYPEDEF;
179}
180
181void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000182 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 Code = pch::TYPE_TYPEOF_EXPR;
184}
185
186void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
187 Writer.AddTypeRef(T->getUnderlyingType(), Record);
188 Code = pch::TYPE_TYPEOF;
189}
190
Anders Carlsson81df7b82009-06-24 19:06:50 +0000191void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
192 Writer.AddStmt(T->getUnderlyingExpr());
193 Code = pch::TYPE_DECLTYPE;
194}
195
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196void PCHTypeWriter::VisitTagType(const TagType *T) {
197 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000198 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199 "Cannot serialize in the middle of a type definition");
200}
201
202void PCHTypeWriter::VisitRecordType(const RecordType *T) {
203 VisitTagType(T);
204 Code = pch::TYPE_RECORD;
205}
206
207void PCHTypeWriter::VisitEnumType(const EnumType *T) {
208 VisitTagType(T);
209 Code = pch::TYPE_ENUM;
210}
211
John McCallfcc33b02009-09-05 00:15:47 +0000212void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
213 Writer.AddTypeRef(T->getUnderlyingType(), Record);
214 Record.push_back(T->getTagKind());
215 Code = pch::TYPE_ELABORATED;
216}
217
Mike Stump11289f42009-09-09 15:08:12 +0000218void
John McCallcebee162009-10-18 09:09:24 +0000219PCHTypeWriter::VisitSubstTemplateTypeParmType(
220 const SubstTemplateTypeParmType *T) {
221 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
222 Writer.AddTypeRef(T->getReplacementType(), Record);
223 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
224}
225
226void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227PCHTypeWriter::VisitTemplateSpecializationType(
228 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000229 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230 assert(false && "Cannot serialize template specialization types");
231}
232
233void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000234 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 assert(false && "Cannot serialize qualified name types");
236}
237
John McCalle78aac42010-03-10 03:28:59 +0000238void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
239 Writer.AddDeclRef(T->getDecl(), Record);
240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
241 Code = pch::TYPE_INJECTED_CLASS_NAME;
242}
243
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
245 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000247 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
248 E = T->qual_end(); I != E; ++I)
249 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000250 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251}
252
Steve Narofffb4330f2009-06-17 22:40:22 +0000253void
254PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000255 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000256 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000257 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000258 E = T->qual_end(); I != E; ++I)
259 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000260 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261}
262
John McCall8f115c62009-10-16 21:56:05 +0000263namespace {
264
265class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
266 PCHWriter &Writer;
267 PCHWriter::RecordData &Record;
268
269public:
270 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
271 : Writer(Writer), Record(Record) { }
272
John McCall17001972009-10-18 01:05:36 +0000273#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000274#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000275 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000276#include "clang/AST/TypeLocNodes.def"
277
John McCall17001972009-10-18 01:05:36 +0000278 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
279 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000280};
281
282}
283
John McCall17001972009-10-18 01:05:36 +0000284void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
285 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000286}
John McCall17001972009-10-18 01:05:36 +0000287void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000288 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
289 if (TL.needsExtraLocalData()) {
290 Record.push_back(TL.getWrittenTypeSpec());
291 Record.push_back(TL.getWrittenSignSpec());
292 Record.push_back(TL.getWrittenWidthSpec());
293 Record.push_back(TL.hasModeAttr());
294 }
John McCall8f115c62009-10-16 21:56:05 +0000295}
John McCall17001972009-10-18 01:05:36 +0000296void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
297 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000298}
John McCall17001972009-10-18 01:05:36 +0000299void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
300 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000301}
John McCall17001972009-10-18 01:05:36 +0000302void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
303 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000304}
John McCall17001972009-10-18 01:05:36 +0000305void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
306 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000307}
John McCall17001972009-10-18 01:05:36 +0000308void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
309 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000310}
John McCall17001972009-10-18 01:05:36 +0000311void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
312 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000313}
John McCall17001972009-10-18 01:05:36 +0000314void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
315 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
316 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
317 Record.push_back(TL.getSizeExpr() ? 1 : 0);
318 if (TL.getSizeExpr())
319 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000320}
John McCall17001972009-10-18 01:05:36 +0000321void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
322 VisitArrayTypeLoc(TL);
323}
324void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
325 VisitArrayTypeLoc(TL);
326}
327void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
328 VisitArrayTypeLoc(TL);
329}
330void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
331 DependentSizedArrayTypeLoc TL) {
332 VisitArrayTypeLoc(TL);
333}
334void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
335 DependentSizedExtVectorTypeLoc TL) {
336 Writer.AddSourceLocation(TL.getNameLoc(), Record);
337}
338void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getNameLoc(), Record);
340}
341void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
342 Writer.AddSourceLocation(TL.getNameLoc(), Record);
343}
344void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
345 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
346 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
347 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
348 Writer.AddDeclRef(TL.getArg(i), Record);
349}
350void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
351 VisitFunctionTypeLoc(TL);
352}
353void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
354 VisitFunctionTypeLoc(TL);
355}
John McCallb96ec562009-12-04 22:46:56 +0000356void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
357 Writer.AddSourceLocation(TL.getNameLoc(), Record);
358}
John McCall17001972009-10-18 01:05:36 +0000359void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
360 Writer.AddSourceLocation(TL.getNameLoc(), Record);
361}
362void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000363 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
364 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
365 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000366}
367void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000368 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
369 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
370 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
371 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000372}
373void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getNameLoc(), Record);
375}
376void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
382void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getNameLoc(), Record);
384}
385void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getNameLoc(), Record);
387}
John McCallcebee162009-10-18 09:09:24 +0000388void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
389 SubstTemplateTypeParmTypeLoc TL) {
390 Writer.AddSourceLocation(TL.getNameLoc(), Record);
391}
John McCall17001972009-10-18 01:05:36 +0000392void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
393 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000394 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
395 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
396 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
397 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
398 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall17001972009-10-18 01:05:36 +0000399}
400void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getNameLoc(), Record);
402}
John McCalle78aac42010-03-10 03:28:59 +0000403void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getNameLoc(), Record);
405}
John McCall17001972009-10-18 01:05:36 +0000406void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getNameLoc(), Record);
408}
409void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000411 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
412 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
413 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
414 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000415}
John McCallfc93cf92009-10-22 22:37:11 +0000416void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
417 Writer.AddSourceLocation(TL.getStarLoc(), Record);
418 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
419 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
420 Record.push_back(TL.hasBaseTypeAsWritten());
421 Record.push_back(TL.hasProtocolsAsWritten());
422 if (TL.hasProtocolsAsWritten())
423 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
424 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
425}
John McCall8f115c62009-10-16 21:56:05 +0000426
Chris Lattner19cea4e2009-04-22 05:57:30 +0000427//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000428// PCHWriter Implementation
429//===----------------------------------------------------------------------===//
430
Chris Lattner28fa4e62009-04-26 22:26:21 +0000431static void EmitBlockID(unsigned ID, const char *Name,
432 llvm::BitstreamWriter &Stream,
433 PCHWriter::RecordData &Record) {
434 Record.clear();
435 Record.push_back(ID);
436 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
437
438 // Emit the block name if present.
439 if (Name == 0 || Name[0] == 0) return;
440 Record.clear();
441 while (*Name)
442 Record.push_back(*Name++);
443 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
444}
445
446static void EmitRecordID(unsigned ID, const char *Name,
447 llvm::BitstreamWriter &Stream,
448 PCHWriter::RecordData &Record) {
449 Record.clear();
450 Record.push_back(ID);
451 while (*Name)
452 Record.push_back(*Name++);
453 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000454}
455
456static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
457 PCHWriter::RecordData &Record) {
458#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
459 RECORD(STMT_STOP);
460 RECORD(STMT_NULL_PTR);
461 RECORD(STMT_NULL);
462 RECORD(STMT_COMPOUND);
463 RECORD(STMT_CASE);
464 RECORD(STMT_DEFAULT);
465 RECORD(STMT_LABEL);
466 RECORD(STMT_IF);
467 RECORD(STMT_SWITCH);
468 RECORD(STMT_WHILE);
469 RECORD(STMT_DO);
470 RECORD(STMT_FOR);
471 RECORD(STMT_GOTO);
472 RECORD(STMT_INDIRECT_GOTO);
473 RECORD(STMT_CONTINUE);
474 RECORD(STMT_BREAK);
475 RECORD(STMT_RETURN);
476 RECORD(STMT_DECL);
477 RECORD(STMT_ASM);
478 RECORD(EXPR_PREDEFINED);
479 RECORD(EXPR_DECL_REF);
480 RECORD(EXPR_INTEGER_LITERAL);
481 RECORD(EXPR_FLOATING_LITERAL);
482 RECORD(EXPR_IMAGINARY_LITERAL);
483 RECORD(EXPR_STRING_LITERAL);
484 RECORD(EXPR_CHARACTER_LITERAL);
485 RECORD(EXPR_PAREN);
486 RECORD(EXPR_UNARY_OPERATOR);
487 RECORD(EXPR_SIZEOF_ALIGN_OF);
488 RECORD(EXPR_ARRAY_SUBSCRIPT);
489 RECORD(EXPR_CALL);
490 RECORD(EXPR_MEMBER);
491 RECORD(EXPR_BINARY_OPERATOR);
492 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
493 RECORD(EXPR_CONDITIONAL_OPERATOR);
494 RECORD(EXPR_IMPLICIT_CAST);
495 RECORD(EXPR_CSTYLE_CAST);
496 RECORD(EXPR_COMPOUND_LITERAL);
497 RECORD(EXPR_EXT_VECTOR_ELEMENT);
498 RECORD(EXPR_INIT_LIST);
499 RECORD(EXPR_DESIGNATED_INIT);
500 RECORD(EXPR_IMPLICIT_VALUE_INIT);
501 RECORD(EXPR_VA_ARG);
502 RECORD(EXPR_ADDR_LABEL);
503 RECORD(EXPR_STMT);
504 RECORD(EXPR_TYPES_COMPATIBLE);
505 RECORD(EXPR_CHOOSE);
506 RECORD(EXPR_GNU_NULL);
507 RECORD(EXPR_SHUFFLE_VECTOR);
508 RECORD(EXPR_BLOCK);
509 RECORD(EXPR_BLOCK_DECL_REF);
510 RECORD(EXPR_OBJC_STRING_LITERAL);
511 RECORD(EXPR_OBJC_ENCODE);
512 RECORD(EXPR_OBJC_SELECTOR_EXPR);
513 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
514 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
515 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
516 RECORD(EXPR_OBJC_KVC_REF_EXPR);
517 RECORD(EXPR_OBJC_MESSAGE_EXPR);
518 RECORD(EXPR_OBJC_SUPER_EXPR);
519 RECORD(STMT_OBJC_FOR_COLLECTION);
520 RECORD(STMT_OBJC_CATCH);
521 RECORD(STMT_OBJC_FINALLY);
522 RECORD(STMT_OBJC_AT_TRY);
523 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
524 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000525 RECORD(EXPR_CXX_OPERATOR_CALL);
526 RECORD(EXPR_CXX_CONSTRUCT);
527 RECORD(EXPR_CXX_STATIC_CAST);
528 RECORD(EXPR_CXX_DYNAMIC_CAST);
529 RECORD(EXPR_CXX_REINTERPRET_CAST);
530 RECORD(EXPR_CXX_CONST_CAST);
531 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
532 RECORD(EXPR_CXX_BOOL_LITERAL);
533 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000534#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000535}
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattner28fa4e62009-04-26 22:26:21 +0000537void PCHWriter::WriteBlockInfoBlock() {
538 RecordData Record;
539 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000540
Chris Lattner64031982009-04-27 00:40:25 +0000541#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000542#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000543
Chris Lattner28fa4e62009-04-26 22:26:21 +0000544 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000545 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000546 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000547 RECORD(TYPE_OFFSET);
548 RECORD(DECL_OFFSET);
549 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000550 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000551 RECORD(IDENTIFIER_OFFSET);
552 RECORD(IDENTIFIER_TABLE);
553 RECORD(EXTERNAL_DEFINITIONS);
554 RECORD(SPECIAL_TYPES);
555 RECORD(STATISTICS);
556 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattner90073802010-02-12 00:07:30 +0000557 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000558 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
559 RECORD(SELECTOR_OFFSETS);
560 RECORD(METHOD_POOL);
561 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000562 RECORD(SOURCE_LOCATION_OFFSETS);
563 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000564 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000565 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000566 RECORD(COMMENT_RANGES);
Ted Kremenek17437132010-01-22 20:59:36 +0000567 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000568
Chris Lattner28fa4e62009-04-26 22:26:21 +0000569 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000570 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000571 RECORD(SM_SLOC_FILE_ENTRY);
572 RECORD(SM_SLOC_BUFFER_ENTRY);
573 RECORD(SM_SLOC_BUFFER_BLOB);
574 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
575 RECORD(SM_LINE_TABLE);
576 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner28fa4e62009-04-26 22:26:21 +0000578 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000579 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000580 RECORD(PP_MACRO_OBJECT_LIKE);
581 RECORD(PP_MACRO_FUNCTION_LIKE);
582 RECORD(PP_TOKEN);
583
Douglas Gregor12bfa382009-10-17 00:13:19 +0000584 // Decls and Types block.
585 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000586 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000587 RECORD(TYPE_COMPLEX);
588 RECORD(TYPE_POINTER);
589 RECORD(TYPE_BLOCK_POINTER);
590 RECORD(TYPE_LVALUE_REFERENCE);
591 RECORD(TYPE_RVALUE_REFERENCE);
592 RECORD(TYPE_MEMBER_POINTER);
593 RECORD(TYPE_CONSTANT_ARRAY);
594 RECORD(TYPE_INCOMPLETE_ARRAY);
595 RECORD(TYPE_VARIABLE_ARRAY);
596 RECORD(TYPE_VECTOR);
597 RECORD(TYPE_EXT_VECTOR);
598 RECORD(TYPE_FUNCTION_PROTO);
599 RECORD(TYPE_FUNCTION_NO_PROTO);
600 RECORD(TYPE_TYPEDEF);
601 RECORD(TYPE_TYPEOF_EXPR);
602 RECORD(TYPE_TYPEOF);
603 RECORD(TYPE_RECORD);
604 RECORD(TYPE_ENUM);
605 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000606 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000607 RECORD(DECL_ATTR);
608 RECORD(DECL_TRANSLATION_UNIT);
609 RECORD(DECL_TYPEDEF);
610 RECORD(DECL_ENUM);
611 RECORD(DECL_RECORD);
612 RECORD(DECL_ENUM_CONSTANT);
613 RECORD(DECL_FUNCTION);
614 RECORD(DECL_OBJC_METHOD);
615 RECORD(DECL_OBJC_INTERFACE);
616 RECORD(DECL_OBJC_PROTOCOL);
617 RECORD(DECL_OBJC_IVAR);
618 RECORD(DECL_OBJC_AT_DEFS_FIELD);
619 RECORD(DECL_OBJC_CLASS);
620 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
621 RECORD(DECL_OBJC_CATEGORY);
622 RECORD(DECL_OBJC_CATEGORY_IMPL);
623 RECORD(DECL_OBJC_IMPLEMENTATION);
624 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
625 RECORD(DECL_OBJC_PROPERTY);
626 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000627 RECORD(DECL_FIELD);
628 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000629 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000630 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000631 RECORD(DECL_FILE_SCOPE_ASM);
632 RECORD(DECL_BLOCK);
633 RECORD(DECL_CONTEXT_LEXICAL);
634 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000635 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000636 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000637#undef RECORD
638#undef BLOCK
639 Stream.ExitBlock();
640}
641
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000642/// \brief Adjusts the given filename to only write out the portion of the
643/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000644///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000645/// \param Filename the file name to adjust.
646///
647/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
648/// the returned filename will be adjusted by this system root.
649///
650/// \returns either the original filename (if it needs no adjustment) or the
651/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000652static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000653adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
654 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000655
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000656 if (!isysroot)
657 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000658
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000659 // Verify that the filename and the system root have the same prefix.
660 unsigned Pos = 0;
661 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
662 if (Filename[Pos] != isysroot[Pos])
663 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000664
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000665 // We hit the end of the filename before we hit the end of the system root.
666 if (!Filename[Pos])
667 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000668
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000669 // If the file name has a '/' at the current position, skip over the '/'.
670 // We distinguish sysroot-based includes from absolute includes by the
671 // absence of '/' at the beginning of sysroot-based includes.
672 if (Filename[Pos] == '/')
673 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000675 return Filename + Pos;
676}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000677
Douglas Gregor7b71e632009-04-27 22:23:34 +0000678/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000679void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000680 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000681
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000682 // Metadata
683 const TargetInfo &Target = Context.Target;
684 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
685 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
686 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
687 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
688 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
689 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
690 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
691 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
692 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000693
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000694 RecordData Record;
695 Record.push_back(pch::METADATA);
696 Record.push_back(pch::VERSION_MAJOR);
697 Record.push_back(pch::VERSION_MINOR);
698 Record.push_back(CLANG_VERSION_MAJOR);
699 Record.push_back(CLANG_VERSION_MINOR);
700 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000701 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000702 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000703
Douglas Gregor45fe0362009-05-12 01:31:05 +0000704 // Original file name
705 SourceManager &SM = Context.getSourceManager();
706 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
707 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
708 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
709 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
710 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
711
712 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000713
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000714 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000715
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000716 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000717 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000718 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000719 RecordData Record;
720 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000721 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000722 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000723
Ted Kremenek18e066f2010-01-22 22:12:47 +0000724 // Repository branch/version information.
725 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
726 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
727 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
728 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000729 Record.clear();
Ted Kremenek17437132010-01-22 20:59:36 +0000730 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000731 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
732 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000733}
734
735/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000736void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
737 RecordData Record;
738 Record.push_back(LangOpts.Trigraphs);
739 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
740 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
741 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
742 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
743 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
744 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
745 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
746 Record.push_back(LangOpts.C99); // C99 Support
747 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
748 Record.push_back(LangOpts.CPlusPlus); // C++ Support
749 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000750 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000751
Douglas Gregor55abb232009-04-10 20:39:37 +0000752 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
753 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000754 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000755 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000756 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000757 // modern abi enabled.
Mike Stump11289f42009-09-09 15:08:12 +0000758
Douglas Gregor55abb232009-04-10 20:39:37 +0000759 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000760 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
761 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000762 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000763 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000764 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000765
766 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
767 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
768 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
769
Chris Lattner258172e2009-04-27 07:35:58 +0000770 // Whether static initializers are protected by locks.
771 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000772 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000773 Record.push_back(LangOpts.Blocks); // block extension to C
774 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
775 // they are unused.
776 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
777 // (modulo the platform support).
778
779 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
780 // signed integer arithmetic overflows.
781
782 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
783 // may be ripped out at any time.
784
785 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000786 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000787 // defined.
788 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
789 // opposed to __DYNAMIC__).
790 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
791
792 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
793 // used (instead of C99 semantics).
794 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000795 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
796 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000797 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
798 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000799 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000800 Record.push_back(LangOpts.getGCMode());
801 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000802 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000803 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000804 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000805 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000806 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000807 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000808}
809
Douglas Gregora7f71a92009-04-10 03:52:48 +0000810//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000811// stat cache Serialization
812//===----------------------------------------------------------------------===//
813
814namespace {
815// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000816class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000817public:
818 typedef const char * key_type;
819 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000820
Douglas Gregorc5046832009-04-27 18:38:38 +0000821 typedef std::pair<int, struct stat> data_type;
822 typedef const data_type& data_type_ref;
823
824 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000825 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
828 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000829 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
830 data_type_ref Data) {
831 unsigned StrLen = strlen(path);
832 clang::io::Emit16(Out, StrLen);
833 unsigned DataLen = 1; // result value
834 if (Data.first == 0)
835 DataLen += 4 + 4 + 2 + 8 + 8;
836 clang::io::Emit8(Out, DataLen);
837 return std::make_pair(StrLen + 1, DataLen);
838 }
Mike Stump11289f42009-09-09 15:08:12 +0000839
Douglas Gregorc5046832009-04-27 18:38:38 +0000840 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
841 Out.write(path, KeyLen);
842 }
Mike Stump11289f42009-09-09 15:08:12 +0000843
Douglas Gregorc5046832009-04-27 18:38:38 +0000844 void EmitData(llvm::raw_ostream& Out, key_type_ref,
845 data_type_ref Data, unsigned DataLen) {
846 using namespace clang::io;
847 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000848
Douglas Gregorc5046832009-04-27 18:38:38 +0000849 // Result of stat()
850 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000851
Douglas Gregorc5046832009-04-27 18:38:38 +0000852 if (Data.first == 0) {
853 Emit32(Out, (uint32_t) Data.second.st_ino);
854 Emit32(Out, (uint32_t) Data.second.st_dev);
855 Emit16(Out, (uint16_t) Data.second.st_mode);
856 Emit64(Out, (uint64_t) Data.second.st_mtime);
857 Emit64(Out, (uint64_t) Data.second.st_size);
858 }
859
860 assert(Out.tell() - Start == DataLen && "Wrong data length");
861 }
862};
863} // end anonymous namespace
864
865/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000866void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
867 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000868 // Build the on-disk hash table containing information about every
869 // stat() call.
870 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
871 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000872 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000873 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000874 Stat != StatEnd; ++Stat, ++NumStatEntries) {
875 const char *Filename = Stat->first();
876 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
877 Generator.insert(Filename, Stat->second);
878 }
Mike Stump11289f42009-09-09 15:08:12 +0000879
Douglas Gregorc5046832009-04-27 18:38:38 +0000880 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000881 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000882 uint32_t BucketOffset;
883 {
884 llvm::raw_svector_ostream Out(StatCacheData);
885 // Make sure that no bucket is at offset 0
886 clang::io::Emit32(Out, 0);
887 BucketOffset = Generator.Emit(Out);
888 }
889
890 // Create a blob abbreviation
891 using namespace llvm;
892 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
893 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
897 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
898
899 // Write the stat cache
900 RecordData Record;
901 Record.push_back(pch::STAT_CACHE);
902 Record.push_back(BucketOffset);
903 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000904 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000905}
906
907//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000908// Source Manager Serialization
909//===----------------------------------------------------------------------===//
910
911/// \brief Create an abbreviation for the SLocEntry that refers to a
912/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000913static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000914 using namespace llvm;
915 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
916 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000922 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000923}
924
925/// \brief Create an abbreviation for the SLocEntry that refers to a
926/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000927static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000928 using namespace llvm;
929 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
930 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000936 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000937}
938
939/// \brief Create an abbreviation for the SLocEntry that refers to a
940/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000941static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000942 using namespace llvm;
943 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
944 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000946 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000947}
948
949/// \brief Create an abbreviation for the SLocEntry that refers to an
950/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000951static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000952 using namespace llvm;
953 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
954 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000960 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000961}
962
963/// \brief Writes the block containing the serialized form of the
964/// source manager.
965///
966/// TODO: We should probably use an on-disk hash table (stored in a
967/// blob), indexed based on the file name, so that we only create
968/// entries for files that we actually need. In the common case (no
969/// errors), we probably won't have to create file entries for any of
970/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000971void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000972 const Preprocessor &PP,
973 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000974 RecordData Record;
975
Chris Lattner0910e3b2009-04-10 17:16:57 +0000976 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000977 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000978
979 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000980 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
981 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
982 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
983 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000984
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000985 // Write the line table.
986 if (SourceMgr.hasLineTable()) {
987 LineTableInfo &LineTable = SourceMgr.getLineTable();
988
989 // Emit the file names
990 Record.push_back(LineTable.getNumFilenames());
991 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
992 // Emit the file name
993 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000994 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000995 unsigned FilenameLen = Filename? strlen(Filename) : 0;
996 Record.push_back(FilenameLen);
997 if (FilenameLen)
998 Record.insert(Record.end(), Filename, Filename + FilenameLen);
999 }
Mike Stump11289f42009-09-09 15:08:12 +00001000
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001001 // Emit the line entries
1002 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1003 L != LEnd; ++L) {
1004 // Emit the file ID
1005 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001006
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001007 // Emit the line entries
1008 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001009 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001010 LEEnd = L->second.end();
1011 LE != LEEnd; ++LE) {
1012 Record.push_back(LE->FileOffset);
1013 Record.push_back(LE->LineNo);
1014 Record.push_back(LE->FilenameID);
1015 Record.push_back((unsigned)LE->FileKind);
1016 Record.push_back(LE->IncludeOffset);
1017 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001018 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +00001019 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001020 }
1021
Douglas Gregor258ae542009-04-27 06:38:32 +00001022 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +00001023 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +00001024 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001025 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +00001026 E = HS.header_file_end();
1027 I != E; ++I) {
1028 Record.push_back(I->isImport);
1029 Record.push_back(I->DirInfo);
1030 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +00001031 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001032 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1033 Record.clear();
1034 }
1035
Douglas Gregor258ae542009-04-27 06:38:32 +00001036 // Write out the source location entry table. We skip the first
1037 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001038 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001039 RecordData PreloadSLocs;
1040 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001041 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1042 // Get this source location entry.
1043 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001044
Douglas Gregor258ae542009-04-27 06:38:32 +00001045 // Record the offset of this source-location entry.
1046 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1047
1048 // Figure out which record code to use.
1049 unsigned Code;
1050 if (SLoc->isFile()) {
1051 if (SLoc->getFile().getContentCache()->Entry)
1052 Code = pch::SM_SLOC_FILE_ENTRY;
1053 else
1054 Code = pch::SM_SLOC_BUFFER_ENTRY;
1055 } else
1056 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1057 Record.clear();
1058 Record.push_back(Code);
1059
1060 Record.push_back(SLoc->getOffset());
1061 if (SLoc->isFile()) {
1062 const SrcMgr::FileInfo &File = SLoc->getFile();
1063 Record.push_back(File.getIncludeLoc().getRawEncoding());
1064 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1065 Record.push_back(File.hasLineDirectives());
1066
1067 const SrcMgr::ContentCache *Content = File.getContentCache();
1068 if (Content->Entry) {
1069 // The source location entry is a file. The blob associated
1070 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001071
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001072 // Turn the file name into an absolute path, if it isn't already.
1073 const char *Filename = Content->Entry->getName();
1074 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001075 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001076 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001077
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001078 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001079 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001080
1081 // FIXME: For now, preload all file source locations, so that
1082 // we get the appropriate File entries in the reader. This is
1083 // a temporary measure.
1084 PreloadSLocs.push_back(SLocEntryOffsets.size());
1085 } else {
1086 // The source location entry is a buffer. The blob associated
1087 // with this entry contains the contents of the buffer.
1088
1089 // We add one to the size so that we capture the trailing NULL
1090 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1091 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001092 const llvm::MemoryBuffer *Buffer
1093 = Content->getBuffer(PP.getDiagnostics());
Douglas Gregor258ae542009-04-27 06:38:32 +00001094 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001095 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1096 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001097 Record.clear();
1098 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1099 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001100 llvm::StringRef(Buffer->getBufferStart(),
1101 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001102
1103 if (strcmp(Name, "<built-in>") == 0)
1104 PreloadSLocs.push_back(SLocEntryOffsets.size());
1105 }
1106 } else {
1107 // The source location entry is an instantiation.
1108 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1109 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1110 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1111 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1112
1113 // Compute the token length for this macro expansion.
1114 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001115 if (I + 1 != N)
1116 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001117 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1118 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1119 }
1120 }
1121
Douglas Gregor8f45df52009-04-16 22:23:12 +00001122 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001123
1124 if (SLocEntryOffsets.empty())
1125 return;
1126
1127 // Write the source-location offsets table into the PCH block. This
1128 // table is used for lazily loading source-location information.
1129 using namespace llvm;
1130 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1131 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1135 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001136
Douglas Gregor258ae542009-04-27 06:38:32 +00001137 Record.clear();
1138 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1139 Record.push_back(SLocEntryOffsets.size());
1140 Record.push_back(SourceMgr.getNextOffset());
1141 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001142 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001143 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001144
1145 // Write the source location entry preloads array, telling the PCH
1146 // reader which source locations entries it should load eagerly.
1147 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001148}
1149
Douglas Gregorc5046832009-04-27 18:38:38 +00001150//===----------------------------------------------------------------------===//
1151// Preprocessor Serialization
1152//===----------------------------------------------------------------------===//
1153
Chris Lattnereeffaef2009-04-10 17:15:23 +00001154/// \brief Writes the block containing the serialized form of the
1155/// preprocessor.
1156///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001157void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001158 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001159
Chris Lattner0af3ba12009-04-13 01:29:17 +00001160 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1161 if (PP.getCounterValue() != 0) {
1162 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001163 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001164 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001165 }
1166
1167 // Enter the preprocessor block.
1168 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001169
Douglas Gregoreda6a892009-04-26 00:07:37 +00001170 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1171 // FIXME: use diagnostics subsystem for localization etc.
1172 if (PP.SawDateOrTime())
1173 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001175 // Loop over all the macro definitions that are live at the end of the file,
1176 // emitting each to the PP section.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001177 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1178 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001179 // FIXME: This emits macros in hash table order, we should do it in a stable
1180 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001181 MacroInfo *MI = I->second;
1182
1183 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1184 // been redefined by the header (in which case they are not isBuiltinMacro).
1185 if (MI->isBuiltinMacro())
1186 continue;
1187
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001188 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001189 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001190 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1191 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001192
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001193 unsigned Code;
1194 if (MI->isObjectLike()) {
1195 Code = pch::PP_MACRO_OBJECT_LIKE;
1196 } else {
1197 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001199 Record.push_back(MI->isC99Varargs());
1200 Record.push_back(MI->isGNUVarargs());
1201 Record.push_back(MI->getNumArgs());
1202 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1203 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001204 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001205 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001206 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001207 Record.clear();
1208
Chris Lattner2199f5b2009-04-10 18:08:30 +00001209 // Emit the tokens array.
1210 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1211 // Note that we know that the preprocessor does not have any annotation
1212 // tokens in it because they are created by the parser, and thus can't be
1213 // in a macro definition.
1214 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001215
Chris Lattner2199f5b2009-04-10 18:08:30 +00001216 Record.push_back(Tok.getLocation().getRawEncoding());
1217 Record.push_back(Tok.getLength());
1218
Chris Lattner2199f5b2009-04-10 18:08:30 +00001219 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1220 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001221 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001222
Chris Lattner2199f5b2009-04-10 18:08:30 +00001223 // FIXME: Should translate token kind to a stable encoding.
1224 Record.push_back(Tok.getKind());
1225 // FIXME: Should translate token flags to a stable encoding.
1226 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001227
Douglas Gregor8f45df52009-04-16 22:23:12 +00001228 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001229 Record.clear();
1230 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001231 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001232 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001233 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001234}
1235
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001236void PCHWriter::WriteComments(ASTContext &Context) {
1237 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001238
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001239 if (Context.Comments.empty())
1240 return;
Mike Stump11289f42009-09-09 15:08:12 +00001241
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001242 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1243 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1244 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1245 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001246
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001247 RecordData Record;
1248 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001249 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001250 (const char*)&Context.Comments[0],
1251 Context.Comments.size() * sizeof(SourceRange));
1252}
1253
Douglas Gregorc5046832009-04-27 18:38:38 +00001254//===----------------------------------------------------------------------===//
1255// Type Serialization
1256//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001257
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001258/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001259void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001260 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001261 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001262 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001263
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001264 // Record the offset for this type.
1265 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001266 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001267 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1268 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001269 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001270 }
1271
1272 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001273
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001274 // Emit the type's representation.
1275 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001276
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001277 if (T.hasLocalNonFastQualifiers()) {
1278 Qualifiers Qs = T.getLocalQualifiers();
1279 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001280 Record.push_back(Qs.getAsOpaqueValue());
1281 W.Code = pch::TYPE_EXT_QUAL;
1282 } else {
1283 switch (T->getTypeClass()) {
1284 // For all of the concrete, non-dependent types, call the
1285 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001286#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001287 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001288#define ABSTRACT_TYPE(Class, Base)
1289#define DEPENDENT_TYPE(Class, Base)
1290#include "clang/AST/TypeNodes.def"
1291
John McCall8ccfcb52009-09-24 19:53:00 +00001292 // For all of the dependent type nodes (which only occur in C++
1293 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001294#define TYPE(Class, Base)
1295#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1296#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001297 assert(false && "Cannot serialize dependent type nodes");
1298 break;
1299 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001300 }
1301
1302 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001303 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001304
1305 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001306 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001307}
1308
Douglas Gregorc5046832009-04-27 18:38:38 +00001309//===----------------------------------------------------------------------===//
1310// Declaration Serialization
1311//===----------------------------------------------------------------------===//
1312
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001313/// \brief Write the block containing all of the declaration IDs
1314/// lexically declared within the given DeclContext.
1315///
1316/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1317/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001318uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001319 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001320 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001321 return 0;
1322
Douglas Gregor8f45df52009-04-16 22:23:12 +00001323 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001324 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001325 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1326 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001327 AddDeclRef(*D, Record);
1328
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001329 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001330 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001331 return Offset;
1332}
1333
1334/// \brief Write the block containing all of the declaration IDs
1335/// visible from the given DeclContext.
1336///
1337/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1338/// bistream, or 0 if no block was written.
1339uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1340 DeclContext *DC) {
1341 if (DC->getPrimaryContext() != DC)
1342 return 0;
1343
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001344 // Since there is no name lookup into functions or methods, and we
1345 // perform name lookup for the translation unit via the
1346 // IdentifierInfo chains, don't bother to build a
1347 // visible-declarations table for these entities.
1348 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001349 return 0;
1350
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001351 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001352 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001353
1354 // Serialize the contents of the mapping used for lookup. Note that,
1355 // although we have two very different code paths, the serialized
1356 // representation is the same for both cases: a declaration name,
1357 // followed by a size, followed by references to the visible
1358 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001359 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001360 RecordData Record;
1361 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001362 if (!Map)
1363 return 0;
1364
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001365 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1366 D != DEnd; ++D) {
1367 AddDeclarationName(D->first, Record);
1368 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1369 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001370 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001371 AddDeclRef(*Result.first, Record);
1372 }
1373
1374 if (Record.size() == 0)
1375 return 0;
1376
Douglas Gregor8f45df52009-04-16 22:23:12 +00001377 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001378 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001379 return Offset;
1380}
1381
Douglas Gregorc5046832009-04-27 18:38:38 +00001382//===----------------------------------------------------------------------===//
1383// Global Method Pool and Selector Serialization
1384//===----------------------------------------------------------------------===//
1385
Douglas Gregore84a9da2009-04-20 20:36:09 +00001386namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001387// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001388class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001389 PCHWriter &Writer;
1390
1391public:
1392 typedef Selector key_type;
1393 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001394
Douglas Gregorc78d3462009-04-24 21:10:55 +00001395 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1396 typedef const data_type& data_type_ref;
1397
1398 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001399
Douglas Gregorc78d3462009-04-24 21:10:55 +00001400 static unsigned ComputeHash(Selector Sel) {
1401 unsigned N = Sel.getNumArgs();
1402 if (N == 0)
1403 ++N;
1404 unsigned R = 5381;
1405 for (unsigned I = 0; I != N; ++I)
1406 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001407 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001408 return R;
1409 }
Mike Stump11289f42009-09-09 15:08:12 +00001410
1411 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001412 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1413 data_type_ref Methods) {
1414 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1415 clang::io::Emit16(Out, KeyLen);
1416 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001417 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001418 Method = Method->Next)
1419 if (Method->Method)
1420 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001421 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001422 Method = Method->Next)
1423 if (Method->Method)
1424 DataLen += 4;
1425 clang::io::Emit16(Out, DataLen);
1426 return std::make_pair(KeyLen, DataLen);
1427 }
Mike Stump11289f42009-09-09 15:08:12 +00001428
Douglas Gregor95c13f52009-04-25 17:48:32 +00001429 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001430 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001431 assert((Start >> 32) == 0 && "Selector key offset too large");
1432 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001433 unsigned N = Sel.getNumArgs();
1434 clang::io::Emit16(Out, N);
1435 if (N == 0)
1436 N = 1;
1437 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001438 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001439 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1440 }
Mike Stump11289f42009-09-09 15:08:12 +00001441
Douglas Gregorc78d3462009-04-24 21:10:55 +00001442 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001443 data_type_ref Methods, unsigned DataLen) {
1444 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001445 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001446 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001447 Method = Method->Next)
1448 if (Method->Method)
1449 ++NumInstanceMethods;
1450
1451 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001452 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001453 Method = Method->Next)
1454 if (Method->Method)
1455 ++NumFactoryMethods;
1456
1457 clang::io::Emit16(Out, NumInstanceMethods);
1458 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001459 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001460 Method = Method->Next)
1461 if (Method->Method)
1462 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001463 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001464 Method = Method->Next)
1465 if (Method->Method)
1466 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001467
1468 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001469 }
1470};
1471} // end anonymous namespace
1472
1473/// \brief Write the method pool into the PCH file.
1474///
1475/// The method pool contains both instance and factory methods, stored
1476/// in an on-disk hash table indexed by the selector.
1477void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1478 using namespace llvm;
1479
1480 // Create and write out the blob that contains the instance and
1481 // factor method pools.
1482 bool Empty = true;
1483 {
1484 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001485
Douglas Gregorc78d3462009-04-24 21:10:55 +00001486 // Create the on-disk hash table representation. Start by
1487 // iterating through the instance method pool.
1488 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001489 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001490 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001491 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001492 InstanceEnd = SemaRef.InstanceMethodPool.end();
1493 Instance != InstanceEnd; ++Instance) {
1494 // Check whether there is a factory method with the same
1495 // selector.
1496 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1497 = SemaRef.FactoryMethodPool.find(Instance->first);
1498
1499 if (Factory == SemaRef.FactoryMethodPool.end())
1500 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001501 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001502 ObjCMethodList()));
1503 else
1504 Generator.insert(Instance->first,
1505 std::make_pair(Instance->second, Factory->second));
1506
Douglas Gregor95c13f52009-04-25 17:48:32 +00001507 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001508 Empty = false;
1509 }
1510
1511 // Now iterate through the factory method pool, to pick up any
1512 // selectors that weren't already in the instance method pool.
1513 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001514 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001515 FactoryEnd = SemaRef.FactoryMethodPool.end();
1516 Factory != FactoryEnd; ++Factory) {
1517 // Check whether there is an instance method with the same
1518 // selector. If so, there is no work to do here.
1519 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1520 = SemaRef.InstanceMethodPool.find(Factory->first);
1521
Douglas Gregor95c13f52009-04-25 17:48:32 +00001522 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001523 Generator.insert(Factory->first,
1524 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001525 ++NumSelectorsInMethodPool;
1526 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001527
1528 Empty = false;
1529 }
1530
Douglas Gregor95c13f52009-04-25 17:48:32 +00001531 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001532 return;
1533
1534 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001535 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001536 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001537 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001538 {
1539 PCHMethodPoolTrait Trait(*this);
1540 llvm::raw_svector_ostream Out(MethodPool);
1541 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001542 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001543 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001544
1545 // For every selector that we have seen but which was not
1546 // written into the hash table, write the selector itself and
1547 // record it's offset.
1548 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1549 if (SelectorOffsets[I] == 0)
1550 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001551 }
1552
1553 // Create a blob abbreviation
1554 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1555 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1559 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1560
Douglas Gregor95c13f52009-04-25 17:48:32 +00001561 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001562 RecordData Record;
1563 Record.push_back(pch::METHOD_POOL);
1564 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001565 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001566 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001567
1568 // Create a blob abbreviation for the selector table offsets.
1569 Abbrev = new BitCodeAbbrev();
1570 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1573 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1574
1575 // Write the selector offsets table.
1576 Record.clear();
1577 Record.push_back(pch::SELECTOR_OFFSETS);
1578 Record.push_back(SelectorOffsets.size());
1579 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1580 (const char *)&SelectorOffsets.front(),
1581 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001582 }
1583}
1584
Douglas Gregorc5046832009-04-27 18:38:38 +00001585//===----------------------------------------------------------------------===//
1586// Identifier Table Serialization
1587//===----------------------------------------------------------------------===//
1588
Douglas Gregorc78d3462009-04-24 21:10:55 +00001589namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001590class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001591 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001592 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001593
Douglas Gregor1d583f22009-04-28 21:18:29 +00001594 /// \brief Determines whether this is an "interesting" identifier
1595 /// that needs a full IdentifierInfo structure written into the hash
1596 /// table.
1597 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1598 return II->isPoisoned() ||
1599 II->isExtensionToken() ||
1600 II->hasMacroDefinition() ||
1601 II->getObjCOrBuiltinID() ||
1602 II->getFETokenInfo<void>();
1603 }
1604
Douglas Gregore84a9da2009-04-20 20:36:09 +00001605public:
1606 typedef const IdentifierInfo* key_type;
1607 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001608
Douglas Gregore84a9da2009-04-20 20:36:09 +00001609 typedef pch::IdentID data_type;
1610 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001611
1612 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001613 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001614
1615 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001616 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001617 }
Mike Stump11289f42009-09-09 15:08:12 +00001618
1619 std::pair<unsigned,unsigned>
1620 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001621 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001622 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001623 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1624 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001625 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001626 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001627 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001628 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001629 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1630 DEnd = IdentifierResolver::end();
1631 D != DEnd; ++D)
1632 DataLen += sizeof(pch::DeclID);
1633 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001634 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001635 // We emit the key length after the data length so that every
1636 // string is preceded by a 16-bit length. This matches the PTH
1637 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001638 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001639 return std::make_pair(KeyLen, DataLen);
1640 }
Mike Stump11289f42009-09-09 15:08:12 +00001641
1642 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001643 unsigned KeyLen) {
1644 // Record the location of the key data. This is used when generating
1645 // the mapping from persistent IDs to strings.
1646 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001647 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001648 }
Mike Stump11289f42009-09-09 15:08:12 +00001649
1650 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001651 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001652 if (!isInterestingIdentifier(II)) {
1653 clang::io::Emit32(Out, ID << 1);
1654 return;
1655 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001656
Douglas Gregor1d583f22009-04-28 21:18:29 +00001657 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001658 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001659 bool hasMacroDefinition =
1660 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001661 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001662 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001663 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1664 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1665 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1666 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001667 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001668
Douglas Gregorc3366a52009-04-21 23:56:24 +00001669 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001670 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001671
Douglas Gregora868bbd2009-04-21 22:25:48 +00001672 // Emit the declaration IDs in reverse order, because the
1673 // IdentifierResolver provides the declarations as they would be
1674 // visible (e.g., the function "stat" would come before the struct
1675 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1676 // adds declarations to the end of the list (so we need to see the
1677 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001678 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001679 IdentifierResolver::end());
1680 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1681 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001682 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001683 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001684 }
1685};
1686} // end anonymous namespace
1687
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001688/// \brief Write the identifier table into the PCH file.
1689///
1690/// The identifier table consists of a blob containing string data
1691/// (the actual identifiers themselves) and a separate "offsets" index
1692/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001693void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001694 using namespace llvm;
1695
1696 // Create and write out the blob that contains the identifier
1697 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001698 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001699 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001700
Douglas Gregore6648fb2009-04-28 20:33:11 +00001701 // Look for any identifiers that were named while processing the
1702 // headers, but are otherwise not needed. We add these to the hash
1703 // table to enable checking of the predefines buffer in the case
1704 // where the user adds new macro definitions when building the PCH
1705 // file.
1706 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1707 IDEnd = PP.getIdentifierTable().end();
1708 ID != IDEnd; ++ID)
1709 getIdentifierRef(ID->second);
1710
Douglas Gregore84a9da2009-04-20 20:36:09 +00001711 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001712 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001713 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1714 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1715 ID != IDEnd; ++ID) {
1716 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001717 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001718 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001719
Douglas Gregore84a9da2009-04-20 20:36:09 +00001720 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001721 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001722 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001723 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001724 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001725 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001726 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001727 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001728 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001729 }
1730
1731 // Create a blob abbreviation
1732 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1733 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001736 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001737
1738 // Write the identifier table
1739 RecordData Record;
1740 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001741 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001742 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001743 }
1744
1745 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001746 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1747 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1750 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1751
1752 RecordData Record;
1753 Record.push_back(pch::IDENTIFIER_OFFSET);
1754 Record.push_back(IdentifierOffsets.size());
1755 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1756 (const char *)&IdentifierOffsets.front(),
1757 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001758}
1759
Douglas Gregorc5046832009-04-27 18:38:38 +00001760//===----------------------------------------------------------------------===//
1761// General Serialization Routines
1762//===----------------------------------------------------------------------===//
1763
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001764/// \brief Write a record containing the given attributes.
1765void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1766 RecordData Record;
1767 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001768 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001769 Record.push_back(Attr->isInherited());
1770 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001771 default:
1772 assert(0 && "Does not support PCH writing for this attribute yet!");
1773 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001774 case Attr::Alias:
1775 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1776 break;
1777
1778 case Attr::Aligned:
1779 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1780 break;
1781
1782 case Attr::AlwaysInline:
1783 break;
Mike Stump11289f42009-09-09 15:08:12 +00001784
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001785 case Attr::AnalyzerNoReturn:
1786 break;
1787
1788 case Attr::Annotate:
1789 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1790 break;
1791
1792 case Attr::AsmLabel:
1793 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1794 break;
1795
Alexis Hunt54a02542009-11-25 04:20:27 +00001796 case Attr::BaseCheck:
1797 break;
1798
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001799 case Attr::Blocks:
1800 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1801 break;
1802
Eli Friedmane4310c82009-11-09 18:38:53 +00001803 case Attr::CDecl:
1804 break;
1805
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001806 case Attr::Cleanup:
1807 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1808 break;
1809
1810 case Attr::Const:
1811 break;
1812
1813 case Attr::Constructor:
1814 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1815 break;
1816
1817 case Attr::DLLExport:
1818 case Attr::DLLImport:
1819 case Attr::Deprecated:
1820 break;
1821
1822 case Attr::Destructor:
1823 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1824 break;
1825
1826 case Attr::FastCall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001827 case Attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001828 break;
1829
1830 case Attr::Format: {
1831 const FormatAttr *Format = cast<FormatAttr>(Attr);
1832 AddString(Format->getType(), Record);
1833 Record.push_back(Format->getFormatIdx());
1834 Record.push_back(Format->getFirstArg());
1835 break;
1836 }
1837
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001838 case Attr::FormatArg: {
1839 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1840 Record.push_back(Format->getFormatIdx());
1841 break;
1842 }
1843
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001844 case Attr::Sentinel : {
1845 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1846 Record.push_back(Sentinel->getSentinel());
1847 Record.push_back(Sentinel->getNullPos());
1848 break;
1849 }
Mike Stump11289f42009-09-09 15:08:12 +00001850
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001851 case Attr::GNUInline:
Alexis Hunt54a02542009-11-25 04:20:27 +00001852 case Attr::Hiding:
Ted Kremenek06be9682010-02-17 02:37:45 +00001853 case Attr::IBActionKind:
Ted Kremenek79478e32010-02-18 00:05:52 +00001854 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001855 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001856 case Attr::NoDebug:
Ted Kremenek79478e32010-02-18 00:05:52 +00001857 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001858 case Attr::NoReturn:
1859 case Attr::NoThrow:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001860 break;
1861
1862 case Attr::NonNull: {
1863 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1864 Record.push_back(NonNull->size());
1865 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1866 break;
1867 }
1868
Ted Kremenekd9c66632010-02-18 00:05:45 +00001869 case Attr::CFReturnsNotRetained:
1870 case Attr::CFReturnsRetained:
1871 case Attr::NSReturnsNotRetained:
1872 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001873 case Attr::ObjCException:
1874 case Attr::ObjCNSObject:
1875 case Attr::Overloadable:
Alexis Hunt54a02542009-11-25 04:20:27 +00001876 case Attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001877 break;
1878
Anders Carlsson68e0b682009-08-08 18:23:56 +00001879 case Attr::PragmaPack:
1880 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001881 break;
1882
Anders Carlsson68e0b682009-08-08 18:23:56 +00001883 case Attr::Packed:
1884 break;
Mike Stump11289f42009-09-09 15:08:12 +00001885
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001886 case Attr::Pure:
1887 break;
1888
1889 case Attr::Regparm:
1890 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1891 break;
Mike Stump11289f42009-09-09 15:08:12 +00001892
Nate Begemanf2758702009-06-26 06:32:41 +00001893 case Attr::ReqdWorkGroupSize:
1894 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1895 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1896 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1897 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001898
1899 case Attr::Section:
1900 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1901 break;
1902
1903 case Attr::StdCall:
1904 case Attr::TransparentUnion:
1905 case Attr::Unavailable:
1906 case Attr::Unused:
1907 case Attr::Used:
1908 break;
1909
1910 case Attr::Visibility:
1911 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001912 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001913 break;
1914
1915 case Attr::WarnUnusedResult:
1916 case Attr::Weak:
Rafael Espindolac18086a2010-02-23 22:00:30 +00001917 case Attr::WeakRef:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001918 case Attr::WeakImport:
1919 break;
1920 }
1921 }
1922
Douglas Gregor8f45df52009-04-16 22:23:12 +00001923 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001924}
1925
1926void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1927 Record.push_back(Str.size());
1928 Record.insert(Record.end(), Str.begin(), Str.end());
1929}
1930
Douglas Gregore84a9da2009-04-20 20:36:09 +00001931/// \brief Note that the identifier II occurs at the given offset
1932/// within the identifier table.
1933void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001934 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001935}
1936
Douglas Gregor95c13f52009-04-25 17:48:32 +00001937/// \brief Note that the selector Sel occurs at the given offset
1938/// within the method pool/selector table.
1939void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1940 unsigned ID = SelectorIDs[Sel];
1941 assert(ID && "Unknown selector");
1942 SelectorOffsets[ID - 1] = Offset;
1943}
1944
Mike Stump11289f42009-09-09 15:08:12 +00001945PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1946 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001947 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1948 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001949
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001950void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1951 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001952 using namespace llvm;
1953
Douglas Gregor162dd022009-04-20 15:53:59 +00001954 ASTContext &Context = SemaRef.Context;
1955 Preprocessor &PP = SemaRef.PP;
1956
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001957 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001958 Stream.Emit((unsigned)'C', 8);
1959 Stream.Emit((unsigned)'P', 8);
1960 Stream.Emit((unsigned)'C', 8);
1961 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001962
Chris Lattner28fa4e62009-04-26 22:26:21 +00001963 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001964
1965 // The translation unit is the first declaration we'll emit.
1966 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001967 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001968
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001969 // Make sure that we emit IdentifierInfos (and any attached
1970 // declarations) for builtins.
1971 {
1972 IdentifierTable &Table = PP.getIdentifierTable();
1973 llvm::SmallVector<const char *, 32> BuiltinNames;
1974 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1975 Context.getLangOptions().NoBuiltin);
1976 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1977 getIdentifierRef(&Table.get(BuiltinNames[I]));
1978 }
1979
Chris Lattner0c797362009-09-08 18:19:27 +00001980 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00001981 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00001982 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001983 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00001984 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
1985 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00001986 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001987
Tanya Lattner90073802010-02-12 00:07:30 +00001988 // Build a record containing all of the static unused functions in this file.
1989 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001990 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattner90073802010-02-12 00:07:30 +00001991 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001992
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001993 // Build a record containing all of the locally-scoped external
1994 // declarations in this header file. Generally, this record will be
1995 // empty.
1996 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001997 // FIXME: This is filling in the PCH file in densemap order which is
1998 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001999 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002000 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2001 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2002 TD != TDEnd; ++TD)
2003 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2004
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002005 // Build a record containing all of the ext_vector declarations.
2006 RecordData ExtVectorDecls;
2007 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2008 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2009
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002010 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002011 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00002012 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002013 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00002014 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002015 if (StatCalls && !isysroot)
2016 WriteStatCache(*StatCalls, isysroot);
2017 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00002018 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00002019 // Write the record of special types.
2020 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002021
Steve Naroffc277ad12009-07-18 15:33:26 +00002022 AddTypeRef(Context.getBuiltinVaListType(), Record);
2023 AddTypeRef(Context.getObjCIdType(), Record);
2024 AddTypeRef(Context.getObjCSelType(), Record);
2025 AddTypeRef(Context.getObjCProtoType(), Record);
2026 AddTypeRef(Context.getObjCClassType(), Record);
2027 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2028 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2029 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002030 AddTypeRef(Context.getjmp_bufType(), Record);
2031 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002032 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2033 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002034#if 0
2035 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian04b258c2009-11-25 23:07:42 +00002036 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002037#endif
Mike Stumpd0153282009-10-20 02:12:22 +00002038 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002039 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002040 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002041
Douglas Gregor1970d882009-04-26 03:49:13 +00002042 // Keep writing types and declarations until all types and
2043 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002044 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2045 WriteDeclsBlockAbbrevs();
2046 while (!DeclTypesToEmit.empty()) {
2047 DeclOrType DOT = DeclTypesToEmit.front();
2048 DeclTypesToEmit.pop();
2049 if (DOT.isType())
2050 WriteType(DOT.getType());
2051 else
2052 WriteDecl(Context, DOT.getDecl());
2053 }
2054 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002055
Douglas Gregor45053152009-10-17 17:25:45 +00002056 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002057 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002058 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002059
2060 // Write the type offsets array
2061 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2062 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2065 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2066 Record.clear();
2067 Record.push_back(pch::TYPE_OFFSET);
2068 Record.push_back(TypeOffsets.size());
2069 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002070 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002071 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002072
Douglas Gregor745ed142009-04-25 18:35:21 +00002073 // Write the declaration offsets array
2074 Abbrev = new BitCodeAbbrev();
2075 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2077 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2078 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2079 Record.clear();
2080 Record.push_back(pch::DECL_OFFSET);
2081 Record.push_back(DeclOffsets.size());
2082 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002083 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002084 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002085
Douglas Gregord4df8652009-04-22 22:02:47 +00002086 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002087 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002088 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002089
2090 // Write the record containing tentative definitions.
2091 if (!TentativeDefinitions.empty())
2092 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002093
Tanya Lattner90073802010-02-12 00:07:30 +00002094 // Write the record containing unused static functions.
2095 if (!UnusedStaticFuncs.empty())
2096 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002097
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002098 // Write the record containing locally-scoped external definitions.
2099 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002100 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002101 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002102
2103 // Write the record containing ext_vector type names.
2104 if (!ExtVectorDecls.empty())
2105 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002106
Douglas Gregor08f01292009-04-17 22:13:46 +00002107 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002108 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002109 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002110 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002111 Record.push_back(NumLexicalDeclContexts);
2112 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002113 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002114 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002115}
2116
2117void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2118 Record.push_back(Loc.getRawEncoding());
2119}
2120
2121void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2122 Record.push_back(Value.getBitWidth());
2123 unsigned N = Value.getNumWords();
2124 const uint64_t* Words = Value.getRawData();
2125 for (unsigned I = 0; I != N; ++I)
2126 Record.push_back(Words[I]);
2127}
2128
Douglas Gregor1daeb692009-04-13 18:14:40 +00002129void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2130 Record.push_back(Value.isUnsigned());
2131 AddAPInt(Value, Record);
2132}
2133
Douglas Gregore0a3a512009-04-14 21:55:33 +00002134void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2135 AddAPInt(Value.bitcastToAPInt(), Record);
2136}
2137
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002138void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002139 Record.push_back(getIdentifierRef(II));
2140}
2141
2142pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2143 if (II == 0)
2144 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002145
2146 pch::IdentID &ID = IdentifierIDs[II];
2147 if (ID == 0)
2148 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002149 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002150}
2151
Steve Naroff2ddea052009-04-23 10:39:46 +00002152void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2153 if (SelRef.getAsOpaquePtr() == 0) {
2154 Record.push_back(0);
2155 return;
2156 }
2157
2158 pch::SelectorID &SID = SelectorIDs[SelRef];
2159 if (SID == 0) {
2160 SID = SelectorIDs.size();
2161 SelVector.push_back(SelRef);
2162 }
2163 Record.push_back(SID);
2164}
2165
John McCall0ad16662009-10-29 08:12:44 +00002166void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2167 RecordData &Record) {
2168 switch (Arg.getArgument().getKind()) {
2169 case TemplateArgument::Expression:
2170 AddStmt(Arg.getLocInfo().getAsExpr());
2171 break;
2172 case TemplateArgument::Type:
John McCallbcd03502009-12-07 02:54:59 +00002173 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002174 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002175 case TemplateArgument::Template:
2176 Record.push_back(
2177 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2178 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2179 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2180 break;
John McCall0ad16662009-10-29 08:12:44 +00002181 case TemplateArgument::Null:
2182 case TemplateArgument::Integral:
2183 case TemplateArgument::Declaration:
2184 case TemplateArgument::Pack:
2185 break;
2186 }
2187}
2188
John McCallbcd03502009-12-07 02:54:59 +00002189void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2190 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002191 AddTypeRef(QualType(), Record);
2192 return;
2193 }
2194
John McCallbcd03502009-12-07 02:54:59 +00002195 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002196 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002197 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002198 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002199}
2200
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002201void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2202 if (T.isNull()) {
2203 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2204 return;
2205 }
2206
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002207 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002208 T.removeFastQualifiers();
2209
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002210 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002211 pch::TypeID &ID = TypeIDs[T];
2212 if (ID == 0) {
2213 // We haven't seen these qualifiers applied to this type before.
2214 // Assign it a new ID. This is the only time we enqueue a
2215 // qualified type, and it has no CV qualifiers.
2216 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002217 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002218 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002219
John McCall8ccfcb52009-09-24 19:53:00 +00002220 // Encode the type qualifiers in the type reference.
2221 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2222 return;
2223 }
2224
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002225 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002226
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002227 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002228 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002229 switch (BT->getKind()) {
2230 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2231 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2232 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2233 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2234 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2235 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2236 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2237 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002238 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002239 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2240 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2241 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2242 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2243 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2244 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2245 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002246 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002247 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2248 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2249 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002250 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002251 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2252 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002253 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2254 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002255 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2256 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002257 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002258 case BuiltinType::UndeducedAuto:
2259 assert(0 && "Should not see undeduced auto here");
2260 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002261 }
2262
John McCall8ccfcb52009-09-24 19:53:00 +00002263 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002264 return;
2265 }
2266
John McCall8ccfcb52009-09-24 19:53:00 +00002267 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002268 if (ID == 0) {
2269 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002270 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002271 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002272 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002273 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002274
2275 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002276 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002277}
2278
2279void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2280 if (D == 0) {
2281 Record.push_back(0);
2282 return;
2283 }
2284
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002285 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002286 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002287 // We haven't seen this declaration before. Give it a new ID and
2288 // enqueue it in the list of declarations to emit.
2289 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002290 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002291 }
2292
2293 Record.push_back(ID);
2294}
2295
Douglas Gregore84a9da2009-04-20 20:36:09 +00002296pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2297 if (D == 0)
2298 return 0;
2299
2300 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2301 return DeclIDs[D];
2302}
2303
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002304void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002305 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002306 Record.push_back(Name.getNameKind());
2307 switch (Name.getNameKind()) {
2308 case DeclarationName::Identifier:
2309 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2310 break;
2311
2312 case DeclarationName::ObjCZeroArgSelector:
2313 case DeclarationName::ObjCOneArgSelector:
2314 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002315 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002316 break;
2317
2318 case DeclarationName::CXXConstructorName:
2319 case DeclarationName::CXXDestructorName:
2320 case DeclarationName::CXXConversionFunctionName:
2321 AddTypeRef(Name.getCXXNameType(), Record);
2322 break;
2323
2324 case DeclarationName::CXXOperatorName:
2325 Record.push_back(Name.getCXXOverloadedOperator());
2326 break;
2327
Alexis Hunt3d221f22009-11-29 07:34:05 +00002328 case DeclarationName::CXXLiteralOperatorName:
2329 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2330 break;
2331
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002332 case DeclarationName::CXXUsingDirective:
2333 // No extra data to emit
2334 break;
2335 }
2336}