blob: fb48df332121803686a8e7544b6f445250be2a1d [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"
36#include "llvm/Support/Compiler.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000045
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046namespace {
47 class VISIBILITY_HIDDEN PCHTypeWriter {
48 PCHWriter &Writer;
49 PCHWriter::RecordData &Record;
50
51 public:
52 /// \brief Type code that corresponds to the record generated.
53 pch::TypeCode Code;
54
Mike Stump11289f42009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000057
58 void VisitArrayType(const ArrayType *T);
59 void VisitFunctionType(const FunctionType *T);
60 void VisitTagType(const TagType *T);
61
62#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63#define ABSTRACT_TYPE(Class, Base)
64#define DEPENDENT_TYPE(Class, Base)
65#include "clang/AST/TypeNodes.def"
66 };
67}
68
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
73void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
74 Record.push_back(T->getWidth());
75 Record.push_back(T->isSigned());
76 Code = pch::TYPE_FIXED_WIDTH_INT;
77}
78
79void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
80 Writer.AddTypeRef(T->getElementType(), Record);
81 Code = pch::TYPE_COMPLEX;
82}
83
84void PCHTypeWriter::VisitPointerType(const PointerType *T) {
85 Writer.AddTypeRef(T->getPointeeType(), Record);
86 Code = pch::TYPE_POINTER;
87}
88
89void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000090 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000091 Code = pch::TYPE_BLOCK_POINTER;
92}
93
94void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
95 Writer.AddTypeRef(T->getPointeeType(), Record);
96 Code = pch::TYPE_LVALUE_REFERENCE;
97}
98
99void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
100 Writer.AddTypeRef(T->getPointeeType(), Record);
101 Code = pch::TYPE_RVALUE_REFERENCE;
102}
103
104void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000105 Writer.AddTypeRef(T->getPointeeType(), Record);
106 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Code = pch::TYPE_MEMBER_POINTER;
108}
109
110void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
111 Writer.AddTypeRef(T->getElementType(), Record);
112 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000113 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000114}
115
116void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
117 VisitArrayType(T);
118 Writer.AddAPInt(T->getSize(), Record);
119 Code = pch::TYPE_CONSTANT_ARRAY;
120}
121
122void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
123 VisitArrayType(T);
124 Code = pch::TYPE_INCOMPLETE_ARRAY;
125}
126
127void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
128 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000129 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
130 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000131 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132 Code = pch::TYPE_VARIABLE_ARRAY;
133}
134
135void PCHTypeWriter::VisitVectorType(const VectorType *T) {
136 Writer.AddTypeRef(T->getElementType(), Record);
137 Record.push_back(T->getNumElements());
138 Code = pch::TYPE_VECTOR;
139}
140
141void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
142 VisitVectorType(T);
143 Code = pch::TYPE_EXT_VECTOR;
144}
145
146void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
147 Writer.AddTypeRef(T->getResultType(), Record);
148}
149
150void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
151 VisitFunctionType(T);
152 Code = pch::TYPE_FUNCTION_NO_PROTO;
153}
154
155void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
156 VisitFunctionType(T);
157 Record.push_back(T->getNumArgs());
158 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
159 Writer.AddTypeRef(T->getArgType(I), Record);
160 Record.push_back(T->isVariadic());
161 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000162 Record.push_back(T->hasExceptionSpec());
163 Record.push_back(T->hasAnyExceptionSpec());
164 Record.push_back(T->getNumExceptions());
165 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
166 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167 Code = pch::TYPE_FUNCTION_PROTO;
168}
169
170void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_TYPEDEF;
173}
174
175void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000176 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177 Code = pch::TYPE_TYPEOF_EXPR;
178}
179
180void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
181 Writer.AddTypeRef(T->getUnderlyingType(), Record);
182 Code = pch::TYPE_TYPEOF;
183}
184
Anders Carlsson81df7b82009-06-24 19:06:50 +0000185void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
186 Writer.AddStmt(T->getUnderlyingExpr());
187 Code = pch::TYPE_DECLTYPE;
188}
189
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190void PCHTypeWriter::VisitTagType(const TagType *T) {
191 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000192 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193 "Cannot serialize in the middle of a type definition");
194}
195
196void PCHTypeWriter::VisitRecordType(const RecordType *T) {
197 VisitTagType(T);
198 Code = pch::TYPE_RECORD;
199}
200
201void PCHTypeWriter::VisitEnumType(const EnumType *T) {
202 VisitTagType(T);
203 Code = pch::TYPE_ENUM;
204}
205
John McCallfcc33b02009-09-05 00:15:47 +0000206void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
207 Writer.AddTypeRef(T->getUnderlyingType(), Record);
208 Record.push_back(T->getTagKind());
209 Code = pch::TYPE_ELABORATED;
210}
211
Mike Stump11289f42009-09-09 15:08:12 +0000212void
John McCallcebee162009-10-18 09:09:24 +0000213PCHTypeWriter::VisitSubstTemplateTypeParmType(
214 const SubstTemplateTypeParmType *T) {
215 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
216 Writer.AddTypeRef(T->getReplacementType(), Record);
217 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
218}
219
220void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000221PCHTypeWriter::VisitTemplateSpecializationType(
222 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000223 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224 assert(false && "Cannot serialize template specialization types");
225}
226
227void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000228 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229 assert(false && "Cannot serialize qualified name types");
230}
231
232void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
233 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000234 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000235 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
236 E = T->qual_end(); I != E; ++I)
237 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000238 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239}
240
Steve Narofffb4330f2009-06-17 22:40:22 +0000241void
242PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000243 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000245 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000246 E = T->qual_end(); I != E; ++I)
247 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000248 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000249}
250
John McCall8f115c62009-10-16 21:56:05 +0000251namespace {
252
253class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
254 PCHWriter &Writer;
255 PCHWriter::RecordData &Record;
256
257public:
258 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
259 : Writer(Writer), Record(Record) { }
260
John McCall17001972009-10-18 01:05:36 +0000261#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000262#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000263 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000264#include "clang/AST/TypeLocNodes.def"
265
John McCall17001972009-10-18 01:05:36 +0000266 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
267 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000268};
269
270}
271
John McCall17001972009-10-18 01:05:36 +0000272void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
273 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000274}
John McCall17001972009-10-18 01:05:36 +0000275void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
276 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000277}
John McCall17001972009-10-18 01:05:36 +0000278void TypeLocWriter::VisitFixedWidthIntTypeLoc(FixedWidthIntTypeLoc TL) {
279 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000280}
John McCall17001972009-10-18 01:05:36 +0000281void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
282 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000283}
John McCall17001972009-10-18 01:05:36 +0000284void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
285 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000286}
John McCall17001972009-10-18 01:05:36 +0000287void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
288 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000289}
John McCall17001972009-10-18 01:05:36 +0000290void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
291 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000292}
John McCall17001972009-10-18 01:05:36 +0000293void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
294 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000295}
John McCall17001972009-10-18 01:05:36 +0000296void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
297 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000298}
John McCall17001972009-10-18 01:05:36 +0000299void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
300 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
301 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
302 Record.push_back(TL.getSizeExpr() ? 1 : 0);
303 if (TL.getSizeExpr())
304 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000305}
John McCall17001972009-10-18 01:05:36 +0000306void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
307 VisitArrayTypeLoc(TL);
308}
309void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
310 VisitArrayTypeLoc(TL);
311}
312void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
313 VisitArrayTypeLoc(TL);
314}
315void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
316 DependentSizedArrayTypeLoc TL) {
317 VisitArrayTypeLoc(TL);
318}
319void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
320 DependentSizedExtVectorTypeLoc TL) {
321 Writer.AddSourceLocation(TL.getNameLoc(), Record);
322}
323void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
324 Writer.AddSourceLocation(TL.getNameLoc(), Record);
325}
326void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
327 Writer.AddSourceLocation(TL.getNameLoc(), Record);
328}
329void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
330 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
331 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
332 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
333 Writer.AddDeclRef(TL.getArg(i), Record);
334}
335void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
336 VisitFunctionTypeLoc(TL);
337}
338void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
339 VisitFunctionTypeLoc(TL);
340}
341void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
342 Writer.AddSourceLocation(TL.getNameLoc(), Record);
343}
344void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
345 Writer.AddSourceLocation(TL.getNameLoc(), Record);
346}
347void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
348 Writer.AddSourceLocation(TL.getNameLoc(), Record);
349}
350void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
351 Writer.AddSourceLocation(TL.getNameLoc(), Record);
352}
353void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
354 Writer.AddSourceLocation(TL.getNameLoc(), Record);
355}
356void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
357 Writer.AddSourceLocation(TL.getNameLoc(), Record);
358}
359void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
360 Writer.AddSourceLocation(TL.getNameLoc(), Record);
361}
362void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
363 Writer.AddSourceLocation(TL.getNameLoc(), Record);
364}
John McCallcebee162009-10-18 09:09:24 +0000365void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
366 SubstTemplateTypeParmTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getNameLoc(), Record);
368}
John McCall17001972009-10-18 01:05:36 +0000369void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
370 TemplateSpecializationTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getNameLoc(), Record);
372}
373void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getNameLoc(), Record);
375}
376void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000381 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
382 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
383 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
384 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000385}
John McCallfc93cf92009-10-22 22:37:11 +0000386void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
387 Writer.AddSourceLocation(TL.getStarLoc(), Record);
388 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
389 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
390 Record.push_back(TL.hasBaseTypeAsWritten());
391 Record.push_back(TL.hasProtocolsAsWritten());
392 if (TL.hasProtocolsAsWritten())
393 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
394 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
395}
John McCall8f115c62009-10-16 21:56:05 +0000396
Chris Lattner19cea4e2009-04-22 05:57:30 +0000397//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000398// PCHWriter Implementation
399//===----------------------------------------------------------------------===//
400
Chris Lattner28fa4e62009-04-26 22:26:21 +0000401static void EmitBlockID(unsigned ID, const char *Name,
402 llvm::BitstreamWriter &Stream,
403 PCHWriter::RecordData &Record) {
404 Record.clear();
405 Record.push_back(ID);
406 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
407
408 // Emit the block name if present.
409 if (Name == 0 || Name[0] == 0) return;
410 Record.clear();
411 while (*Name)
412 Record.push_back(*Name++);
413 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
414}
415
416static void EmitRecordID(unsigned ID, const char *Name,
417 llvm::BitstreamWriter &Stream,
418 PCHWriter::RecordData &Record) {
419 Record.clear();
420 Record.push_back(ID);
421 while (*Name)
422 Record.push_back(*Name++);
423 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000424}
425
426static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
427 PCHWriter::RecordData &Record) {
428#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
429 RECORD(STMT_STOP);
430 RECORD(STMT_NULL_PTR);
431 RECORD(STMT_NULL);
432 RECORD(STMT_COMPOUND);
433 RECORD(STMT_CASE);
434 RECORD(STMT_DEFAULT);
435 RECORD(STMT_LABEL);
436 RECORD(STMT_IF);
437 RECORD(STMT_SWITCH);
438 RECORD(STMT_WHILE);
439 RECORD(STMT_DO);
440 RECORD(STMT_FOR);
441 RECORD(STMT_GOTO);
442 RECORD(STMT_INDIRECT_GOTO);
443 RECORD(STMT_CONTINUE);
444 RECORD(STMT_BREAK);
445 RECORD(STMT_RETURN);
446 RECORD(STMT_DECL);
447 RECORD(STMT_ASM);
448 RECORD(EXPR_PREDEFINED);
449 RECORD(EXPR_DECL_REF);
450 RECORD(EXPR_INTEGER_LITERAL);
451 RECORD(EXPR_FLOATING_LITERAL);
452 RECORD(EXPR_IMAGINARY_LITERAL);
453 RECORD(EXPR_STRING_LITERAL);
454 RECORD(EXPR_CHARACTER_LITERAL);
455 RECORD(EXPR_PAREN);
456 RECORD(EXPR_UNARY_OPERATOR);
457 RECORD(EXPR_SIZEOF_ALIGN_OF);
458 RECORD(EXPR_ARRAY_SUBSCRIPT);
459 RECORD(EXPR_CALL);
460 RECORD(EXPR_MEMBER);
461 RECORD(EXPR_BINARY_OPERATOR);
462 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
463 RECORD(EXPR_CONDITIONAL_OPERATOR);
464 RECORD(EXPR_IMPLICIT_CAST);
465 RECORD(EXPR_CSTYLE_CAST);
466 RECORD(EXPR_COMPOUND_LITERAL);
467 RECORD(EXPR_EXT_VECTOR_ELEMENT);
468 RECORD(EXPR_INIT_LIST);
469 RECORD(EXPR_DESIGNATED_INIT);
470 RECORD(EXPR_IMPLICIT_VALUE_INIT);
471 RECORD(EXPR_VA_ARG);
472 RECORD(EXPR_ADDR_LABEL);
473 RECORD(EXPR_STMT);
474 RECORD(EXPR_TYPES_COMPATIBLE);
475 RECORD(EXPR_CHOOSE);
476 RECORD(EXPR_GNU_NULL);
477 RECORD(EXPR_SHUFFLE_VECTOR);
478 RECORD(EXPR_BLOCK);
479 RECORD(EXPR_BLOCK_DECL_REF);
480 RECORD(EXPR_OBJC_STRING_LITERAL);
481 RECORD(EXPR_OBJC_ENCODE);
482 RECORD(EXPR_OBJC_SELECTOR_EXPR);
483 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
484 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
485 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
486 RECORD(EXPR_OBJC_KVC_REF_EXPR);
487 RECORD(EXPR_OBJC_MESSAGE_EXPR);
488 RECORD(EXPR_OBJC_SUPER_EXPR);
489 RECORD(STMT_OBJC_FOR_COLLECTION);
490 RECORD(STMT_OBJC_CATCH);
491 RECORD(STMT_OBJC_FINALLY);
492 RECORD(STMT_OBJC_AT_TRY);
493 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
494 RECORD(STMT_OBJC_AT_THROW);
495#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000496}
Mike Stump11289f42009-09-09 15:08:12 +0000497
Chris Lattner28fa4e62009-04-26 22:26:21 +0000498void PCHWriter::WriteBlockInfoBlock() {
499 RecordData Record;
500 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattner64031982009-04-27 00:40:25 +0000502#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000503#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattner28fa4e62009-04-26 22:26:21 +0000505 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000506 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000507 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000508 RECORD(TYPE_OFFSET);
509 RECORD(DECL_OFFSET);
510 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000511 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000512 RECORD(IDENTIFIER_OFFSET);
513 RECORD(IDENTIFIER_TABLE);
514 RECORD(EXTERNAL_DEFINITIONS);
515 RECORD(SPECIAL_TYPES);
516 RECORD(STATISTICS);
517 RECORD(TENTATIVE_DEFINITIONS);
518 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
519 RECORD(SELECTOR_OFFSETS);
520 RECORD(METHOD_POOL);
521 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000522 RECORD(SOURCE_LOCATION_OFFSETS);
523 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000524 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000525 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000526 RECORD(COMMENT_RANGES);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000527 RECORD(SVN_BRANCH_REVISION);
528
Chris Lattner28fa4e62009-04-26 22:26:21 +0000529 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000530 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000531 RECORD(SM_SLOC_FILE_ENTRY);
532 RECORD(SM_SLOC_BUFFER_ENTRY);
533 RECORD(SM_SLOC_BUFFER_BLOB);
534 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
535 RECORD(SM_LINE_TABLE);
536 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000537
Chris Lattner28fa4e62009-04-26 22:26:21 +0000538 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000539 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000540 RECORD(PP_MACRO_OBJECT_LIKE);
541 RECORD(PP_MACRO_FUNCTION_LIKE);
542 RECORD(PP_TOKEN);
543
Douglas Gregor12bfa382009-10-17 00:13:19 +0000544 // Decls and Types block.
545 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000546 RECORD(TYPE_EXT_QUAL);
547 RECORD(TYPE_FIXED_WIDTH_INT);
548 RECORD(TYPE_COMPLEX);
549 RECORD(TYPE_POINTER);
550 RECORD(TYPE_BLOCK_POINTER);
551 RECORD(TYPE_LVALUE_REFERENCE);
552 RECORD(TYPE_RVALUE_REFERENCE);
553 RECORD(TYPE_MEMBER_POINTER);
554 RECORD(TYPE_CONSTANT_ARRAY);
555 RECORD(TYPE_INCOMPLETE_ARRAY);
556 RECORD(TYPE_VARIABLE_ARRAY);
557 RECORD(TYPE_VECTOR);
558 RECORD(TYPE_EXT_VECTOR);
559 RECORD(TYPE_FUNCTION_PROTO);
560 RECORD(TYPE_FUNCTION_NO_PROTO);
561 RECORD(TYPE_TYPEDEF);
562 RECORD(TYPE_TYPEOF_EXPR);
563 RECORD(TYPE_TYPEOF);
564 RECORD(TYPE_RECORD);
565 RECORD(TYPE_ENUM);
566 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000567 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000568 RECORD(DECL_ATTR);
569 RECORD(DECL_TRANSLATION_UNIT);
570 RECORD(DECL_TYPEDEF);
571 RECORD(DECL_ENUM);
572 RECORD(DECL_RECORD);
573 RECORD(DECL_ENUM_CONSTANT);
574 RECORD(DECL_FUNCTION);
575 RECORD(DECL_OBJC_METHOD);
576 RECORD(DECL_OBJC_INTERFACE);
577 RECORD(DECL_OBJC_PROTOCOL);
578 RECORD(DECL_OBJC_IVAR);
579 RECORD(DECL_OBJC_AT_DEFS_FIELD);
580 RECORD(DECL_OBJC_CLASS);
581 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
582 RECORD(DECL_OBJC_CATEGORY);
583 RECORD(DECL_OBJC_CATEGORY_IMPL);
584 RECORD(DECL_OBJC_IMPLEMENTATION);
585 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
586 RECORD(DECL_OBJC_PROPERTY);
587 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000588 RECORD(DECL_FIELD);
589 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000590 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000591 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000592 RECORD(DECL_ORIGINAL_PARM_VAR);
593 RECORD(DECL_FILE_SCOPE_ASM);
594 RECORD(DECL_BLOCK);
595 RECORD(DECL_CONTEXT_LEXICAL);
596 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000597 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000598 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000599#undef RECORD
600#undef BLOCK
601 Stream.ExitBlock();
602}
603
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000604/// \brief Adjusts the given filename to only write out the portion of the
605/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000606///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000607/// \param Filename the file name to adjust.
608///
609/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
610/// the returned filename will be adjusted by this system root.
611///
612/// \returns either the original filename (if it needs no adjustment) or the
613/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000614static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000615adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
616 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000617
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000618 if (!isysroot)
619 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000620
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000621 // Verify that the filename and the system root have the same prefix.
622 unsigned Pos = 0;
623 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
624 if (Filename[Pos] != isysroot[Pos])
625 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000626
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000627 // We hit the end of the filename before we hit the end of the system root.
628 if (!Filename[Pos])
629 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000630
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000631 // If the file name has a '/' at the current position, skip over the '/'.
632 // We distinguish sysroot-based includes from absolute includes by the
633 // absence of '/' at the beginning of sysroot-based includes.
634 if (Filename[Pos] == '/')
635 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000636
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000637 return Filename + Pos;
638}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000639
Douglas Gregor7b71e632009-04-27 22:23:34 +0000640/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000641void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000642 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000643
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000644 // Metadata
645 const TargetInfo &Target = Context.Target;
646 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
647 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
648 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
649 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
650 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
651 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
652 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
653 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
654 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000655
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000656 RecordData Record;
657 Record.push_back(pch::METADATA);
658 Record.push_back(pch::VERSION_MAJOR);
659 Record.push_back(pch::VERSION_MINOR);
660 Record.push_back(CLANG_VERSION_MAJOR);
661 Record.push_back(CLANG_VERSION_MINOR);
662 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000663 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000664 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000665
Douglas Gregor45fe0362009-05-12 01:31:05 +0000666 // Original file name
667 SourceManager &SM = Context.getSourceManager();
668 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
669 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
670 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
671 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
672 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
673
674 llvm::sys::Path MainFilePath(MainFile->getName());
675 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregor45fe0362009-05-12 01:31:05 +0000677 if (!MainFilePath.isAbsolute()) {
678 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000679 P.appendComponent(MainFilePath.str());
680 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000681 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000682 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000683 }
684
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000685 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000686 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000687 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000688 RecordData Record;
689 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000690 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000691 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000692
693 // Subversion branch/version information.
694 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
695 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
696 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
697 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
698 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
699 Record.clear();
700 Record.push_back(pch::SVN_BRANCH_REVISION);
701 Record.push_back(getClangSubversionRevision());
702 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000703}
704
705/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000706void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
707 RecordData Record;
708 Record.push_back(LangOpts.Trigraphs);
709 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
710 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
711 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
712 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
713 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
714 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
715 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
716 Record.push_back(LangOpts.C99); // C99 Support
717 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
718 Record.push_back(LangOpts.CPlusPlus); // C++ Support
719 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000720 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000721
Douglas Gregor55abb232009-04-10 20:39:37 +0000722 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
723 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
724 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000725
Douglas Gregor55abb232009-04-10 20:39:37 +0000726 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000727 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
728 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000729 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000730 Record.push_back(LangOpts.Exceptions); // Support exception handling.
731
732 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
733 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
734 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
735
Chris Lattner258172e2009-04-27 07:35:58 +0000736 // Whether static initializers are protected by locks.
737 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000738 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000739 Record.push_back(LangOpts.Blocks); // block extension to C
740 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
741 // they are unused.
742 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
743 // (modulo the platform support).
744
745 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
746 // signed integer arithmetic overflows.
747
748 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
749 // may be ripped out at any time.
750
751 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000752 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000753 // defined.
754 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
755 // opposed to __DYNAMIC__).
756 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
757
758 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
759 // used (instead of C99 semantics).
760 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000761 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
762 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000763 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
764 // unsigned type
Douglas Gregor55abb232009-04-10 20:39:37 +0000765 Record.push_back(LangOpts.getGCMode());
766 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000767 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000768 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000769 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000770 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000771 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000772}
773
Douglas Gregora7f71a92009-04-10 03:52:48 +0000774//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000775// stat cache Serialization
776//===----------------------------------------------------------------------===//
777
778namespace {
779// Trait used for the on-disk hash table of stat cache results.
780class VISIBILITY_HIDDEN PCHStatCacheTrait {
781public:
782 typedef const char * key_type;
783 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000784
Douglas Gregorc5046832009-04-27 18:38:38 +0000785 typedef std::pair<int, struct stat> data_type;
786 typedef const data_type& data_type_ref;
787
788 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000789 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000790 }
Mike Stump11289f42009-09-09 15:08:12 +0000791
792 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000793 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
794 data_type_ref Data) {
795 unsigned StrLen = strlen(path);
796 clang::io::Emit16(Out, StrLen);
797 unsigned DataLen = 1; // result value
798 if (Data.first == 0)
799 DataLen += 4 + 4 + 2 + 8 + 8;
800 clang::io::Emit8(Out, DataLen);
801 return std::make_pair(StrLen + 1, DataLen);
802 }
Mike Stump11289f42009-09-09 15:08:12 +0000803
Douglas Gregorc5046832009-04-27 18:38:38 +0000804 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
805 Out.write(path, KeyLen);
806 }
Mike Stump11289f42009-09-09 15:08:12 +0000807
Douglas Gregorc5046832009-04-27 18:38:38 +0000808 void EmitData(llvm::raw_ostream& Out, key_type_ref,
809 data_type_ref Data, unsigned DataLen) {
810 using namespace clang::io;
811 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000812
Douglas Gregorc5046832009-04-27 18:38:38 +0000813 // Result of stat()
814 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000815
Douglas Gregorc5046832009-04-27 18:38:38 +0000816 if (Data.first == 0) {
817 Emit32(Out, (uint32_t) Data.second.st_ino);
818 Emit32(Out, (uint32_t) Data.second.st_dev);
819 Emit16(Out, (uint16_t) Data.second.st_mode);
820 Emit64(Out, (uint64_t) Data.second.st_mtime);
821 Emit64(Out, (uint64_t) Data.second.st_size);
822 }
823
824 assert(Out.tell() - Start == DataLen && "Wrong data length");
825 }
826};
827} // end anonymous namespace
828
829/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000830void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
831 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000832 // Build the on-disk hash table containing information about every
833 // stat() call.
834 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
835 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000836 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000837 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000838 Stat != StatEnd; ++Stat, ++NumStatEntries) {
839 const char *Filename = Stat->first();
840 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
841 Generator.insert(Filename, Stat->second);
842 }
Mike Stump11289f42009-09-09 15:08:12 +0000843
Douglas Gregorc5046832009-04-27 18:38:38 +0000844 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000845 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000846 uint32_t BucketOffset;
847 {
848 llvm::raw_svector_ostream Out(StatCacheData);
849 // Make sure that no bucket is at offset 0
850 clang::io::Emit32(Out, 0);
851 BucketOffset = Generator.Emit(Out);
852 }
853
854 // Create a blob abbreviation
855 using namespace llvm;
856 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
857 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
858 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
859 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
860 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
861 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
862
863 // Write the stat cache
864 RecordData Record;
865 Record.push_back(pch::STAT_CACHE);
866 Record.push_back(BucketOffset);
867 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000868 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000869}
870
871//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000872// Source Manager Serialization
873//===----------------------------------------------------------------------===//
874
875/// \brief Create an abbreviation for the SLocEntry that refers to a
876/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000877static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000878 using namespace llvm;
879 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
880 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
881 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000886 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000887}
888
889/// \brief Create an abbreviation for the SLocEntry that refers to a
890/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000891static unsigned CreateSLocBufferAbbrev(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_BUFFER_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
899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
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's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000905static unsigned CreateSLocBufferBlobAbbrev(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_BLOB));
909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000910 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000911}
912
913/// \brief Create an abbreviation for the SLocEntry that refers to an
914/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000915static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000916 using namespace llvm;
917 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
918 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000924 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000925}
926
927/// \brief Writes the block containing the serialized form of the
928/// source manager.
929///
930/// TODO: We should probably use an on-disk hash table (stored in a
931/// blob), indexed based on the file name, so that we only create
932/// entries for files that we actually need. In the common case (no
933/// errors), we probably won't have to create file entries for any of
934/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000935void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000936 const Preprocessor &PP,
937 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000938 RecordData Record;
939
Chris Lattner0910e3b2009-04-10 17:16:57 +0000940 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000941 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000942
943 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000944 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
945 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
946 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
947 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000948
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000949 // Write the line table.
950 if (SourceMgr.hasLineTable()) {
951 LineTableInfo &LineTable = SourceMgr.getLineTable();
952
953 // Emit the file names
954 Record.push_back(LineTable.getNumFilenames());
955 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
956 // Emit the file name
957 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000958 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000959 unsigned FilenameLen = Filename? strlen(Filename) : 0;
960 Record.push_back(FilenameLen);
961 if (FilenameLen)
962 Record.insert(Record.end(), Filename, Filename + FilenameLen);
963 }
Mike Stump11289f42009-09-09 15:08:12 +0000964
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000965 // Emit the line entries
966 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
967 L != LEnd; ++L) {
968 // Emit the file ID
969 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000970
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000971 // Emit the line entries
972 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000973 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000974 LEEnd = L->second.end();
975 LE != LEEnd; ++LE) {
976 Record.push_back(LE->FileOffset);
977 Record.push_back(LE->LineNo);
978 Record.push_back(LE->FilenameID);
979 Record.push_back((unsigned)LE->FileKind);
980 Record.push_back(LE->IncludeOffset);
981 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000982 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +0000983 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000984 }
985
Douglas Gregor258ae542009-04-27 06:38:32 +0000986 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +0000987 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +0000988 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000989 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +0000990 E = HS.header_file_end();
991 I != E; ++I) {
992 Record.push_back(I->isImport);
993 Record.push_back(I->DirInfo);
994 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +0000995 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +0000996 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
997 Record.clear();
998 }
999
Douglas Gregor258ae542009-04-27 06:38:32 +00001000 // Write out the source location entry table. We skip the first
1001 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001002 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001003 RecordData PreloadSLocs;
1004 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001005 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1006 // Get this source location entry.
1007 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1008
Douglas Gregor258ae542009-04-27 06:38:32 +00001009 // Record the offset of this source-location entry.
1010 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1011
1012 // Figure out which record code to use.
1013 unsigned Code;
1014 if (SLoc->isFile()) {
1015 if (SLoc->getFile().getContentCache()->Entry)
1016 Code = pch::SM_SLOC_FILE_ENTRY;
1017 else
1018 Code = pch::SM_SLOC_BUFFER_ENTRY;
1019 } else
1020 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1021 Record.clear();
1022 Record.push_back(Code);
1023
1024 Record.push_back(SLoc->getOffset());
1025 if (SLoc->isFile()) {
1026 const SrcMgr::FileInfo &File = SLoc->getFile();
1027 Record.push_back(File.getIncludeLoc().getRawEncoding());
1028 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1029 Record.push_back(File.hasLineDirectives());
1030
1031 const SrcMgr::ContentCache *Content = File.getContentCache();
1032 if (Content->Entry) {
1033 // The source location entry is a file. The blob associated
1034 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001035
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001036 // Turn the file name into an absolute path, if it isn't already.
1037 const char *Filename = Content->Entry->getName();
1038 llvm::sys::Path FilePath(Filename, strlen(Filename));
1039 std::string FilenameStr;
1040 if (!FilePath.isAbsolute()) {
1041 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001042 P.appendComponent(FilePath.str());
1043 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001044 Filename = FilenameStr.c_str();
1045 }
Mike Stump11289f42009-09-09 15:08:12 +00001046
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001047 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001048 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001049
1050 // FIXME: For now, preload all file source locations, so that
1051 // we get the appropriate File entries in the reader. This is
1052 // a temporary measure.
1053 PreloadSLocs.push_back(SLocEntryOffsets.size());
1054 } else {
1055 // The source location entry is a buffer. The blob associated
1056 // with this entry contains the contents of the buffer.
1057
1058 // We add one to the size so that we capture the trailing NULL
1059 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1060 // the reader side).
1061 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1062 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001063 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1064 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001065 Record.clear();
1066 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1067 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001068 llvm::StringRef(Buffer->getBufferStart(),
1069 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001070
1071 if (strcmp(Name, "<built-in>") == 0)
1072 PreloadSLocs.push_back(SLocEntryOffsets.size());
1073 }
1074 } else {
1075 // The source location entry is an instantiation.
1076 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1077 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1078 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1079 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1080
1081 // Compute the token length for this macro expansion.
1082 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001083 if (I + 1 != N)
1084 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001085 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1086 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1087 }
1088 }
1089
Douglas Gregor8f45df52009-04-16 22:23:12 +00001090 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001091
1092 if (SLocEntryOffsets.empty())
1093 return;
1094
1095 // Write the source-location offsets table into the PCH block. This
1096 // table is used for lazily loading source-location information.
1097 using namespace llvm;
1098 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1099 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1101 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1102 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1103 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001104
Douglas Gregor258ae542009-04-27 06:38:32 +00001105 Record.clear();
1106 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1107 Record.push_back(SLocEntryOffsets.size());
1108 Record.push_back(SourceMgr.getNextOffset());
1109 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001110 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001111 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001112
1113 // Write the source location entry preloads array, telling the PCH
1114 // reader which source locations entries it should load eagerly.
1115 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001116}
1117
Douglas Gregorc5046832009-04-27 18:38:38 +00001118//===----------------------------------------------------------------------===//
1119// Preprocessor Serialization
1120//===----------------------------------------------------------------------===//
1121
Chris Lattnereeffaef2009-04-10 17:15:23 +00001122/// \brief Writes the block containing the serialized form of the
1123/// preprocessor.
1124///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001125void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001126 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001127
Chris Lattner0af3ba12009-04-13 01:29:17 +00001128 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1129 if (PP.getCounterValue() != 0) {
1130 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001131 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001132 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001133 }
1134
1135 // Enter the preprocessor block.
1136 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001137
Douglas Gregoreda6a892009-04-26 00:07:37 +00001138 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1139 // FIXME: use diagnostics subsystem for localization etc.
1140 if (PP.SawDateOrTime())
1141 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001142
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001143 // Loop over all the macro definitions that are live at the end of the file,
1144 // emitting each to the PP section.
Douglas Gregor45053152009-10-17 17:25:45 +00001145 // FIXME: Make sure that this sees macros defined in included PCH files.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001146 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1147 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001148 // FIXME: This emits macros in hash table order, we should do it in a stable
1149 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001150 MacroInfo *MI = I->second;
1151
1152 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1153 // been redefined by the header (in which case they are not isBuiltinMacro).
1154 if (MI->isBuiltinMacro())
1155 continue;
1156
Douglas Gregorc3366a52009-04-21 23:56:24 +00001157 // FIXME: Remove this identifier reference?
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001158 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001159 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001160 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1161 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001162
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001163 unsigned Code;
1164 if (MI->isObjectLike()) {
1165 Code = pch::PP_MACRO_OBJECT_LIKE;
1166 } else {
1167 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001168
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001169 Record.push_back(MI->isC99Varargs());
1170 Record.push_back(MI->isGNUVarargs());
1171 Record.push_back(MI->getNumArgs());
1172 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1173 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001174 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001175 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001176 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001177 Record.clear();
1178
Chris Lattner2199f5b2009-04-10 18:08:30 +00001179 // Emit the tokens array.
1180 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1181 // Note that we know that the preprocessor does not have any annotation
1182 // tokens in it because they are created by the parser, and thus can't be
1183 // in a macro definition.
1184 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001185
Chris Lattner2199f5b2009-04-10 18:08:30 +00001186 Record.push_back(Tok.getLocation().getRawEncoding());
1187 Record.push_back(Tok.getLength());
1188
Chris Lattner2199f5b2009-04-10 18:08:30 +00001189 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1190 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001191 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001192
Chris Lattner2199f5b2009-04-10 18:08:30 +00001193 // FIXME: Should translate token kind to a stable encoding.
1194 Record.push_back(Tok.getKind());
1195 // FIXME: Should translate token flags to a stable encoding.
1196 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001197
Douglas Gregor8f45df52009-04-16 22:23:12 +00001198 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001199 Record.clear();
1200 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001201 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001202 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001203 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001204}
1205
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001206void PCHWriter::WriteComments(ASTContext &Context) {
1207 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001208
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001209 if (Context.Comments.empty())
1210 return;
Mike Stump11289f42009-09-09 15:08:12 +00001211
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001212 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1213 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1214 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1215 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001216
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001217 RecordData Record;
1218 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001219 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001220 (const char*)&Context.Comments[0],
1221 Context.Comments.size() * sizeof(SourceRange));
1222}
1223
Douglas Gregorc5046832009-04-27 18:38:38 +00001224//===----------------------------------------------------------------------===//
1225// Type Serialization
1226//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001227
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001228/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001229void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001230 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001231 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001232 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001233
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001234 // Record the offset for this type.
1235 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001236 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001237 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1238 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001239 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001240 }
1241
1242 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001243
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001244 // Emit the type's representation.
1245 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001246
1247 if (T.hasNonFastQualifiers()) {
1248 Qualifiers Qs = T.getQualifiers();
1249 AddTypeRef(T.getUnqualifiedType(), Record);
1250 Record.push_back(Qs.getAsOpaqueValue());
1251 W.Code = pch::TYPE_EXT_QUAL;
1252 } else {
1253 switch (T->getTypeClass()) {
1254 // For all of the concrete, non-dependent types, call the
1255 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001256#define TYPE(Class, Base) \
John McCall8ccfcb52009-09-24 19:53:00 +00001257 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001258#define ABSTRACT_TYPE(Class, Base)
1259#define DEPENDENT_TYPE(Class, Base)
1260#include "clang/AST/TypeNodes.def"
1261
John McCall8ccfcb52009-09-24 19:53:00 +00001262 // For all of the dependent type nodes (which only occur in C++
1263 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001264#define TYPE(Class, Base)
1265#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1266#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001267 assert(false && "Cannot serialize dependent type nodes");
1268 break;
1269 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001270 }
1271
1272 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001273 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001274
1275 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001276 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001277}
1278
Douglas Gregorc5046832009-04-27 18:38:38 +00001279//===----------------------------------------------------------------------===//
1280// Declaration Serialization
1281//===----------------------------------------------------------------------===//
1282
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001283/// \brief Write the block containing all of the declaration IDs
1284/// lexically declared within the given DeclContext.
1285///
1286/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1287/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001288uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001289 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001290 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001291 return 0;
1292
Douglas Gregor8f45df52009-04-16 22:23:12 +00001293 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001294 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001295 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1296 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001297 AddDeclRef(*D, Record);
1298
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001299 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001300 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001301 return Offset;
1302}
1303
1304/// \brief Write the block containing all of the declaration IDs
1305/// visible from the given DeclContext.
1306///
1307/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1308/// bistream, or 0 if no block was written.
1309uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1310 DeclContext *DC) {
1311 if (DC->getPrimaryContext() != DC)
1312 return 0;
1313
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001314 // Since there is no name lookup into functions or methods, and we
1315 // perform name lookup for the translation unit via the
1316 // IdentifierInfo chains, don't bother to build a
1317 // visible-declarations table for these entities.
1318 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001319 return 0;
1320
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001321 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001322 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001323
1324 // Serialize the contents of the mapping used for lookup. Note that,
1325 // although we have two very different code paths, the serialized
1326 // representation is the same for both cases: a declaration name,
1327 // followed by a size, followed by references to the visible
1328 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001329 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001330 RecordData Record;
1331 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001332 if (!Map)
1333 return 0;
1334
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001335 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1336 D != DEnd; ++D) {
1337 AddDeclarationName(D->first, Record);
1338 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1339 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001340 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001341 AddDeclRef(*Result.first, Record);
1342 }
1343
1344 if (Record.size() == 0)
1345 return 0;
1346
Douglas Gregor8f45df52009-04-16 22:23:12 +00001347 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001348 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001349 return Offset;
1350}
1351
Douglas Gregorc5046832009-04-27 18:38:38 +00001352//===----------------------------------------------------------------------===//
1353// Global Method Pool and Selector Serialization
1354//===----------------------------------------------------------------------===//
1355
Douglas Gregore84a9da2009-04-20 20:36:09 +00001356namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001357// Trait used for the on-disk hash table used in the method pool.
1358class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1359 PCHWriter &Writer;
1360
1361public:
1362 typedef Selector key_type;
1363 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001364
Douglas Gregorc78d3462009-04-24 21:10:55 +00001365 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1366 typedef const data_type& data_type_ref;
1367
1368 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001369
Douglas Gregorc78d3462009-04-24 21:10:55 +00001370 static unsigned ComputeHash(Selector Sel) {
1371 unsigned N = Sel.getNumArgs();
1372 if (N == 0)
1373 ++N;
1374 unsigned R = 5381;
1375 for (unsigned I = 0; I != N; ++I)
1376 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001377 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001378 return R;
1379 }
Mike Stump11289f42009-09-09 15:08:12 +00001380
1381 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001382 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1383 data_type_ref Methods) {
1384 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1385 clang::io::Emit16(Out, KeyLen);
1386 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001387 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001388 Method = Method->Next)
1389 if (Method->Method)
1390 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001391 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001392 Method = Method->Next)
1393 if (Method->Method)
1394 DataLen += 4;
1395 clang::io::Emit16(Out, DataLen);
1396 return std::make_pair(KeyLen, DataLen);
1397 }
Mike Stump11289f42009-09-09 15:08:12 +00001398
Douglas Gregor95c13f52009-04-25 17:48:32 +00001399 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001400 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001401 assert((Start >> 32) == 0 && "Selector key offset too large");
1402 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001403 unsigned N = Sel.getNumArgs();
1404 clang::io::Emit16(Out, N);
1405 if (N == 0)
1406 N = 1;
1407 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001408 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001409 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1410 }
Mike Stump11289f42009-09-09 15:08:12 +00001411
Douglas Gregorc78d3462009-04-24 21:10:55 +00001412 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001413 data_type_ref Methods, unsigned DataLen) {
1414 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001415 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001416 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001417 Method = Method->Next)
1418 if (Method->Method)
1419 ++NumInstanceMethods;
1420
1421 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001422 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001423 Method = Method->Next)
1424 if (Method->Method)
1425 ++NumFactoryMethods;
1426
1427 clang::io::Emit16(Out, NumInstanceMethods);
1428 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001429 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001430 Method = Method->Next)
1431 if (Method->Method)
1432 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001433 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001434 Method = Method->Next)
1435 if (Method->Method)
1436 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001437
1438 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001439 }
1440};
1441} // end anonymous namespace
1442
1443/// \brief Write the method pool into the PCH file.
1444///
1445/// The method pool contains both instance and factory methods, stored
1446/// in an on-disk hash table indexed by the selector.
1447void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1448 using namespace llvm;
1449
1450 // Create and write out the blob that contains the instance and
1451 // factor method pools.
1452 bool Empty = true;
1453 {
1454 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001455
Douglas Gregorc78d3462009-04-24 21:10:55 +00001456 // Create the on-disk hash table representation. Start by
1457 // iterating through the instance method pool.
1458 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001459 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001460 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001461 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001462 InstanceEnd = SemaRef.InstanceMethodPool.end();
1463 Instance != InstanceEnd; ++Instance) {
1464 // Check whether there is a factory method with the same
1465 // selector.
1466 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1467 = SemaRef.FactoryMethodPool.find(Instance->first);
1468
1469 if (Factory == SemaRef.FactoryMethodPool.end())
1470 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001471 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001472 ObjCMethodList()));
1473 else
1474 Generator.insert(Instance->first,
1475 std::make_pair(Instance->second, Factory->second));
1476
Douglas Gregor95c13f52009-04-25 17:48:32 +00001477 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001478 Empty = false;
1479 }
1480
1481 // Now iterate through the factory method pool, to pick up any
1482 // selectors that weren't already in the instance method pool.
1483 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001484 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001485 FactoryEnd = SemaRef.FactoryMethodPool.end();
1486 Factory != FactoryEnd; ++Factory) {
1487 // Check whether there is an instance method with the same
1488 // selector. If so, there is no work to do here.
1489 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1490 = SemaRef.InstanceMethodPool.find(Factory->first);
1491
Douglas Gregor95c13f52009-04-25 17:48:32 +00001492 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001493 Generator.insert(Factory->first,
1494 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001495 ++NumSelectorsInMethodPool;
1496 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001497
1498 Empty = false;
1499 }
1500
Douglas Gregor95c13f52009-04-25 17:48:32 +00001501 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001502 return;
1503
1504 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001505 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001506 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001507 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001508 {
1509 PCHMethodPoolTrait Trait(*this);
1510 llvm::raw_svector_ostream Out(MethodPool);
1511 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001512 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001513 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001514
1515 // For every selector that we have seen but which was not
1516 // written into the hash table, write the selector itself and
1517 // record it's offset.
1518 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1519 if (SelectorOffsets[I] == 0)
1520 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001521 }
1522
1523 // Create a blob abbreviation
1524 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1525 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1526 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1529 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1530
Douglas Gregor95c13f52009-04-25 17:48:32 +00001531 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001532 RecordData Record;
1533 Record.push_back(pch::METHOD_POOL);
1534 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001535 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001536 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001537
1538 // Create a blob abbreviation for the selector table offsets.
1539 Abbrev = new BitCodeAbbrev();
1540 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1543 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1544
1545 // Write the selector offsets table.
1546 Record.clear();
1547 Record.push_back(pch::SELECTOR_OFFSETS);
1548 Record.push_back(SelectorOffsets.size());
1549 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1550 (const char *)&SelectorOffsets.front(),
1551 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001552 }
1553}
1554
Douglas Gregorc5046832009-04-27 18:38:38 +00001555//===----------------------------------------------------------------------===//
1556// Identifier Table Serialization
1557//===----------------------------------------------------------------------===//
1558
Douglas Gregorc78d3462009-04-24 21:10:55 +00001559namespace {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001560class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1561 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001562 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001563
Douglas Gregor1d583f22009-04-28 21:18:29 +00001564 /// \brief Determines whether this is an "interesting" identifier
1565 /// that needs a full IdentifierInfo structure written into the hash
1566 /// table.
1567 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1568 return II->isPoisoned() ||
1569 II->isExtensionToken() ||
1570 II->hasMacroDefinition() ||
1571 II->getObjCOrBuiltinID() ||
1572 II->getFETokenInfo<void>();
1573 }
1574
Douglas Gregore84a9da2009-04-20 20:36:09 +00001575public:
1576 typedef const IdentifierInfo* key_type;
1577 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001578
Douglas Gregore84a9da2009-04-20 20:36:09 +00001579 typedef pch::IdentID data_type;
1580 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001581
1582 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001583 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001584
1585 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001586 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001587 }
Mike Stump11289f42009-09-09 15:08:12 +00001588
1589 std::pair<unsigned,unsigned>
1590 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001591 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001592 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001593 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1594 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001595 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001596 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001597 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001598 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001599 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1600 DEnd = IdentifierResolver::end();
1601 D != DEnd; ++D)
1602 DataLen += sizeof(pch::DeclID);
1603 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001604 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001605 // We emit the key length after the data length so that every
1606 // string is preceded by a 16-bit length. This matches the PTH
1607 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001608 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001609 return std::make_pair(KeyLen, DataLen);
1610 }
Mike Stump11289f42009-09-09 15:08:12 +00001611
1612 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001613 unsigned KeyLen) {
1614 // Record the location of the key data. This is used when generating
1615 // the mapping from persistent IDs to strings.
1616 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001617 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001618 }
Mike Stump11289f42009-09-09 15:08:12 +00001619
1620 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001621 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001622 if (!isInterestingIdentifier(II)) {
1623 clang::io::Emit32(Out, ID << 1);
1624 return;
1625 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001626
Douglas Gregor1d583f22009-04-28 21:18:29 +00001627 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001628 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001629 bool hasMacroDefinition =
1630 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001631 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001632 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001633 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001634 Bits = (Bits << 1) | II->isExtensionToken();
1635 Bits = (Bits << 1) | II->isPoisoned();
1636 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregorb9256522009-04-28 21:32:13 +00001637 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001638
Douglas Gregorc3366a52009-04-21 23:56:24 +00001639 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001640 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001641
Douglas Gregora868bbd2009-04-21 22:25:48 +00001642 // Emit the declaration IDs in reverse order, because the
1643 // IdentifierResolver provides the declarations as they would be
1644 // visible (e.g., the function "stat" would come before the struct
1645 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1646 // adds declarations to the end of the list (so we need to see the
1647 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001648 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001649 IdentifierResolver::end());
1650 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1651 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001652 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001653 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001654 }
1655};
1656} // end anonymous namespace
1657
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001658/// \brief Write the identifier table into the PCH file.
1659///
1660/// The identifier table consists of a blob containing string data
1661/// (the actual identifiers themselves) and a separate "offsets" index
1662/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001663void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001664 using namespace llvm;
1665
1666 // Create and write out the blob that contains the identifier
1667 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001668 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001669 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001670
Douglas Gregore6648fb2009-04-28 20:33:11 +00001671 // Look for any identifiers that were named while processing the
1672 // headers, but are otherwise not needed. We add these to the hash
1673 // table to enable checking of the predefines buffer in the case
1674 // where the user adds new macro definitions when building the PCH
1675 // file.
1676 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1677 IDEnd = PP.getIdentifierTable().end();
1678 ID != IDEnd; ++ID)
1679 getIdentifierRef(ID->second);
1680
Douglas Gregore84a9da2009-04-20 20:36:09 +00001681 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001682 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001683 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1684 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1685 ID != IDEnd; ++ID) {
1686 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001687 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001688 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001689
Douglas Gregore84a9da2009-04-20 20:36:09 +00001690 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001691 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001692 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001693 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001694 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001695 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001696 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001697 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001698 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001699 }
1700
1701 // Create a blob abbreviation
1702 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1703 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001704 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001705 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001706 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001707
1708 // Write the identifier table
1709 RecordData Record;
1710 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001711 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001712 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001713 }
1714
1715 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001716 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1717 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1720 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1721
1722 RecordData Record;
1723 Record.push_back(pch::IDENTIFIER_OFFSET);
1724 Record.push_back(IdentifierOffsets.size());
1725 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1726 (const char *)&IdentifierOffsets.front(),
1727 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001728}
1729
Douglas Gregorc5046832009-04-27 18:38:38 +00001730//===----------------------------------------------------------------------===//
1731// General Serialization Routines
1732//===----------------------------------------------------------------------===//
1733
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001734/// \brief Write a record containing the given attributes.
1735void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1736 RecordData Record;
1737 for (; Attr; Attr = Attr->getNext()) {
1738 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1739 Record.push_back(Attr->isInherited());
1740 switch (Attr->getKind()) {
1741 case Attr::Alias:
1742 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1743 break;
1744
1745 case Attr::Aligned:
1746 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1747 break;
1748
1749 case Attr::AlwaysInline:
1750 break;
Mike Stump11289f42009-09-09 15:08:12 +00001751
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001752 case Attr::AnalyzerNoReturn:
1753 break;
1754
1755 case Attr::Annotate:
1756 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1757 break;
1758
1759 case Attr::AsmLabel:
1760 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1761 break;
1762
1763 case Attr::Blocks:
1764 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1765 break;
1766
1767 case Attr::Cleanup:
1768 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1769 break;
1770
1771 case Attr::Const:
1772 break;
1773
1774 case Attr::Constructor:
1775 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1776 break;
1777
1778 case Attr::DLLExport:
1779 case Attr::DLLImport:
1780 case Attr::Deprecated:
1781 break;
1782
1783 case Attr::Destructor:
1784 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1785 break;
1786
1787 case Attr::FastCall:
1788 break;
1789
1790 case Attr::Format: {
1791 const FormatAttr *Format = cast<FormatAttr>(Attr);
1792 AddString(Format->getType(), Record);
1793 Record.push_back(Format->getFormatIdx());
1794 Record.push_back(Format->getFirstArg());
1795 break;
1796 }
1797
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001798 case Attr::FormatArg: {
1799 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1800 Record.push_back(Format->getFormatIdx());
1801 break;
1802 }
1803
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001804 case Attr::Sentinel : {
1805 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1806 Record.push_back(Sentinel->getSentinel());
1807 Record.push_back(Sentinel->getNullPos());
1808 break;
1809 }
Mike Stump11289f42009-09-09 15:08:12 +00001810
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001811 case Attr::GNUInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001812 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001813 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001814 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001815 case Attr::NoReturn:
1816 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001817 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001818 break;
1819
1820 case Attr::NonNull: {
1821 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1822 Record.push_back(NonNull->size());
1823 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1824 break;
1825 }
1826
1827 case Attr::ObjCException:
1828 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001829 case Attr::CFReturnsRetained:
1830 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001831 case Attr::Overloadable:
1832 break;
1833
Anders Carlsson68e0b682009-08-08 18:23:56 +00001834 case Attr::PragmaPack:
1835 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001836 break;
1837
Anders Carlsson68e0b682009-08-08 18:23:56 +00001838 case Attr::Packed:
1839 break;
Mike Stump11289f42009-09-09 15:08:12 +00001840
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001841 case Attr::Pure:
1842 break;
1843
1844 case Attr::Regparm:
1845 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1846 break;
Mike Stump11289f42009-09-09 15:08:12 +00001847
Nate Begemanf2758702009-06-26 06:32:41 +00001848 case Attr::ReqdWorkGroupSize:
1849 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1850 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1851 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1852 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001853
1854 case Attr::Section:
1855 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1856 break;
1857
1858 case Attr::StdCall:
1859 case Attr::TransparentUnion:
1860 case Attr::Unavailable:
1861 case Attr::Unused:
1862 case Attr::Used:
1863 break;
1864
1865 case Attr::Visibility:
1866 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001867 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001868 break;
1869
1870 case Attr::WarnUnusedResult:
1871 case Attr::Weak:
1872 case Attr::WeakImport:
1873 break;
1874 }
1875 }
1876
Douglas Gregor8f45df52009-04-16 22:23:12 +00001877 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001878}
1879
1880void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1881 Record.push_back(Str.size());
1882 Record.insert(Record.end(), Str.begin(), Str.end());
1883}
1884
Douglas Gregore84a9da2009-04-20 20:36:09 +00001885/// \brief Note that the identifier II occurs at the given offset
1886/// within the identifier table.
1887void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001888 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001889}
1890
Douglas Gregor95c13f52009-04-25 17:48:32 +00001891/// \brief Note that the selector Sel occurs at the given offset
1892/// within the method pool/selector table.
1893void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1894 unsigned ID = SelectorIDs[Sel];
1895 assert(ID && "Unknown selector");
1896 SelectorOffsets[ID - 1] = Offset;
1897}
1898
Mike Stump11289f42009-09-09 15:08:12 +00001899PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1900 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001901 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1902 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001903
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001904void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1905 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001906 using namespace llvm;
1907
Douglas Gregor162dd022009-04-20 15:53:59 +00001908 ASTContext &Context = SemaRef.Context;
1909 Preprocessor &PP = SemaRef.PP;
1910
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001911 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001912 Stream.Emit((unsigned)'C', 8);
1913 Stream.Emit((unsigned)'P', 8);
1914 Stream.Emit((unsigned)'C', 8);
1915 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001916
Chris Lattner28fa4e62009-04-26 22:26:21 +00001917 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001918
1919 // The translation unit is the first declaration we'll emit.
1920 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001921 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001922
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001923 // Make sure that we emit IdentifierInfos (and any attached
1924 // declarations) for builtins.
1925 {
1926 IdentifierTable &Table = PP.getIdentifierTable();
1927 llvm::SmallVector<const char *, 32> BuiltinNames;
1928 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1929 Context.getLangOptions().NoBuiltin);
1930 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1931 getIdentifierRef(&Table.get(BuiltinNames[I]));
1932 }
1933
Chris Lattner0c797362009-09-08 18:19:27 +00001934 // Build a record containing all of the tentative definitions in this file, in
1935 // TentativeDefinitionList order. Generally, this record will be empty for
1936 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001937 RecordData TentativeDefinitions;
Chris Lattner0c797362009-09-08 18:19:27 +00001938 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1939 VarDecl *VD =
1940 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1941 if (VD) AddDeclRef(VD, TentativeDefinitions);
1942 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001943
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001944 // Build a record containing all of the locally-scoped external
1945 // declarations in this header file. Generally, this record will be
1946 // empty.
1947 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001948 // FIXME: This is filling in the PCH file in densemap order which is
1949 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001950 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001951 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1952 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1953 TD != TDEnd; ++TD)
1954 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1955
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001956 // Build a record containing all of the ext_vector declarations.
1957 RecordData ExtVectorDecls;
1958 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1959 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1960
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001961 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001962 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001963 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001964 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001965 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001966 if (StatCalls && !isysroot)
1967 WriteStatCache(*StatCalls, isysroot);
1968 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00001969 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001970 // Write the record of special types.
1971 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001972
Steve Naroffc277ad12009-07-18 15:33:26 +00001973 AddTypeRef(Context.getBuiltinVaListType(), Record);
1974 AddTypeRef(Context.getObjCIdType(), Record);
1975 AddTypeRef(Context.getObjCSelType(), Record);
1976 AddTypeRef(Context.getObjCProtoType(), Record);
1977 AddTypeRef(Context.getObjCClassType(), Record);
1978 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1979 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1980 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00001981 AddTypeRef(Context.getjmp_bufType(), Record);
1982 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001983 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1984 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00001985 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00001986 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00001987 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00001988
Douglas Gregor1970d882009-04-26 03:49:13 +00001989 // Keep writing types and declarations until all types and
1990 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00001991 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
1992 WriteDeclsBlockAbbrevs();
1993 while (!DeclTypesToEmit.empty()) {
1994 DeclOrType DOT = DeclTypesToEmit.front();
1995 DeclTypesToEmit.pop();
1996 if (DOT.isType())
1997 WriteType(DOT.getType());
1998 else
1999 WriteDecl(Context, DOT.getDecl());
2000 }
2001 Stream.ExitBlock();
2002
Douglas Gregor45053152009-10-17 17:25:45 +00002003 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002004 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002005 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002006
2007 // Write the type offsets array
2008 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2009 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2012 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2013 Record.clear();
2014 Record.push_back(pch::TYPE_OFFSET);
2015 Record.push_back(TypeOffsets.size());
2016 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002017 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002018 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002019
Douglas Gregor745ed142009-04-25 18:35:21 +00002020 // Write the declaration offsets array
2021 Abbrev = new BitCodeAbbrev();
2022 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2025 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2026 Record.clear();
2027 Record.push_back(pch::DECL_OFFSET);
2028 Record.push_back(DeclOffsets.size());
2029 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002030 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002031 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002032
Douglas Gregord4df8652009-04-22 22:02:47 +00002033 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002034 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002035 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002036
2037 // Write the record containing tentative definitions.
2038 if (!TentativeDefinitions.empty())
2039 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002040
2041 // Write the record containing locally-scoped external definitions.
2042 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002043 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002044 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002045
2046 // Write the record containing ext_vector type names.
2047 if (!ExtVectorDecls.empty())
2048 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002049
Douglas Gregor08f01292009-04-17 22:13:46 +00002050 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002051 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002052 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002053 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002054 Record.push_back(NumLexicalDeclContexts);
2055 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002056 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002057 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002058}
2059
2060void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2061 Record.push_back(Loc.getRawEncoding());
2062}
2063
2064void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2065 Record.push_back(Value.getBitWidth());
2066 unsigned N = Value.getNumWords();
2067 const uint64_t* Words = Value.getRawData();
2068 for (unsigned I = 0; I != N; ++I)
2069 Record.push_back(Words[I]);
2070}
2071
Douglas Gregor1daeb692009-04-13 18:14:40 +00002072void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2073 Record.push_back(Value.isUnsigned());
2074 AddAPInt(Value, Record);
2075}
2076
Douglas Gregore0a3a512009-04-14 21:55:33 +00002077void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2078 AddAPInt(Value.bitcastToAPInt(), Record);
2079}
2080
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002081void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002082 Record.push_back(getIdentifierRef(II));
2083}
2084
2085pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2086 if (II == 0)
2087 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002088
2089 pch::IdentID &ID = IdentifierIDs[II];
2090 if (ID == 0)
2091 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002092 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002093}
2094
Steve Naroff2ddea052009-04-23 10:39:46 +00002095void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2096 if (SelRef.getAsOpaquePtr() == 0) {
2097 Record.push_back(0);
2098 return;
2099 }
2100
2101 pch::SelectorID &SID = SelectorIDs[SelRef];
2102 if (SID == 0) {
2103 SID = SelectorIDs.size();
2104 SelVector.push_back(SelRef);
2105 }
2106 Record.push_back(SID);
2107}
2108
John McCall8f115c62009-10-16 21:56:05 +00002109void PCHWriter::AddDeclaratorInfo(DeclaratorInfo *DInfo, RecordData &Record) {
2110 if (DInfo == 0) {
2111 AddTypeRef(QualType(), Record);
2112 return;
2113 }
2114
John McCall17001972009-10-18 01:05:36 +00002115 AddTypeRef(DInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002116 TypeLocWriter TLW(*this, Record);
2117 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2118 TLW.Visit(TL);
2119}
2120
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002121void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2122 if (T.isNull()) {
2123 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2124 return;
2125 }
2126
John McCall8ccfcb52009-09-24 19:53:00 +00002127 unsigned FastQuals = T.getFastQualifiers();
2128 T.removeFastQualifiers();
2129
2130 if (T.hasNonFastQualifiers()) {
2131 pch::TypeID &ID = TypeIDs[T];
2132 if (ID == 0) {
2133 // We haven't seen these qualifiers applied to this type before.
2134 // Assign it a new ID. This is the only time we enqueue a
2135 // qualified type, and it has no CV qualifiers.
2136 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002137 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002138 }
2139
2140 // Encode the type qualifiers in the type reference.
2141 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2142 return;
2143 }
2144
2145 assert(!T.hasQualifiers());
2146
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002147 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002148 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002149 switch (BT->getKind()) {
2150 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2151 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2152 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2153 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2154 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2155 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2156 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2157 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002158 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002159 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2160 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2161 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2162 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2163 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2164 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2165 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002166 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002167 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2168 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2169 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002170 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002171 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2172 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002173 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2174 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002175 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2176 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002177 case BuiltinType::UndeducedAuto:
2178 assert(0 && "Should not see undeduced auto here");
2179 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002180 }
2181
John McCall8ccfcb52009-09-24 19:53:00 +00002182 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002183 return;
2184 }
2185
John McCall8ccfcb52009-09-24 19:53:00 +00002186 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002187 if (ID == 0) {
2188 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002189 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002190 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002191 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002192 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002193
2194 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002195 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002196}
2197
2198void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2199 if (D == 0) {
2200 Record.push_back(0);
2201 return;
2202 }
2203
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002204 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002205 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002206 // We haven't seen this declaration before. Give it a new ID and
2207 // enqueue it in the list of declarations to emit.
2208 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002209 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002210 }
2211
2212 Record.push_back(ID);
2213}
2214
Douglas Gregore84a9da2009-04-20 20:36:09 +00002215pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2216 if (D == 0)
2217 return 0;
2218
2219 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2220 return DeclIDs[D];
2221}
2222
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002223void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002224 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002225 Record.push_back(Name.getNameKind());
2226 switch (Name.getNameKind()) {
2227 case DeclarationName::Identifier:
2228 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2229 break;
2230
2231 case DeclarationName::ObjCZeroArgSelector:
2232 case DeclarationName::ObjCOneArgSelector:
2233 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002234 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002235 break;
2236
2237 case DeclarationName::CXXConstructorName:
2238 case DeclarationName::CXXDestructorName:
2239 case DeclarationName::CXXConversionFunctionName:
2240 AddTypeRef(Name.getCXXNameType(), Record);
2241 break;
2242
2243 case DeclarationName::CXXOperatorName:
2244 Record.push_back(Name.getCXXOverloadedOperator());
2245 break;
2246
2247 case DeclarationName::CXXUsingDirective:
2248 // No extra data to emit
2249 break;
2250 }
2251}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002252