blob: 0e6d1d2f669f2f22ff790caed5b73601d468102e [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
72void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
73 Record.push_back(T->getWidth());
74 Record.push_back(T->isSigned());
75 Code = pch::TYPE_FIXED_WIDTH_INT;
76}
77
78void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
79 Writer.AddTypeRef(T->getElementType(), Record);
80 Code = pch::TYPE_COMPLEX;
81}
82
83void PCHTypeWriter::VisitPointerType(const PointerType *T) {
84 Writer.AddTypeRef(T->getPointeeType(), Record);
85 Code = pch::TYPE_POINTER;
86}
87
88void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000089 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090 Code = pch::TYPE_BLOCK_POINTER;
91}
92
93void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_LVALUE_REFERENCE;
96}
97
98void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
99 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Code = pch::TYPE_RVALUE_REFERENCE;
101}
102
103void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
105 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106 Code = pch::TYPE_MEMBER_POINTER;
107}
108
109void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
110 Writer.AddTypeRef(T->getElementType(), Record);
111 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000112 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
115void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
116 VisitArrayType(T);
117 Writer.AddAPInt(T->getSize(), Record);
118 Code = pch::TYPE_CONSTANT_ARRAY;
119}
120
121void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
122 VisitArrayType(T);
123 Code = pch::TYPE_INCOMPLETE_ARRAY;
124}
125
126void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
127 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000128 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
129 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000130 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131 Code = pch::TYPE_VARIABLE_ARRAY;
132}
133
134void PCHTypeWriter::VisitVectorType(const VectorType *T) {
135 Writer.AddTypeRef(T->getElementType(), Record);
136 Record.push_back(T->getNumElements());
137 Code = pch::TYPE_VECTOR;
138}
139
140void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
141 VisitVectorType(T);
142 Code = pch::TYPE_EXT_VECTOR;
143}
144
145void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
146 Writer.AddTypeRef(T->getResultType(), Record);
147}
148
149void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
150 VisitFunctionType(T);
151 Code = pch::TYPE_FUNCTION_NO_PROTO;
152}
153
154void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
155 VisitFunctionType(T);
156 Record.push_back(T->getNumArgs());
157 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
158 Writer.AddTypeRef(T->getArgType(I), Record);
159 Record.push_back(T->isVariadic());
160 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000161 Record.push_back(T->hasExceptionSpec());
162 Record.push_back(T->hasAnyExceptionSpec());
163 Record.push_back(T->getNumExceptions());
164 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
165 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166 Code = pch::TYPE_FUNCTION_PROTO;
167}
168
John McCallb96ec562009-12-04 22:46:56 +0000169#if 0
170// For when we want it....
171void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
172 Writer.AddDeclRef(T->getDecl(), Record);
173 Code = pch::TYPE_UNRESOLVED_USING;
174}
175#endif
176
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
178 Writer.AddDeclRef(T->getDecl(), Record);
179 Code = pch::TYPE_TYPEDEF;
180}
181
182void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000183 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000184 Code = pch::TYPE_TYPEOF_EXPR;
185}
186
187void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
188 Writer.AddTypeRef(T->getUnderlyingType(), Record);
189 Code = pch::TYPE_TYPEOF;
190}
191
Anders Carlsson81df7b82009-06-24 19:06:50 +0000192void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
193 Writer.AddStmt(T->getUnderlyingExpr());
194 Code = pch::TYPE_DECLTYPE;
195}
196
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197void PCHTypeWriter::VisitTagType(const TagType *T) {
198 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000199 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000200 "Cannot serialize in the middle of a type definition");
201}
202
203void PCHTypeWriter::VisitRecordType(const RecordType *T) {
204 VisitTagType(T);
205 Code = pch::TYPE_RECORD;
206}
207
208void PCHTypeWriter::VisitEnumType(const EnumType *T) {
209 VisitTagType(T);
210 Code = pch::TYPE_ENUM;
211}
212
John McCallfcc33b02009-09-05 00:15:47 +0000213void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
214 Writer.AddTypeRef(T->getUnderlyingType(), Record);
215 Record.push_back(T->getTagKind());
216 Code = pch::TYPE_ELABORATED;
217}
218
Mike Stump11289f42009-09-09 15:08:12 +0000219void
John McCallcebee162009-10-18 09:09:24 +0000220PCHTypeWriter::VisitSubstTemplateTypeParmType(
221 const SubstTemplateTypeParmType *T) {
222 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
223 Writer.AddTypeRef(T->getReplacementType(), Record);
224 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
225}
226
227void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228PCHTypeWriter::VisitTemplateSpecializationType(
229 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000230 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000231 assert(false && "Cannot serialize template specialization types");
232}
233
234void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000235 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236 assert(false && "Cannot serialize qualified name types");
237}
238
239void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
240 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000242 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
243 E = T->qual_end(); I != E; ++I)
244 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000245 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246}
247
Steve Narofffb4330f2009-06-17 22:40:22 +0000248void
249PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000250 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000252 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000253 E = T->qual_end(); I != E; ++I)
254 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000255 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000256}
257
John McCall8f115c62009-10-16 21:56:05 +0000258namespace {
259
260class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
261 PCHWriter &Writer;
262 PCHWriter::RecordData &Record;
263
264public:
265 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
266 : Writer(Writer), Record(Record) { }
267
John McCall17001972009-10-18 01:05:36 +0000268#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000269#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000270 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000271#include "clang/AST/TypeLocNodes.def"
272
John McCall17001972009-10-18 01:05:36 +0000273 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
274 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000275};
276
277}
278
John McCall17001972009-10-18 01:05:36 +0000279void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
280 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000281}
John McCall17001972009-10-18 01:05:36 +0000282void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
283 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000284}
John McCall17001972009-10-18 01:05:36 +0000285void TypeLocWriter::VisitFixedWidthIntTypeLoc(FixedWidthIntTypeLoc TL) {
286 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000287}
John McCall17001972009-10-18 01:05:36 +0000288void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
289 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000290}
John McCall17001972009-10-18 01:05:36 +0000291void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
292 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000293}
John McCall17001972009-10-18 01:05:36 +0000294void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
295 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000296}
John McCall17001972009-10-18 01:05:36 +0000297void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
298 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000299}
John McCall17001972009-10-18 01:05:36 +0000300void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
301 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000302}
John McCall17001972009-10-18 01:05:36 +0000303void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
304 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000305}
John McCall17001972009-10-18 01:05:36 +0000306void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
307 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
308 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
309 Record.push_back(TL.getSizeExpr() ? 1 : 0);
310 if (TL.getSizeExpr())
311 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000312}
John McCall17001972009-10-18 01:05:36 +0000313void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
314 VisitArrayTypeLoc(TL);
315}
316void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
317 VisitArrayTypeLoc(TL);
318}
319void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
320 VisitArrayTypeLoc(TL);
321}
322void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
323 DependentSizedArrayTypeLoc TL) {
324 VisitArrayTypeLoc(TL);
325}
326void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
327 DependentSizedExtVectorTypeLoc TL) {
328 Writer.AddSourceLocation(TL.getNameLoc(), Record);
329}
330void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
331 Writer.AddSourceLocation(TL.getNameLoc(), Record);
332}
333void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
334 Writer.AddSourceLocation(TL.getNameLoc(), Record);
335}
336void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
337 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
338 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
339 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
340 Writer.AddDeclRef(TL.getArg(i), Record);
341}
342void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
343 VisitFunctionTypeLoc(TL);
344}
345void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
346 VisitFunctionTypeLoc(TL);
347}
John McCallb96ec562009-12-04 22:46:56 +0000348void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
349 Writer.AddSourceLocation(TL.getNameLoc(), Record);
350}
John McCall17001972009-10-18 01:05:36 +0000351void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
352 Writer.AddSourceLocation(TL.getNameLoc(), Record);
353}
354void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
355 Writer.AddSourceLocation(TL.getNameLoc(), Record);
356}
357void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
358 Writer.AddSourceLocation(TL.getNameLoc(), Record);
359}
360void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getNameLoc(), Record);
362}
363void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getNameLoc(), Record);
365}
366void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getNameLoc(), Record);
368}
369void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
370 Writer.AddSourceLocation(TL.getNameLoc(), Record);
371}
372void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
373 Writer.AddSourceLocation(TL.getNameLoc(), Record);
374}
John McCallcebee162009-10-18 09:09:24 +0000375void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
376 SubstTemplateTypeParmTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
John McCall17001972009-10-18 01:05:36 +0000379void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
380 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000381 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
382 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
383 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
384 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
385 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall17001972009-10-18 01:05:36 +0000386}
387void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
388 Writer.AddSourceLocation(TL.getNameLoc(), Record);
389}
390void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getNameLoc(), Record);
392}
393void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
394 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000395 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
396 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
397 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
398 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000399}
John McCallfc93cf92009-10-22 22:37:11 +0000400void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getStarLoc(), Record);
402 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
403 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
404 Record.push_back(TL.hasBaseTypeAsWritten());
405 Record.push_back(TL.hasProtocolsAsWritten());
406 if (TL.hasProtocolsAsWritten())
407 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
408 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
409}
John McCall8f115c62009-10-16 21:56:05 +0000410
Chris Lattner19cea4e2009-04-22 05:57:30 +0000411//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000412// PCHWriter Implementation
413//===----------------------------------------------------------------------===//
414
Chris Lattner28fa4e62009-04-26 22:26:21 +0000415static void EmitBlockID(unsigned ID, const char *Name,
416 llvm::BitstreamWriter &Stream,
417 PCHWriter::RecordData &Record) {
418 Record.clear();
419 Record.push_back(ID);
420 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
421
422 // Emit the block name if present.
423 if (Name == 0 || Name[0] == 0) return;
424 Record.clear();
425 while (*Name)
426 Record.push_back(*Name++);
427 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
428}
429
430static void EmitRecordID(unsigned ID, const char *Name,
431 llvm::BitstreamWriter &Stream,
432 PCHWriter::RecordData &Record) {
433 Record.clear();
434 Record.push_back(ID);
435 while (*Name)
436 Record.push_back(*Name++);
437 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000438}
439
440static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
441 PCHWriter::RecordData &Record) {
442#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
443 RECORD(STMT_STOP);
444 RECORD(STMT_NULL_PTR);
445 RECORD(STMT_NULL);
446 RECORD(STMT_COMPOUND);
447 RECORD(STMT_CASE);
448 RECORD(STMT_DEFAULT);
449 RECORD(STMT_LABEL);
450 RECORD(STMT_IF);
451 RECORD(STMT_SWITCH);
452 RECORD(STMT_WHILE);
453 RECORD(STMT_DO);
454 RECORD(STMT_FOR);
455 RECORD(STMT_GOTO);
456 RECORD(STMT_INDIRECT_GOTO);
457 RECORD(STMT_CONTINUE);
458 RECORD(STMT_BREAK);
459 RECORD(STMT_RETURN);
460 RECORD(STMT_DECL);
461 RECORD(STMT_ASM);
462 RECORD(EXPR_PREDEFINED);
463 RECORD(EXPR_DECL_REF);
464 RECORD(EXPR_INTEGER_LITERAL);
465 RECORD(EXPR_FLOATING_LITERAL);
466 RECORD(EXPR_IMAGINARY_LITERAL);
467 RECORD(EXPR_STRING_LITERAL);
468 RECORD(EXPR_CHARACTER_LITERAL);
469 RECORD(EXPR_PAREN);
470 RECORD(EXPR_UNARY_OPERATOR);
471 RECORD(EXPR_SIZEOF_ALIGN_OF);
472 RECORD(EXPR_ARRAY_SUBSCRIPT);
473 RECORD(EXPR_CALL);
474 RECORD(EXPR_MEMBER);
475 RECORD(EXPR_BINARY_OPERATOR);
476 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
477 RECORD(EXPR_CONDITIONAL_OPERATOR);
478 RECORD(EXPR_IMPLICIT_CAST);
479 RECORD(EXPR_CSTYLE_CAST);
480 RECORD(EXPR_COMPOUND_LITERAL);
481 RECORD(EXPR_EXT_VECTOR_ELEMENT);
482 RECORD(EXPR_INIT_LIST);
483 RECORD(EXPR_DESIGNATED_INIT);
484 RECORD(EXPR_IMPLICIT_VALUE_INIT);
485 RECORD(EXPR_VA_ARG);
486 RECORD(EXPR_ADDR_LABEL);
487 RECORD(EXPR_STMT);
488 RECORD(EXPR_TYPES_COMPATIBLE);
489 RECORD(EXPR_CHOOSE);
490 RECORD(EXPR_GNU_NULL);
491 RECORD(EXPR_SHUFFLE_VECTOR);
492 RECORD(EXPR_BLOCK);
493 RECORD(EXPR_BLOCK_DECL_REF);
494 RECORD(EXPR_OBJC_STRING_LITERAL);
495 RECORD(EXPR_OBJC_ENCODE);
496 RECORD(EXPR_OBJC_SELECTOR_EXPR);
497 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
498 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
499 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
500 RECORD(EXPR_OBJC_KVC_REF_EXPR);
501 RECORD(EXPR_OBJC_MESSAGE_EXPR);
502 RECORD(EXPR_OBJC_SUPER_EXPR);
503 RECORD(STMT_OBJC_FOR_COLLECTION);
504 RECORD(STMT_OBJC_CATCH);
505 RECORD(STMT_OBJC_FINALLY);
506 RECORD(STMT_OBJC_AT_TRY);
507 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
508 RECORD(STMT_OBJC_AT_THROW);
509#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000510}
Mike Stump11289f42009-09-09 15:08:12 +0000511
Chris Lattner28fa4e62009-04-26 22:26:21 +0000512void PCHWriter::WriteBlockInfoBlock() {
513 RecordData Record;
514 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000515
Chris Lattner64031982009-04-27 00:40:25 +0000516#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000517#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner28fa4e62009-04-26 22:26:21 +0000519 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000520 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000521 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000522 RECORD(TYPE_OFFSET);
523 RECORD(DECL_OFFSET);
524 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000525 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000526 RECORD(IDENTIFIER_OFFSET);
527 RECORD(IDENTIFIER_TABLE);
528 RECORD(EXTERNAL_DEFINITIONS);
529 RECORD(SPECIAL_TYPES);
530 RECORD(STATISTICS);
531 RECORD(TENTATIVE_DEFINITIONS);
532 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
533 RECORD(SELECTOR_OFFSETS);
534 RECORD(METHOD_POOL);
535 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000536 RECORD(SOURCE_LOCATION_OFFSETS);
537 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000538 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000539 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000540 RECORD(COMMENT_RANGES);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000541 RECORD(SVN_BRANCH_REVISION);
542
Chris Lattner28fa4e62009-04-26 22:26:21 +0000543 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000544 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000545 RECORD(SM_SLOC_FILE_ENTRY);
546 RECORD(SM_SLOC_BUFFER_ENTRY);
547 RECORD(SM_SLOC_BUFFER_BLOB);
548 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
549 RECORD(SM_LINE_TABLE);
550 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000551
Chris Lattner28fa4e62009-04-26 22:26:21 +0000552 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000553 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000554 RECORD(PP_MACRO_OBJECT_LIKE);
555 RECORD(PP_MACRO_FUNCTION_LIKE);
556 RECORD(PP_TOKEN);
557
Douglas Gregor12bfa382009-10-17 00:13:19 +0000558 // Decls and Types block.
559 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000560 RECORD(TYPE_EXT_QUAL);
561 RECORD(TYPE_FIXED_WIDTH_INT);
562 RECORD(TYPE_COMPLEX);
563 RECORD(TYPE_POINTER);
564 RECORD(TYPE_BLOCK_POINTER);
565 RECORD(TYPE_LVALUE_REFERENCE);
566 RECORD(TYPE_RVALUE_REFERENCE);
567 RECORD(TYPE_MEMBER_POINTER);
568 RECORD(TYPE_CONSTANT_ARRAY);
569 RECORD(TYPE_INCOMPLETE_ARRAY);
570 RECORD(TYPE_VARIABLE_ARRAY);
571 RECORD(TYPE_VECTOR);
572 RECORD(TYPE_EXT_VECTOR);
573 RECORD(TYPE_FUNCTION_PROTO);
574 RECORD(TYPE_FUNCTION_NO_PROTO);
575 RECORD(TYPE_TYPEDEF);
576 RECORD(TYPE_TYPEOF_EXPR);
577 RECORD(TYPE_TYPEOF);
578 RECORD(TYPE_RECORD);
579 RECORD(TYPE_ENUM);
580 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000581 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000582 RECORD(DECL_ATTR);
583 RECORD(DECL_TRANSLATION_UNIT);
584 RECORD(DECL_TYPEDEF);
585 RECORD(DECL_ENUM);
586 RECORD(DECL_RECORD);
587 RECORD(DECL_ENUM_CONSTANT);
588 RECORD(DECL_FUNCTION);
589 RECORD(DECL_OBJC_METHOD);
590 RECORD(DECL_OBJC_INTERFACE);
591 RECORD(DECL_OBJC_PROTOCOL);
592 RECORD(DECL_OBJC_IVAR);
593 RECORD(DECL_OBJC_AT_DEFS_FIELD);
594 RECORD(DECL_OBJC_CLASS);
595 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
596 RECORD(DECL_OBJC_CATEGORY);
597 RECORD(DECL_OBJC_CATEGORY_IMPL);
598 RECORD(DECL_OBJC_IMPLEMENTATION);
599 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
600 RECORD(DECL_OBJC_PROPERTY);
601 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000602 RECORD(DECL_FIELD);
603 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000604 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000605 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000606 RECORD(DECL_FILE_SCOPE_ASM);
607 RECORD(DECL_BLOCK);
608 RECORD(DECL_CONTEXT_LEXICAL);
609 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000610 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000611 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000612#undef RECORD
613#undef BLOCK
614 Stream.ExitBlock();
615}
616
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000617/// \brief Adjusts the given filename to only write out the portion of the
618/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000619///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000620/// \param Filename the file name to adjust.
621///
622/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
623/// the returned filename will be adjusted by this system root.
624///
625/// \returns either the original filename (if it needs no adjustment) or the
626/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000627static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000628adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
629 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000630
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000631 if (!isysroot)
632 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000633
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000634 // Verify that the filename and the system root have the same prefix.
635 unsigned Pos = 0;
636 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
637 if (Filename[Pos] != isysroot[Pos])
638 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000639
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000640 // We hit the end of the filename before we hit the end of the system root.
641 if (!Filename[Pos])
642 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000643
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000644 // If the file name has a '/' at the current position, skip over the '/'.
645 // We distinguish sysroot-based includes from absolute includes by the
646 // absence of '/' at the beginning of sysroot-based includes.
647 if (Filename[Pos] == '/')
648 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000649
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000650 return Filename + Pos;
651}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000652
Douglas Gregor7b71e632009-04-27 22:23:34 +0000653/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000654void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000655 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000656
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000657 // Metadata
658 const TargetInfo &Target = Context.Target;
659 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
660 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
661 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
662 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
663 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
664 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
665 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
666 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
667 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000668
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000669 RecordData Record;
670 Record.push_back(pch::METADATA);
671 Record.push_back(pch::VERSION_MAJOR);
672 Record.push_back(pch::VERSION_MINOR);
673 Record.push_back(CLANG_VERSION_MAJOR);
674 Record.push_back(CLANG_VERSION_MINOR);
675 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000676 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000677 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000678
Douglas Gregor45fe0362009-05-12 01:31:05 +0000679 // Original file name
680 SourceManager &SM = Context.getSourceManager();
681 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
682 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
683 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
684 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
685 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
686
687 llvm::sys::Path MainFilePath(MainFile->getName());
688 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000689
Douglas Gregor45fe0362009-05-12 01:31:05 +0000690 if (!MainFilePath.isAbsolute()) {
691 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000692 P.appendComponent(MainFilePath.str());
693 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000694 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000695 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000696 }
697
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000698 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000699 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000700 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000701 RecordData Record;
702 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000703 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000704 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000705
706 // Subversion branch/version information.
707 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
708 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
709 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
710 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
711 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
712 Record.clear();
713 Record.push_back(pch::SVN_BRANCH_REVISION);
714 Record.push_back(getClangSubversionRevision());
715 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000716}
717
718/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000719void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
720 RecordData Record;
721 Record.push_back(LangOpts.Trigraphs);
722 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
723 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
724 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
725 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
726 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
727 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
728 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
729 Record.push_back(LangOpts.C99); // C99 Support
730 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
731 Record.push_back(LangOpts.CPlusPlus); // C++ Support
732 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000733 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000734
Douglas Gregor55abb232009-04-10 20:39:37 +0000735 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
736 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
737 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000738
Douglas Gregor55abb232009-04-10 20:39:37 +0000739 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000740 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
741 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000742 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000743 Record.push_back(LangOpts.Exceptions); // Support exception handling.
744
745 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
746 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
747 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
748
Chris Lattner258172e2009-04-27 07:35:58 +0000749 // Whether static initializers are protected by locks.
750 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000751 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000752 Record.push_back(LangOpts.Blocks); // block extension to C
753 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
754 // they are unused.
755 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
756 // (modulo the platform support).
757
758 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
759 // signed integer arithmetic overflows.
760
761 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
762 // may be ripped out at any time.
763
764 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000765 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000766 // defined.
767 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
768 // opposed to __DYNAMIC__).
769 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
770
771 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
772 // used (instead of C99 semantics).
773 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000774 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
775 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000776 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
777 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000778 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000779 Record.push_back(LangOpts.getGCMode());
780 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000781 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000782 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000783 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000784 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000785 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000786}
787
Douglas Gregora7f71a92009-04-10 03:52:48 +0000788//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000789// stat cache Serialization
790//===----------------------------------------------------------------------===//
791
792namespace {
793// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000794class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000795public:
796 typedef const char * key_type;
797 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000798
Douglas Gregorc5046832009-04-27 18:38:38 +0000799 typedef std::pair<int, struct stat> data_type;
800 typedef const data_type& data_type_ref;
801
802 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000803 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000804 }
Mike Stump11289f42009-09-09 15:08:12 +0000805
806 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000807 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
808 data_type_ref Data) {
809 unsigned StrLen = strlen(path);
810 clang::io::Emit16(Out, StrLen);
811 unsigned DataLen = 1; // result value
812 if (Data.first == 0)
813 DataLen += 4 + 4 + 2 + 8 + 8;
814 clang::io::Emit8(Out, DataLen);
815 return std::make_pair(StrLen + 1, DataLen);
816 }
Mike Stump11289f42009-09-09 15:08:12 +0000817
Douglas Gregorc5046832009-04-27 18:38:38 +0000818 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
819 Out.write(path, KeyLen);
820 }
Mike Stump11289f42009-09-09 15:08:12 +0000821
Douglas Gregorc5046832009-04-27 18:38:38 +0000822 void EmitData(llvm::raw_ostream& Out, key_type_ref,
823 data_type_ref Data, unsigned DataLen) {
824 using namespace clang::io;
825 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000826
Douglas Gregorc5046832009-04-27 18:38:38 +0000827 // Result of stat()
828 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000829
Douglas Gregorc5046832009-04-27 18:38:38 +0000830 if (Data.first == 0) {
831 Emit32(Out, (uint32_t) Data.second.st_ino);
832 Emit32(Out, (uint32_t) Data.second.st_dev);
833 Emit16(Out, (uint16_t) Data.second.st_mode);
834 Emit64(Out, (uint64_t) Data.second.st_mtime);
835 Emit64(Out, (uint64_t) Data.second.st_size);
836 }
837
838 assert(Out.tell() - Start == DataLen && "Wrong data length");
839 }
840};
841} // end anonymous namespace
842
843/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000844void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
845 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000846 // Build the on-disk hash table containing information about every
847 // stat() call.
848 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
849 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000850 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000851 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000852 Stat != StatEnd; ++Stat, ++NumStatEntries) {
853 const char *Filename = Stat->first();
854 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
855 Generator.insert(Filename, Stat->second);
856 }
Mike Stump11289f42009-09-09 15:08:12 +0000857
Douglas Gregorc5046832009-04-27 18:38:38 +0000858 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000859 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000860 uint32_t BucketOffset;
861 {
862 llvm::raw_svector_ostream Out(StatCacheData);
863 // Make sure that no bucket is at offset 0
864 clang::io::Emit32(Out, 0);
865 BucketOffset = Generator.Emit(Out);
866 }
867
868 // Create a blob abbreviation
869 using namespace llvm;
870 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
871 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
873 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
875 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
876
877 // Write the stat cache
878 RecordData Record;
879 Record.push_back(pch::STAT_CACHE);
880 Record.push_back(BucketOffset);
881 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000882 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000883}
884
885//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000886// Source Manager Serialization
887//===----------------------------------------------------------------------===//
888
889/// \brief Create an abbreviation for the SLocEntry that refers to a
890/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000891static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000892 using namespace llvm;
893 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
894 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000900 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000901}
902
903/// \brief Create an abbreviation for the SLocEntry that refers to a
904/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000905static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000906 using namespace llvm;
907 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
908 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000914 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000915}
916
917/// \brief Create an abbreviation for the SLocEntry that refers to a
918/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000919static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000920 using namespace llvm;
921 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
922 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000924 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000925}
926
927/// \brief Create an abbreviation for the SLocEntry that refers to an
928/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000929static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000930 using namespace llvm;
931 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
932 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000938 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000939}
940
941/// \brief Writes the block containing the serialized form of the
942/// source manager.
943///
944/// TODO: We should probably use an on-disk hash table (stored in a
945/// blob), indexed based on the file name, so that we only create
946/// entries for files that we actually need. In the common case (no
947/// errors), we probably won't have to create file entries for any of
948/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000949void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000950 const Preprocessor &PP,
951 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000952 RecordData Record;
953
Chris Lattner0910e3b2009-04-10 17:16:57 +0000954 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000955 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000956
957 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000958 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
959 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
960 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
961 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000962
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000963 // Write the line table.
964 if (SourceMgr.hasLineTable()) {
965 LineTableInfo &LineTable = SourceMgr.getLineTable();
966
967 // Emit the file names
968 Record.push_back(LineTable.getNumFilenames());
969 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
970 // Emit the file name
971 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000972 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000973 unsigned FilenameLen = Filename? strlen(Filename) : 0;
974 Record.push_back(FilenameLen);
975 if (FilenameLen)
976 Record.insert(Record.end(), Filename, Filename + FilenameLen);
977 }
Mike Stump11289f42009-09-09 15:08:12 +0000978
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000979 // Emit the line entries
980 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
981 L != LEnd; ++L) {
982 // Emit the file ID
983 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000984
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000985 // Emit the line entries
986 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000987 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000988 LEEnd = L->second.end();
989 LE != LEEnd; ++LE) {
990 Record.push_back(LE->FileOffset);
991 Record.push_back(LE->LineNo);
992 Record.push_back(LE->FilenameID);
993 Record.push_back((unsigned)LE->FileKind);
994 Record.push_back(LE->IncludeOffset);
995 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000996 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +0000997 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000998 }
999
Douglas Gregor258ae542009-04-27 06:38:32 +00001000 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +00001001 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +00001002 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001003 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +00001004 E = HS.header_file_end();
1005 I != E; ++I) {
1006 Record.push_back(I->isImport);
1007 Record.push_back(I->DirInfo);
1008 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +00001009 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001010 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1011 Record.clear();
1012 }
1013
Douglas Gregor258ae542009-04-27 06:38:32 +00001014 // Write out the source location entry table. We skip the first
1015 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001016 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001017 RecordData PreloadSLocs;
1018 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001019 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1020 // Get this source location entry.
1021 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1022
Douglas Gregor258ae542009-04-27 06:38:32 +00001023 // Record the offset of this source-location entry.
1024 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1025
1026 // Figure out which record code to use.
1027 unsigned Code;
1028 if (SLoc->isFile()) {
1029 if (SLoc->getFile().getContentCache()->Entry)
1030 Code = pch::SM_SLOC_FILE_ENTRY;
1031 else
1032 Code = pch::SM_SLOC_BUFFER_ENTRY;
1033 } else
1034 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1035 Record.clear();
1036 Record.push_back(Code);
1037
1038 Record.push_back(SLoc->getOffset());
1039 if (SLoc->isFile()) {
1040 const SrcMgr::FileInfo &File = SLoc->getFile();
1041 Record.push_back(File.getIncludeLoc().getRawEncoding());
1042 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1043 Record.push_back(File.hasLineDirectives());
1044
1045 const SrcMgr::ContentCache *Content = File.getContentCache();
1046 if (Content->Entry) {
1047 // The source location entry is a file. The blob associated
1048 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001049
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001050 // Turn the file name into an absolute path, if it isn't already.
1051 const char *Filename = Content->Entry->getName();
1052 llvm::sys::Path FilePath(Filename, strlen(Filename));
1053 std::string FilenameStr;
1054 if (!FilePath.isAbsolute()) {
1055 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001056 P.appendComponent(FilePath.str());
1057 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001058 Filename = FilenameStr.c_str();
1059 }
Mike Stump11289f42009-09-09 15:08:12 +00001060
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001061 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001062 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001063
1064 // FIXME: For now, preload all file source locations, so that
1065 // we get the appropriate File entries in the reader. This is
1066 // a temporary measure.
1067 PreloadSLocs.push_back(SLocEntryOffsets.size());
1068 } else {
1069 // The source location entry is a buffer. The blob associated
1070 // with this entry contains the contents of the buffer.
1071
1072 // We add one to the size so that we capture the trailing NULL
1073 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1074 // the reader side).
1075 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1076 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001077 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1078 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001079 Record.clear();
1080 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1081 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001082 llvm::StringRef(Buffer->getBufferStart(),
1083 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001084
1085 if (strcmp(Name, "<built-in>") == 0)
1086 PreloadSLocs.push_back(SLocEntryOffsets.size());
1087 }
1088 } else {
1089 // The source location entry is an instantiation.
1090 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1091 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1092 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1093 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1094
1095 // Compute the token length for this macro expansion.
1096 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001097 if (I + 1 != N)
1098 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001099 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1100 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1101 }
1102 }
1103
Douglas Gregor8f45df52009-04-16 22:23:12 +00001104 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001105
1106 if (SLocEntryOffsets.empty())
1107 return;
1108
1109 // Write the source-location offsets table into the PCH block. This
1110 // table is used for lazily loading source-location information.
1111 using namespace llvm;
1112 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1113 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1115 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1117 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001118
Douglas Gregor258ae542009-04-27 06:38:32 +00001119 Record.clear();
1120 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1121 Record.push_back(SLocEntryOffsets.size());
1122 Record.push_back(SourceMgr.getNextOffset());
1123 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001124 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001125 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001126
1127 // Write the source location entry preloads array, telling the PCH
1128 // reader which source locations entries it should load eagerly.
1129 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001130}
1131
Douglas Gregorc5046832009-04-27 18:38:38 +00001132//===----------------------------------------------------------------------===//
1133// Preprocessor Serialization
1134//===----------------------------------------------------------------------===//
1135
Chris Lattnereeffaef2009-04-10 17:15:23 +00001136/// \brief Writes the block containing the serialized form of the
1137/// preprocessor.
1138///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001139void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001140 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001141
Chris Lattner0af3ba12009-04-13 01:29:17 +00001142 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1143 if (PP.getCounterValue() != 0) {
1144 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001145 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001146 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001147 }
1148
1149 // Enter the preprocessor block.
1150 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001151
Douglas Gregoreda6a892009-04-26 00:07:37 +00001152 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1153 // FIXME: use diagnostics subsystem for localization etc.
1154 if (PP.SawDateOrTime())
1155 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001156
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001157 // Loop over all the macro definitions that are live at the end of the file,
1158 // emitting each to the PP section.
Douglas Gregor45053152009-10-17 17:25:45 +00001159 // FIXME: Make sure that this sees macros defined in included PCH files.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001160 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1161 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001162 // FIXME: This emits macros in hash table order, we should do it in a stable
1163 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001164 MacroInfo *MI = I->second;
1165
1166 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1167 // been redefined by the header (in which case they are not isBuiltinMacro).
1168 if (MI->isBuiltinMacro())
1169 continue;
1170
Douglas Gregorc3366a52009-04-21 23:56:24 +00001171 // FIXME: Remove this identifier reference?
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001172 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001173 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001174 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1175 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001176
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001177 unsigned Code;
1178 if (MI->isObjectLike()) {
1179 Code = pch::PP_MACRO_OBJECT_LIKE;
1180 } else {
1181 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001182
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001183 Record.push_back(MI->isC99Varargs());
1184 Record.push_back(MI->isGNUVarargs());
1185 Record.push_back(MI->getNumArgs());
1186 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1187 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001188 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001189 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001190 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001191 Record.clear();
1192
Chris Lattner2199f5b2009-04-10 18:08:30 +00001193 // Emit the tokens array.
1194 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1195 // Note that we know that the preprocessor does not have any annotation
1196 // tokens in it because they are created by the parser, and thus can't be
1197 // in a macro definition.
1198 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001199
Chris Lattner2199f5b2009-04-10 18:08:30 +00001200 Record.push_back(Tok.getLocation().getRawEncoding());
1201 Record.push_back(Tok.getLength());
1202
Chris Lattner2199f5b2009-04-10 18:08:30 +00001203 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1204 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001205 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001206
Chris Lattner2199f5b2009-04-10 18:08:30 +00001207 // FIXME: Should translate token kind to a stable encoding.
1208 Record.push_back(Tok.getKind());
1209 // FIXME: Should translate token flags to a stable encoding.
1210 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001211
Douglas Gregor8f45df52009-04-16 22:23:12 +00001212 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001213 Record.clear();
1214 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001215 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001216 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001217 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001218}
1219
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001220void PCHWriter::WriteComments(ASTContext &Context) {
1221 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001222
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001223 if (Context.Comments.empty())
1224 return;
Mike Stump11289f42009-09-09 15:08:12 +00001225
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001226 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1227 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1228 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1229 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001230
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001231 RecordData Record;
1232 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001233 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001234 (const char*)&Context.Comments[0],
1235 Context.Comments.size() * sizeof(SourceRange));
1236}
1237
Douglas Gregorc5046832009-04-27 18:38:38 +00001238//===----------------------------------------------------------------------===//
1239// Type Serialization
1240//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001241
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001242/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001243void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001244 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001245 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001246 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001247
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001248 // Record the offset for this type.
1249 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001250 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001251 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1252 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001253 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001254 }
1255
1256 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001257
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001258 // Emit the type's representation.
1259 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001260
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001261 if (T.hasLocalNonFastQualifiers()) {
1262 Qualifiers Qs = T.getLocalQualifiers();
1263 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001264 Record.push_back(Qs.getAsOpaqueValue());
1265 W.Code = pch::TYPE_EXT_QUAL;
1266 } else {
1267 switch (T->getTypeClass()) {
1268 // For all of the concrete, non-dependent types, call the
1269 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001270#define TYPE(Class, Base) \
John McCall8ccfcb52009-09-24 19:53:00 +00001271 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001272#define ABSTRACT_TYPE(Class, Base)
1273#define DEPENDENT_TYPE(Class, Base)
1274#include "clang/AST/TypeNodes.def"
1275
John McCall8ccfcb52009-09-24 19:53:00 +00001276 // For all of the dependent type nodes (which only occur in C++
1277 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001278#define TYPE(Class, Base)
1279#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1280#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001281 assert(false && "Cannot serialize dependent type nodes");
1282 break;
1283 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001284 }
1285
1286 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001287 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001288
1289 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001290 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001291}
1292
Douglas Gregorc5046832009-04-27 18:38:38 +00001293//===----------------------------------------------------------------------===//
1294// Declaration Serialization
1295//===----------------------------------------------------------------------===//
1296
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001297/// \brief Write the block containing all of the declaration IDs
1298/// lexically declared within the given DeclContext.
1299///
1300/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1301/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001302uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001303 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001304 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001305 return 0;
1306
Douglas Gregor8f45df52009-04-16 22:23:12 +00001307 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001308 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001309 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1310 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001311 AddDeclRef(*D, Record);
1312
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001313 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001314 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001315 return Offset;
1316}
1317
1318/// \brief Write the block containing all of the declaration IDs
1319/// visible from the given DeclContext.
1320///
1321/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1322/// bistream, or 0 if no block was written.
1323uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1324 DeclContext *DC) {
1325 if (DC->getPrimaryContext() != DC)
1326 return 0;
1327
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001328 // Since there is no name lookup into functions or methods, and we
1329 // perform name lookup for the translation unit via the
1330 // IdentifierInfo chains, don't bother to build a
1331 // visible-declarations table for these entities.
1332 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001333 return 0;
1334
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001335 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001336 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001337
1338 // Serialize the contents of the mapping used for lookup. Note that,
1339 // although we have two very different code paths, the serialized
1340 // representation is the same for both cases: a declaration name,
1341 // followed by a size, followed by references to the visible
1342 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001343 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001344 RecordData Record;
1345 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001346 if (!Map)
1347 return 0;
1348
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001349 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1350 D != DEnd; ++D) {
1351 AddDeclarationName(D->first, Record);
1352 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1353 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001354 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001355 AddDeclRef(*Result.first, Record);
1356 }
1357
1358 if (Record.size() == 0)
1359 return 0;
1360
Douglas Gregor8f45df52009-04-16 22:23:12 +00001361 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001362 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001363 return Offset;
1364}
1365
Douglas Gregorc5046832009-04-27 18:38:38 +00001366//===----------------------------------------------------------------------===//
1367// Global Method Pool and Selector Serialization
1368//===----------------------------------------------------------------------===//
1369
Douglas Gregore84a9da2009-04-20 20:36:09 +00001370namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001371// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001372class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001373 PCHWriter &Writer;
1374
1375public:
1376 typedef Selector key_type;
1377 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregorc78d3462009-04-24 21:10:55 +00001379 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1380 typedef const data_type& data_type_ref;
1381
1382 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001383
Douglas Gregorc78d3462009-04-24 21:10:55 +00001384 static unsigned ComputeHash(Selector Sel) {
1385 unsigned N = Sel.getNumArgs();
1386 if (N == 0)
1387 ++N;
1388 unsigned R = 5381;
1389 for (unsigned I = 0; I != N; ++I)
1390 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001391 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001392 return R;
1393 }
Mike Stump11289f42009-09-09 15:08:12 +00001394
1395 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001396 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1397 data_type_ref Methods) {
1398 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1399 clang::io::Emit16(Out, KeyLen);
1400 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001401 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001402 Method = Method->Next)
1403 if (Method->Method)
1404 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001405 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001406 Method = Method->Next)
1407 if (Method->Method)
1408 DataLen += 4;
1409 clang::io::Emit16(Out, DataLen);
1410 return std::make_pair(KeyLen, DataLen);
1411 }
Mike Stump11289f42009-09-09 15:08:12 +00001412
Douglas Gregor95c13f52009-04-25 17:48:32 +00001413 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001414 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001415 assert((Start >> 32) == 0 && "Selector key offset too large");
1416 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001417 unsigned N = Sel.getNumArgs();
1418 clang::io::Emit16(Out, N);
1419 if (N == 0)
1420 N = 1;
1421 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001422 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001423 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1424 }
Mike Stump11289f42009-09-09 15:08:12 +00001425
Douglas Gregorc78d3462009-04-24 21:10:55 +00001426 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001427 data_type_ref Methods, unsigned DataLen) {
1428 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001429 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001430 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001431 Method = Method->Next)
1432 if (Method->Method)
1433 ++NumInstanceMethods;
1434
1435 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001436 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001437 Method = Method->Next)
1438 if (Method->Method)
1439 ++NumFactoryMethods;
1440
1441 clang::io::Emit16(Out, NumInstanceMethods);
1442 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001443 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001444 Method = Method->Next)
1445 if (Method->Method)
1446 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001447 for (const ObjCMethodList *Method = &Methods.second; 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));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001451
1452 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001453 }
1454};
1455} // end anonymous namespace
1456
1457/// \brief Write the method pool into the PCH file.
1458///
1459/// The method pool contains both instance and factory methods, stored
1460/// in an on-disk hash table indexed by the selector.
1461void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1462 using namespace llvm;
1463
1464 // Create and write out the blob that contains the instance and
1465 // factor method pools.
1466 bool Empty = true;
1467 {
1468 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001469
Douglas Gregorc78d3462009-04-24 21:10:55 +00001470 // Create the on-disk hash table representation. Start by
1471 // iterating through the instance method pool.
1472 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001473 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001474 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001475 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001476 InstanceEnd = SemaRef.InstanceMethodPool.end();
1477 Instance != InstanceEnd; ++Instance) {
1478 // Check whether there is a factory method with the same
1479 // selector.
1480 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1481 = SemaRef.FactoryMethodPool.find(Instance->first);
1482
1483 if (Factory == SemaRef.FactoryMethodPool.end())
1484 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001485 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001486 ObjCMethodList()));
1487 else
1488 Generator.insert(Instance->first,
1489 std::make_pair(Instance->second, Factory->second));
1490
Douglas Gregor95c13f52009-04-25 17:48:32 +00001491 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001492 Empty = false;
1493 }
1494
1495 // Now iterate through the factory method pool, to pick up any
1496 // selectors that weren't already in the instance method pool.
1497 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001498 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001499 FactoryEnd = SemaRef.FactoryMethodPool.end();
1500 Factory != FactoryEnd; ++Factory) {
1501 // Check whether there is an instance method with the same
1502 // selector. If so, there is no work to do here.
1503 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1504 = SemaRef.InstanceMethodPool.find(Factory->first);
1505
Douglas Gregor95c13f52009-04-25 17:48:32 +00001506 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001507 Generator.insert(Factory->first,
1508 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001509 ++NumSelectorsInMethodPool;
1510 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001511
1512 Empty = false;
1513 }
1514
Douglas Gregor95c13f52009-04-25 17:48:32 +00001515 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001516 return;
1517
1518 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001519 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001520 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001521 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001522 {
1523 PCHMethodPoolTrait Trait(*this);
1524 llvm::raw_svector_ostream Out(MethodPool);
1525 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001526 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001527 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001528
1529 // For every selector that we have seen but which was not
1530 // written into the hash table, write the selector itself and
1531 // record it's offset.
1532 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1533 if (SelectorOffsets[I] == 0)
1534 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001535 }
1536
1537 // Create a blob abbreviation
1538 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1539 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1543 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1544
Douglas Gregor95c13f52009-04-25 17:48:32 +00001545 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001546 RecordData Record;
1547 Record.push_back(pch::METHOD_POOL);
1548 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001549 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001550 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001551
1552 // Create a blob abbreviation for the selector table offsets.
1553 Abbrev = new BitCodeAbbrev();
1554 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1557 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1558
1559 // Write the selector offsets table.
1560 Record.clear();
1561 Record.push_back(pch::SELECTOR_OFFSETS);
1562 Record.push_back(SelectorOffsets.size());
1563 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1564 (const char *)&SelectorOffsets.front(),
1565 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001566 }
1567}
1568
Douglas Gregorc5046832009-04-27 18:38:38 +00001569//===----------------------------------------------------------------------===//
1570// Identifier Table Serialization
1571//===----------------------------------------------------------------------===//
1572
Douglas Gregorc78d3462009-04-24 21:10:55 +00001573namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001574class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001575 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001576 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001577
Douglas Gregor1d583f22009-04-28 21:18:29 +00001578 /// \brief Determines whether this is an "interesting" identifier
1579 /// that needs a full IdentifierInfo structure written into the hash
1580 /// table.
1581 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1582 return II->isPoisoned() ||
1583 II->isExtensionToken() ||
1584 II->hasMacroDefinition() ||
1585 II->getObjCOrBuiltinID() ||
1586 II->getFETokenInfo<void>();
1587 }
1588
Douglas Gregore84a9da2009-04-20 20:36:09 +00001589public:
1590 typedef const IdentifierInfo* key_type;
1591 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001592
Douglas Gregore84a9da2009-04-20 20:36:09 +00001593 typedef pch::IdentID data_type;
1594 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001595
1596 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001597 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001598
1599 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001600 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001601 }
Mike Stump11289f42009-09-09 15:08:12 +00001602
1603 std::pair<unsigned,unsigned>
1604 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001605 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001606 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001607 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1608 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001609 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001610 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001611 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001612 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001613 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1614 DEnd = IdentifierResolver::end();
1615 D != DEnd; ++D)
1616 DataLen += sizeof(pch::DeclID);
1617 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001618 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001619 // We emit the key length after the data length so that every
1620 // string is preceded by a 16-bit length. This matches the PTH
1621 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001622 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001623 return std::make_pair(KeyLen, DataLen);
1624 }
Mike Stump11289f42009-09-09 15:08:12 +00001625
1626 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001627 unsigned KeyLen) {
1628 // Record the location of the key data. This is used when generating
1629 // the mapping from persistent IDs to strings.
1630 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001631 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001632 }
Mike Stump11289f42009-09-09 15:08:12 +00001633
1634 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001635 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001636 if (!isInterestingIdentifier(II)) {
1637 clang::io::Emit32(Out, ID << 1);
1638 return;
1639 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001640
Douglas Gregor1d583f22009-04-28 21:18:29 +00001641 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001642 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001643 bool hasMacroDefinition =
1644 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001645 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001646 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001647 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001648 Bits = (Bits << 1) | II->isExtensionToken();
1649 Bits = (Bits << 1) | II->isPoisoned();
1650 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregorb9256522009-04-28 21:32:13 +00001651 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001652
Douglas Gregorc3366a52009-04-21 23:56:24 +00001653 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001654 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001655
Douglas Gregora868bbd2009-04-21 22:25:48 +00001656 // Emit the declaration IDs in reverse order, because the
1657 // IdentifierResolver provides the declarations as they would be
1658 // visible (e.g., the function "stat" would come before the struct
1659 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1660 // adds declarations to the end of the list (so we need to see the
1661 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001662 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001663 IdentifierResolver::end());
1664 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1665 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001666 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001667 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001668 }
1669};
1670} // end anonymous namespace
1671
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001672/// \brief Write the identifier table into the PCH file.
1673///
1674/// The identifier table consists of a blob containing string data
1675/// (the actual identifiers themselves) and a separate "offsets" index
1676/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001677void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001678 using namespace llvm;
1679
1680 // Create and write out the blob that contains the identifier
1681 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001682 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001683 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001684
Douglas Gregore6648fb2009-04-28 20:33:11 +00001685 // Look for any identifiers that were named while processing the
1686 // headers, but are otherwise not needed. We add these to the hash
1687 // table to enable checking of the predefines buffer in the case
1688 // where the user adds new macro definitions when building the PCH
1689 // file.
1690 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1691 IDEnd = PP.getIdentifierTable().end();
1692 ID != IDEnd; ++ID)
1693 getIdentifierRef(ID->second);
1694
Douglas Gregore84a9da2009-04-20 20:36:09 +00001695 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001696 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001697 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1698 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1699 ID != IDEnd; ++ID) {
1700 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001701 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001702 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001703
Douglas Gregore84a9da2009-04-20 20:36:09 +00001704 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001705 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001706 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001707 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001708 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001709 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001710 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001711 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001712 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001713 }
1714
1715 // Create a blob abbreviation
1716 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1717 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001720 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001721
1722 // Write the identifier table
1723 RecordData Record;
1724 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001725 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001726 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001727 }
1728
1729 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001730 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1731 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1734 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1735
1736 RecordData Record;
1737 Record.push_back(pch::IDENTIFIER_OFFSET);
1738 Record.push_back(IdentifierOffsets.size());
1739 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1740 (const char *)&IdentifierOffsets.front(),
1741 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001742}
1743
Douglas Gregorc5046832009-04-27 18:38:38 +00001744//===----------------------------------------------------------------------===//
1745// General Serialization Routines
1746//===----------------------------------------------------------------------===//
1747
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001748/// \brief Write a record containing the given attributes.
1749void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1750 RecordData Record;
1751 for (; Attr; Attr = Attr->getNext()) {
1752 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1753 Record.push_back(Attr->isInherited());
1754 switch (Attr->getKind()) {
1755 case Attr::Alias:
1756 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1757 break;
1758
1759 case Attr::Aligned:
1760 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1761 break;
1762
1763 case Attr::AlwaysInline:
1764 break;
Mike Stump11289f42009-09-09 15:08:12 +00001765
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001766 case Attr::AnalyzerNoReturn:
1767 break;
1768
1769 case Attr::Annotate:
1770 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1771 break;
1772
1773 case Attr::AsmLabel:
1774 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1775 break;
1776
Alexis Hunt54a02542009-11-25 04:20:27 +00001777 case Attr::BaseCheck:
1778 break;
1779
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001780 case Attr::Blocks:
1781 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1782 break;
1783
Eli Friedmane4310c82009-11-09 18:38:53 +00001784 case Attr::CDecl:
1785 break;
1786
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001787 case Attr::Cleanup:
1788 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1789 break;
1790
1791 case Attr::Const:
1792 break;
1793
1794 case Attr::Constructor:
1795 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1796 break;
1797
1798 case Attr::DLLExport:
1799 case Attr::DLLImport:
1800 case Attr::Deprecated:
1801 break;
1802
1803 case Attr::Destructor:
1804 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1805 break;
1806
1807 case Attr::FastCall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001808 case Attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001809 break;
1810
1811 case Attr::Format: {
1812 const FormatAttr *Format = cast<FormatAttr>(Attr);
1813 AddString(Format->getType(), Record);
1814 Record.push_back(Format->getFormatIdx());
1815 Record.push_back(Format->getFirstArg());
1816 break;
1817 }
1818
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001819 case Attr::FormatArg: {
1820 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1821 Record.push_back(Format->getFormatIdx());
1822 break;
1823 }
1824
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001825 case Attr::Sentinel : {
1826 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1827 Record.push_back(Sentinel->getSentinel());
1828 Record.push_back(Sentinel->getNullPos());
1829 break;
1830 }
Mike Stump11289f42009-09-09 15:08:12 +00001831
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001832 case Attr::GNUInline:
Alexis Hunt54a02542009-11-25 04:20:27 +00001833 case Attr::Hiding:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001834 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001835 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001836 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001837 case Attr::NoReturn:
1838 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001839 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001840 break;
1841
1842 case Attr::NonNull: {
1843 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1844 Record.push_back(NonNull->size());
1845 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1846 break;
1847 }
1848
1849 case Attr::ObjCException:
1850 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001851 case Attr::CFReturnsRetained:
1852 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001853 case Attr::Overloadable:
Alexis Hunt54a02542009-11-25 04:20:27 +00001854 case Attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001855 break;
1856
Anders Carlsson68e0b682009-08-08 18:23:56 +00001857 case Attr::PragmaPack:
1858 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001859 break;
1860
Anders Carlsson68e0b682009-08-08 18:23:56 +00001861 case Attr::Packed:
1862 break;
Mike Stump11289f42009-09-09 15:08:12 +00001863
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001864 case Attr::Pure:
1865 break;
1866
1867 case Attr::Regparm:
1868 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1869 break;
Mike Stump11289f42009-09-09 15:08:12 +00001870
Nate Begemanf2758702009-06-26 06:32:41 +00001871 case Attr::ReqdWorkGroupSize:
1872 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1873 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1874 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1875 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001876
1877 case Attr::Section:
1878 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1879 break;
1880
1881 case Attr::StdCall:
1882 case Attr::TransparentUnion:
1883 case Attr::Unavailable:
1884 case Attr::Unused:
1885 case Attr::Used:
1886 break;
1887
1888 case Attr::Visibility:
1889 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001890 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001891 break;
1892
1893 case Attr::WarnUnusedResult:
1894 case Attr::Weak:
1895 case Attr::WeakImport:
1896 break;
1897 }
1898 }
1899
Douglas Gregor8f45df52009-04-16 22:23:12 +00001900 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001901}
1902
1903void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1904 Record.push_back(Str.size());
1905 Record.insert(Record.end(), Str.begin(), Str.end());
1906}
1907
Douglas Gregore84a9da2009-04-20 20:36:09 +00001908/// \brief Note that the identifier II occurs at the given offset
1909/// within the identifier table.
1910void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001911 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001912}
1913
Douglas Gregor95c13f52009-04-25 17:48:32 +00001914/// \brief Note that the selector Sel occurs at the given offset
1915/// within the method pool/selector table.
1916void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1917 unsigned ID = SelectorIDs[Sel];
1918 assert(ID && "Unknown selector");
1919 SelectorOffsets[ID - 1] = Offset;
1920}
1921
Mike Stump11289f42009-09-09 15:08:12 +00001922PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1923 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001924 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1925 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001926
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001927void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1928 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001929 using namespace llvm;
1930
Douglas Gregor162dd022009-04-20 15:53:59 +00001931 ASTContext &Context = SemaRef.Context;
1932 Preprocessor &PP = SemaRef.PP;
1933
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001934 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001935 Stream.Emit((unsigned)'C', 8);
1936 Stream.Emit((unsigned)'P', 8);
1937 Stream.Emit((unsigned)'C', 8);
1938 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001939
Chris Lattner28fa4e62009-04-26 22:26:21 +00001940 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001941
1942 // The translation unit is the first declaration we'll emit.
1943 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001944 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001945
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001946 // Make sure that we emit IdentifierInfos (and any attached
1947 // declarations) for builtins.
1948 {
1949 IdentifierTable &Table = PP.getIdentifierTable();
1950 llvm::SmallVector<const char *, 32> BuiltinNames;
1951 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1952 Context.getLangOptions().NoBuiltin);
1953 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1954 getIdentifierRef(&Table.get(BuiltinNames[I]));
1955 }
1956
Chris Lattner0c797362009-09-08 18:19:27 +00001957 // Build a record containing all of the tentative definitions in this file, in
1958 // TentativeDefinitionList order. Generally, this record will be empty for
1959 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001960 RecordData TentativeDefinitions;
Chris Lattner0c797362009-09-08 18:19:27 +00001961 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1962 VarDecl *VD =
1963 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1964 if (VD) AddDeclRef(VD, TentativeDefinitions);
1965 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001966
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001967 // Build a record containing all of the locally-scoped external
1968 // declarations in this header file. Generally, this record will be
1969 // empty.
1970 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001971 // FIXME: This is filling in the PCH file in densemap order which is
1972 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001973 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001974 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1975 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1976 TD != TDEnd; ++TD)
1977 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1978
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001979 // Build a record containing all of the ext_vector declarations.
1980 RecordData ExtVectorDecls;
1981 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1982 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1983
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001984 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001985 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001986 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001987 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001988 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001989 if (StatCalls && !isysroot)
1990 WriteStatCache(*StatCalls, isysroot);
1991 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00001992 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001993 // Write the record of special types.
1994 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001995
Steve Naroffc277ad12009-07-18 15:33:26 +00001996 AddTypeRef(Context.getBuiltinVaListType(), Record);
1997 AddTypeRef(Context.getObjCIdType(), Record);
1998 AddTypeRef(Context.getObjCSelType(), Record);
1999 AddTypeRef(Context.getObjCProtoType(), Record);
2000 AddTypeRef(Context.getObjCClassType(), Record);
2001 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2002 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2003 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002004 AddTypeRef(Context.getjmp_bufType(), Record);
2005 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002006 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2007 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002008#if 0
2009 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian04b258c2009-11-25 23:07:42 +00002010 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002011#endif
Mike Stumpd0153282009-10-20 02:12:22 +00002012 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002013 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002014 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002015
Douglas Gregor1970d882009-04-26 03:49:13 +00002016 // Keep writing types and declarations until all types and
2017 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002018 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2019 WriteDeclsBlockAbbrevs();
2020 while (!DeclTypesToEmit.empty()) {
2021 DeclOrType DOT = DeclTypesToEmit.front();
2022 DeclTypesToEmit.pop();
2023 if (DOT.isType())
2024 WriteType(DOT.getType());
2025 else
2026 WriteDecl(Context, DOT.getDecl());
2027 }
2028 Stream.ExitBlock();
2029
Douglas Gregor45053152009-10-17 17:25:45 +00002030 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002031 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002032 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002033
2034 // Write the type offsets array
2035 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2036 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2039 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2040 Record.clear();
2041 Record.push_back(pch::TYPE_OFFSET);
2042 Record.push_back(TypeOffsets.size());
2043 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002044 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002045 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002046
Douglas Gregor745ed142009-04-25 18:35:21 +00002047 // Write the declaration offsets array
2048 Abbrev = new BitCodeAbbrev();
2049 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2052 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2053 Record.clear();
2054 Record.push_back(pch::DECL_OFFSET);
2055 Record.push_back(DeclOffsets.size());
2056 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002057 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002058 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002059
Douglas Gregord4df8652009-04-22 22:02:47 +00002060 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002061 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002062 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002063
2064 // Write the record containing tentative definitions.
2065 if (!TentativeDefinitions.empty())
2066 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002067
2068 // Write the record containing locally-scoped external definitions.
2069 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002070 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002071 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002072
2073 // Write the record containing ext_vector type names.
2074 if (!ExtVectorDecls.empty())
2075 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002076
Douglas Gregor08f01292009-04-17 22:13:46 +00002077 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002078 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002079 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002080 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002081 Record.push_back(NumLexicalDeclContexts);
2082 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002083 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002084 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002085}
2086
2087void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2088 Record.push_back(Loc.getRawEncoding());
2089}
2090
2091void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2092 Record.push_back(Value.getBitWidth());
2093 unsigned N = Value.getNumWords();
2094 const uint64_t* Words = Value.getRawData();
2095 for (unsigned I = 0; I != N; ++I)
2096 Record.push_back(Words[I]);
2097}
2098
Douglas Gregor1daeb692009-04-13 18:14:40 +00002099void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2100 Record.push_back(Value.isUnsigned());
2101 AddAPInt(Value, Record);
2102}
2103
Douglas Gregore0a3a512009-04-14 21:55:33 +00002104void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2105 AddAPInt(Value.bitcastToAPInt(), Record);
2106}
2107
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002108void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002109 Record.push_back(getIdentifierRef(II));
2110}
2111
2112pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2113 if (II == 0)
2114 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002115
2116 pch::IdentID &ID = IdentifierIDs[II];
2117 if (ID == 0)
2118 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002119 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002120}
2121
Steve Naroff2ddea052009-04-23 10:39:46 +00002122void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2123 if (SelRef.getAsOpaquePtr() == 0) {
2124 Record.push_back(0);
2125 return;
2126 }
2127
2128 pch::SelectorID &SID = SelectorIDs[SelRef];
2129 if (SID == 0) {
2130 SID = SelectorIDs.size();
2131 SelVector.push_back(SelRef);
2132 }
2133 Record.push_back(SID);
2134}
2135
John McCall0ad16662009-10-29 08:12:44 +00002136void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2137 RecordData &Record) {
2138 switch (Arg.getArgument().getKind()) {
2139 case TemplateArgument::Expression:
2140 AddStmt(Arg.getLocInfo().getAsExpr());
2141 break;
2142 case TemplateArgument::Type:
2143 AddDeclaratorInfo(Arg.getLocInfo().getAsDeclaratorInfo(), Record);
2144 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002145 case TemplateArgument::Template:
2146 Record.push_back(
2147 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2148 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2149 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2150 break;
John McCall0ad16662009-10-29 08:12:44 +00002151 case TemplateArgument::Null:
2152 case TemplateArgument::Integral:
2153 case TemplateArgument::Declaration:
2154 case TemplateArgument::Pack:
2155 break;
2156 }
2157}
2158
John McCall8f115c62009-10-16 21:56:05 +00002159void PCHWriter::AddDeclaratorInfo(DeclaratorInfo *DInfo, RecordData &Record) {
2160 if (DInfo == 0) {
2161 AddTypeRef(QualType(), Record);
2162 return;
2163 }
2164
John McCall17001972009-10-18 01:05:36 +00002165 AddTypeRef(DInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002166 TypeLocWriter TLW(*this, Record);
2167 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2168 TLW.Visit(TL);
2169}
2170
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002171void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2172 if (T.isNull()) {
2173 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2174 return;
2175 }
2176
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002177 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002178 T.removeFastQualifiers();
2179
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002180 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002181 pch::TypeID &ID = TypeIDs[T];
2182 if (ID == 0) {
2183 // We haven't seen these qualifiers applied to this type before.
2184 // Assign it a new ID. This is the only time we enqueue a
2185 // qualified type, and it has no CV qualifiers.
2186 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002187 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002188 }
2189
2190 // Encode the type qualifiers in the type reference.
2191 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2192 return;
2193 }
2194
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002195 assert(!T.hasLocalQualifiers());
John McCall8ccfcb52009-09-24 19:53:00 +00002196
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002197 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002198 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002199 switch (BT->getKind()) {
2200 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2201 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2202 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2203 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2204 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2205 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2206 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2207 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002208 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002209 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2210 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2211 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2212 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2213 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2214 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2215 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002216 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002217 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2218 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2219 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002220 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002221 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2222 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002223 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2224 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002225 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2226 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002227 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002228 case BuiltinType::UndeducedAuto:
2229 assert(0 && "Should not see undeduced auto here");
2230 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002231 }
2232
John McCall8ccfcb52009-09-24 19:53:00 +00002233 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002234 return;
2235 }
2236
John McCall8ccfcb52009-09-24 19:53:00 +00002237 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002238 if (ID == 0) {
2239 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002240 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002241 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002242 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002243 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002244
2245 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002246 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002247}
2248
2249void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2250 if (D == 0) {
2251 Record.push_back(0);
2252 return;
2253 }
2254
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002255 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002256 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002257 // We haven't seen this declaration before. Give it a new ID and
2258 // enqueue it in the list of declarations to emit.
2259 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002260 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002261 }
2262
2263 Record.push_back(ID);
2264}
2265
Douglas Gregore84a9da2009-04-20 20:36:09 +00002266pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2267 if (D == 0)
2268 return 0;
2269
2270 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2271 return DeclIDs[D];
2272}
2273
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002274void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002275 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002276 Record.push_back(Name.getNameKind());
2277 switch (Name.getNameKind()) {
2278 case DeclarationName::Identifier:
2279 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2280 break;
2281
2282 case DeclarationName::ObjCZeroArgSelector:
2283 case DeclarationName::ObjCOneArgSelector:
2284 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002285 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002286 break;
2287
2288 case DeclarationName::CXXConstructorName:
2289 case DeclarationName::CXXDestructorName:
2290 case DeclarationName::CXXConversionFunctionName:
2291 AddTypeRef(Name.getCXXNameType(), Record);
2292 break;
2293
2294 case DeclarationName::CXXOperatorName:
2295 Record.push_back(Name.getCXXOverloadedOperator());
2296 break;
2297
Alexis Hunt3d221f22009-11-29 07:34:05 +00002298 case DeclarationName::CXXLiteralOperatorName:
2299 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2300 break;
2301
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002302 case DeclarationName::CXXUsingDirective:
2303 // No extra data to emit
2304 break;
2305 }
2306}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002307