blob: 5085cf43d023c9f01ab75a5ea626e7b07eaa4600 [file] [log] [blame]
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000031#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000037#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000038#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42// Type serialization
43//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000044
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000046 class PCHTypeWriter {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047 PCHWriter &Writer;
48 PCHWriter::RecordData &Record;
49
50 public:
51 /// \brief Type code that corresponds to the record generated.
52 pch::TypeCode Code;
53
Mike Stump11289f42009-09-09 15:08:12 +000054 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000055 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
57 void VisitArrayType(const ArrayType *T);
58 void VisitFunctionType(const FunctionType *T);
59 void VisitTagType(const TagType *T);
60
61#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
62#define ABSTRACT_TYPE(Class, Base)
63#define DEPENDENT_TYPE(Class, Base)
64#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
73 Writer.AddTypeRef(T->getElementType(), Record);
74 Code = pch::TYPE_COMPLEX;
75}
76
77void PCHTypeWriter::VisitPointerType(const PointerType *T) {
78 Writer.AddTypeRef(T->getPointeeType(), Record);
79 Code = pch::TYPE_POINTER;
80}
81
82void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000083 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000084 Code = pch::TYPE_BLOCK_POINTER;
85}
86
87void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_LVALUE_REFERENCE;
90}
91
92void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_RVALUE_REFERENCE;
95}
96
97void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100 Code = pch::TYPE_MEMBER_POINTER;
101}
102
103void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
104 Writer.AddTypeRef(T->getElementType(), Record);
105 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000106 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107}
108
109void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
110 VisitArrayType(T);
111 Writer.AddAPInt(T->getSize(), Record);
112 Code = pch::TYPE_CONSTANT_ARRAY;
113}
114
115void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
116 VisitArrayType(T);
117 Code = pch::TYPE_INCOMPLETE_ARRAY;
118}
119
120void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
121 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000122 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
123 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000124 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125 Code = pch::TYPE_VARIABLE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVectorType(const VectorType *T) {
129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getNumElements());
John Thompson22334602010-02-05 00:12:22 +0000131 Record.push_back(T->isAltiVec());
132 Record.push_back(T->isPixel());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133 Code = pch::TYPE_VECTOR;
134}
135
136void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
137 VisitVectorType(T);
138 Code = pch::TYPE_EXT_VECTOR;
139}
140
141void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
142 Writer.AddTypeRef(T->getResultType(), Record);
Douglas Gregordc728752009-12-22 18:11:50 +0000143 Record.push_back(T->getNoReturnAttr());
Douglas Gregor8c940862010-01-18 17:14:39 +0000144 // FIXME: need to stabilize encoding of calling convention...
145 Record.push_back(T->getCallConv());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146}
147
148void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
149 VisitFunctionType(T);
150 Code = pch::TYPE_FUNCTION_NO_PROTO;
151}
152
153void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
154 VisitFunctionType(T);
155 Record.push_back(T->getNumArgs());
156 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
157 Writer.AddTypeRef(T->getArgType(I), Record);
158 Record.push_back(T->isVariadic());
159 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000160 Record.push_back(T->hasExceptionSpec());
161 Record.push_back(T->hasAnyExceptionSpec());
162 Record.push_back(T->getNumExceptions());
163 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
164 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165 Code = pch::TYPE_FUNCTION_PROTO;
166}
167
John McCallb96ec562009-12-04 22:46:56 +0000168#if 0
169// For when we want it....
170void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_UNRESOLVED_USING;
173}
174#endif
175
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
177 Writer.AddDeclRef(T->getDecl(), Record);
178 Code = pch::TYPE_TYPEDEF;
179}
180
181void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000182 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 Code = pch::TYPE_TYPEOF_EXPR;
184}
185
186void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
187 Writer.AddTypeRef(T->getUnderlyingType(), Record);
188 Code = pch::TYPE_TYPEOF;
189}
190
Anders Carlsson81df7b82009-06-24 19:06:50 +0000191void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
192 Writer.AddStmt(T->getUnderlyingExpr());
193 Code = pch::TYPE_DECLTYPE;
194}
195
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196void PCHTypeWriter::VisitTagType(const TagType *T) {
197 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000198 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199 "Cannot serialize in the middle of a type definition");
200}
201
202void PCHTypeWriter::VisitRecordType(const RecordType *T) {
203 VisitTagType(T);
204 Code = pch::TYPE_RECORD;
205}
206
207void PCHTypeWriter::VisitEnumType(const EnumType *T) {
208 VisitTagType(T);
209 Code = pch::TYPE_ENUM;
210}
211
John McCallfcc33b02009-09-05 00:15:47 +0000212void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
213 Writer.AddTypeRef(T->getUnderlyingType(), Record);
214 Record.push_back(T->getTagKind());
215 Code = pch::TYPE_ELABORATED;
216}
217
Mike Stump11289f42009-09-09 15:08:12 +0000218void
John McCallcebee162009-10-18 09:09:24 +0000219PCHTypeWriter::VisitSubstTemplateTypeParmType(
220 const SubstTemplateTypeParmType *T) {
221 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
222 Writer.AddTypeRef(T->getReplacementType(), Record);
223 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
224}
225
226void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227PCHTypeWriter::VisitTemplateSpecializationType(
228 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000229 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230 assert(false && "Cannot serialize template specialization types");
231}
232
233void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000234 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 assert(false && "Cannot serialize qualified name types");
236}
237
238void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
239 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000241 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
242 E = T->qual_end(); I != E; ++I)
243 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000244 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245}
246
Steve Narofffb4330f2009-06-17 22:40:22 +0000247void
248PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000249 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000250 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000251 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000252 E = T->qual_end(); I != E; ++I)
253 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000254 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255}
256
John McCall8f115c62009-10-16 21:56:05 +0000257namespace {
258
259class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
260 PCHWriter &Writer;
261 PCHWriter::RecordData &Record;
262
263public:
264 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
265 : Writer(Writer), Record(Record) { }
266
John McCall17001972009-10-18 01:05:36 +0000267#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000268#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000269 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000270#include "clang/AST/TypeLocNodes.def"
271
John McCall17001972009-10-18 01:05:36 +0000272 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
273 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000274};
275
276}
277
John McCall17001972009-10-18 01:05:36 +0000278void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
279 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000280}
John McCall17001972009-10-18 01:05:36 +0000281void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000282 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
283 if (TL.needsExtraLocalData()) {
284 Record.push_back(TL.getWrittenTypeSpec());
285 Record.push_back(TL.getWrittenSignSpec());
286 Record.push_back(TL.getWrittenWidthSpec());
287 Record.push_back(TL.hasModeAttr());
288 }
John McCall8f115c62009-10-16 21:56:05 +0000289}
John McCall17001972009-10-18 01:05:36 +0000290void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
291 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000292}
John McCall17001972009-10-18 01:05:36 +0000293void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
294 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000295}
John McCall17001972009-10-18 01:05:36 +0000296void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
297 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000298}
John McCall17001972009-10-18 01:05:36 +0000299void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
300 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000301}
John McCall17001972009-10-18 01:05:36 +0000302void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
303 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000304}
John McCall17001972009-10-18 01:05:36 +0000305void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
306 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000307}
John McCall17001972009-10-18 01:05:36 +0000308void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
309 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
310 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
311 Record.push_back(TL.getSizeExpr() ? 1 : 0);
312 if (TL.getSizeExpr())
313 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000314}
John McCall17001972009-10-18 01:05:36 +0000315void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
316 VisitArrayTypeLoc(TL);
317}
318void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
319 VisitArrayTypeLoc(TL);
320}
321void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
322 VisitArrayTypeLoc(TL);
323}
324void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
325 DependentSizedArrayTypeLoc TL) {
326 VisitArrayTypeLoc(TL);
327}
328void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
329 DependentSizedExtVectorTypeLoc TL) {
330 Writer.AddSourceLocation(TL.getNameLoc(), Record);
331}
332void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
333 Writer.AddSourceLocation(TL.getNameLoc(), Record);
334}
335void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
336 Writer.AddSourceLocation(TL.getNameLoc(), Record);
337}
338void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
340 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
341 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
342 Writer.AddDeclRef(TL.getArg(i), Record);
343}
344void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
345 VisitFunctionTypeLoc(TL);
346}
347void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
348 VisitFunctionTypeLoc(TL);
349}
John McCallb96ec562009-12-04 22:46:56 +0000350void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
351 Writer.AddSourceLocation(TL.getNameLoc(), Record);
352}
John McCall17001972009-10-18 01:05:36 +0000353void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
354 Writer.AddSourceLocation(TL.getNameLoc(), Record);
355}
356void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000357 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
358 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
359 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000360}
361void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000362 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
363 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
364 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
365 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000366}
367void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
368 Writer.AddSourceLocation(TL.getNameLoc(), Record);
369}
370void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getNameLoc(), Record);
372}
373void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getNameLoc(), Record);
375}
376void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
John McCallcebee162009-10-18 09:09:24 +0000382void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
383 SubstTemplateTypeParmTypeLoc TL) {
384 Writer.AddSourceLocation(TL.getNameLoc(), Record);
385}
John McCall17001972009-10-18 01:05:36 +0000386void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
387 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000388 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
389 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
390 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
391 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
392 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall17001972009-10-18 01:05:36 +0000393}
394void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
395 Writer.AddSourceLocation(TL.getNameLoc(), Record);
396}
397void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
398 Writer.AddSourceLocation(TL.getNameLoc(), Record);
399}
400void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000402 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
403 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
404 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
405 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000406}
John McCallfc93cf92009-10-22 22:37:11 +0000407void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getStarLoc(), Record);
409 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
410 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
411 Record.push_back(TL.hasBaseTypeAsWritten());
412 Record.push_back(TL.hasProtocolsAsWritten());
413 if (TL.hasProtocolsAsWritten())
414 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
415 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
416}
John McCall8f115c62009-10-16 21:56:05 +0000417
Chris Lattner19cea4e2009-04-22 05:57:30 +0000418//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000419// PCHWriter Implementation
420//===----------------------------------------------------------------------===//
421
Chris Lattner28fa4e62009-04-26 22:26:21 +0000422static void EmitBlockID(unsigned ID, const char *Name,
423 llvm::BitstreamWriter &Stream,
424 PCHWriter::RecordData &Record) {
425 Record.clear();
426 Record.push_back(ID);
427 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
428
429 // Emit the block name if present.
430 if (Name == 0 || Name[0] == 0) return;
431 Record.clear();
432 while (*Name)
433 Record.push_back(*Name++);
434 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
435}
436
437static void EmitRecordID(unsigned ID, const char *Name,
438 llvm::BitstreamWriter &Stream,
439 PCHWriter::RecordData &Record) {
440 Record.clear();
441 Record.push_back(ID);
442 while (*Name)
443 Record.push_back(*Name++);
444 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000445}
446
447static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
448 PCHWriter::RecordData &Record) {
449#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
450 RECORD(STMT_STOP);
451 RECORD(STMT_NULL_PTR);
452 RECORD(STMT_NULL);
453 RECORD(STMT_COMPOUND);
454 RECORD(STMT_CASE);
455 RECORD(STMT_DEFAULT);
456 RECORD(STMT_LABEL);
457 RECORD(STMT_IF);
458 RECORD(STMT_SWITCH);
459 RECORD(STMT_WHILE);
460 RECORD(STMT_DO);
461 RECORD(STMT_FOR);
462 RECORD(STMT_GOTO);
463 RECORD(STMT_INDIRECT_GOTO);
464 RECORD(STMT_CONTINUE);
465 RECORD(STMT_BREAK);
466 RECORD(STMT_RETURN);
467 RECORD(STMT_DECL);
468 RECORD(STMT_ASM);
469 RECORD(EXPR_PREDEFINED);
470 RECORD(EXPR_DECL_REF);
471 RECORD(EXPR_INTEGER_LITERAL);
472 RECORD(EXPR_FLOATING_LITERAL);
473 RECORD(EXPR_IMAGINARY_LITERAL);
474 RECORD(EXPR_STRING_LITERAL);
475 RECORD(EXPR_CHARACTER_LITERAL);
476 RECORD(EXPR_PAREN);
477 RECORD(EXPR_UNARY_OPERATOR);
478 RECORD(EXPR_SIZEOF_ALIGN_OF);
479 RECORD(EXPR_ARRAY_SUBSCRIPT);
480 RECORD(EXPR_CALL);
481 RECORD(EXPR_MEMBER);
482 RECORD(EXPR_BINARY_OPERATOR);
483 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
484 RECORD(EXPR_CONDITIONAL_OPERATOR);
485 RECORD(EXPR_IMPLICIT_CAST);
486 RECORD(EXPR_CSTYLE_CAST);
487 RECORD(EXPR_COMPOUND_LITERAL);
488 RECORD(EXPR_EXT_VECTOR_ELEMENT);
489 RECORD(EXPR_INIT_LIST);
490 RECORD(EXPR_DESIGNATED_INIT);
491 RECORD(EXPR_IMPLICIT_VALUE_INIT);
492 RECORD(EXPR_VA_ARG);
493 RECORD(EXPR_ADDR_LABEL);
494 RECORD(EXPR_STMT);
495 RECORD(EXPR_TYPES_COMPATIBLE);
496 RECORD(EXPR_CHOOSE);
497 RECORD(EXPR_GNU_NULL);
498 RECORD(EXPR_SHUFFLE_VECTOR);
499 RECORD(EXPR_BLOCK);
500 RECORD(EXPR_BLOCK_DECL_REF);
501 RECORD(EXPR_OBJC_STRING_LITERAL);
502 RECORD(EXPR_OBJC_ENCODE);
503 RECORD(EXPR_OBJC_SELECTOR_EXPR);
504 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
505 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
506 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
507 RECORD(EXPR_OBJC_KVC_REF_EXPR);
508 RECORD(EXPR_OBJC_MESSAGE_EXPR);
509 RECORD(EXPR_OBJC_SUPER_EXPR);
510 RECORD(STMT_OBJC_FOR_COLLECTION);
511 RECORD(STMT_OBJC_CATCH);
512 RECORD(STMT_OBJC_FINALLY);
513 RECORD(STMT_OBJC_AT_TRY);
514 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
515 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000516 RECORD(EXPR_CXX_OPERATOR_CALL);
517 RECORD(EXPR_CXX_CONSTRUCT);
518 RECORD(EXPR_CXX_STATIC_CAST);
519 RECORD(EXPR_CXX_DYNAMIC_CAST);
520 RECORD(EXPR_CXX_REINTERPRET_CAST);
521 RECORD(EXPR_CXX_CONST_CAST);
522 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
523 RECORD(EXPR_CXX_BOOL_LITERAL);
524 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000525#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000526}
Mike Stump11289f42009-09-09 15:08:12 +0000527
Chris Lattner28fa4e62009-04-26 22:26:21 +0000528void PCHWriter::WriteBlockInfoBlock() {
529 RecordData Record;
530 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000531
Chris Lattner64031982009-04-27 00:40:25 +0000532#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000533#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000534
Chris Lattner28fa4e62009-04-26 22:26:21 +0000535 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000536 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000537 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000538 RECORD(TYPE_OFFSET);
539 RECORD(DECL_OFFSET);
540 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000541 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000542 RECORD(IDENTIFIER_OFFSET);
543 RECORD(IDENTIFIER_TABLE);
544 RECORD(EXTERNAL_DEFINITIONS);
545 RECORD(SPECIAL_TYPES);
546 RECORD(STATISTICS);
547 RECORD(TENTATIVE_DEFINITIONS);
548 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
549 RECORD(SELECTOR_OFFSETS);
550 RECORD(METHOD_POOL);
551 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000552 RECORD(SOURCE_LOCATION_OFFSETS);
553 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000554 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000555 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000556 RECORD(COMMENT_RANGES);
Ted Kremenek17437132010-01-22 20:59:36 +0000557 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000558
Chris Lattner28fa4e62009-04-26 22:26:21 +0000559 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000560 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000561 RECORD(SM_SLOC_FILE_ENTRY);
562 RECORD(SM_SLOC_BUFFER_ENTRY);
563 RECORD(SM_SLOC_BUFFER_BLOB);
564 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
565 RECORD(SM_LINE_TABLE);
566 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner28fa4e62009-04-26 22:26:21 +0000568 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000569 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000570 RECORD(PP_MACRO_OBJECT_LIKE);
571 RECORD(PP_MACRO_FUNCTION_LIKE);
572 RECORD(PP_TOKEN);
573
Douglas Gregor12bfa382009-10-17 00:13:19 +0000574 // Decls and Types block.
575 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000576 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000577 RECORD(TYPE_COMPLEX);
578 RECORD(TYPE_POINTER);
579 RECORD(TYPE_BLOCK_POINTER);
580 RECORD(TYPE_LVALUE_REFERENCE);
581 RECORD(TYPE_RVALUE_REFERENCE);
582 RECORD(TYPE_MEMBER_POINTER);
583 RECORD(TYPE_CONSTANT_ARRAY);
584 RECORD(TYPE_INCOMPLETE_ARRAY);
585 RECORD(TYPE_VARIABLE_ARRAY);
586 RECORD(TYPE_VECTOR);
587 RECORD(TYPE_EXT_VECTOR);
588 RECORD(TYPE_FUNCTION_PROTO);
589 RECORD(TYPE_FUNCTION_NO_PROTO);
590 RECORD(TYPE_TYPEDEF);
591 RECORD(TYPE_TYPEOF_EXPR);
592 RECORD(TYPE_TYPEOF);
593 RECORD(TYPE_RECORD);
594 RECORD(TYPE_ENUM);
595 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000596 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000597 RECORD(DECL_ATTR);
598 RECORD(DECL_TRANSLATION_UNIT);
599 RECORD(DECL_TYPEDEF);
600 RECORD(DECL_ENUM);
601 RECORD(DECL_RECORD);
602 RECORD(DECL_ENUM_CONSTANT);
603 RECORD(DECL_FUNCTION);
604 RECORD(DECL_OBJC_METHOD);
605 RECORD(DECL_OBJC_INTERFACE);
606 RECORD(DECL_OBJC_PROTOCOL);
607 RECORD(DECL_OBJC_IVAR);
608 RECORD(DECL_OBJC_AT_DEFS_FIELD);
609 RECORD(DECL_OBJC_CLASS);
610 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
611 RECORD(DECL_OBJC_CATEGORY);
612 RECORD(DECL_OBJC_CATEGORY_IMPL);
613 RECORD(DECL_OBJC_IMPLEMENTATION);
614 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
615 RECORD(DECL_OBJC_PROPERTY);
616 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000617 RECORD(DECL_FIELD);
618 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000619 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000620 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000621 RECORD(DECL_FILE_SCOPE_ASM);
622 RECORD(DECL_BLOCK);
623 RECORD(DECL_CONTEXT_LEXICAL);
624 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000625 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000626 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000627#undef RECORD
628#undef BLOCK
629 Stream.ExitBlock();
630}
631
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000632/// \brief Adjusts the given filename to only write out the portion of the
633/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000634///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000635/// \param Filename the file name to adjust.
636///
637/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
638/// the returned filename will be adjusted by this system root.
639///
640/// \returns either the original filename (if it needs no adjustment) or the
641/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000642static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000643adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
644 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000645
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000646 if (!isysroot)
647 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000648
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000649 // Verify that the filename and the system root have the same prefix.
650 unsigned Pos = 0;
651 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
652 if (Filename[Pos] != isysroot[Pos])
653 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000654
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000655 // We hit the end of the filename before we hit the end of the system root.
656 if (!Filename[Pos])
657 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000658
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000659 // If the file name has a '/' at the current position, skip over the '/'.
660 // We distinguish sysroot-based includes from absolute includes by the
661 // absence of '/' at the beginning of sysroot-based includes.
662 if (Filename[Pos] == '/')
663 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000664
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000665 return Filename + Pos;
666}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000667
Douglas Gregor7b71e632009-04-27 22:23:34 +0000668/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000669void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000670 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000671
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000672 // Metadata
673 const TargetInfo &Target = Context.Target;
674 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
675 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
676 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
677 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
678 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
679 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
680 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
681 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
682 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000683
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000684 RecordData Record;
685 Record.push_back(pch::METADATA);
686 Record.push_back(pch::VERSION_MAJOR);
687 Record.push_back(pch::VERSION_MINOR);
688 Record.push_back(CLANG_VERSION_MAJOR);
689 Record.push_back(CLANG_VERSION_MINOR);
690 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000691 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000692 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000693
Douglas Gregor45fe0362009-05-12 01:31:05 +0000694 // Original file name
695 SourceManager &SM = Context.getSourceManager();
696 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
697 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
698 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
699 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
700 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
701
702 llvm::sys::Path MainFilePath(MainFile->getName());
703 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000704
Douglas Gregor45fe0362009-05-12 01:31:05 +0000705 if (!MainFilePath.isAbsolute()) {
706 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000707 P.appendComponent(MainFilePath.str());
708 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000709 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000710 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000711 }
712
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000713 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000714 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000715 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000716 RecordData Record;
717 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000718 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000719 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000720
Ted Kremenek18e066f2010-01-22 22:12:47 +0000721 // Repository branch/version information.
722 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
723 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
724 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
725 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000726 Record.clear();
Ted Kremenek17437132010-01-22 20:59:36 +0000727 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000728 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
729 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000730}
731
732/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000733void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
734 RecordData Record;
735 Record.push_back(LangOpts.Trigraphs);
736 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
737 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
738 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
739 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
740 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
741 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
742 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
743 Record.push_back(LangOpts.C99); // C99 Support
744 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
745 Record.push_back(LangOpts.CPlusPlus); // C++ Support
746 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000747 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000748
Douglas Gregor55abb232009-04-10 20:39:37 +0000749 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
750 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
751 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000752
Douglas Gregor55abb232009-04-10 20:39:37 +0000753 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000754 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
755 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000756 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000757 Record.push_back(LangOpts.Exceptions); // Support exception handling.
758
759 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
760 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
761 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
762
Chris Lattner258172e2009-04-27 07:35:58 +0000763 // Whether static initializers are protected by locks.
764 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000765 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000766 Record.push_back(LangOpts.Blocks); // block extension to C
767 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
768 // they are unused.
769 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
770 // (modulo the platform support).
771
772 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
773 // signed integer arithmetic overflows.
774
775 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
776 // may be ripped out at any time.
777
778 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000779 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000780 // defined.
781 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
782 // opposed to __DYNAMIC__).
783 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
784
785 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
786 // used (instead of C99 semantics).
787 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000788 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
789 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000790 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
791 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000792 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000793 Record.push_back(LangOpts.getGCMode());
794 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000795 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000796 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000797 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000798 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000799 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000800 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000801}
802
Douglas Gregora7f71a92009-04-10 03:52:48 +0000803//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000804// stat cache Serialization
805//===----------------------------------------------------------------------===//
806
807namespace {
808// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000809class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000810public:
811 typedef const char * key_type;
812 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000813
Douglas Gregorc5046832009-04-27 18:38:38 +0000814 typedef std::pair<int, struct stat> data_type;
815 typedef const data_type& data_type_ref;
816
817 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000818 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000819 }
Mike Stump11289f42009-09-09 15:08:12 +0000820
821 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000822 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
823 data_type_ref Data) {
824 unsigned StrLen = strlen(path);
825 clang::io::Emit16(Out, StrLen);
826 unsigned DataLen = 1; // result value
827 if (Data.first == 0)
828 DataLen += 4 + 4 + 2 + 8 + 8;
829 clang::io::Emit8(Out, DataLen);
830 return std::make_pair(StrLen + 1, DataLen);
831 }
Mike Stump11289f42009-09-09 15:08:12 +0000832
Douglas Gregorc5046832009-04-27 18:38:38 +0000833 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
834 Out.write(path, KeyLen);
835 }
Mike Stump11289f42009-09-09 15:08:12 +0000836
Douglas Gregorc5046832009-04-27 18:38:38 +0000837 void EmitData(llvm::raw_ostream& Out, key_type_ref,
838 data_type_ref Data, unsigned DataLen) {
839 using namespace clang::io;
840 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000841
Douglas Gregorc5046832009-04-27 18:38:38 +0000842 // Result of stat()
843 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000844
Douglas Gregorc5046832009-04-27 18:38:38 +0000845 if (Data.first == 0) {
846 Emit32(Out, (uint32_t) Data.second.st_ino);
847 Emit32(Out, (uint32_t) Data.second.st_dev);
848 Emit16(Out, (uint16_t) Data.second.st_mode);
849 Emit64(Out, (uint64_t) Data.second.st_mtime);
850 Emit64(Out, (uint64_t) Data.second.st_size);
851 }
852
853 assert(Out.tell() - Start == DataLen && "Wrong data length");
854 }
855};
856} // end anonymous namespace
857
858/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000859void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
860 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000861 // Build the on-disk hash table containing information about every
862 // stat() call.
863 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
864 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000865 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000866 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000867 Stat != StatEnd; ++Stat, ++NumStatEntries) {
868 const char *Filename = Stat->first();
869 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
870 Generator.insert(Filename, Stat->second);
871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Douglas Gregorc5046832009-04-27 18:38:38 +0000873 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000874 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000875 uint32_t BucketOffset;
876 {
877 llvm::raw_svector_ostream Out(StatCacheData);
878 // Make sure that no bucket is at offset 0
879 clang::io::Emit32(Out, 0);
880 BucketOffset = Generator.Emit(Out);
881 }
882
883 // Create a blob abbreviation
884 using namespace llvm;
885 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
886 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
889 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
890 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
891
892 // Write the stat cache
893 RecordData Record;
894 Record.push_back(pch::STAT_CACHE);
895 Record.push_back(BucketOffset);
896 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000897 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000898}
899
900//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000901// Source Manager Serialization
902//===----------------------------------------------------------------------===//
903
904/// \brief Create an abbreviation for the SLocEntry that refers to a
905/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000906static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000907 using namespace llvm;
908 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
909 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000915 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000916}
917
918/// \brief Create an abbreviation for the SLocEntry that refers to a
919/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000920static unsigned CreateSLocBufferAbbrev(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_BUFFER_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
928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000929 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000930}
931
932/// \brief Create an abbreviation for the SLocEntry that refers to a
933/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000934static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000935 using namespace llvm;
936 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
937 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000939 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000940}
941
942/// \brief Create an abbreviation for the SLocEntry that refers to an
943/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000944static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000945 using namespace llvm;
946 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
947 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000953 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000954}
955
956/// \brief Writes the block containing the serialized form of the
957/// source manager.
958///
959/// TODO: We should probably use an on-disk hash table (stored in a
960/// blob), indexed based on the file name, so that we only create
961/// entries for files that we actually need. In the common case (no
962/// errors), we probably won't have to create file entries for any of
963/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000964void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000965 const Preprocessor &PP,
966 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000967 RecordData Record;
968
Chris Lattner0910e3b2009-04-10 17:16:57 +0000969 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000970 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000971
972 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000973 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
974 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
975 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
976 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000977
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000978 // Write the line table.
979 if (SourceMgr.hasLineTable()) {
980 LineTableInfo &LineTable = SourceMgr.getLineTable();
981
982 // Emit the file names
983 Record.push_back(LineTable.getNumFilenames());
984 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
985 // Emit the file name
986 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000987 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000988 unsigned FilenameLen = Filename? strlen(Filename) : 0;
989 Record.push_back(FilenameLen);
990 if (FilenameLen)
991 Record.insert(Record.end(), Filename, Filename + FilenameLen);
992 }
Mike Stump11289f42009-09-09 15:08:12 +0000993
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000994 // Emit the line entries
995 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
996 L != LEnd; ++L) {
997 // Emit the file ID
998 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000999
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001000 // Emit the line entries
1001 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001002 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001003 LEEnd = L->second.end();
1004 LE != LEEnd; ++LE) {
1005 Record.push_back(LE->FileOffset);
1006 Record.push_back(LE->LineNo);
1007 Record.push_back(LE->FilenameID);
1008 Record.push_back((unsigned)LE->FileKind);
1009 Record.push_back(LE->IncludeOffset);
1010 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001011 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +00001012 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001013 }
1014
Douglas Gregor258ae542009-04-27 06:38:32 +00001015 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +00001016 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +00001017 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001018 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +00001019 E = HS.header_file_end();
1020 I != E; ++I) {
1021 Record.push_back(I->isImport);
1022 Record.push_back(I->DirInfo);
1023 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +00001024 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001025 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1026 Record.clear();
1027 }
1028
Douglas Gregor258ae542009-04-27 06:38:32 +00001029 // Write out the source location entry table. We skip the first
1030 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001031 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001032 RecordData PreloadSLocs;
1033 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001034 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1035 // Get this source location entry.
1036 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1037
Douglas Gregor258ae542009-04-27 06:38:32 +00001038 // Record the offset of this source-location entry.
1039 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1040
1041 // Figure out which record code to use.
1042 unsigned Code;
1043 if (SLoc->isFile()) {
1044 if (SLoc->getFile().getContentCache()->Entry)
1045 Code = pch::SM_SLOC_FILE_ENTRY;
1046 else
1047 Code = pch::SM_SLOC_BUFFER_ENTRY;
1048 } else
1049 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1050 Record.clear();
1051 Record.push_back(Code);
1052
1053 Record.push_back(SLoc->getOffset());
1054 if (SLoc->isFile()) {
1055 const SrcMgr::FileInfo &File = SLoc->getFile();
1056 Record.push_back(File.getIncludeLoc().getRawEncoding());
1057 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1058 Record.push_back(File.hasLineDirectives());
1059
1060 const SrcMgr::ContentCache *Content = File.getContentCache();
1061 if (Content->Entry) {
1062 // The source location entry is a file. The blob associated
1063 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001064
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001065 // Turn the file name into an absolute path, if it isn't already.
1066 const char *Filename = Content->Entry->getName();
1067 llvm::sys::Path FilePath(Filename, strlen(Filename));
1068 std::string FilenameStr;
1069 if (!FilePath.isAbsolute()) {
1070 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001071 P.appendComponent(FilePath.str());
1072 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001073 Filename = FilenameStr.c_str();
1074 }
Mike Stump11289f42009-09-09 15:08:12 +00001075
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001076 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001077 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001078
1079 // FIXME: For now, preload all file source locations, so that
1080 // we get the appropriate File entries in the reader. This is
1081 // a temporary measure.
1082 PreloadSLocs.push_back(SLocEntryOffsets.size());
1083 } else {
1084 // The source location entry is a buffer. The blob associated
1085 // with this entry contains the contents of the buffer.
1086
1087 // We add one to the size so that we capture the trailing NULL
1088 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1089 // the reader side).
1090 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1091 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001092 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1093 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001094 Record.clear();
1095 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1096 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001097 llvm::StringRef(Buffer->getBufferStart(),
1098 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001099
1100 if (strcmp(Name, "<built-in>") == 0)
1101 PreloadSLocs.push_back(SLocEntryOffsets.size());
1102 }
1103 } else {
1104 // The source location entry is an instantiation.
1105 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1106 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1107 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1108 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1109
1110 // Compute the token length for this macro expansion.
1111 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001112 if (I + 1 != N)
1113 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001114 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1115 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1116 }
1117 }
1118
Douglas Gregor8f45df52009-04-16 22:23:12 +00001119 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001120
1121 if (SLocEntryOffsets.empty())
1122 return;
1123
1124 // Write the source-location offsets table into the PCH block. This
1125 // table is used for lazily loading source-location information.
1126 using namespace llvm;
1127 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1128 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1129 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1130 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1131 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1132 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001133
Douglas Gregor258ae542009-04-27 06:38:32 +00001134 Record.clear();
1135 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1136 Record.push_back(SLocEntryOffsets.size());
1137 Record.push_back(SourceMgr.getNextOffset());
1138 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001139 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001140 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001141
1142 // Write the source location entry preloads array, telling the PCH
1143 // reader which source locations entries it should load eagerly.
1144 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001145}
1146
Douglas Gregorc5046832009-04-27 18:38:38 +00001147//===----------------------------------------------------------------------===//
1148// Preprocessor Serialization
1149//===----------------------------------------------------------------------===//
1150
Chris Lattnereeffaef2009-04-10 17:15:23 +00001151/// \brief Writes the block containing the serialized form of the
1152/// preprocessor.
1153///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001154void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001155 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001156
Chris Lattner0af3ba12009-04-13 01:29:17 +00001157 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1158 if (PP.getCounterValue() != 0) {
1159 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001160 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001161 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001162 }
1163
1164 // Enter the preprocessor block.
1165 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001166
Douglas Gregoreda6a892009-04-26 00:07:37 +00001167 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1168 // FIXME: use diagnostics subsystem for localization etc.
1169 if (PP.SawDateOrTime())
1170 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001171
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001172 // Loop over all the macro definitions that are live at the end of the file,
1173 // emitting each to the PP section.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001174 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1175 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001176 // FIXME: This emits macros in hash table order, we should do it in a stable
1177 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001178 MacroInfo *MI = I->second;
1179
1180 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1181 // been redefined by the header (in which case they are not isBuiltinMacro).
1182 if (MI->isBuiltinMacro())
1183 continue;
1184
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001185 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001186 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001187 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1188 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001189
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001190 unsigned Code;
1191 if (MI->isObjectLike()) {
1192 Code = pch::PP_MACRO_OBJECT_LIKE;
1193 } else {
1194 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001196 Record.push_back(MI->isC99Varargs());
1197 Record.push_back(MI->isGNUVarargs());
1198 Record.push_back(MI->getNumArgs());
1199 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1200 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001201 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001202 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001203 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001204 Record.clear();
1205
Chris Lattner2199f5b2009-04-10 18:08:30 +00001206 // Emit the tokens array.
1207 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1208 // Note that we know that the preprocessor does not have any annotation
1209 // tokens in it because they are created by the parser, and thus can't be
1210 // in a macro definition.
1211 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001212
Chris Lattner2199f5b2009-04-10 18:08:30 +00001213 Record.push_back(Tok.getLocation().getRawEncoding());
1214 Record.push_back(Tok.getLength());
1215
Chris Lattner2199f5b2009-04-10 18:08:30 +00001216 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1217 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001218 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001219
Chris Lattner2199f5b2009-04-10 18:08:30 +00001220 // FIXME: Should translate token kind to a stable encoding.
1221 Record.push_back(Tok.getKind());
1222 // FIXME: Should translate token flags to a stable encoding.
1223 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001224
Douglas Gregor8f45df52009-04-16 22:23:12 +00001225 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001226 Record.clear();
1227 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001228 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001229 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001230 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001231}
1232
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001233void PCHWriter::WriteComments(ASTContext &Context) {
1234 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001235
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001236 if (Context.Comments.empty())
1237 return;
Mike Stump11289f42009-09-09 15:08:12 +00001238
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001239 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1240 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1241 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1242 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001243
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001244 RecordData Record;
1245 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001246 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001247 (const char*)&Context.Comments[0],
1248 Context.Comments.size() * sizeof(SourceRange));
1249}
1250
Douglas Gregorc5046832009-04-27 18:38:38 +00001251//===----------------------------------------------------------------------===//
1252// Type Serialization
1253//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001254
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001255/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001256void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001257 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001258 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001259 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001260
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001261 // Record the offset for this type.
1262 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001263 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001264 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1265 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001266 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001267 }
1268
1269 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001270
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001271 // Emit the type's representation.
1272 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001273
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001274 if (T.hasLocalNonFastQualifiers()) {
1275 Qualifiers Qs = T.getLocalQualifiers();
1276 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001277 Record.push_back(Qs.getAsOpaqueValue());
1278 W.Code = pch::TYPE_EXT_QUAL;
1279 } else {
1280 switch (T->getTypeClass()) {
1281 // For all of the concrete, non-dependent types, call the
1282 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001283#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001284 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001285#define ABSTRACT_TYPE(Class, Base)
1286#define DEPENDENT_TYPE(Class, Base)
1287#include "clang/AST/TypeNodes.def"
1288
John McCall8ccfcb52009-09-24 19:53:00 +00001289 // For all of the dependent type nodes (which only occur in C++
1290 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001291#define TYPE(Class, Base)
1292#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1293#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001294 assert(false && "Cannot serialize dependent type nodes");
1295 break;
1296 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001297 }
1298
1299 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001300 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001301
1302 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001303 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001304}
1305
Douglas Gregorc5046832009-04-27 18:38:38 +00001306//===----------------------------------------------------------------------===//
1307// Declaration Serialization
1308//===----------------------------------------------------------------------===//
1309
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001310/// \brief Write the block containing all of the declaration IDs
1311/// lexically declared within the given DeclContext.
1312///
1313/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1314/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001315uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001316 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001317 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001318 return 0;
1319
Douglas Gregor8f45df52009-04-16 22:23:12 +00001320 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001321 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001322 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1323 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001324 AddDeclRef(*D, Record);
1325
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001326 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001327 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001328 return Offset;
1329}
1330
1331/// \brief Write the block containing all of the declaration IDs
1332/// visible from the given DeclContext.
1333///
1334/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1335/// bistream, or 0 if no block was written.
1336uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1337 DeclContext *DC) {
1338 if (DC->getPrimaryContext() != DC)
1339 return 0;
1340
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001341 // Since there is no name lookup into functions or methods, and we
1342 // perform name lookup for the translation unit via the
1343 // IdentifierInfo chains, don't bother to build a
1344 // visible-declarations table for these entities.
1345 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001346 return 0;
1347
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001348 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001349 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001350
1351 // Serialize the contents of the mapping used for lookup. Note that,
1352 // although we have two very different code paths, the serialized
1353 // representation is the same for both cases: a declaration name,
1354 // followed by a size, followed by references to the visible
1355 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001356 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001357 RecordData Record;
1358 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001359 if (!Map)
1360 return 0;
1361
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001362 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1363 D != DEnd; ++D) {
1364 AddDeclarationName(D->first, Record);
1365 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1366 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001367 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001368 AddDeclRef(*Result.first, Record);
1369 }
1370
1371 if (Record.size() == 0)
1372 return 0;
1373
Douglas Gregor8f45df52009-04-16 22:23:12 +00001374 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001375 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001376 return Offset;
1377}
1378
Douglas Gregorc5046832009-04-27 18:38:38 +00001379//===----------------------------------------------------------------------===//
1380// Global Method Pool and Selector Serialization
1381//===----------------------------------------------------------------------===//
1382
Douglas Gregore84a9da2009-04-20 20:36:09 +00001383namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001384// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001385class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001386 PCHWriter &Writer;
1387
1388public:
1389 typedef Selector key_type;
1390 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001391
Douglas Gregorc78d3462009-04-24 21:10:55 +00001392 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1393 typedef const data_type& data_type_ref;
1394
1395 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001396
Douglas Gregorc78d3462009-04-24 21:10:55 +00001397 static unsigned ComputeHash(Selector Sel) {
1398 unsigned N = Sel.getNumArgs();
1399 if (N == 0)
1400 ++N;
1401 unsigned R = 5381;
1402 for (unsigned I = 0; I != N; ++I)
1403 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001404 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001405 return R;
1406 }
Mike Stump11289f42009-09-09 15:08:12 +00001407
1408 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001409 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1410 data_type_ref Methods) {
1411 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1412 clang::io::Emit16(Out, KeyLen);
1413 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001414 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001415 Method = Method->Next)
1416 if (Method->Method)
1417 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001418 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001419 Method = Method->Next)
1420 if (Method->Method)
1421 DataLen += 4;
1422 clang::io::Emit16(Out, DataLen);
1423 return std::make_pair(KeyLen, DataLen);
1424 }
Mike Stump11289f42009-09-09 15:08:12 +00001425
Douglas Gregor95c13f52009-04-25 17:48:32 +00001426 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001427 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001428 assert((Start >> 32) == 0 && "Selector key offset too large");
1429 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001430 unsigned N = Sel.getNumArgs();
1431 clang::io::Emit16(Out, N);
1432 if (N == 0)
1433 N = 1;
1434 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001435 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001436 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1437 }
Mike Stump11289f42009-09-09 15:08:12 +00001438
Douglas Gregorc78d3462009-04-24 21:10:55 +00001439 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001440 data_type_ref Methods, unsigned DataLen) {
1441 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001442 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001443 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001444 Method = Method->Next)
1445 if (Method->Method)
1446 ++NumInstanceMethods;
1447
1448 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001449 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001450 Method = Method->Next)
1451 if (Method->Method)
1452 ++NumFactoryMethods;
1453
1454 clang::io::Emit16(Out, NumInstanceMethods);
1455 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001456 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001457 Method = Method->Next)
1458 if (Method->Method)
1459 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001460 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001461 Method = Method->Next)
1462 if (Method->Method)
1463 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001464
1465 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001466 }
1467};
1468} // end anonymous namespace
1469
1470/// \brief Write the method pool into the PCH file.
1471///
1472/// The method pool contains both instance and factory methods, stored
1473/// in an on-disk hash table indexed by the selector.
1474void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1475 using namespace llvm;
1476
1477 // Create and write out the blob that contains the instance and
1478 // factor method pools.
1479 bool Empty = true;
1480 {
1481 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001482
Douglas Gregorc78d3462009-04-24 21:10:55 +00001483 // Create the on-disk hash table representation. Start by
1484 // iterating through the instance method pool.
1485 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001486 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001487 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001488 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001489 InstanceEnd = SemaRef.InstanceMethodPool.end();
1490 Instance != InstanceEnd; ++Instance) {
1491 // Check whether there is a factory method with the same
1492 // selector.
1493 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1494 = SemaRef.FactoryMethodPool.find(Instance->first);
1495
1496 if (Factory == SemaRef.FactoryMethodPool.end())
1497 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001498 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001499 ObjCMethodList()));
1500 else
1501 Generator.insert(Instance->first,
1502 std::make_pair(Instance->second, Factory->second));
1503
Douglas Gregor95c13f52009-04-25 17:48:32 +00001504 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001505 Empty = false;
1506 }
1507
1508 // Now iterate through the factory method pool, to pick up any
1509 // selectors that weren't already in the instance method pool.
1510 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001511 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001512 FactoryEnd = SemaRef.FactoryMethodPool.end();
1513 Factory != FactoryEnd; ++Factory) {
1514 // Check whether there is an instance method with the same
1515 // selector. If so, there is no work to do here.
1516 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1517 = SemaRef.InstanceMethodPool.find(Factory->first);
1518
Douglas Gregor95c13f52009-04-25 17:48:32 +00001519 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001520 Generator.insert(Factory->first,
1521 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001522 ++NumSelectorsInMethodPool;
1523 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001524
1525 Empty = false;
1526 }
1527
Douglas Gregor95c13f52009-04-25 17:48:32 +00001528 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001529 return;
1530
1531 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001532 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001533 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001534 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001535 {
1536 PCHMethodPoolTrait Trait(*this);
1537 llvm::raw_svector_ostream Out(MethodPool);
1538 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001539 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001540 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001541
1542 // For every selector that we have seen but which was not
1543 // written into the hash table, write the selector itself and
1544 // record it's offset.
1545 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1546 if (SelectorOffsets[I] == 0)
1547 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001548 }
1549
1550 // Create a blob abbreviation
1551 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1552 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1556 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1557
Douglas Gregor95c13f52009-04-25 17:48:32 +00001558 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001559 RecordData Record;
1560 Record.push_back(pch::METHOD_POOL);
1561 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001562 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001563 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001564
1565 // Create a blob abbreviation for the selector table offsets.
1566 Abbrev = new BitCodeAbbrev();
1567 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1570 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1571
1572 // Write the selector offsets table.
1573 Record.clear();
1574 Record.push_back(pch::SELECTOR_OFFSETS);
1575 Record.push_back(SelectorOffsets.size());
1576 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1577 (const char *)&SelectorOffsets.front(),
1578 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001579 }
1580}
1581
Douglas Gregorc5046832009-04-27 18:38:38 +00001582//===----------------------------------------------------------------------===//
1583// Identifier Table Serialization
1584//===----------------------------------------------------------------------===//
1585
Douglas Gregorc78d3462009-04-24 21:10:55 +00001586namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001587class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001588 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001589 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001590
Douglas Gregor1d583f22009-04-28 21:18:29 +00001591 /// \brief Determines whether this is an "interesting" identifier
1592 /// that needs a full IdentifierInfo structure written into the hash
1593 /// table.
1594 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1595 return II->isPoisoned() ||
1596 II->isExtensionToken() ||
1597 II->hasMacroDefinition() ||
1598 II->getObjCOrBuiltinID() ||
1599 II->getFETokenInfo<void>();
1600 }
1601
Douglas Gregore84a9da2009-04-20 20:36:09 +00001602public:
1603 typedef const IdentifierInfo* key_type;
1604 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001605
Douglas Gregore84a9da2009-04-20 20:36:09 +00001606 typedef pch::IdentID data_type;
1607 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001608
1609 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001610 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001611
1612 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001613 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001614 }
Mike Stump11289f42009-09-09 15:08:12 +00001615
1616 std::pair<unsigned,unsigned>
1617 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001618 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001619 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001620 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1621 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001622 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001623 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001624 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001625 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001626 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1627 DEnd = IdentifierResolver::end();
1628 D != DEnd; ++D)
1629 DataLen += sizeof(pch::DeclID);
1630 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001631 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001632 // We emit the key length after the data length so that every
1633 // string is preceded by a 16-bit length. This matches the PTH
1634 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001635 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001636 return std::make_pair(KeyLen, DataLen);
1637 }
Mike Stump11289f42009-09-09 15:08:12 +00001638
1639 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001640 unsigned KeyLen) {
1641 // Record the location of the key data. This is used when generating
1642 // the mapping from persistent IDs to strings.
1643 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001644 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001645 }
Mike Stump11289f42009-09-09 15:08:12 +00001646
1647 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001648 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001649 if (!isInterestingIdentifier(II)) {
1650 clang::io::Emit32(Out, ID << 1);
1651 return;
1652 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001653
Douglas Gregor1d583f22009-04-28 21:18:29 +00001654 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001655 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001656 bool hasMacroDefinition =
1657 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001658 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001659 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001660 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1661 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1662 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1663 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001664 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001665
Douglas Gregorc3366a52009-04-21 23:56:24 +00001666 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001667 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001668
Douglas Gregora868bbd2009-04-21 22:25:48 +00001669 // Emit the declaration IDs in reverse order, because the
1670 // IdentifierResolver provides the declarations as they would be
1671 // visible (e.g., the function "stat" would come before the struct
1672 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1673 // adds declarations to the end of the list (so we need to see the
1674 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001675 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001676 IdentifierResolver::end());
1677 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1678 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001679 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001680 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001681 }
1682};
1683} // end anonymous namespace
1684
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001685/// \brief Write the identifier table into the PCH file.
1686///
1687/// The identifier table consists of a blob containing string data
1688/// (the actual identifiers themselves) and a separate "offsets" index
1689/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001690void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001691 using namespace llvm;
1692
1693 // Create and write out the blob that contains the identifier
1694 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001695 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001696 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001697
Douglas Gregore6648fb2009-04-28 20:33:11 +00001698 // Look for any identifiers that were named while processing the
1699 // headers, but are otherwise not needed. We add these to the hash
1700 // table to enable checking of the predefines buffer in the case
1701 // where the user adds new macro definitions when building the PCH
1702 // file.
1703 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1704 IDEnd = PP.getIdentifierTable().end();
1705 ID != IDEnd; ++ID)
1706 getIdentifierRef(ID->second);
1707
Douglas Gregore84a9da2009-04-20 20:36:09 +00001708 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001709 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001710 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1711 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1712 ID != IDEnd; ++ID) {
1713 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001714 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001715 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001716
Douglas Gregore84a9da2009-04-20 20:36:09 +00001717 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001718 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001719 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001720 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001721 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001722 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001723 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001724 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001725 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001726 }
1727
1728 // Create a blob abbreviation
1729 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1730 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001731 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001733 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001734
1735 // Write the identifier table
1736 RecordData Record;
1737 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001738 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001739 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001740 }
1741
1742 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001743 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1744 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1747 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1748
1749 RecordData Record;
1750 Record.push_back(pch::IDENTIFIER_OFFSET);
1751 Record.push_back(IdentifierOffsets.size());
1752 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1753 (const char *)&IdentifierOffsets.front(),
1754 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001755}
1756
Douglas Gregorc5046832009-04-27 18:38:38 +00001757//===----------------------------------------------------------------------===//
1758// General Serialization Routines
1759//===----------------------------------------------------------------------===//
1760
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001761/// \brief Write a record containing the given attributes.
1762void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1763 RecordData Record;
1764 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001765 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001766 Record.push_back(Attr->isInherited());
1767 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001768 default:
1769 assert(0 && "Does not support PCH writing for this attribute yet!");
1770 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001771 case Attr::Alias:
1772 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1773 break;
1774
1775 case Attr::Aligned:
1776 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1777 break;
1778
1779 case Attr::AlwaysInline:
1780 break;
Mike Stump11289f42009-09-09 15:08:12 +00001781
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001782 case Attr::AnalyzerNoReturn:
1783 break;
1784
1785 case Attr::Annotate:
1786 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1787 break;
1788
1789 case Attr::AsmLabel:
1790 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1791 break;
1792
Alexis Hunt54a02542009-11-25 04:20:27 +00001793 case Attr::BaseCheck:
1794 break;
1795
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001796 case Attr::Blocks:
1797 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1798 break;
1799
Eli Friedmane4310c82009-11-09 18:38:53 +00001800 case Attr::CDecl:
1801 break;
1802
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001803 case Attr::Cleanup:
1804 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1805 break;
1806
1807 case Attr::Const:
1808 break;
1809
1810 case Attr::Constructor:
1811 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1812 break;
1813
1814 case Attr::DLLExport:
1815 case Attr::DLLImport:
1816 case Attr::Deprecated:
1817 break;
1818
1819 case Attr::Destructor:
1820 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1821 break;
1822
1823 case Attr::FastCall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001824 case Attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001825 break;
1826
1827 case Attr::Format: {
1828 const FormatAttr *Format = cast<FormatAttr>(Attr);
1829 AddString(Format->getType(), Record);
1830 Record.push_back(Format->getFormatIdx());
1831 Record.push_back(Format->getFirstArg());
1832 break;
1833 }
1834
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001835 case Attr::FormatArg: {
1836 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1837 Record.push_back(Format->getFormatIdx());
1838 break;
1839 }
1840
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001841 case Attr::Sentinel : {
1842 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1843 Record.push_back(Sentinel->getSentinel());
1844 Record.push_back(Sentinel->getNullPos());
1845 break;
1846 }
Mike Stump11289f42009-09-09 15:08:12 +00001847
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001848 case Attr::GNUInline:
Alexis Hunt54a02542009-11-25 04:20:27 +00001849 case Attr::Hiding:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001850 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001851 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001852 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001853 case Attr::NoReturn:
1854 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001855 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001856 break;
1857
1858 case Attr::NonNull: {
1859 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1860 Record.push_back(NonNull->size());
1861 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1862 break;
1863 }
1864
1865 case Attr::ObjCException:
1866 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001867 case Attr::CFReturnsRetained:
1868 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001869 case Attr::Overloadable:
Alexis Hunt54a02542009-11-25 04:20:27 +00001870 case Attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001871 break;
1872
Anders Carlsson68e0b682009-08-08 18:23:56 +00001873 case Attr::PragmaPack:
1874 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001875 break;
1876
Anders Carlsson68e0b682009-08-08 18:23:56 +00001877 case Attr::Packed:
1878 break;
Mike Stump11289f42009-09-09 15:08:12 +00001879
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001880 case Attr::Pure:
1881 break;
1882
1883 case Attr::Regparm:
1884 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1885 break;
Mike Stump11289f42009-09-09 15:08:12 +00001886
Nate Begemanf2758702009-06-26 06:32:41 +00001887 case Attr::ReqdWorkGroupSize:
1888 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1889 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1890 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1891 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001892
1893 case Attr::Section:
1894 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1895 break;
1896
1897 case Attr::StdCall:
1898 case Attr::TransparentUnion:
1899 case Attr::Unavailable:
1900 case Attr::Unused:
1901 case Attr::Used:
1902 break;
1903
1904 case Attr::Visibility:
1905 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001906 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001907 break;
1908
1909 case Attr::WarnUnusedResult:
1910 case Attr::Weak:
1911 case Attr::WeakImport:
1912 break;
1913 }
1914 }
1915
Douglas Gregor8f45df52009-04-16 22:23:12 +00001916 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001917}
1918
1919void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1920 Record.push_back(Str.size());
1921 Record.insert(Record.end(), Str.begin(), Str.end());
1922}
1923
Douglas Gregore84a9da2009-04-20 20:36:09 +00001924/// \brief Note that the identifier II occurs at the given offset
1925/// within the identifier table.
1926void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001927 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001928}
1929
Douglas Gregor95c13f52009-04-25 17:48:32 +00001930/// \brief Note that the selector Sel occurs at the given offset
1931/// within the method pool/selector table.
1932void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1933 unsigned ID = SelectorIDs[Sel];
1934 assert(ID && "Unknown selector");
1935 SelectorOffsets[ID - 1] = Offset;
1936}
1937
Mike Stump11289f42009-09-09 15:08:12 +00001938PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1939 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001940 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1941 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001942
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001943void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1944 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001945 using namespace llvm;
1946
Douglas Gregor162dd022009-04-20 15:53:59 +00001947 ASTContext &Context = SemaRef.Context;
1948 Preprocessor &PP = SemaRef.PP;
1949
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001950 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001951 Stream.Emit((unsigned)'C', 8);
1952 Stream.Emit((unsigned)'P', 8);
1953 Stream.Emit((unsigned)'C', 8);
1954 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001955
Chris Lattner28fa4e62009-04-26 22:26:21 +00001956 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001957
1958 // The translation unit is the first declaration we'll emit.
1959 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001960 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001961
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001962 // Make sure that we emit IdentifierInfos (and any attached
1963 // declarations) for builtins.
1964 {
1965 IdentifierTable &Table = PP.getIdentifierTable();
1966 llvm::SmallVector<const char *, 32> BuiltinNames;
1967 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1968 Context.getLangOptions().NoBuiltin);
1969 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1970 getIdentifierRef(&Table.get(BuiltinNames[I]));
1971 }
1972
Chris Lattner0c797362009-09-08 18:19:27 +00001973 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00001974 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00001975 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001976 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00001977 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
1978 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00001979 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001980
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001981 // Build a record containing all of the locally-scoped external
1982 // declarations in this header file. Generally, this record will be
1983 // empty.
1984 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001985 // FIXME: This is filling in the PCH file in densemap order which is
1986 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001987 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001988 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1989 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1990 TD != TDEnd; ++TD)
1991 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1992
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001993 // Build a record containing all of the ext_vector declarations.
1994 RecordData ExtVectorDecls;
1995 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1996 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1997
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001998 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001999 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00002000 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002001 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00002002 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002003 if (StatCalls && !isysroot)
2004 WriteStatCache(*StatCalls, isysroot);
2005 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00002006 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00002007 // Write the record of special types.
2008 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002009
Steve Naroffc277ad12009-07-18 15:33:26 +00002010 AddTypeRef(Context.getBuiltinVaListType(), Record);
2011 AddTypeRef(Context.getObjCIdType(), Record);
2012 AddTypeRef(Context.getObjCSelType(), Record);
2013 AddTypeRef(Context.getObjCProtoType(), Record);
2014 AddTypeRef(Context.getObjCClassType(), Record);
2015 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2016 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2017 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002018 AddTypeRef(Context.getjmp_bufType(), Record);
2019 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002020 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2021 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002022#if 0
2023 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian04b258c2009-11-25 23:07:42 +00002024 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002025#endif
Mike Stumpd0153282009-10-20 02:12:22 +00002026 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002027 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002028 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002029
Douglas Gregor1970d882009-04-26 03:49:13 +00002030 // Keep writing types and declarations until all types and
2031 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002032 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2033 WriteDeclsBlockAbbrevs();
2034 while (!DeclTypesToEmit.empty()) {
2035 DeclOrType DOT = DeclTypesToEmit.front();
2036 DeclTypesToEmit.pop();
2037 if (DOT.isType())
2038 WriteType(DOT.getType());
2039 else
2040 WriteDecl(Context, DOT.getDecl());
2041 }
2042 Stream.ExitBlock();
2043
Douglas Gregor45053152009-10-17 17:25:45 +00002044 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002045 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002046 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002047
2048 // Write the type offsets array
2049 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2050 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2052 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2053 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2054 Record.clear();
2055 Record.push_back(pch::TYPE_OFFSET);
2056 Record.push_back(TypeOffsets.size());
2057 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002058 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002059 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002060
Douglas Gregor745ed142009-04-25 18:35:21 +00002061 // Write the declaration offsets array
2062 Abbrev = new BitCodeAbbrev();
2063 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2066 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2067 Record.clear();
2068 Record.push_back(pch::DECL_OFFSET);
2069 Record.push_back(DeclOffsets.size());
2070 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002071 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002072 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002073
Douglas Gregord4df8652009-04-22 22:02:47 +00002074 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002075 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002076 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002077
2078 // Write the record containing tentative definitions.
2079 if (!TentativeDefinitions.empty())
2080 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002081
2082 // Write the record containing locally-scoped external definitions.
2083 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002084 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002085 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002086
2087 // Write the record containing ext_vector type names.
2088 if (!ExtVectorDecls.empty())
2089 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002090
Douglas Gregor08f01292009-04-17 22:13:46 +00002091 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002092 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002093 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002094 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002095 Record.push_back(NumLexicalDeclContexts);
2096 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002097 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002098 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002099}
2100
2101void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2102 Record.push_back(Loc.getRawEncoding());
2103}
2104
2105void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2106 Record.push_back(Value.getBitWidth());
2107 unsigned N = Value.getNumWords();
2108 const uint64_t* Words = Value.getRawData();
2109 for (unsigned I = 0; I != N; ++I)
2110 Record.push_back(Words[I]);
2111}
2112
Douglas Gregor1daeb692009-04-13 18:14:40 +00002113void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2114 Record.push_back(Value.isUnsigned());
2115 AddAPInt(Value, Record);
2116}
2117
Douglas Gregore0a3a512009-04-14 21:55:33 +00002118void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2119 AddAPInt(Value.bitcastToAPInt(), Record);
2120}
2121
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002122void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002123 Record.push_back(getIdentifierRef(II));
2124}
2125
2126pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2127 if (II == 0)
2128 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002129
2130 pch::IdentID &ID = IdentifierIDs[II];
2131 if (ID == 0)
2132 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002133 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002134}
2135
Steve Naroff2ddea052009-04-23 10:39:46 +00002136void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2137 if (SelRef.getAsOpaquePtr() == 0) {
2138 Record.push_back(0);
2139 return;
2140 }
2141
2142 pch::SelectorID &SID = SelectorIDs[SelRef];
2143 if (SID == 0) {
2144 SID = SelectorIDs.size();
2145 SelVector.push_back(SelRef);
2146 }
2147 Record.push_back(SID);
2148}
2149
John McCall0ad16662009-10-29 08:12:44 +00002150void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2151 RecordData &Record) {
2152 switch (Arg.getArgument().getKind()) {
2153 case TemplateArgument::Expression:
2154 AddStmt(Arg.getLocInfo().getAsExpr());
2155 break;
2156 case TemplateArgument::Type:
John McCallbcd03502009-12-07 02:54:59 +00002157 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002158 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002159 case TemplateArgument::Template:
2160 Record.push_back(
2161 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2162 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2163 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2164 break;
John McCall0ad16662009-10-29 08:12:44 +00002165 case TemplateArgument::Null:
2166 case TemplateArgument::Integral:
2167 case TemplateArgument::Declaration:
2168 case TemplateArgument::Pack:
2169 break;
2170 }
2171}
2172
John McCallbcd03502009-12-07 02:54:59 +00002173void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2174 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002175 AddTypeRef(QualType(), Record);
2176 return;
2177 }
2178
John McCallbcd03502009-12-07 02:54:59 +00002179 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002180 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002181 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00002182 TLW.Visit(TL);
2183}
2184
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002185void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2186 if (T.isNull()) {
2187 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2188 return;
2189 }
2190
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002191 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002192 T.removeFastQualifiers();
2193
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002194 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002195 pch::TypeID &ID = TypeIDs[T];
2196 if (ID == 0) {
2197 // We haven't seen these qualifiers applied to this type before.
2198 // Assign it a new ID. This is the only time we enqueue a
2199 // qualified type, and it has no CV qualifiers.
2200 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002201 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002202 }
2203
2204 // Encode the type qualifiers in the type reference.
2205 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2206 return;
2207 }
2208
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002209 assert(!T.hasLocalQualifiers());
John McCall8ccfcb52009-09-24 19:53:00 +00002210
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002211 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002212 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002213 switch (BT->getKind()) {
2214 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2215 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2216 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2217 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2218 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2219 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2220 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2221 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002222 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002223 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2224 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2225 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2226 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2227 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2228 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2229 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002230 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002231 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2232 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2233 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002234 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002235 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2236 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002237 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2238 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002239 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2240 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002241 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002242 case BuiltinType::UndeducedAuto:
2243 assert(0 && "Should not see undeduced auto here");
2244 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002245 }
2246
John McCall8ccfcb52009-09-24 19:53:00 +00002247 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002248 return;
2249 }
2250
John McCall8ccfcb52009-09-24 19:53:00 +00002251 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002252 if (ID == 0) {
2253 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002254 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002255 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002256 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002257 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002258
2259 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002260 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002261}
2262
2263void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2264 if (D == 0) {
2265 Record.push_back(0);
2266 return;
2267 }
2268
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002269 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002270 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002271 // We haven't seen this declaration before. Give it a new ID and
2272 // enqueue it in the list of declarations to emit.
2273 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002274 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002275 }
2276
2277 Record.push_back(ID);
2278}
2279
Douglas Gregore84a9da2009-04-20 20:36:09 +00002280pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2281 if (D == 0)
2282 return 0;
2283
2284 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2285 return DeclIDs[D];
2286}
2287
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002288void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002289 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002290 Record.push_back(Name.getNameKind());
2291 switch (Name.getNameKind()) {
2292 case DeclarationName::Identifier:
2293 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2294 break;
2295
2296 case DeclarationName::ObjCZeroArgSelector:
2297 case DeclarationName::ObjCOneArgSelector:
2298 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002299 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002300 break;
2301
2302 case DeclarationName::CXXConstructorName:
2303 case DeclarationName::CXXDestructorName:
2304 case DeclarationName::CXXConversionFunctionName:
2305 AddTypeRef(Name.getCXXNameType(), Record);
2306 break;
2307
2308 case DeclarationName::CXXOperatorName:
2309 Record.push_back(Name.getCXXOverloadedOperator());
2310 break;
2311
Alexis Hunt3d221f22009-11-29 07:34:05 +00002312 case DeclarationName::CXXLiteralOperatorName:
2313 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2314 break;
2315
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002316 case DeclarationName::CXXUsingDirective:
2317 // No extra data to emit
2318 break;
2319 }
2320}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002321