blob: a87b8cde2ca1cf026e4577e37332ebc268c6d1f2 [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);
516#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000517}
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner28fa4e62009-04-26 22:26:21 +0000519void PCHWriter::WriteBlockInfoBlock() {
520 RecordData Record;
521 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000522
Chris Lattner64031982009-04-27 00:40:25 +0000523#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000524#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000525
Chris Lattner28fa4e62009-04-26 22:26:21 +0000526 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000527 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000528 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000529 RECORD(TYPE_OFFSET);
530 RECORD(DECL_OFFSET);
531 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000532 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000533 RECORD(IDENTIFIER_OFFSET);
534 RECORD(IDENTIFIER_TABLE);
535 RECORD(EXTERNAL_DEFINITIONS);
536 RECORD(SPECIAL_TYPES);
537 RECORD(STATISTICS);
538 RECORD(TENTATIVE_DEFINITIONS);
539 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
540 RECORD(SELECTOR_OFFSETS);
541 RECORD(METHOD_POOL);
542 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000543 RECORD(SOURCE_LOCATION_OFFSETS);
544 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000545 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000546 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000547 RECORD(COMMENT_RANGES);
Ted Kremenek17437132010-01-22 20:59:36 +0000548 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000549
Chris Lattner28fa4e62009-04-26 22:26:21 +0000550 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000551 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000552 RECORD(SM_SLOC_FILE_ENTRY);
553 RECORD(SM_SLOC_BUFFER_ENTRY);
554 RECORD(SM_SLOC_BUFFER_BLOB);
555 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
556 RECORD(SM_LINE_TABLE);
557 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000558
Chris Lattner28fa4e62009-04-26 22:26:21 +0000559 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000560 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000561 RECORD(PP_MACRO_OBJECT_LIKE);
562 RECORD(PP_MACRO_FUNCTION_LIKE);
563 RECORD(PP_TOKEN);
564
Douglas Gregor12bfa382009-10-17 00:13:19 +0000565 // Decls and Types block.
566 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000567 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000568 RECORD(TYPE_COMPLEX);
569 RECORD(TYPE_POINTER);
570 RECORD(TYPE_BLOCK_POINTER);
571 RECORD(TYPE_LVALUE_REFERENCE);
572 RECORD(TYPE_RVALUE_REFERENCE);
573 RECORD(TYPE_MEMBER_POINTER);
574 RECORD(TYPE_CONSTANT_ARRAY);
575 RECORD(TYPE_INCOMPLETE_ARRAY);
576 RECORD(TYPE_VARIABLE_ARRAY);
577 RECORD(TYPE_VECTOR);
578 RECORD(TYPE_EXT_VECTOR);
579 RECORD(TYPE_FUNCTION_PROTO);
580 RECORD(TYPE_FUNCTION_NO_PROTO);
581 RECORD(TYPE_TYPEDEF);
582 RECORD(TYPE_TYPEOF_EXPR);
583 RECORD(TYPE_TYPEOF);
584 RECORD(TYPE_RECORD);
585 RECORD(TYPE_ENUM);
586 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000587 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000588 RECORD(DECL_ATTR);
589 RECORD(DECL_TRANSLATION_UNIT);
590 RECORD(DECL_TYPEDEF);
591 RECORD(DECL_ENUM);
592 RECORD(DECL_RECORD);
593 RECORD(DECL_ENUM_CONSTANT);
594 RECORD(DECL_FUNCTION);
595 RECORD(DECL_OBJC_METHOD);
596 RECORD(DECL_OBJC_INTERFACE);
597 RECORD(DECL_OBJC_PROTOCOL);
598 RECORD(DECL_OBJC_IVAR);
599 RECORD(DECL_OBJC_AT_DEFS_FIELD);
600 RECORD(DECL_OBJC_CLASS);
601 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
602 RECORD(DECL_OBJC_CATEGORY);
603 RECORD(DECL_OBJC_CATEGORY_IMPL);
604 RECORD(DECL_OBJC_IMPLEMENTATION);
605 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
606 RECORD(DECL_OBJC_PROPERTY);
607 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000608 RECORD(DECL_FIELD);
609 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000610 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000611 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000612 RECORD(DECL_FILE_SCOPE_ASM);
613 RECORD(DECL_BLOCK);
614 RECORD(DECL_CONTEXT_LEXICAL);
615 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000616 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000617 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000618#undef RECORD
619#undef BLOCK
620 Stream.ExitBlock();
621}
622
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000623/// \brief Adjusts the given filename to only write out the portion of the
624/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000625///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000626/// \param Filename the file name to adjust.
627///
628/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
629/// the returned filename will be adjusted by this system root.
630///
631/// \returns either the original filename (if it needs no adjustment) or the
632/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000633static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000634adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
635 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000636
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000637 if (!isysroot)
638 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000639
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000640 // Verify that the filename and the system root have the same prefix.
641 unsigned Pos = 0;
642 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
643 if (Filename[Pos] != isysroot[Pos])
644 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000645
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000646 // We hit the end of the filename before we hit the end of the system root.
647 if (!Filename[Pos])
648 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000649
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000650 // If the file name has a '/' at the current position, skip over the '/'.
651 // We distinguish sysroot-based includes from absolute includes by the
652 // absence of '/' at the beginning of sysroot-based includes.
653 if (Filename[Pos] == '/')
654 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000655
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000656 return Filename + Pos;
657}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000658
Douglas Gregor7b71e632009-04-27 22:23:34 +0000659/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000660void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000661 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000662
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000663 // Metadata
664 const TargetInfo &Target = Context.Target;
665 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
666 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
667 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
668 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
669 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
670 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
671 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
672 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
673 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000675 RecordData Record;
676 Record.push_back(pch::METADATA);
677 Record.push_back(pch::VERSION_MAJOR);
678 Record.push_back(pch::VERSION_MINOR);
679 Record.push_back(CLANG_VERSION_MAJOR);
680 Record.push_back(CLANG_VERSION_MINOR);
681 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000682 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000683 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000684
Douglas Gregor45fe0362009-05-12 01:31:05 +0000685 // Original file name
686 SourceManager &SM = Context.getSourceManager();
687 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
688 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
689 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
690 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
691 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
692
693 llvm::sys::Path MainFilePath(MainFile->getName());
694 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000695
Douglas Gregor45fe0362009-05-12 01:31:05 +0000696 if (!MainFilePath.isAbsolute()) {
697 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000698 P.appendComponent(MainFilePath.str());
699 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000700 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000701 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000702 }
703
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000704 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000705 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000706 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000707 RecordData Record;
708 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000709 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000710 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000711
Ted Kremenek18e066f2010-01-22 22:12:47 +0000712 // Repository branch/version information.
713 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
714 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
715 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
716 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000717 Record.clear();
Ted Kremenek17437132010-01-22 20:59:36 +0000718 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000719 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
720 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000721}
722
723/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000724void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
725 RecordData Record;
726 Record.push_back(LangOpts.Trigraphs);
727 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
728 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
729 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
730 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
731 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
732 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
733 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
734 Record.push_back(LangOpts.C99); // C99 Support
735 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
736 Record.push_back(LangOpts.CPlusPlus); // C++ Support
737 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000738 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000739
Douglas Gregor55abb232009-04-10 20:39:37 +0000740 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
741 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
742 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000743
Douglas Gregor55abb232009-04-10 20:39:37 +0000744 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000745 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
746 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000747 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000748 Record.push_back(LangOpts.Exceptions); // Support exception handling.
749
750 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
751 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
752 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
753
Chris Lattner258172e2009-04-27 07:35:58 +0000754 // Whether static initializers are protected by locks.
755 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000756 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000757 Record.push_back(LangOpts.Blocks); // block extension to C
758 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
759 // they are unused.
760 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
761 // (modulo the platform support).
762
763 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
764 // signed integer arithmetic overflows.
765
766 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
767 // may be ripped out at any time.
768
769 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000770 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000771 // defined.
772 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
773 // opposed to __DYNAMIC__).
774 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
775
776 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
777 // used (instead of C99 semantics).
778 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000779 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
780 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000781 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
782 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000783 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000784 Record.push_back(LangOpts.getGCMode());
785 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000786 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000787 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000788 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000789 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000790 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000791 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000792}
793
Douglas Gregora7f71a92009-04-10 03:52:48 +0000794//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000795// stat cache Serialization
796//===----------------------------------------------------------------------===//
797
798namespace {
799// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000800class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000801public:
802 typedef const char * key_type;
803 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000804
Douglas Gregorc5046832009-04-27 18:38:38 +0000805 typedef std::pair<int, struct stat> data_type;
806 typedef const data_type& data_type_ref;
807
808 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000809 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000810 }
Mike Stump11289f42009-09-09 15:08:12 +0000811
812 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000813 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
814 data_type_ref Data) {
815 unsigned StrLen = strlen(path);
816 clang::io::Emit16(Out, StrLen);
817 unsigned DataLen = 1; // result value
818 if (Data.first == 0)
819 DataLen += 4 + 4 + 2 + 8 + 8;
820 clang::io::Emit8(Out, DataLen);
821 return std::make_pair(StrLen + 1, DataLen);
822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
Douglas Gregorc5046832009-04-27 18:38:38 +0000824 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
825 Out.write(path, KeyLen);
826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Douglas Gregorc5046832009-04-27 18:38:38 +0000828 void EmitData(llvm::raw_ostream& Out, key_type_ref,
829 data_type_ref Data, unsigned DataLen) {
830 using namespace clang::io;
831 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000832
Douglas Gregorc5046832009-04-27 18:38:38 +0000833 // Result of stat()
834 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000835
Douglas Gregorc5046832009-04-27 18:38:38 +0000836 if (Data.first == 0) {
837 Emit32(Out, (uint32_t) Data.second.st_ino);
838 Emit32(Out, (uint32_t) Data.second.st_dev);
839 Emit16(Out, (uint16_t) Data.second.st_mode);
840 Emit64(Out, (uint64_t) Data.second.st_mtime);
841 Emit64(Out, (uint64_t) Data.second.st_size);
842 }
843
844 assert(Out.tell() - Start == DataLen && "Wrong data length");
845 }
846};
847} // end anonymous namespace
848
849/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000850void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
851 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000852 // Build the on-disk hash table containing information about every
853 // stat() call.
854 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
855 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000856 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000857 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000858 Stat != StatEnd; ++Stat, ++NumStatEntries) {
859 const char *Filename = Stat->first();
860 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
861 Generator.insert(Filename, Stat->second);
862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
Douglas Gregorc5046832009-04-27 18:38:38 +0000864 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000865 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000866 uint32_t BucketOffset;
867 {
868 llvm::raw_svector_ostream Out(StatCacheData);
869 // Make sure that no bucket is at offset 0
870 clang::io::Emit32(Out, 0);
871 BucketOffset = Generator.Emit(Out);
872 }
873
874 // Create a blob abbreviation
875 using namespace llvm;
876 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
877 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
880 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
881 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
882
883 // Write the stat cache
884 RecordData Record;
885 Record.push_back(pch::STAT_CACHE);
886 Record.push_back(BucketOffset);
887 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000888 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000889}
890
891//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000892// Source Manager Serialization
893//===----------------------------------------------------------------------===//
894
895/// \brief Create an abbreviation for the SLocEntry that refers to a
896/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000897static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000898 using namespace llvm;
899 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
900 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000906 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000907}
908
909/// \brief Create an abbreviation for the SLocEntry that refers to a
910/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000911static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000912 using namespace llvm;
913 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
914 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000920 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000921}
922
923/// \brief Create an abbreviation for the SLocEntry that refers to a
924/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000925static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000926 using namespace llvm;
927 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
928 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000930 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000931}
932
933/// \brief Create an abbreviation for the SLocEntry that refers to an
934/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000935static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000936 using namespace llvm;
937 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
938 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000944 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000945}
946
947/// \brief Writes the block containing the serialized form of the
948/// source manager.
949///
950/// TODO: We should probably use an on-disk hash table (stored in a
951/// blob), indexed based on the file name, so that we only create
952/// entries for files that we actually need. In the common case (no
953/// errors), we probably won't have to create file entries for any of
954/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000955void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000956 const Preprocessor &PP,
957 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000958 RecordData Record;
959
Chris Lattner0910e3b2009-04-10 17:16:57 +0000960 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000961 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000962
963 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000964 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
965 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
966 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
967 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000968
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000969 // Write the line table.
970 if (SourceMgr.hasLineTable()) {
971 LineTableInfo &LineTable = SourceMgr.getLineTable();
972
973 // Emit the file names
974 Record.push_back(LineTable.getNumFilenames());
975 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
976 // Emit the file name
977 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000978 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000979 unsigned FilenameLen = Filename? strlen(Filename) : 0;
980 Record.push_back(FilenameLen);
981 if (FilenameLen)
982 Record.insert(Record.end(), Filename, Filename + FilenameLen);
983 }
Mike Stump11289f42009-09-09 15:08:12 +0000984
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000985 // Emit the line entries
986 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
987 L != LEnd; ++L) {
988 // Emit the file ID
989 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000990
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000991 // Emit the line entries
992 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000993 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000994 LEEnd = L->second.end();
995 LE != LEEnd; ++LE) {
996 Record.push_back(LE->FileOffset);
997 Record.push_back(LE->LineNo);
998 Record.push_back(LE->FilenameID);
999 Record.push_back((unsigned)LE->FileKind);
1000 Record.push_back(LE->IncludeOffset);
1001 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001002 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +00001003 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001004 }
1005
Douglas Gregor258ae542009-04-27 06:38:32 +00001006 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +00001007 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +00001008 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001009 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +00001010 E = HS.header_file_end();
1011 I != E; ++I) {
1012 Record.push_back(I->isImport);
1013 Record.push_back(I->DirInfo);
1014 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +00001015 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001016 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1017 Record.clear();
1018 }
1019
Douglas Gregor258ae542009-04-27 06:38:32 +00001020 // Write out the source location entry table. We skip the first
1021 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001022 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001023 RecordData PreloadSLocs;
1024 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001025 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1026 // Get this source location entry.
1027 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1028
Douglas Gregor258ae542009-04-27 06:38:32 +00001029 // Record the offset of this source-location entry.
1030 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1031
1032 // Figure out which record code to use.
1033 unsigned Code;
1034 if (SLoc->isFile()) {
1035 if (SLoc->getFile().getContentCache()->Entry)
1036 Code = pch::SM_SLOC_FILE_ENTRY;
1037 else
1038 Code = pch::SM_SLOC_BUFFER_ENTRY;
1039 } else
1040 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1041 Record.clear();
1042 Record.push_back(Code);
1043
1044 Record.push_back(SLoc->getOffset());
1045 if (SLoc->isFile()) {
1046 const SrcMgr::FileInfo &File = SLoc->getFile();
1047 Record.push_back(File.getIncludeLoc().getRawEncoding());
1048 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1049 Record.push_back(File.hasLineDirectives());
1050
1051 const SrcMgr::ContentCache *Content = File.getContentCache();
1052 if (Content->Entry) {
1053 // The source location entry is a file. The blob associated
1054 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001055
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001056 // Turn the file name into an absolute path, if it isn't already.
1057 const char *Filename = Content->Entry->getName();
1058 llvm::sys::Path FilePath(Filename, strlen(Filename));
1059 std::string FilenameStr;
1060 if (!FilePath.isAbsolute()) {
1061 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001062 P.appendComponent(FilePath.str());
1063 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001064 Filename = FilenameStr.c_str();
1065 }
Mike Stump11289f42009-09-09 15:08:12 +00001066
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001067 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001068 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001069
1070 // FIXME: For now, preload all file source locations, so that
1071 // we get the appropriate File entries in the reader. This is
1072 // a temporary measure.
1073 PreloadSLocs.push_back(SLocEntryOffsets.size());
1074 } else {
1075 // The source location entry is a buffer. The blob associated
1076 // with this entry contains the contents of the buffer.
1077
1078 // We add one to the size so that we capture the trailing NULL
1079 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1080 // the reader side).
1081 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1082 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001083 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1084 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001085 Record.clear();
1086 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1087 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001088 llvm::StringRef(Buffer->getBufferStart(),
1089 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001090
1091 if (strcmp(Name, "<built-in>") == 0)
1092 PreloadSLocs.push_back(SLocEntryOffsets.size());
1093 }
1094 } else {
1095 // The source location entry is an instantiation.
1096 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1097 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1098 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1099 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1100
1101 // Compute the token length for this macro expansion.
1102 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001103 if (I + 1 != N)
1104 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001105 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1106 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1107 }
1108 }
1109
Douglas Gregor8f45df52009-04-16 22:23:12 +00001110 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001111
1112 if (SLocEntryOffsets.empty())
1113 return;
1114
1115 // Write the source-location offsets table into the PCH block. This
1116 // table is used for lazily loading source-location information.
1117 using namespace llvm;
1118 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1119 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1120 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1123 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001124
Douglas Gregor258ae542009-04-27 06:38:32 +00001125 Record.clear();
1126 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1127 Record.push_back(SLocEntryOffsets.size());
1128 Record.push_back(SourceMgr.getNextOffset());
1129 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001130 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001131 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001132
1133 // Write the source location entry preloads array, telling the PCH
1134 // reader which source locations entries it should load eagerly.
1135 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001136}
1137
Douglas Gregorc5046832009-04-27 18:38:38 +00001138//===----------------------------------------------------------------------===//
1139// Preprocessor Serialization
1140//===----------------------------------------------------------------------===//
1141
Chris Lattnereeffaef2009-04-10 17:15:23 +00001142/// \brief Writes the block containing the serialized form of the
1143/// preprocessor.
1144///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001145void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001146 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001147
Chris Lattner0af3ba12009-04-13 01:29:17 +00001148 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1149 if (PP.getCounterValue() != 0) {
1150 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001151 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001152 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001153 }
1154
1155 // Enter the preprocessor block.
1156 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001157
Douglas Gregoreda6a892009-04-26 00:07:37 +00001158 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1159 // FIXME: use diagnostics subsystem for localization etc.
1160 if (PP.SawDateOrTime())
1161 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001162
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001163 // Loop over all the macro definitions that are live at the end of the file,
1164 // emitting each to the PP section.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001165 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1166 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001167 // FIXME: This emits macros in hash table order, we should do it in a stable
1168 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001169 MacroInfo *MI = I->second;
1170
1171 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1172 // been redefined by the header (in which case they are not isBuiltinMacro).
1173 if (MI->isBuiltinMacro())
1174 continue;
1175
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001176 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001177 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001178 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1179 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001180
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001181 unsigned Code;
1182 if (MI->isObjectLike()) {
1183 Code = pch::PP_MACRO_OBJECT_LIKE;
1184 } else {
1185 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001186
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001187 Record.push_back(MI->isC99Varargs());
1188 Record.push_back(MI->isGNUVarargs());
1189 Record.push_back(MI->getNumArgs());
1190 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1191 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001192 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001193 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001194 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001195 Record.clear();
1196
Chris Lattner2199f5b2009-04-10 18:08:30 +00001197 // Emit the tokens array.
1198 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1199 // Note that we know that the preprocessor does not have any annotation
1200 // tokens in it because they are created by the parser, and thus can't be
1201 // in a macro definition.
1202 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001203
Chris Lattner2199f5b2009-04-10 18:08:30 +00001204 Record.push_back(Tok.getLocation().getRawEncoding());
1205 Record.push_back(Tok.getLength());
1206
Chris Lattner2199f5b2009-04-10 18:08:30 +00001207 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1208 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001209 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001210
Chris Lattner2199f5b2009-04-10 18:08:30 +00001211 // FIXME: Should translate token kind to a stable encoding.
1212 Record.push_back(Tok.getKind());
1213 // FIXME: Should translate token flags to a stable encoding.
1214 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001215
Douglas Gregor8f45df52009-04-16 22:23:12 +00001216 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001217 Record.clear();
1218 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001219 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001220 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001221 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001222}
1223
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001224void PCHWriter::WriteComments(ASTContext &Context) {
1225 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001226
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001227 if (Context.Comments.empty())
1228 return;
Mike Stump11289f42009-09-09 15:08:12 +00001229
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001230 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1231 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1232 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1233 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001234
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001235 RecordData Record;
1236 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001237 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001238 (const char*)&Context.Comments[0],
1239 Context.Comments.size() * sizeof(SourceRange));
1240}
1241
Douglas Gregorc5046832009-04-27 18:38:38 +00001242//===----------------------------------------------------------------------===//
1243// Type Serialization
1244//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001245
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001246/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001247void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001248 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001249 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001250 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001251
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001252 // Record the offset for this type.
1253 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001254 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001255 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1256 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001257 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001258 }
1259
1260 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001261
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001262 // Emit the type's representation.
1263 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001264
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001265 if (T.hasLocalNonFastQualifiers()) {
1266 Qualifiers Qs = T.getLocalQualifiers();
1267 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001268 Record.push_back(Qs.getAsOpaqueValue());
1269 W.Code = pch::TYPE_EXT_QUAL;
1270 } else {
1271 switch (T->getTypeClass()) {
1272 // For all of the concrete, non-dependent types, call the
1273 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001274#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001275 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001276#define ABSTRACT_TYPE(Class, Base)
1277#define DEPENDENT_TYPE(Class, Base)
1278#include "clang/AST/TypeNodes.def"
1279
John McCall8ccfcb52009-09-24 19:53:00 +00001280 // For all of the dependent type nodes (which only occur in C++
1281 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001282#define TYPE(Class, Base)
1283#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1284#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001285 assert(false && "Cannot serialize dependent type nodes");
1286 break;
1287 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001288 }
1289
1290 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001291 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001292
1293 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001294 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001295}
1296
Douglas Gregorc5046832009-04-27 18:38:38 +00001297//===----------------------------------------------------------------------===//
1298// Declaration Serialization
1299//===----------------------------------------------------------------------===//
1300
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001301/// \brief Write the block containing all of the declaration IDs
1302/// lexically declared within the given DeclContext.
1303///
1304/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1305/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001306uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001307 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001308 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001309 return 0;
1310
Douglas Gregor8f45df52009-04-16 22:23:12 +00001311 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001312 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001313 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1314 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001315 AddDeclRef(*D, Record);
1316
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001317 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001318 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001319 return Offset;
1320}
1321
1322/// \brief Write the block containing all of the declaration IDs
1323/// visible from the given DeclContext.
1324///
1325/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1326/// bistream, or 0 if no block was written.
1327uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1328 DeclContext *DC) {
1329 if (DC->getPrimaryContext() != DC)
1330 return 0;
1331
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001332 // Since there is no name lookup into functions or methods, and we
1333 // perform name lookup for the translation unit via the
1334 // IdentifierInfo chains, don't bother to build a
1335 // visible-declarations table for these entities.
1336 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001337 return 0;
1338
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001339 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001340 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001341
1342 // Serialize the contents of the mapping used for lookup. Note that,
1343 // although we have two very different code paths, the serialized
1344 // representation is the same for both cases: a declaration name,
1345 // followed by a size, followed by references to the visible
1346 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001347 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001348 RecordData Record;
1349 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001350 if (!Map)
1351 return 0;
1352
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001353 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1354 D != DEnd; ++D) {
1355 AddDeclarationName(D->first, Record);
1356 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1357 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001358 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001359 AddDeclRef(*Result.first, Record);
1360 }
1361
1362 if (Record.size() == 0)
1363 return 0;
1364
Douglas Gregor8f45df52009-04-16 22:23:12 +00001365 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001366 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001367 return Offset;
1368}
1369
Douglas Gregorc5046832009-04-27 18:38:38 +00001370//===----------------------------------------------------------------------===//
1371// Global Method Pool and Selector Serialization
1372//===----------------------------------------------------------------------===//
1373
Douglas Gregore84a9da2009-04-20 20:36:09 +00001374namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001375// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001376class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001377 PCHWriter &Writer;
1378
1379public:
1380 typedef Selector key_type;
1381 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001382
Douglas Gregorc78d3462009-04-24 21:10:55 +00001383 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1384 typedef const data_type& data_type_ref;
1385
1386 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001387
Douglas Gregorc78d3462009-04-24 21:10:55 +00001388 static unsigned ComputeHash(Selector Sel) {
1389 unsigned N = Sel.getNumArgs();
1390 if (N == 0)
1391 ++N;
1392 unsigned R = 5381;
1393 for (unsigned I = 0; I != N; ++I)
1394 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001395 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001396 return R;
1397 }
Mike Stump11289f42009-09-09 15:08:12 +00001398
1399 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001400 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1401 data_type_ref Methods) {
1402 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1403 clang::io::Emit16(Out, KeyLen);
1404 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001405 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001406 Method = Method->Next)
1407 if (Method->Method)
1408 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001409 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001410 Method = Method->Next)
1411 if (Method->Method)
1412 DataLen += 4;
1413 clang::io::Emit16(Out, DataLen);
1414 return std::make_pair(KeyLen, DataLen);
1415 }
Mike Stump11289f42009-09-09 15:08:12 +00001416
Douglas Gregor95c13f52009-04-25 17:48:32 +00001417 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001418 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001419 assert((Start >> 32) == 0 && "Selector key offset too large");
1420 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001421 unsigned N = Sel.getNumArgs();
1422 clang::io::Emit16(Out, N);
1423 if (N == 0)
1424 N = 1;
1425 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001426 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001427 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1428 }
Mike Stump11289f42009-09-09 15:08:12 +00001429
Douglas Gregorc78d3462009-04-24 21:10:55 +00001430 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001431 data_type_ref Methods, unsigned DataLen) {
1432 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001433 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001434 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001435 Method = Method->Next)
1436 if (Method->Method)
1437 ++NumInstanceMethods;
1438
1439 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001440 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001441 Method = Method->Next)
1442 if (Method->Method)
1443 ++NumFactoryMethods;
1444
1445 clang::io::Emit16(Out, NumInstanceMethods);
1446 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001447 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001448 Method = Method->Next)
1449 if (Method->Method)
1450 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001451 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001452 Method = Method->Next)
1453 if (Method->Method)
1454 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001455
1456 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001457 }
1458};
1459} // end anonymous namespace
1460
1461/// \brief Write the method pool into the PCH file.
1462///
1463/// The method pool contains both instance and factory methods, stored
1464/// in an on-disk hash table indexed by the selector.
1465void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1466 using namespace llvm;
1467
1468 // Create and write out the blob that contains the instance and
1469 // factor method pools.
1470 bool Empty = true;
1471 {
1472 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001473
Douglas Gregorc78d3462009-04-24 21:10:55 +00001474 // Create the on-disk hash table representation. Start by
1475 // iterating through the instance method pool.
1476 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001477 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001478 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001479 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001480 InstanceEnd = SemaRef.InstanceMethodPool.end();
1481 Instance != InstanceEnd; ++Instance) {
1482 // Check whether there is a factory method with the same
1483 // selector.
1484 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1485 = SemaRef.FactoryMethodPool.find(Instance->first);
1486
1487 if (Factory == SemaRef.FactoryMethodPool.end())
1488 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001489 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001490 ObjCMethodList()));
1491 else
1492 Generator.insert(Instance->first,
1493 std::make_pair(Instance->second, Factory->second));
1494
Douglas Gregor95c13f52009-04-25 17:48:32 +00001495 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001496 Empty = false;
1497 }
1498
1499 // Now iterate through the factory method pool, to pick up any
1500 // selectors that weren't already in the instance method pool.
1501 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001502 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001503 FactoryEnd = SemaRef.FactoryMethodPool.end();
1504 Factory != FactoryEnd; ++Factory) {
1505 // Check whether there is an instance method with the same
1506 // selector. If so, there is no work to do here.
1507 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1508 = SemaRef.InstanceMethodPool.find(Factory->first);
1509
Douglas Gregor95c13f52009-04-25 17:48:32 +00001510 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001511 Generator.insert(Factory->first,
1512 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001513 ++NumSelectorsInMethodPool;
1514 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001515
1516 Empty = false;
1517 }
1518
Douglas Gregor95c13f52009-04-25 17:48:32 +00001519 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001520 return;
1521
1522 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001523 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001524 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001525 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001526 {
1527 PCHMethodPoolTrait Trait(*this);
1528 llvm::raw_svector_ostream Out(MethodPool);
1529 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001530 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001531 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001532
1533 // For every selector that we have seen but which was not
1534 // written into the hash table, write the selector itself and
1535 // record it's offset.
1536 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1537 if (SelectorOffsets[I] == 0)
1538 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001539 }
1540
1541 // Create a blob abbreviation
1542 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1543 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1547 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1548
Douglas Gregor95c13f52009-04-25 17:48:32 +00001549 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001550 RecordData Record;
1551 Record.push_back(pch::METHOD_POOL);
1552 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001553 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001554 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001555
1556 // Create a blob abbreviation for the selector table offsets.
1557 Abbrev = new BitCodeAbbrev();
1558 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1561 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1562
1563 // Write the selector offsets table.
1564 Record.clear();
1565 Record.push_back(pch::SELECTOR_OFFSETS);
1566 Record.push_back(SelectorOffsets.size());
1567 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1568 (const char *)&SelectorOffsets.front(),
1569 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001570 }
1571}
1572
Douglas Gregorc5046832009-04-27 18:38:38 +00001573//===----------------------------------------------------------------------===//
1574// Identifier Table Serialization
1575//===----------------------------------------------------------------------===//
1576
Douglas Gregorc78d3462009-04-24 21:10:55 +00001577namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001578class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001579 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001580 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001581
Douglas Gregor1d583f22009-04-28 21:18:29 +00001582 /// \brief Determines whether this is an "interesting" identifier
1583 /// that needs a full IdentifierInfo structure written into the hash
1584 /// table.
1585 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1586 return II->isPoisoned() ||
1587 II->isExtensionToken() ||
1588 II->hasMacroDefinition() ||
1589 II->getObjCOrBuiltinID() ||
1590 II->getFETokenInfo<void>();
1591 }
1592
Douglas Gregore84a9da2009-04-20 20:36:09 +00001593public:
1594 typedef const IdentifierInfo* key_type;
1595 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001596
Douglas Gregore84a9da2009-04-20 20:36:09 +00001597 typedef pch::IdentID data_type;
1598 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001599
1600 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001601 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001602
1603 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001604 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001605 }
Mike Stump11289f42009-09-09 15:08:12 +00001606
1607 std::pair<unsigned,unsigned>
1608 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001609 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001610 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001611 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1612 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001613 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001614 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001615 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001616 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001617 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1618 DEnd = IdentifierResolver::end();
1619 D != DEnd; ++D)
1620 DataLen += sizeof(pch::DeclID);
1621 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001622 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001623 // We emit the key length after the data length so that every
1624 // string is preceded by a 16-bit length. This matches the PTH
1625 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001626 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001627 return std::make_pair(KeyLen, DataLen);
1628 }
Mike Stump11289f42009-09-09 15:08:12 +00001629
1630 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001631 unsigned KeyLen) {
1632 // Record the location of the key data. This is used when generating
1633 // the mapping from persistent IDs to strings.
1634 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001635 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001636 }
Mike Stump11289f42009-09-09 15:08:12 +00001637
1638 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001639 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001640 if (!isInterestingIdentifier(II)) {
1641 clang::io::Emit32(Out, ID << 1);
1642 return;
1643 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001644
Douglas Gregor1d583f22009-04-28 21:18:29 +00001645 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001646 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001647 bool hasMacroDefinition =
1648 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001649 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001650 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001651 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1652 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1653 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1654 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001655 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001656
Douglas Gregorc3366a52009-04-21 23:56:24 +00001657 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001658 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001659
Douglas Gregora868bbd2009-04-21 22:25:48 +00001660 // Emit the declaration IDs in reverse order, because the
1661 // IdentifierResolver provides the declarations as they would be
1662 // visible (e.g., the function "stat" would come before the struct
1663 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1664 // adds declarations to the end of the list (so we need to see the
1665 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001666 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001667 IdentifierResolver::end());
1668 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1669 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001670 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001671 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001672 }
1673};
1674} // end anonymous namespace
1675
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001676/// \brief Write the identifier table into the PCH file.
1677///
1678/// The identifier table consists of a blob containing string data
1679/// (the actual identifiers themselves) and a separate "offsets" index
1680/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001681void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001682 using namespace llvm;
1683
1684 // Create and write out the blob that contains the identifier
1685 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001686 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001687 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001688
Douglas Gregore6648fb2009-04-28 20:33:11 +00001689 // Look for any identifiers that were named while processing the
1690 // headers, but are otherwise not needed. We add these to the hash
1691 // table to enable checking of the predefines buffer in the case
1692 // where the user adds new macro definitions when building the PCH
1693 // file.
1694 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1695 IDEnd = PP.getIdentifierTable().end();
1696 ID != IDEnd; ++ID)
1697 getIdentifierRef(ID->second);
1698
Douglas Gregore84a9da2009-04-20 20:36:09 +00001699 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001700 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001701 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1702 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1703 ID != IDEnd; ++ID) {
1704 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001705 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001706 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001707
Douglas Gregore84a9da2009-04-20 20:36:09 +00001708 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001709 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001710 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001711 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001712 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001713 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001714 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001715 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001716 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001717 }
1718
1719 // Create a blob abbreviation
1720 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1721 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001722 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001723 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001724 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001725
1726 // Write the identifier table
1727 RecordData Record;
1728 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001729 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001730 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001731 }
1732
1733 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001734 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1735 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1738 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1739
1740 RecordData Record;
1741 Record.push_back(pch::IDENTIFIER_OFFSET);
1742 Record.push_back(IdentifierOffsets.size());
1743 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1744 (const char *)&IdentifierOffsets.front(),
1745 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001746}
1747
Douglas Gregorc5046832009-04-27 18:38:38 +00001748//===----------------------------------------------------------------------===//
1749// General Serialization Routines
1750//===----------------------------------------------------------------------===//
1751
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001752/// \brief Write a record containing the given attributes.
1753void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1754 RecordData Record;
1755 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001756 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001757 Record.push_back(Attr->isInherited());
1758 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001759 default:
1760 assert(0 && "Does not support PCH writing for this attribute yet!");
1761 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001762 case Attr::Alias:
1763 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1764 break;
1765
1766 case Attr::Aligned:
1767 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1768 break;
1769
1770 case Attr::AlwaysInline:
1771 break;
Mike Stump11289f42009-09-09 15:08:12 +00001772
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001773 case Attr::AnalyzerNoReturn:
1774 break;
1775
1776 case Attr::Annotate:
1777 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1778 break;
1779
1780 case Attr::AsmLabel:
1781 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1782 break;
1783
Alexis Hunt54a02542009-11-25 04:20:27 +00001784 case Attr::BaseCheck:
1785 break;
1786
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001787 case Attr::Blocks:
1788 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1789 break;
1790
Eli Friedmane4310c82009-11-09 18:38:53 +00001791 case Attr::CDecl:
1792 break;
1793
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001794 case Attr::Cleanup:
1795 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1796 break;
1797
1798 case Attr::Const:
1799 break;
1800
1801 case Attr::Constructor:
1802 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1803 break;
1804
1805 case Attr::DLLExport:
1806 case Attr::DLLImport:
1807 case Attr::Deprecated:
1808 break;
1809
1810 case Attr::Destructor:
1811 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1812 break;
1813
1814 case Attr::FastCall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001815 case Attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001816 break;
1817
1818 case Attr::Format: {
1819 const FormatAttr *Format = cast<FormatAttr>(Attr);
1820 AddString(Format->getType(), Record);
1821 Record.push_back(Format->getFormatIdx());
1822 Record.push_back(Format->getFirstArg());
1823 break;
1824 }
1825
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001826 case Attr::FormatArg: {
1827 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1828 Record.push_back(Format->getFormatIdx());
1829 break;
1830 }
1831
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001832 case Attr::Sentinel : {
1833 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1834 Record.push_back(Sentinel->getSentinel());
1835 Record.push_back(Sentinel->getNullPos());
1836 break;
1837 }
Mike Stump11289f42009-09-09 15:08:12 +00001838
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001839 case Attr::GNUInline:
Alexis Hunt54a02542009-11-25 04:20:27 +00001840 case Attr::Hiding:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001841 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001842 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001843 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001844 case Attr::NoReturn:
1845 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001846 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001847 break;
1848
1849 case Attr::NonNull: {
1850 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1851 Record.push_back(NonNull->size());
1852 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1853 break;
1854 }
1855
1856 case Attr::ObjCException:
1857 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001858 case Attr::CFReturnsRetained:
1859 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001860 case Attr::Overloadable:
Alexis Hunt54a02542009-11-25 04:20:27 +00001861 case Attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001862 break;
1863
Anders Carlsson68e0b682009-08-08 18:23:56 +00001864 case Attr::PragmaPack:
1865 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001866 break;
1867
Anders Carlsson68e0b682009-08-08 18:23:56 +00001868 case Attr::Packed:
1869 break;
Mike Stump11289f42009-09-09 15:08:12 +00001870
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001871 case Attr::Pure:
1872 break;
1873
1874 case Attr::Regparm:
1875 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1876 break;
Mike Stump11289f42009-09-09 15:08:12 +00001877
Nate Begemanf2758702009-06-26 06:32:41 +00001878 case Attr::ReqdWorkGroupSize:
1879 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1880 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1881 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1882 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001883
1884 case Attr::Section:
1885 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1886 break;
1887
1888 case Attr::StdCall:
1889 case Attr::TransparentUnion:
1890 case Attr::Unavailable:
1891 case Attr::Unused:
1892 case Attr::Used:
1893 break;
1894
1895 case Attr::Visibility:
1896 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001897 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001898 break;
1899
1900 case Attr::WarnUnusedResult:
1901 case Attr::Weak:
1902 case Attr::WeakImport:
1903 break;
1904 }
1905 }
1906
Douglas Gregor8f45df52009-04-16 22:23:12 +00001907 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001908}
1909
1910void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1911 Record.push_back(Str.size());
1912 Record.insert(Record.end(), Str.begin(), Str.end());
1913}
1914
Douglas Gregore84a9da2009-04-20 20:36:09 +00001915/// \brief Note that the identifier II occurs at the given offset
1916/// within the identifier table.
1917void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001918 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001919}
1920
Douglas Gregor95c13f52009-04-25 17:48:32 +00001921/// \brief Note that the selector Sel occurs at the given offset
1922/// within the method pool/selector table.
1923void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1924 unsigned ID = SelectorIDs[Sel];
1925 assert(ID && "Unknown selector");
1926 SelectorOffsets[ID - 1] = Offset;
1927}
1928
Mike Stump11289f42009-09-09 15:08:12 +00001929PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1930 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001931 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1932 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001933
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001934void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1935 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001936 using namespace llvm;
1937
Douglas Gregor162dd022009-04-20 15:53:59 +00001938 ASTContext &Context = SemaRef.Context;
1939 Preprocessor &PP = SemaRef.PP;
1940
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001941 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001942 Stream.Emit((unsigned)'C', 8);
1943 Stream.Emit((unsigned)'P', 8);
1944 Stream.Emit((unsigned)'C', 8);
1945 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001946
Chris Lattner28fa4e62009-04-26 22:26:21 +00001947 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001948
1949 // The translation unit is the first declaration we'll emit.
1950 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001951 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001952
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001953 // Make sure that we emit IdentifierInfos (and any attached
1954 // declarations) for builtins.
1955 {
1956 IdentifierTable &Table = PP.getIdentifierTable();
1957 llvm::SmallVector<const char *, 32> BuiltinNames;
1958 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1959 Context.getLangOptions().NoBuiltin);
1960 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1961 getIdentifierRef(&Table.get(BuiltinNames[I]));
1962 }
1963
Chris Lattner0c797362009-09-08 18:19:27 +00001964 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00001965 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00001966 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001967 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00001968 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
1969 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00001970 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001971
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001972 // Build a record containing all of the locally-scoped external
1973 // declarations in this header file. Generally, this record will be
1974 // empty.
1975 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001976 // FIXME: This is filling in the PCH file in densemap order which is
1977 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001978 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001979 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1980 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1981 TD != TDEnd; ++TD)
1982 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1983
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001984 // Build a record containing all of the ext_vector declarations.
1985 RecordData ExtVectorDecls;
1986 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1987 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1988
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001989 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001990 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001991 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001992 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001993 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001994 if (StatCalls && !isysroot)
1995 WriteStatCache(*StatCalls, isysroot);
1996 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00001997 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001998 // Write the record of special types.
1999 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002000
Steve Naroffc277ad12009-07-18 15:33:26 +00002001 AddTypeRef(Context.getBuiltinVaListType(), Record);
2002 AddTypeRef(Context.getObjCIdType(), Record);
2003 AddTypeRef(Context.getObjCSelType(), Record);
2004 AddTypeRef(Context.getObjCProtoType(), Record);
2005 AddTypeRef(Context.getObjCClassType(), Record);
2006 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2007 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2008 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002009 AddTypeRef(Context.getjmp_bufType(), Record);
2010 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002011 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2012 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002013#if 0
2014 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian04b258c2009-11-25 23:07:42 +00002015 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002016#endif
Mike Stumpd0153282009-10-20 02:12:22 +00002017 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002018 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002019 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002020
Douglas Gregor1970d882009-04-26 03:49:13 +00002021 // Keep writing types and declarations until all types and
2022 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002023 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2024 WriteDeclsBlockAbbrevs();
2025 while (!DeclTypesToEmit.empty()) {
2026 DeclOrType DOT = DeclTypesToEmit.front();
2027 DeclTypesToEmit.pop();
2028 if (DOT.isType())
2029 WriteType(DOT.getType());
2030 else
2031 WriteDecl(Context, DOT.getDecl());
2032 }
2033 Stream.ExitBlock();
2034
Douglas Gregor45053152009-10-17 17:25:45 +00002035 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002036 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002037 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002038
2039 // Write the type offsets array
2040 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2041 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2044 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2045 Record.clear();
2046 Record.push_back(pch::TYPE_OFFSET);
2047 Record.push_back(TypeOffsets.size());
2048 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002049 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002050 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002051
Douglas Gregor745ed142009-04-25 18:35:21 +00002052 // Write the declaration offsets array
2053 Abbrev = new BitCodeAbbrev();
2054 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2057 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2058 Record.clear();
2059 Record.push_back(pch::DECL_OFFSET);
2060 Record.push_back(DeclOffsets.size());
2061 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002062 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002063 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002064
Douglas Gregord4df8652009-04-22 22:02:47 +00002065 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002066 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002067 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002068
2069 // Write the record containing tentative definitions.
2070 if (!TentativeDefinitions.empty())
2071 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002072
2073 // Write the record containing locally-scoped external definitions.
2074 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002075 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002076 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002077
2078 // Write the record containing ext_vector type names.
2079 if (!ExtVectorDecls.empty())
2080 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002081
Douglas Gregor08f01292009-04-17 22:13:46 +00002082 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002083 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002084 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002085 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002086 Record.push_back(NumLexicalDeclContexts);
2087 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002088 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002089 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002090}
2091
2092void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2093 Record.push_back(Loc.getRawEncoding());
2094}
2095
2096void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2097 Record.push_back(Value.getBitWidth());
2098 unsigned N = Value.getNumWords();
2099 const uint64_t* Words = Value.getRawData();
2100 for (unsigned I = 0; I != N; ++I)
2101 Record.push_back(Words[I]);
2102}
2103
Douglas Gregor1daeb692009-04-13 18:14:40 +00002104void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2105 Record.push_back(Value.isUnsigned());
2106 AddAPInt(Value, Record);
2107}
2108
Douglas Gregore0a3a512009-04-14 21:55:33 +00002109void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2110 AddAPInt(Value.bitcastToAPInt(), Record);
2111}
2112
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002113void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002114 Record.push_back(getIdentifierRef(II));
2115}
2116
2117pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2118 if (II == 0)
2119 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002120
2121 pch::IdentID &ID = IdentifierIDs[II];
2122 if (ID == 0)
2123 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002124 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002125}
2126
Steve Naroff2ddea052009-04-23 10:39:46 +00002127void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2128 if (SelRef.getAsOpaquePtr() == 0) {
2129 Record.push_back(0);
2130 return;
2131 }
2132
2133 pch::SelectorID &SID = SelectorIDs[SelRef];
2134 if (SID == 0) {
2135 SID = SelectorIDs.size();
2136 SelVector.push_back(SelRef);
2137 }
2138 Record.push_back(SID);
2139}
2140
John McCall0ad16662009-10-29 08:12:44 +00002141void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2142 RecordData &Record) {
2143 switch (Arg.getArgument().getKind()) {
2144 case TemplateArgument::Expression:
2145 AddStmt(Arg.getLocInfo().getAsExpr());
2146 break;
2147 case TemplateArgument::Type:
John McCallbcd03502009-12-07 02:54:59 +00002148 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002149 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002150 case TemplateArgument::Template:
2151 Record.push_back(
2152 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2153 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2154 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2155 break;
John McCall0ad16662009-10-29 08:12:44 +00002156 case TemplateArgument::Null:
2157 case TemplateArgument::Integral:
2158 case TemplateArgument::Declaration:
2159 case TemplateArgument::Pack:
2160 break;
2161 }
2162}
2163
John McCallbcd03502009-12-07 02:54:59 +00002164void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2165 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002166 AddTypeRef(QualType(), Record);
2167 return;
2168 }
2169
John McCallbcd03502009-12-07 02:54:59 +00002170 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002171 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002172 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00002173 TLW.Visit(TL);
2174}
2175
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002176void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2177 if (T.isNull()) {
2178 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2179 return;
2180 }
2181
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002182 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002183 T.removeFastQualifiers();
2184
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002185 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002186 pch::TypeID &ID = TypeIDs[T];
2187 if (ID == 0) {
2188 // We haven't seen these qualifiers applied to this type before.
2189 // Assign it a new ID. This is the only time we enqueue a
2190 // qualified type, and it has no CV qualifiers.
2191 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002192 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002193 }
2194
2195 // Encode the type qualifiers in the type reference.
2196 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2197 return;
2198 }
2199
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002200 assert(!T.hasLocalQualifiers());
John McCall8ccfcb52009-09-24 19:53:00 +00002201
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002202 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002203 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002204 switch (BT->getKind()) {
2205 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2206 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2207 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2208 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2209 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2210 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2211 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2212 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002213 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002214 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2215 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2216 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2217 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2218 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2219 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2220 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002221 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002222 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2223 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2224 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002225 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002226 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2227 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002228 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2229 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002230 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2231 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002232 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002233 case BuiltinType::UndeducedAuto:
2234 assert(0 && "Should not see undeduced auto here");
2235 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002236 }
2237
John McCall8ccfcb52009-09-24 19:53:00 +00002238 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002239 return;
2240 }
2241
John McCall8ccfcb52009-09-24 19:53:00 +00002242 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002243 if (ID == 0) {
2244 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002245 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002246 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002247 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002248 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002249
2250 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002251 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002252}
2253
2254void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2255 if (D == 0) {
2256 Record.push_back(0);
2257 return;
2258 }
2259
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002260 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002261 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002262 // We haven't seen this declaration before. Give it a new ID and
2263 // enqueue it in the list of declarations to emit.
2264 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002265 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002266 }
2267
2268 Record.push_back(ID);
2269}
2270
Douglas Gregore84a9da2009-04-20 20:36:09 +00002271pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2272 if (D == 0)
2273 return 0;
2274
2275 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2276 return DeclIDs[D];
2277}
2278
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002279void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002280 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002281 Record.push_back(Name.getNameKind());
2282 switch (Name.getNameKind()) {
2283 case DeclarationName::Identifier:
2284 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2285 break;
2286
2287 case DeclarationName::ObjCZeroArgSelector:
2288 case DeclarationName::ObjCOneArgSelector:
2289 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002290 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002291 break;
2292
2293 case DeclarationName::CXXConstructorName:
2294 case DeclarationName::CXXDestructorName:
2295 case DeclarationName::CXXConversionFunctionName:
2296 AddTypeRef(Name.getCXXNameType(), Record);
2297 break;
2298
2299 case DeclarationName::CXXOperatorName:
2300 Record.push_back(Name.getCXXOverloadedOperator());
2301 break;
2302
Alexis Hunt3d221f22009-11-29 07:34:05 +00002303 case DeclarationName::CXXLiteralOperatorName:
2304 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2305 break;
2306
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002307 case DeclarationName::CXXUsingDirective:
2308 // No extra data to emit
2309 break;
2310 }
2311}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002312