blob: 633532fdabb92f55696fb74ab1fdebe8f78e079b [file] [log] [blame]
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001//===--- PCHWriter.cpp - Precompiled Headers Writer -----------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000033#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000045
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000047 class PCHTypeWriter {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048 PCHWriter &Writer;
49 PCHWriter::RecordData &Record;
50
51 public:
52 /// \brief Type code that corresponds to the record generated.
53 pch::TypeCode Code;
54
Mike Stump11289f42009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000057
58 void VisitArrayType(const ArrayType *T);
59 void VisitFunctionType(const FunctionType *T);
60 void VisitTagType(const TagType *T);
61
62#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63#define ABSTRACT_TYPE(Class, Base)
64#define DEPENDENT_TYPE(Class, Base)
65#include "clang/AST/TypeNodes.def"
66 };
67}
68
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
Douglas Gregoref84c4b2009-04-09 22:27:44 +000073void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
74 Writer.AddTypeRef(T->getElementType(), Record);
75 Code = pch::TYPE_COMPLEX;
76}
77
78void PCHTypeWriter::VisitPointerType(const PointerType *T) {
79 Writer.AddTypeRef(T->getPointeeType(), Record);
80 Code = pch::TYPE_POINTER;
81}
82
83void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000084 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000085 Code = pch::TYPE_BLOCK_POINTER;
86}
87
88void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_LVALUE_REFERENCE;
91}
92
93void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_RVALUE_REFERENCE;
96}
97
98void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000099 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000101 Code = pch::TYPE_MEMBER_POINTER;
102}
103
104void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
105 Writer.AddTypeRef(T->getElementType(), Record);
106 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000107 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108}
109
110void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
111 VisitArrayType(T);
112 Writer.AddAPInt(T->getSize(), Record);
113 Code = pch::TYPE_CONSTANT_ARRAY;
114}
115
116void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
117 VisitArrayType(T);
118 Code = pch::TYPE_INCOMPLETE_ARRAY;
119}
120
121void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
122 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000123 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
124 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000125 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126 Code = pch::TYPE_VARIABLE_ARRAY;
127}
128
129void PCHTypeWriter::VisitVectorType(const VectorType *T) {
130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getNumElements());
John Thompson22334602010-02-05 00:12:22 +0000132 Record.push_back(T->isAltiVec());
133 Record.push_back(T->isPixel());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134 Code = pch::TYPE_VECTOR;
135}
136
137void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
138 VisitVectorType(T);
139 Code = pch::TYPE_EXT_VECTOR;
140}
141
142void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
143 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000144 FunctionType::ExtInfo C = T->getExtInfo();
145 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000146 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000147 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000148 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000149}
150
151void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
152 VisitFunctionType(T);
153 Code = pch::TYPE_FUNCTION_NO_PROTO;
154}
155
156void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
157 VisitFunctionType(T);
158 Record.push_back(T->getNumArgs());
159 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
160 Writer.AddTypeRef(T->getArgType(I), Record);
161 Record.push_back(T->isVariadic());
162 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000163 Record.push_back(T->hasExceptionSpec());
164 Record.push_back(T->hasAnyExceptionSpec());
165 Record.push_back(T->getNumExceptions());
166 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
167 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 Code = pch::TYPE_FUNCTION_PROTO;
169}
170
John McCallb96ec562009-12-04 22:46:56 +0000171#if 0
172// For when we want it....
173void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
174 Writer.AddDeclRef(T->getDecl(), Record);
175 Code = pch::TYPE_UNRESOLVED_USING;
176}
177#endif
178
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
180 Writer.AddDeclRef(T->getDecl(), Record);
181 Code = pch::TYPE_TYPEDEF;
182}
183
184void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000185 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000186 Code = pch::TYPE_TYPEOF_EXPR;
187}
188
189void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
190 Writer.AddTypeRef(T->getUnderlyingType(), Record);
191 Code = pch::TYPE_TYPEOF;
192}
193
Anders Carlsson81df7b82009-06-24 19:06:50 +0000194void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
195 Writer.AddStmt(T->getUnderlyingExpr());
196 Code = pch::TYPE_DECLTYPE;
197}
198
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199void PCHTypeWriter::VisitTagType(const TagType *T) {
200 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000201 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202 "Cannot serialize in the middle of a type definition");
203}
204
205void PCHTypeWriter::VisitRecordType(const RecordType *T) {
206 VisitTagType(T);
207 Code = pch::TYPE_RECORD;
208}
209
210void PCHTypeWriter::VisitEnumType(const EnumType *T) {
211 VisitTagType(T);
212 Code = pch::TYPE_ENUM;
213}
214
John McCallfcc33b02009-09-05 00:15:47 +0000215void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
216 Writer.AddTypeRef(T->getUnderlyingType(), Record);
217 Record.push_back(T->getTagKind());
218 Code = pch::TYPE_ELABORATED;
219}
220
Mike Stump11289f42009-09-09 15:08:12 +0000221void
John McCallcebee162009-10-18 09:09:24 +0000222PCHTypeWriter::VisitSubstTemplateTypeParmType(
223 const SubstTemplateTypeParmType *T) {
224 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
225 Writer.AddTypeRef(T->getReplacementType(), Record);
226 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
227}
228
229void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230PCHTypeWriter::VisitTemplateSpecializationType(
231 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000232 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000233 assert(false && "Cannot serialize template specialization types");
234}
235
236void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000237 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000238 assert(false && "Cannot serialize qualified name types");
239}
240
John McCalle78aac42010-03-10 03:28:59 +0000241void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
242 Writer.AddDeclRef(T->getDecl(), Record);
243 Writer.AddTypeRef(T->getUnderlyingType(), Record);
244 Code = pch::TYPE_INJECTED_CLASS_NAME;
245}
246
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000247void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
248 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000249 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000250 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
251 E = T->qual_end(); I != E; ++I)
252 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000253 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000254}
255
Steve Narofffb4330f2009-06-17 22:40:22 +0000256void
257PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000258 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000259 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000260 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000261 E = T->qual_end(); I != E; ++I)
262 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000263 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000264}
265
John McCall8f115c62009-10-16 21:56:05 +0000266namespace {
267
268class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
269 PCHWriter &Writer;
270 PCHWriter::RecordData &Record;
271
272public:
273 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
274 : Writer(Writer), Record(Record) { }
275
John McCall17001972009-10-18 01:05:36 +0000276#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000277#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000278 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000279#include "clang/AST/TypeLocNodes.def"
280
John McCall17001972009-10-18 01:05:36 +0000281 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
282 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000283};
284
285}
286
John McCall17001972009-10-18 01:05:36 +0000287void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
288 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000289}
John McCall17001972009-10-18 01:05:36 +0000290void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000291 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
292 if (TL.needsExtraLocalData()) {
293 Record.push_back(TL.getWrittenTypeSpec());
294 Record.push_back(TL.getWrittenSignSpec());
295 Record.push_back(TL.getWrittenWidthSpec());
296 Record.push_back(TL.hasModeAttr());
297 }
John McCall8f115c62009-10-16 21:56:05 +0000298}
John McCall17001972009-10-18 01:05:36 +0000299void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
300 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000301}
John McCall17001972009-10-18 01:05:36 +0000302void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
303 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000304}
John McCall17001972009-10-18 01:05:36 +0000305void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
306 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000307}
John McCall17001972009-10-18 01:05:36 +0000308void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
309 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000310}
John McCall17001972009-10-18 01:05:36 +0000311void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
312 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000313}
John McCall17001972009-10-18 01:05:36 +0000314void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
315 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000316}
John McCall17001972009-10-18 01:05:36 +0000317void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
318 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
319 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
320 Record.push_back(TL.getSizeExpr() ? 1 : 0);
321 if (TL.getSizeExpr())
322 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000323}
John McCall17001972009-10-18 01:05:36 +0000324void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
325 VisitArrayTypeLoc(TL);
326}
327void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
328 VisitArrayTypeLoc(TL);
329}
330void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
331 VisitArrayTypeLoc(TL);
332}
333void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
334 DependentSizedArrayTypeLoc TL) {
335 VisitArrayTypeLoc(TL);
336}
337void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
338 DependentSizedExtVectorTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getNameLoc(), Record);
340}
341void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
342 Writer.AddSourceLocation(TL.getNameLoc(), Record);
343}
344void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
345 Writer.AddSourceLocation(TL.getNameLoc(), Record);
346}
347void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
348 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
349 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
350 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
351 Writer.AddDeclRef(TL.getArg(i), Record);
352}
353void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
354 VisitFunctionTypeLoc(TL);
355}
356void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
357 VisitFunctionTypeLoc(TL);
358}
John McCallb96ec562009-12-04 22:46:56 +0000359void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
360 Writer.AddSourceLocation(TL.getNameLoc(), Record);
361}
John McCall17001972009-10-18 01:05:36 +0000362void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
363 Writer.AddSourceLocation(TL.getNameLoc(), Record);
364}
365void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000366 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
367 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
368 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000369}
370void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000371 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
372 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
373 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
374 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000375}
376void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
382void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getNameLoc(), Record);
384}
385void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getNameLoc(), Record);
387}
388void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
389 Writer.AddSourceLocation(TL.getNameLoc(), Record);
390}
John McCallcebee162009-10-18 09:09:24 +0000391void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
392 SubstTemplateTypeParmTypeLoc TL) {
393 Writer.AddSourceLocation(TL.getNameLoc(), Record);
394}
John McCall17001972009-10-18 01:05:36 +0000395void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
396 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000397 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
398 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
399 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
400 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
401 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall17001972009-10-18 01:05:36 +0000402}
403void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getNameLoc(), Record);
405}
John McCalle78aac42010-03-10 03:28:59 +0000406void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getNameLoc(), Record);
408}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000409void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
John McCall17001972009-10-18 01:05:36 +0000410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
411}
412void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000414 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
415 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
416 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
417 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000418}
John McCallfc93cf92009-10-22 22:37:11 +0000419void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
420 Writer.AddSourceLocation(TL.getStarLoc(), Record);
421 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
422 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
423 Record.push_back(TL.hasBaseTypeAsWritten());
424 Record.push_back(TL.hasProtocolsAsWritten());
425 if (TL.hasProtocolsAsWritten())
426 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
427 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
428}
John McCall8f115c62009-10-16 21:56:05 +0000429
Chris Lattner19cea4e2009-04-22 05:57:30 +0000430//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000431// PCHWriter Implementation
432//===----------------------------------------------------------------------===//
433
Chris Lattner28fa4e62009-04-26 22:26:21 +0000434static void EmitBlockID(unsigned ID, const char *Name,
435 llvm::BitstreamWriter &Stream,
436 PCHWriter::RecordData &Record) {
437 Record.clear();
438 Record.push_back(ID);
439 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
440
441 // Emit the block name if present.
442 if (Name == 0 || Name[0] == 0) return;
443 Record.clear();
444 while (*Name)
445 Record.push_back(*Name++);
446 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
447}
448
449static void EmitRecordID(unsigned ID, const char *Name,
450 llvm::BitstreamWriter &Stream,
451 PCHWriter::RecordData &Record) {
452 Record.clear();
453 Record.push_back(ID);
454 while (*Name)
455 Record.push_back(*Name++);
456 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000457}
458
459static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
460 PCHWriter::RecordData &Record) {
461#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
462 RECORD(STMT_STOP);
463 RECORD(STMT_NULL_PTR);
464 RECORD(STMT_NULL);
465 RECORD(STMT_COMPOUND);
466 RECORD(STMT_CASE);
467 RECORD(STMT_DEFAULT);
468 RECORD(STMT_LABEL);
469 RECORD(STMT_IF);
470 RECORD(STMT_SWITCH);
471 RECORD(STMT_WHILE);
472 RECORD(STMT_DO);
473 RECORD(STMT_FOR);
474 RECORD(STMT_GOTO);
475 RECORD(STMT_INDIRECT_GOTO);
476 RECORD(STMT_CONTINUE);
477 RECORD(STMT_BREAK);
478 RECORD(STMT_RETURN);
479 RECORD(STMT_DECL);
480 RECORD(STMT_ASM);
481 RECORD(EXPR_PREDEFINED);
482 RECORD(EXPR_DECL_REF);
483 RECORD(EXPR_INTEGER_LITERAL);
484 RECORD(EXPR_FLOATING_LITERAL);
485 RECORD(EXPR_IMAGINARY_LITERAL);
486 RECORD(EXPR_STRING_LITERAL);
487 RECORD(EXPR_CHARACTER_LITERAL);
488 RECORD(EXPR_PAREN);
489 RECORD(EXPR_UNARY_OPERATOR);
490 RECORD(EXPR_SIZEOF_ALIGN_OF);
491 RECORD(EXPR_ARRAY_SUBSCRIPT);
492 RECORD(EXPR_CALL);
493 RECORD(EXPR_MEMBER);
494 RECORD(EXPR_BINARY_OPERATOR);
495 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
496 RECORD(EXPR_CONDITIONAL_OPERATOR);
497 RECORD(EXPR_IMPLICIT_CAST);
498 RECORD(EXPR_CSTYLE_CAST);
499 RECORD(EXPR_COMPOUND_LITERAL);
500 RECORD(EXPR_EXT_VECTOR_ELEMENT);
501 RECORD(EXPR_INIT_LIST);
502 RECORD(EXPR_DESIGNATED_INIT);
503 RECORD(EXPR_IMPLICIT_VALUE_INIT);
504 RECORD(EXPR_VA_ARG);
505 RECORD(EXPR_ADDR_LABEL);
506 RECORD(EXPR_STMT);
507 RECORD(EXPR_TYPES_COMPATIBLE);
508 RECORD(EXPR_CHOOSE);
509 RECORD(EXPR_GNU_NULL);
510 RECORD(EXPR_SHUFFLE_VECTOR);
511 RECORD(EXPR_BLOCK);
512 RECORD(EXPR_BLOCK_DECL_REF);
513 RECORD(EXPR_OBJC_STRING_LITERAL);
514 RECORD(EXPR_OBJC_ENCODE);
515 RECORD(EXPR_OBJC_SELECTOR_EXPR);
516 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
517 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
518 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
519 RECORD(EXPR_OBJC_KVC_REF_EXPR);
520 RECORD(EXPR_OBJC_MESSAGE_EXPR);
521 RECORD(EXPR_OBJC_SUPER_EXPR);
522 RECORD(STMT_OBJC_FOR_COLLECTION);
523 RECORD(STMT_OBJC_CATCH);
524 RECORD(STMT_OBJC_FINALLY);
525 RECORD(STMT_OBJC_AT_TRY);
526 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
527 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000528 RECORD(EXPR_CXX_OPERATOR_CALL);
529 RECORD(EXPR_CXX_CONSTRUCT);
530 RECORD(EXPR_CXX_STATIC_CAST);
531 RECORD(EXPR_CXX_DYNAMIC_CAST);
532 RECORD(EXPR_CXX_REINTERPRET_CAST);
533 RECORD(EXPR_CXX_CONST_CAST);
534 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
535 RECORD(EXPR_CXX_BOOL_LITERAL);
536 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000537#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000538}
Mike Stump11289f42009-09-09 15:08:12 +0000539
Chris Lattner28fa4e62009-04-26 22:26:21 +0000540void PCHWriter::WriteBlockInfoBlock() {
541 RecordData Record;
542 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000543
Chris Lattner64031982009-04-27 00:40:25 +0000544#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000545#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000546
Chris Lattner28fa4e62009-04-26 22:26:21 +0000547 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000548 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000549 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000550 RECORD(TYPE_OFFSET);
551 RECORD(DECL_OFFSET);
552 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000553 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000554 RECORD(IDENTIFIER_OFFSET);
555 RECORD(IDENTIFIER_TABLE);
556 RECORD(EXTERNAL_DEFINITIONS);
557 RECORD(SPECIAL_TYPES);
558 RECORD(STATISTICS);
559 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattner90073802010-02-12 00:07:30 +0000560 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000561 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
562 RECORD(SELECTOR_OFFSETS);
563 RECORD(METHOD_POOL);
564 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000565 RECORD(SOURCE_LOCATION_OFFSETS);
566 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000567 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000568 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000569 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000570 RECORD(UNUSED_STATIC_FUNCS);
571 RECORD(MACRO_DEFINITION_OFFSETS);
572
Chris Lattner28fa4e62009-04-26 22:26:21 +0000573 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000574 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000575 RECORD(SM_SLOC_FILE_ENTRY);
576 RECORD(SM_SLOC_BUFFER_ENTRY);
577 RECORD(SM_SLOC_BUFFER_BLOB);
578 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
579 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattner28fa4e62009-04-26 22:26:21 +0000581 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000582 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000583 RECORD(PP_MACRO_OBJECT_LIKE);
584 RECORD(PP_MACRO_FUNCTION_LIKE);
585 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000586 RECORD(PP_MACRO_INSTANTIATION);
587 RECORD(PP_MACRO_DEFINITION);
588
Douglas Gregor12bfa382009-10-17 00:13:19 +0000589 // Decls and Types block.
590 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000591 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000592 RECORD(TYPE_COMPLEX);
593 RECORD(TYPE_POINTER);
594 RECORD(TYPE_BLOCK_POINTER);
595 RECORD(TYPE_LVALUE_REFERENCE);
596 RECORD(TYPE_RVALUE_REFERENCE);
597 RECORD(TYPE_MEMBER_POINTER);
598 RECORD(TYPE_CONSTANT_ARRAY);
599 RECORD(TYPE_INCOMPLETE_ARRAY);
600 RECORD(TYPE_VARIABLE_ARRAY);
601 RECORD(TYPE_VECTOR);
602 RECORD(TYPE_EXT_VECTOR);
603 RECORD(TYPE_FUNCTION_PROTO);
604 RECORD(TYPE_FUNCTION_NO_PROTO);
605 RECORD(TYPE_TYPEDEF);
606 RECORD(TYPE_TYPEOF_EXPR);
607 RECORD(TYPE_TYPEOF);
608 RECORD(TYPE_RECORD);
609 RECORD(TYPE_ENUM);
610 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000611 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000612 RECORD(DECL_ATTR);
613 RECORD(DECL_TRANSLATION_UNIT);
614 RECORD(DECL_TYPEDEF);
615 RECORD(DECL_ENUM);
616 RECORD(DECL_RECORD);
617 RECORD(DECL_ENUM_CONSTANT);
618 RECORD(DECL_FUNCTION);
619 RECORD(DECL_OBJC_METHOD);
620 RECORD(DECL_OBJC_INTERFACE);
621 RECORD(DECL_OBJC_PROTOCOL);
622 RECORD(DECL_OBJC_IVAR);
623 RECORD(DECL_OBJC_AT_DEFS_FIELD);
624 RECORD(DECL_OBJC_CLASS);
625 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
626 RECORD(DECL_OBJC_CATEGORY);
627 RECORD(DECL_OBJC_CATEGORY_IMPL);
628 RECORD(DECL_OBJC_IMPLEMENTATION);
629 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
630 RECORD(DECL_OBJC_PROPERTY);
631 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000632 RECORD(DECL_FIELD);
633 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000634 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000635 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000636 RECORD(DECL_FILE_SCOPE_ASM);
637 RECORD(DECL_BLOCK);
638 RECORD(DECL_CONTEXT_LEXICAL);
639 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000640 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000641 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000642#undef RECORD
643#undef BLOCK
644 Stream.ExitBlock();
645}
646
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000647/// \brief Adjusts the given filename to only write out the portion of the
648/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000649///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000650/// \param Filename the file name to adjust.
651///
652/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
653/// the returned filename will be adjusted by this system root.
654///
655/// \returns either the original filename (if it needs no adjustment) or the
656/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000657static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000658adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
659 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000660
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000661 if (!isysroot)
662 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000663
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000664 // Verify that the filename and the system root have the same prefix.
665 unsigned Pos = 0;
666 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
667 if (Filename[Pos] != isysroot[Pos])
668 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000669
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000670 // We hit the end of the filename before we hit the end of the system root.
671 if (!Filename[Pos])
672 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000673
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000674 // If the file name has a '/' at the current position, skip over the '/'.
675 // We distinguish sysroot-based includes from absolute includes by the
676 // absence of '/' at the beginning of sysroot-based includes.
677 if (Filename[Pos] == '/')
678 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000679
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000680 return Filename + Pos;
681}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000682
Douglas Gregor7b71e632009-04-27 22:23:34 +0000683/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000684void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000685 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000686
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000687 // Metadata
688 const TargetInfo &Target = Context.Target;
689 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
690 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
691 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
692 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
693 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
694 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
695 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
696 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
697 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000698
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000699 RecordData Record;
700 Record.push_back(pch::METADATA);
701 Record.push_back(pch::VERSION_MAJOR);
702 Record.push_back(pch::VERSION_MINOR);
703 Record.push_back(CLANG_VERSION_MAJOR);
704 Record.push_back(CLANG_VERSION_MINOR);
705 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000706 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000707 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000708
Douglas Gregor45fe0362009-05-12 01:31:05 +0000709 // Original file name
710 SourceManager &SM = Context.getSourceManager();
711 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
712 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
713 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
714 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
715 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
716
717 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000718
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000719 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000720
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000721 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000722 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000723 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000724 RecordData Record;
725 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000726 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000727 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000728
Ted Kremenek18e066f2010-01-22 22:12:47 +0000729 // Repository branch/version information.
730 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
731 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
732 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
733 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000734 Record.clear();
Ted Kremenek17437132010-01-22 20:59:36 +0000735 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000736 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
737 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000738}
739
740/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000741void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
742 RecordData Record;
743 Record.push_back(LangOpts.Trigraphs);
744 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
745 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
746 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
747 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000748 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000749 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
750 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
751 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
752 Record.push_back(LangOpts.C99); // C99 Support
753 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
754 Record.push_back(LangOpts.CPlusPlus); // C++ Support
755 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000756 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000757
Douglas Gregor55abb232009-04-10 20:39:37 +0000758 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
759 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000760 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000761 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000762 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000763 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000764 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000765
Douglas Gregor55abb232009-04-10 20:39:37 +0000766 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000767 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
768 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000769 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000770 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000771 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000772
773 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
774 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
775 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
776
Chris Lattner258172e2009-04-27 07:35:58 +0000777 // Whether static initializers are protected by locks.
778 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000779 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000780 Record.push_back(LangOpts.Blocks); // block extension to C
781 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
782 // they are unused.
783 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
784 // (modulo the platform support).
785
786 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
787 // signed integer arithmetic overflows.
788
789 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
790 // may be ripped out at any time.
791
792 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000793 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000794 // defined.
795 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
796 // opposed to __DYNAMIC__).
797 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
798
799 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
800 // used (instead of C99 semantics).
801 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000802 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
803 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000804 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
805 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000806 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000807 Record.push_back(LangOpts.getGCMode());
808 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000809 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000810 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000811 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000812 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000813 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000814 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000815}
816
Douglas Gregora7f71a92009-04-10 03:52:48 +0000817//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000818// stat cache Serialization
819//===----------------------------------------------------------------------===//
820
821namespace {
822// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000823class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000824public:
825 typedef const char * key_type;
826 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000827
Douglas Gregorc5046832009-04-27 18:38:38 +0000828 typedef std::pair<int, struct stat> data_type;
829 typedef const data_type& data_type_ref;
830
831 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000832 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000833 }
Mike Stump11289f42009-09-09 15:08:12 +0000834
835 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000836 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
837 data_type_ref Data) {
838 unsigned StrLen = strlen(path);
839 clang::io::Emit16(Out, StrLen);
840 unsigned DataLen = 1; // result value
841 if (Data.first == 0)
842 DataLen += 4 + 4 + 2 + 8 + 8;
843 clang::io::Emit8(Out, DataLen);
844 return std::make_pair(StrLen + 1, DataLen);
845 }
Mike Stump11289f42009-09-09 15:08:12 +0000846
Douglas Gregorc5046832009-04-27 18:38:38 +0000847 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
848 Out.write(path, KeyLen);
849 }
Mike Stump11289f42009-09-09 15:08:12 +0000850
Douglas Gregorc5046832009-04-27 18:38:38 +0000851 void EmitData(llvm::raw_ostream& Out, key_type_ref,
852 data_type_ref Data, unsigned DataLen) {
853 using namespace clang::io;
854 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000855
Douglas Gregorc5046832009-04-27 18:38:38 +0000856 // Result of stat()
857 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000858
Douglas Gregorc5046832009-04-27 18:38:38 +0000859 if (Data.first == 0) {
860 Emit32(Out, (uint32_t) Data.second.st_ino);
861 Emit32(Out, (uint32_t) Data.second.st_dev);
862 Emit16(Out, (uint16_t) Data.second.st_mode);
863 Emit64(Out, (uint64_t) Data.second.st_mtime);
864 Emit64(Out, (uint64_t) Data.second.st_size);
865 }
866
867 assert(Out.tell() - Start == DataLen && "Wrong data length");
868 }
869};
870} // end anonymous namespace
871
872/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000873void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
874 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000875 // Build the on-disk hash table containing information about every
876 // stat() call.
877 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
878 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000879 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000880 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000881 Stat != StatEnd; ++Stat, ++NumStatEntries) {
882 const char *Filename = Stat->first();
883 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
884 Generator.insert(Filename, Stat->second);
885 }
Mike Stump11289f42009-09-09 15:08:12 +0000886
Douglas Gregorc5046832009-04-27 18:38:38 +0000887 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000888 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000889 uint32_t BucketOffset;
890 {
891 llvm::raw_svector_ostream Out(StatCacheData);
892 // Make sure that no bucket is at offset 0
893 clang::io::Emit32(Out, 0);
894 BucketOffset = Generator.Emit(Out);
895 }
896
897 // Create a blob abbreviation
898 using namespace llvm;
899 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
900 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
904 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
905
906 // Write the stat cache
907 RecordData Record;
908 Record.push_back(pch::STAT_CACHE);
909 Record.push_back(BucketOffset);
910 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000911 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000912}
913
914//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000915// Source Manager Serialization
916//===----------------------------------------------------------------------===//
917
918/// \brief Create an abbreviation for the SLocEntry that refers to a
919/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000920static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000921 using namespace llvm;
922 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
923 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +0000928 // FileEntry fields.
929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000931 // HeaderFileInfo fields.
932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +0000936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000937 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000938}
939
940/// \brief Create an abbreviation for the SLocEntry that refers to a
941/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000942static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000943 using namespace llvm;
944 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
945 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000951 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000952}
953
954/// \brief Create an abbreviation for the SLocEntry that refers to a
955/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000956static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000957 using namespace llvm;
958 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
959 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000961 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000962}
963
964/// \brief Create an abbreviation for the SLocEntry that refers to an
965/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000966static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000967 using namespace llvm;
968 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
969 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000975 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000976}
977
978/// \brief Writes the block containing the serialized form of the
979/// source manager.
980///
981/// TODO: We should probably use an on-disk hash table (stored in a
982/// blob), indexed based on the file name, so that we only create
983/// entries for files that we actually need. In the common case (no
984/// errors), we probably won't have to create file entries for any of
985/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000986void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000987 const Preprocessor &PP,
988 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000989 RecordData Record;
990
Chris Lattner0910e3b2009-04-10 17:16:57 +0000991 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000992 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000993
994 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000995 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
996 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
997 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
998 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000999
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001000 // Write the line table.
1001 if (SourceMgr.hasLineTable()) {
1002 LineTableInfo &LineTable = SourceMgr.getLineTable();
1003
1004 // Emit the file names
1005 Record.push_back(LineTable.getNumFilenames());
1006 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1007 // Emit the file name
1008 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001009 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001010 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1011 Record.push_back(FilenameLen);
1012 if (FilenameLen)
1013 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1014 }
Mike Stump11289f42009-09-09 15:08:12 +00001015
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001016 // Emit the line entries
1017 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1018 L != LEnd; ++L) {
1019 // Emit the file ID
1020 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001021
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001022 // Emit the line entries
1023 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001024 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001025 LEEnd = L->second.end();
1026 LE != LEEnd; ++LE) {
1027 Record.push_back(LE->FileOffset);
1028 Record.push_back(LE->LineNo);
1029 Record.push_back(LE->FilenameID);
1030 Record.push_back((unsigned)LE->FileKind);
1031 Record.push_back(LE->IncludeOffset);
1032 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001033 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +00001034 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001035 }
1036
Douglas Gregor258ae542009-04-27 06:38:32 +00001037 // Write out the source location entry table. We skip the first
1038 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001039 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001040 RecordData PreloadSLocs;
1041 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001042 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1043 // Get this source location entry.
1044 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001045
Douglas Gregor258ae542009-04-27 06:38:32 +00001046 // Record the offset of this source-location entry.
1047 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1048
1049 // Figure out which record code to use.
1050 unsigned Code;
1051 if (SLoc->isFile()) {
1052 if (SLoc->getFile().getContentCache()->Entry)
1053 Code = pch::SM_SLOC_FILE_ENTRY;
1054 else
1055 Code = pch::SM_SLOC_BUFFER_ENTRY;
1056 } else
1057 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1058 Record.clear();
1059 Record.push_back(Code);
1060
1061 Record.push_back(SLoc->getOffset());
1062 if (SLoc->isFile()) {
1063 const SrcMgr::FileInfo &File = SLoc->getFile();
1064 Record.push_back(File.getIncludeLoc().getRawEncoding());
1065 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1066 Record.push_back(File.hasLineDirectives());
1067
1068 const SrcMgr::ContentCache *Content = File.getContentCache();
1069 if (Content->Entry) {
1070 // The source location entry is a file. The blob associated
1071 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001072
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001073 // Emit size/modification time for this file.
1074 Record.push_back(Content->Entry->getSize());
1075 Record.push_back(Content->Entry->getModificationTime());
1076
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001077 // Emit header-search information associated with this file.
1078 HeaderFileInfo HFI;
1079 HeaderSearch &HS = PP.getHeaderSearchInfo();
1080 if (Content->Entry->getUID() < HS.header_file_size())
1081 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1082 Record.push_back(HFI.isImport);
1083 Record.push_back(HFI.DirInfo);
1084 Record.push_back(HFI.NumIncludes);
1085 AddIdentifierRef(HFI.ControllingMacro, Record);
1086
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001087 // Turn the file name into an absolute path, if it isn't already.
1088 const char *Filename = Content->Entry->getName();
1089 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001090 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001091 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001092
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001093 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001094 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001095
1096 // FIXME: For now, preload all file source locations, so that
1097 // we get the appropriate File entries in the reader. This is
1098 // a temporary measure.
1099 PreloadSLocs.push_back(SLocEntryOffsets.size());
1100 } else {
1101 // The source location entry is a buffer. The blob associated
1102 // with this entry contains the contents of the buffer.
1103
1104 // We add one to the size so that we capture the trailing NULL
1105 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1106 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001107 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001108 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001109 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001110 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1111 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001112 Record.clear();
1113 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1114 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001115 llvm::StringRef(Buffer->getBufferStart(),
1116 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001117
1118 if (strcmp(Name, "<built-in>") == 0)
1119 PreloadSLocs.push_back(SLocEntryOffsets.size());
1120 }
1121 } else {
1122 // The source location entry is an instantiation.
1123 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1124 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1125 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1126 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1127
1128 // Compute the token length for this macro expansion.
1129 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001130 if (I + 1 != N)
1131 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001132 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1133 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1134 }
1135 }
1136
Douglas Gregor8f45df52009-04-16 22:23:12 +00001137 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001138
1139 if (SLocEntryOffsets.empty())
1140 return;
1141
1142 // Write the source-location offsets table into the PCH block. This
1143 // table is used for lazily loading source-location information.
1144 using namespace llvm;
1145 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1146 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1150 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001151
Douglas Gregor258ae542009-04-27 06:38:32 +00001152 Record.clear();
1153 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1154 Record.push_back(SLocEntryOffsets.size());
1155 Record.push_back(SourceMgr.getNextOffset());
1156 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001157 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001158 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001159
1160 // Write the source location entry preloads array, telling the PCH
1161 // reader which source locations entries it should load eagerly.
1162 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001163}
1164
Douglas Gregorc5046832009-04-27 18:38:38 +00001165//===----------------------------------------------------------------------===//
1166// Preprocessor Serialization
1167//===----------------------------------------------------------------------===//
1168
Chris Lattnereeffaef2009-04-10 17:15:23 +00001169/// \brief Writes the block containing the serialized form of the
1170/// preprocessor.
1171///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001172void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001173 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001174
Chris Lattner0af3ba12009-04-13 01:29:17 +00001175 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1176 if (PP.getCounterValue() != 0) {
1177 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001178 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001179 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001180 }
1181
1182 // Enter the preprocessor block.
1183 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001184
Douglas Gregoreda6a892009-04-26 00:07:37 +00001185 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1186 // FIXME: use diagnostics subsystem for localization etc.
1187 if (PP.SawDateOrTime())
1188 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001189
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001190 // Loop over all the macro definitions that are live at the end of the file,
1191 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001192 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001193 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1194 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001195 // FIXME: This emits macros in hash table order, we should do it in a stable
1196 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001197 MacroInfo *MI = I->second;
1198
1199 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1200 // been redefined by the header (in which case they are not isBuiltinMacro).
1201 if (MI->isBuiltinMacro())
1202 continue;
1203
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001204 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001205 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001206 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1207 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001209 unsigned Code;
1210 if (MI->isObjectLike()) {
1211 Code = pch::PP_MACRO_OBJECT_LIKE;
1212 } else {
1213 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001214
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001215 Record.push_back(MI->isC99Varargs());
1216 Record.push_back(MI->isGNUVarargs());
1217 Record.push_back(MI->getNumArgs());
1218 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1219 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001220 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001221 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001222
1223 // If we have a detailed preprocessing record, record the macro definition
1224 // ID that corresponds to this macro.
1225 if (PPRec)
1226 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1227
Douglas Gregor8f45df52009-04-16 22:23:12 +00001228 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001229 Record.clear();
1230
Chris Lattner2199f5b2009-04-10 18:08:30 +00001231 // Emit the tokens array.
1232 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1233 // Note that we know that the preprocessor does not have any annotation
1234 // tokens in it because they are created by the parser, and thus can't be
1235 // in a macro definition.
1236 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001237
Chris Lattner2199f5b2009-04-10 18:08:30 +00001238 Record.push_back(Tok.getLocation().getRawEncoding());
1239 Record.push_back(Tok.getLength());
1240
Chris Lattner2199f5b2009-04-10 18:08:30 +00001241 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1242 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001243 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001244
Chris Lattner2199f5b2009-04-10 18:08:30 +00001245 // FIXME: Should translate token kind to a stable encoding.
1246 Record.push_back(Tok.getKind());
1247 // FIXME: Should translate token flags to a stable encoding.
1248 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001249
Douglas Gregor8f45df52009-04-16 22:23:12 +00001250 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001251 Record.clear();
1252 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001253 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001254 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001255
1256 // If the preprocessor has a preprocessing record, emit it.
1257 unsigned NumPreprocessingRecords = 0;
1258 if (PPRec) {
1259 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1260 E != EEnd; ++E) {
1261 Record.clear();
1262
1263 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1264 Record.push_back(NumPreprocessingRecords++);
1265 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1266 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1267 AddIdentifierRef(MI->getName(), Record);
1268 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1269 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1270 continue;
1271 }
1272
1273 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1274 // Record this macro definition's location.
1275 pch::IdentID ID = getMacroDefinitionID(MD);
1276 if (ID != MacroDefinitionOffsets.size()) {
1277 if (ID > MacroDefinitionOffsets.size())
1278 MacroDefinitionOffsets.resize(ID + 1);
1279
1280 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1281 } else
1282 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1283
1284 Record.push_back(NumPreprocessingRecords++);
1285 Record.push_back(ID);
1286 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1287 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1288 AddIdentifierRef(MD->getName(), Record);
1289 AddSourceLocation(MD->getLocation(), Record);
1290 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1291 continue;
1292 }
1293 }
1294 }
1295
Douglas Gregor8f45df52009-04-16 22:23:12 +00001296 Stream.ExitBlock();
Douglas Gregoraae92242010-03-19 21:51:54 +00001297
1298 // Write the offsets table for the preprocessing record.
1299 if (NumPreprocessingRecords > 0) {
1300 // Write the offsets table for identifier IDs.
1301 using namespace llvm;
1302 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1303 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1304 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1307 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1308
1309 Record.clear();
1310 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1311 Record.push_back(NumPreprocessingRecords);
1312 Record.push_back(MacroDefinitionOffsets.size());
1313 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1314 (const char *)&MacroDefinitionOffsets.front(),
1315 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1316 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001317}
1318
Douglas Gregorc5046832009-04-27 18:38:38 +00001319//===----------------------------------------------------------------------===//
1320// Type Serialization
1321//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001322
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001323/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001324void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001325 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001326 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001327 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001328
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001329 // Record the offset for this type.
1330 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001331 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001332 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1333 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001334 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001335 }
1336
1337 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001338
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001339 // Emit the type's representation.
1340 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001341
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001342 if (T.hasLocalNonFastQualifiers()) {
1343 Qualifiers Qs = T.getLocalQualifiers();
1344 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001345 Record.push_back(Qs.getAsOpaqueValue());
1346 W.Code = pch::TYPE_EXT_QUAL;
1347 } else {
1348 switch (T->getTypeClass()) {
1349 // For all of the concrete, non-dependent types, call the
1350 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001351#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001352 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001353#define ABSTRACT_TYPE(Class, Base)
1354#define DEPENDENT_TYPE(Class, Base)
1355#include "clang/AST/TypeNodes.def"
1356
John McCall8ccfcb52009-09-24 19:53:00 +00001357 // For all of the dependent type nodes (which only occur in C++
1358 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001359#define TYPE(Class, Base)
1360#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1361#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001362 assert(false && "Cannot serialize dependent type nodes");
1363 break;
1364 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001365 }
1366
1367 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001368 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001369
1370 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001371 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001372}
1373
Douglas Gregorc5046832009-04-27 18:38:38 +00001374//===----------------------------------------------------------------------===//
1375// Declaration Serialization
1376//===----------------------------------------------------------------------===//
1377
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001378/// \brief Write the block containing all of the declaration IDs
1379/// lexically declared within the given DeclContext.
1380///
1381/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1382/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001383uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001384 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001385 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001386 return 0;
1387
Douglas Gregor8f45df52009-04-16 22:23:12 +00001388 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001389 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001390 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1391 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001392 AddDeclRef(*D, Record);
1393
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001394 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001395 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001396 return Offset;
1397}
1398
1399/// \brief Write the block containing all of the declaration IDs
1400/// visible from the given DeclContext.
1401///
1402/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1403/// bistream, or 0 if no block was written.
1404uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1405 DeclContext *DC) {
1406 if (DC->getPrimaryContext() != DC)
1407 return 0;
1408
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001409 // Since there is no name lookup into functions or methods, and we
1410 // perform name lookup for the translation unit via the
1411 // IdentifierInfo chains, don't bother to build a
1412 // visible-declarations table for these entities.
1413 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001414 return 0;
1415
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001416 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001417 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001418
1419 // Serialize the contents of the mapping used for lookup. Note that,
1420 // although we have two very different code paths, the serialized
1421 // representation is the same for both cases: a declaration name,
1422 // followed by a size, followed by references to the visible
1423 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001424 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001425 RecordData Record;
1426 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001427 if (!Map)
1428 return 0;
1429
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001430 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1431 D != DEnd; ++D) {
1432 AddDeclarationName(D->first, Record);
1433 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1434 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001435 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001436 AddDeclRef(*Result.first, Record);
1437 }
1438
1439 if (Record.size() == 0)
1440 return 0;
1441
Douglas Gregor8f45df52009-04-16 22:23:12 +00001442 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001443 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001444 return Offset;
1445}
1446
Douglas Gregorc5046832009-04-27 18:38:38 +00001447//===----------------------------------------------------------------------===//
1448// Global Method Pool and Selector Serialization
1449//===----------------------------------------------------------------------===//
1450
Douglas Gregore84a9da2009-04-20 20:36:09 +00001451namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001452// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001453class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001454 PCHWriter &Writer;
1455
1456public:
1457 typedef Selector key_type;
1458 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001459
Douglas Gregorc78d3462009-04-24 21:10:55 +00001460 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1461 typedef const data_type& data_type_ref;
1462
1463 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001464
Douglas Gregorc78d3462009-04-24 21:10:55 +00001465 static unsigned ComputeHash(Selector Sel) {
1466 unsigned N = Sel.getNumArgs();
1467 if (N == 0)
1468 ++N;
1469 unsigned R = 5381;
1470 for (unsigned I = 0; I != N; ++I)
1471 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001472 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001473 return R;
1474 }
Mike Stump11289f42009-09-09 15:08:12 +00001475
1476 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001477 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1478 data_type_ref Methods) {
1479 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1480 clang::io::Emit16(Out, KeyLen);
1481 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001482 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001483 Method = Method->Next)
1484 if (Method->Method)
1485 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001486 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001487 Method = Method->Next)
1488 if (Method->Method)
1489 DataLen += 4;
1490 clang::io::Emit16(Out, DataLen);
1491 return std::make_pair(KeyLen, DataLen);
1492 }
Mike Stump11289f42009-09-09 15:08:12 +00001493
Douglas Gregor95c13f52009-04-25 17:48:32 +00001494 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001495 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001496 assert((Start >> 32) == 0 && "Selector key offset too large");
1497 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001498 unsigned N = Sel.getNumArgs();
1499 clang::io::Emit16(Out, N);
1500 if (N == 0)
1501 N = 1;
1502 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001503 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001504 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1505 }
Mike Stump11289f42009-09-09 15:08:12 +00001506
Douglas Gregorc78d3462009-04-24 21:10:55 +00001507 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001508 data_type_ref Methods, unsigned DataLen) {
1509 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001510 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001511 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001512 Method = Method->Next)
1513 if (Method->Method)
1514 ++NumInstanceMethods;
1515
1516 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001517 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001518 Method = Method->Next)
1519 if (Method->Method)
1520 ++NumFactoryMethods;
1521
1522 clang::io::Emit16(Out, NumInstanceMethods);
1523 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001524 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001525 Method = Method->Next)
1526 if (Method->Method)
1527 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001528 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001529 Method = Method->Next)
1530 if (Method->Method)
1531 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001532
1533 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001534 }
1535};
1536} // end anonymous namespace
1537
1538/// \brief Write the method pool into the PCH file.
1539///
1540/// The method pool contains both instance and factory methods, stored
1541/// in an on-disk hash table indexed by the selector.
1542void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1543 using namespace llvm;
1544
1545 // Create and write out the blob that contains the instance and
1546 // factor method pools.
1547 bool Empty = true;
1548 {
1549 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001550
Douglas Gregorc78d3462009-04-24 21:10:55 +00001551 // Create the on-disk hash table representation. Start by
1552 // iterating through the instance method pool.
1553 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001554 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001555 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001556 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001557 InstanceEnd = SemaRef.InstanceMethodPool.end();
1558 Instance != InstanceEnd; ++Instance) {
1559 // Check whether there is a factory method with the same
1560 // selector.
1561 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1562 = SemaRef.FactoryMethodPool.find(Instance->first);
1563
1564 if (Factory == SemaRef.FactoryMethodPool.end())
1565 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001566 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001567 ObjCMethodList()));
1568 else
1569 Generator.insert(Instance->first,
1570 std::make_pair(Instance->second, Factory->second));
1571
Douglas Gregor95c13f52009-04-25 17:48:32 +00001572 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001573 Empty = false;
1574 }
1575
1576 // Now iterate through the factory method pool, to pick up any
1577 // selectors that weren't already in the instance method pool.
1578 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001579 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001580 FactoryEnd = SemaRef.FactoryMethodPool.end();
1581 Factory != FactoryEnd; ++Factory) {
1582 // Check whether there is an instance method with the same
1583 // selector. If so, there is no work to do here.
1584 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1585 = SemaRef.InstanceMethodPool.find(Factory->first);
1586
Douglas Gregor95c13f52009-04-25 17:48:32 +00001587 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001588 Generator.insert(Factory->first,
1589 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001590 ++NumSelectorsInMethodPool;
1591 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001592
1593 Empty = false;
1594 }
1595
Douglas Gregor95c13f52009-04-25 17:48:32 +00001596 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001597 return;
1598
1599 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001600 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001601 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001602 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001603 {
1604 PCHMethodPoolTrait Trait(*this);
1605 llvm::raw_svector_ostream Out(MethodPool);
1606 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001607 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001608 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001609
1610 // For every selector that we have seen but which was not
1611 // written into the hash table, write the selector itself and
1612 // record it's offset.
1613 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1614 if (SelectorOffsets[I] == 0)
1615 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001616 }
1617
1618 // Create a blob abbreviation
1619 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1620 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001623 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1624 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1625
Douglas Gregor95c13f52009-04-25 17:48:32 +00001626 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001627 RecordData Record;
1628 Record.push_back(pch::METHOD_POOL);
1629 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001630 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001631 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001632
1633 // Create a blob abbreviation for the selector table offsets.
1634 Abbrev = new BitCodeAbbrev();
1635 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1636 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1637 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1638 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1639
1640 // Write the selector offsets table.
1641 Record.clear();
1642 Record.push_back(pch::SELECTOR_OFFSETS);
1643 Record.push_back(SelectorOffsets.size());
1644 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1645 (const char *)&SelectorOffsets.front(),
1646 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001647 }
1648}
1649
Douglas Gregorc5046832009-04-27 18:38:38 +00001650//===----------------------------------------------------------------------===//
1651// Identifier Table Serialization
1652//===----------------------------------------------------------------------===//
1653
Douglas Gregorc78d3462009-04-24 21:10:55 +00001654namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001655class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001656 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001657 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001658
Douglas Gregor1d583f22009-04-28 21:18:29 +00001659 /// \brief Determines whether this is an "interesting" identifier
1660 /// that needs a full IdentifierInfo structure written into the hash
1661 /// table.
1662 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1663 return II->isPoisoned() ||
1664 II->isExtensionToken() ||
1665 II->hasMacroDefinition() ||
1666 II->getObjCOrBuiltinID() ||
1667 II->getFETokenInfo<void>();
1668 }
1669
Douglas Gregore84a9da2009-04-20 20:36:09 +00001670public:
1671 typedef const IdentifierInfo* key_type;
1672 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001673
Douglas Gregore84a9da2009-04-20 20:36:09 +00001674 typedef pch::IdentID data_type;
1675 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001676
1677 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001678 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001679
1680 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001681 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001682 }
Mike Stump11289f42009-09-09 15:08:12 +00001683
1684 std::pair<unsigned,unsigned>
1685 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001686 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001687 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001688 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1689 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001690 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001691 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001692 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001693 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001694 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1695 DEnd = IdentifierResolver::end();
1696 D != DEnd; ++D)
1697 DataLen += sizeof(pch::DeclID);
1698 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001699 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001700 // We emit the key length after the data length so that every
1701 // string is preceded by a 16-bit length. This matches the PTH
1702 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001703 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001704 return std::make_pair(KeyLen, DataLen);
1705 }
Mike Stump11289f42009-09-09 15:08:12 +00001706
1707 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001708 unsigned KeyLen) {
1709 // Record the location of the key data. This is used when generating
1710 // the mapping from persistent IDs to strings.
1711 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001712 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001713 }
Mike Stump11289f42009-09-09 15:08:12 +00001714
1715 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001716 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001717 if (!isInterestingIdentifier(II)) {
1718 clang::io::Emit32(Out, ID << 1);
1719 return;
1720 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001721
Douglas Gregor1d583f22009-04-28 21:18:29 +00001722 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001723 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001724 bool hasMacroDefinition =
1725 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001726 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001727 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001728 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1729 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1730 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1731 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001732 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001733
Douglas Gregorc3366a52009-04-21 23:56:24 +00001734 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001735 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001736
Douglas Gregora868bbd2009-04-21 22:25:48 +00001737 // Emit the declaration IDs in reverse order, because the
1738 // IdentifierResolver provides the declarations as they would be
1739 // visible (e.g., the function "stat" would come before the struct
1740 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1741 // adds declarations to the end of the list (so we need to see the
1742 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001743 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001744 IdentifierResolver::end());
1745 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1746 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001747 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001748 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001749 }
1750};
1751} // end anonymous namespace
1752
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001753/// \brief Write the identifier table into the PCH file.
1754///
1755/// The identifier table consists of a blob containing string data
1756/// (the actual identifiers themselves) and a separate "offsets" index
1757/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001758void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001759 using namespace llvm;
1760
1761 // Create and write out the blob that contains the identifier
1762 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001763 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001764 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001765
Douglas Gregore6648fb2009-04-28 20:33:11 +00001766 // Look for any identifiers that were named while processing the
1767 // headers, but are otherwise not needed. We add these to the hash
1768 // table to enable checking of the predefines buffer in the case
1769 // where the user adds new macro definitions when building the PCH
1770 // file.
1771 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1772 IDEnd = PP.getIdentifierTable().end();
1773 ID != IDEnd; ++ID)
1774 getIdentifierRef(ID->second);
1775
Douglas Gregore84a9da2009-04-20 20:36:09 +00001776 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001777 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001778 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1779 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1780 ID != IDEnd; ++ID) {
1781 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001782 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001783 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001784
Douglas Gregore84a9da2009-04-20 20:36:09 +00001785 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001786 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001787 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001788 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001789 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001790 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001791 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001792 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001793 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001794 }
1795
1796 // Create a blob abbreviation
1797 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1798 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001800 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001801 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001802
1803 // Write the identifier table
1804 RecordData Record;
1805 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001806 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001807 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001808 }
1809
1810 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001811 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1812 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1815 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1816
1817 RecordData Record;
1818 Record.push_back(pch::IDENTIFIER_OFFSET);
1819 Record.push_back(IdentifierOffsets.size());
1820 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1821 (const char *)&IdentifierOffsets.front(),
1822 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001823}
1824
Douglas Gregorc5046832009-04-27 18:38:38 +00001825//===----------------------------------------------------------------------===//
1826// General Serialization Routines
1827//===----------------------------------------------------------------------===//
1828
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001829/// \brief Write a record containing the given attributes.
1830void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1831 RecordData Record;
1832 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001833 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001834 Record.push_back(Attr->isInherited());
1835 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001836 default:
1837 assert(0 && "Does not support PCH writing for this attribute yet!");
1838 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001839 case Attr::Alias:
1840 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1841 break;
1842
1843 case Attr::Aligned:
1844 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1845 break;
1846
1847 case Attr::AlwaysInline:
1848 break;
Mike Stump11289f42009-09-09 15:08:12 +00001849
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001850 case Attr::AnalyzerNoReturn:
1851 break;
1852
1853 case Attr::Annotate:
1854 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1855 break;
1856
1857 case Attr::AsmLabel:
1858 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1859 break;
1860
Alexis Hunt54a02542009-11-25 04:20:27 +00001861 case Attr::BaseCheck:
1862 break;
1863
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001864 case Attr::Blocks:
1865 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1866 break;
1867
Eli Friedmane4310c82009-11-09 18:38:53 +00001868 case Attr::CDecl:
1869 break;
1870
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001871 case Attr::Cleanup:
1872 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1873 break;
1874
1875 case Attr::Const:
1876 break;
1877
1878 case Attr::Constructor:
1879 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1880 break;
1881
1882 case Attr::DLLExport:
1883 case Attr::DLLImport:
1884 case Attr::Deprecated:
1885 break;
1886
1887 case Attr::Destructor:
1888 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1889 break;
1890
1891 case Attr::FastCall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001892 case Attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001893 break;
1894
1895 case Attr::Format: {
1896 const FormatAttr *Format = cast<FormatAttr>(Attr);
1897 AddString(Format->getType(), Record);
1898 Record.push_back(Format->getFormatIdx());
1899 Record.push_back(Format->getFirstArg());
1900 break;
1901 }
1902
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001903 case Attr::FormatArg: {
1904 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1905 Record.push_back(Format->getFormatIdx());
1906 break;
1907 }
1908
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001909 case Attr::Sentinel : {
1910 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1911 Record.push_back(Sentinel->getSentinel());
1912 Record.push_back(Sentinel->getNullPos());
1913 break;
1914 }
Mike Stump11289f42009-09-09 15:08:12 +00001915
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001916 case Attr::GNUInline:
Alexis Hunt54a02542009-11-25 04:20:27 +00001917 case Attr::Hiding:
Ted Kremenek06be9682010-02-17 02:37:45 +00001918 case Attr::IBActionKind:
Ted Kremenek79478e32010-02-18 00:05:52 +00001919 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001920 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001921 case Attr::NoDebug:
Ted Kremenek79478e32010-02-18 00:05:52 +00001922 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001923 case Attr::NoReturn:
1924 case Attr::NoThrow:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001925 break;
1926
1927 case Attr::NonNull: {
1928 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1929 Record.push_back(NonNull->size());
1930 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1931 break;
1932 }
1933
Ted Kremenekd9c66632010-02-18 00:05:45 +00001934 case Attr::CFReturnsNotRetained:
1935 case Attr::CFReturnsRetained:
1936 case Attr::NSReturnsNotRetained:
1937 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001938 case Attr::ObjCException:
1939 case Attr::ObjCNSObject:
1940 case Attr::Overloadable:
Alexis Hunt54a02542009-11-25 04:20:27 +00001941 case Attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001942 break;
1943
Anders Carlsson68e0b682009-08-08 18:23:56 +00001944 case Attr::PragmaPack:
1945 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001946 break;
1947
Anders Carlsson68e0b682009-08-08 18:23:56 +00001948 case Attr::Packed:
1949 break;
Mike Stump11289f42009-09-09 15:08:12 +00001950
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001951 case Attr::Pure:
1952 break;
1953
1954 case Attr::Regparm:
1955 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1956 break;
Mike Stump11289f42009-09-09 15:08:12 +00001957
Nate Begemanf2758702009-06-26 06:32:41 +00001958 case Attr::ReqdWorkGroupSize:
1959 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1960 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1961 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1962 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001963
1964 case Attr::Section:
1965 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1966 break;
1967
1968 case Attr::StdCall:
1969 case Attr::TransparentUnion:
1970 case Attr::Unavailable:
1971 case Attr::Unused:
1972 case Attr::Used:
1973 break;
1974
1975 case Attr::Visibility:
1976 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001977 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001978 break;
1979
1980 case Attr::WarnUnusedResult:
1981 case Attr::Weak:
Rafael Espindolac18086a2010-02-23 22:00:30 +00001982 case Attr::WeakRef:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001983 case Attr::WeakImport:
1984 break;
1985 }
1986 }
1987
Douglas Gregor8f45df52009-04-16 22:23:12 +00001988 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001989}
1990
1991void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1992 Record.push_back(Str.size());
1993 Record.insert(Record.end(), Str.begin(), Str.end());
1994}
1995
Douglas Gregore84a9da2009-04-20 20:36:09 +00001996/// \brief Note that the identifier II occurs at the given offset
1997/// within the identifier table.
1998void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001999 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002000}
2001
Douglas Gregor95c13f52009-04-25 17:48:32 +00002002/// \brief Note that the selector Sel occurs at the given offset
2003/// within the method pool/selector table.
2004void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2005 unsigned ID = SelectorIDs[Sel];
2006 assert(ID && "Unknown selector");
2007 SelectorOffsets[ID - 1] = Offset;
2008}
2009
Mike Stump11289f42009-09-09 15:08:12 +00002010PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2011 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002012 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2013 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002014
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002015void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2016 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00002017 using namespace llvm;
2018
Douglas Gregor162dd022009-04-20 15:53:59 +00002019 ASTContext &Context = SemaRef.Context;
2020 Preprocessor &PP = SemaRef.PP;
2021
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002022 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002023 Stream.Emit((unsigned)'C', 8);
2024 Stream.Emit((unsigned)'P', 8);
2025 Stream.Emit((unsigned)'C', 8);
2026 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002027
Chris Lattner28fa4e62009-04-26 22:26:21 +00002028 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002029
2030 // The translation unit is the first declaration we'll emit.
2031 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002032 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002033
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002034 // Make sure that we emit IdentifierInfos (and any attached
2035 // declarations) for builtins.
2036 {
2037 IdentifierTable &Table = PP.getIdentifierTable();
2038 llvm::SmallVector<const char *, 32> BuiltinNames;
2039 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2040 Context.getLangOptions().NoBuiltin);
2041 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2042 getIdentifierRef(&Table.get(BuiltinNames[I]));
2043 }
2044
Chris Lattner0c797362009-09-08 18:19:27 +00002045 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002046 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002047 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002048 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002049 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2050 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002051 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002052
Tanya Lattner90073802010-02-12 00:07:30 +00002053 // Build a record containing all of the static unused functions in this file.
2054 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002055 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattner90073802010-02-12 00:07:30 +00002056 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002057
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002058 // Build a record containing all of the locally-scoped external
2059 // declarations in this header file. Generally, this record will be
2060 // empty.
2061 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00002062 // FIXME: This is filling in the PCH file in densemap order which is
2063 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002064 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002065 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2066 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2067 TD != TDEnd; ++TD)
2068 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2069
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002070 // Build a record containing all of the ext_vector declarations.
2071 RecordData ExtVectorDecls;
2072 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2073 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2074
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002075 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002076 RecordData Record;
Douglas Gregoraae92242010-03-19 21:51:54 +00002077 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002078 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00002079 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002080 if (StatCalls && !isysroot)
2081 WriteStatCache(*StatCalls, isysroot);
2082 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002083 // Write the record of special types.
2084 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002085
Steve Naroffc277ad12009-07-18 15:33:26 +00002086 AddTypeRef(Context.getBuiltinVaListType(), Record);
2087 AddTypeRef(Context.getObjCIdType(), Record);
2088 AddTypeRef(Context.getObjCSelType(), Record);
2089 AddTypeRef(Context.getObjCProtoType(), Record);
2090 AddTypeRef(Context.getObjCClassType(), Record);
2091 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2092 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2093 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002094 AddTypeRef(Context.getjmp_bufType(), Record);
2095 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002096 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2097 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002098 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002099 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002100 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2101 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002102 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002103
Douglas Gregor1970d882009-04-26 03:49:13 +00002104 // Keep writing types and declarations until all types and
2105 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002106 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2107 WriteDeclsBlockAbbrevs();
2108 while (!DeclTypesToEmit.empty()) {
2109 DeclOrType DOT = DeclTypesToEmit.front();
2110 DeclTypesToEmit.pop();
2111 if (DOT.isType())
2112 WriteType(DOT.getType());
2113 else
2114 WriteDecl(Context, DOT.getDecl());
2115 }
2116 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002117
Douglas Gregor45053152009-10-17 17:25:45 +00002118 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002119 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002120 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002121
2122 // Write the type offsets array
2123 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2124 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2127 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2128 Record.clear();
2129 Record.push_back(pch::TYPE_OFFSET);
2130 Record.push_back(TypeOffsets.size());
2131 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002132 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002133 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002134
Douglas Gregor745ed142009-04-25 18:35:21 +00002135 // Write the declaration offsets array
2136 Abbrev = new BitCodeAbbrev();
2137 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2139 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2140 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2141 Record.clear();
2142 Record.push_back(pch::DECL_OFFSET);
2143 Record.push_back(DeclOffsets.size());
2144 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002145 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002146 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002147
Douglas Gregord4df8652009-04-22 22:02:47 +00002148 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002149 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002150 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002151
2152 // Write the record containing tentative definitions.
2153 if (!TentativeDefinitions.empty())
2154 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002155
Tanya Lattner90073802010-02-12 00:07:30 +00002156 // Write the record containing unused static functions.
2157 if (!UnusedStaticFuncs.empty())
2158 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002159
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002160 // Write the record containing locally-scoped external definitions.
2161 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002162 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002163 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002164
2165 // Write the record containing ext_vector type names.
2166 if (!ExtVectorDecls.empty())
2167 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002168
Douglas Gregor08f01292009-04-17 22:13:46 +00002169 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002170 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002171 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002172 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002173 Record.push_back(NumLexicalDeclContexts);
2174 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002175 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002176 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002177}
2178
2179void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2180 Record.push_back(Loc.getRawEncoding());
2181}
2182
2183void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2184 Record.push_back(Value.getBitWidth());
2185 unsigned N = Value.getNumWords();
2186 const uint64_t* Words = Value.getRawData();
2187 for (unsigned I = 0; I != N; ++I)
2188 Record.push_back(Words[I]);
2189}
2190
Douglas Gregor1daeb692009-04-13 18:14:40 +00002191void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2192 Record.push_back(Value.isUnsigned());
2193 AddAPInt(Value, Record);
2194}
2195
Douglas Gregore0a3a512009-04-14 21:55:33 +00002196void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2197 AddAPInt(Value.bitcastToAPInt(), Record);
2198}
2199
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002200void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002201 Record.push_back(getIdentifierRef(II));
2202}
2203
2204pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2205 if (II == 0)
2206 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002207
2208 pch::IdentID &ID = IdentifierIDs[II];
2209 if (ID == 0)
2210 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002211 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002212}
2213
Douglas Gregoraae92242010-03-19 21:51:54 +00002214pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2215 if (MD == 0)
2216 return 0;
2217
2218 pch::IdentID &ID = MacroDefinitions[MD];
2219 if (ID == 0)
2220 ID = MacroDefinitions.size();
2221 return ID;
2222}
2223
Steve Naroff2ddea052009-04-23 10:39:46 +00002224void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2225 if (SelRef.getAsOpaquePtr() == 0) {
2226 Record.push_back(0);
2227 return;
2228 }
2229
2230 pch::SelectorID &SID = SelectorIDs[SelRef];
2231 if (SID == 0) {
2232 SID = SelectorIDs.size();
2233 SelVector.push_back(SelRef);
2234 }
2235 Record.push_back(SID);
2236}
2237
John McCall0ad16662009-10-29 08:12:44 +00002238void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2239 RecordData &Record) {
2240 switch (Arg.getArgument().getKind()) {
2241 case TemplateArgument::Expression:
2242 AddStmt(Arg.getLocInfo().getAsExpr());
2243 break;
2244 case TemplateArgument::Type:
John McCallbcd03502009-12-07 02:54:59 +00002245 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002246 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002247 case TemplateArgument::Template:
2248 Record.push_back(
2249 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2250 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2251 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2252 break;
John McCall0ad16662009-10-29 08:12:44 +00002253 case TemplateArgument::Null:
2254 case TemplateArgument::Integral:
2255 case TemplateArgument::Declaration:
2256 case TemplateArgument::Pack:
2257 break;
2258 }
2259}
2260
John McCallbcd03502009-12-07 02:54:59 +00002261void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2262 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002263 AddTypeRef(QualType(), Record);
2264 return;
2265 }
2266
John McCallbcd03502009-12-07 02:54:59 +00002267 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002268 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002269 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002270 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002271}
2272
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002273void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2274 if (T.isNull()) {
2275 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2276 return;
2277 }
2278
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002279 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002280 T.removeFastQualifiers();
2281
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002282 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002283 pch::TypeID &ID = TypeIDs[T];
2284 if (ID == 0) {
2285 // We haven't seen these qualifiers applied to this type before.
2286 // Assign it a new ID. This is the only time we enqueue a
2287 // qualified type, and it has no CV qualifiers.
2288 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002289 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002290 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002291
John McCall8ccfcb52009-09-24 19:53:00 +00002292 // Encode the type qualifiers in the type reference.
2293 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2294 return;
2295 }
2296
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002297 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002298
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002299 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002300 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002301 switch (BT->getKind()) {
2302 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2303 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2304 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2305 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2306 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2307 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2308 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2309 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002310 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002311 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2312 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2313 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2314 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2315 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2316 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2317 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002318 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002319 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2320 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2321 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002322 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002323 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2324 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002325 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2326 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002327 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2328 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002329 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002330 case BuiltinType::UndeducedAuto:
2331 assert(0 && "Should not see undeduced auto here");
2332 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002333 }
2334
John McCall8ccfcb52009-09-24 19:53:00 +00002335 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002336 return;
2337 }
2338
John McCall8ccfcb52009-09-24 19:53:00 +00002339 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002340 if (ID == 0) {
2341 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002342 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002343 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002344 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002345 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002346
2347 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002348 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002349}
2350
2351void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2352 if (D == 0) {
2353 Record.push_back(0);
2354 return;
2355 }
2356
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002357 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002358 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002359 // We haven't seen this declaration before. Give it a new ID and
2360 // enqueue it in the list of declarations to emit.
2361 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002362 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002363 }
2364
2365 Record.push_back(ID);
2366}
2367
Douglas Gregore84a9da2009-04-20 20:36:09 +00002368pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2369 if (D == 0)
2370 return 0;
2371
2372 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2373 return DeclIDs[D];
2374}
2375
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002376void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002377 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002378 Record.push_back(Name.getNameKind());
2379 switch (Name.getNameKind()) {
2380 case DeclarationName::Identifier:
2381 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2382 break;
2383
2384 case DeclarationName::ObjCZeroArgSelector:
2385 case DeclarationName::ObjCOneArgSelector:
2386 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002387 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002388 break;
2389
2390 case DeclarationName::CXXConstructorName:
2391 case DeclarationName::CXXDestructorName:
2392 case DeclarationName::CXXConversionFunctionName:
2393 AddTypeRef(Name.getCXXNameType(), Record);
2394 break;
2395
2396 case DeclarationName::CXXOperatorName:
2397 Record.push_back(Name.getCXXOverloadedOperator());
2398 break;
2399
Alexis Hunt3d221f22009-11-29 07:34:05 +00002400 case DeclarationName::CXXLiteralOperatorName:
2401 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2402 break;
2403
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002404 case DeclarationName::CXXUsingDirective:
2405 // No extra data to emit
2406 break;
2407 }
2408}