blob: 15dbe6e18ed7bffea7ef6b644c0de8d2636ec66f [file] [log] [blame]
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000031#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000037#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000038#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42// Type serialization
43//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000044
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000046 class PCHTypeWriter {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047 PCHWriter &Writer;
48 PCHWriter::RecordData &Record;
49
50 public:
51 /// \brief Type code that corresponds to the record generated.
52 pch::TypeCode Code;
53
Mike Stump11289f42009-09-09 15:08:12 +000054 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000055 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
57 void VisitArrayType(const ArrayType *T);
58 void VisitFunctionType(const FunctionType *T);
59 void VisitTagType(const TagType *T);
60
61#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
62#define ABSTRACT_TYPE(Class, Base)
63#define DEPENDENT_TYPE(Class, Base)
64#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
73 Writer.AddTypeRef(T->getElementType(), Record);
74 Code = pch::TYPE_COMPLEX;
75}
76
77void PCHTypeWriter::VisitPointerType(const PointerType *T) {
78 Writer.AddTypeRef(T->getPointeeType(), Record);
79 Code = pch::TYPE_POINTER;
80}
81
82void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000083 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000084 Code = pch::TYPE_BLOCK_POINTER;
85}
86
87void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_LVALUE_REFERENCE;
90}
91
92void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_RVALUE_REFERENCE;
95}
96
97void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100 Code = pch::TYPE_MEMBER_POINTER;
101}
102
103void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
104 Writer.AddTypeRef(T->getElementType(), Record);
105 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000106 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107}
108
109void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
110 VisitArrayType(T);
111 Writer.AddAPInt(T->getSize(), Record);
112 Code = pch::TYPE_CONSTANT_ARRAY;
113}
114
115void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
116 VisitArrayType(T);
117 Code = pch::TYPE_INCOMPLETE_ARRAY;
118}
119
120void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
121 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000122 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
123 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000124 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125 Code = pch::TYPE_VARIABLE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVectorType(const VectorType *T) {
129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getNumElements());
John Thompson22334602010-02-05 00:12:22 +0000131 Record.push_back(T->isAltiVec());
132 Record.push_back(T->isPixel());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133 Code = pch::TYPE_VECTOR;
134}
135
136void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
137 VisitVectorType(T);
138 Code = pch::TYPE_EXT_VECTOR;
139}
140
141void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
142 Writer.AddTypeRef(T->getResultType(), Record);
Douglas Gregordc728752009-12-22 18:11:50 +0000143 Record.push_back(T->getNoReturnAttr());
Douglas Gregor8c940862010-01-18 17:14:39 +0000144 // FIXME: need to stabilize encoding of calling convention...
145 Record.push_back(T->getCallConv());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146}
147
148void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
149 VisitFunctionType(T);
150 Code = pch::TYPE_FUNCTION_NO_PROTO;
151}
152
153void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
154 VisitFunctionType(T);
155 Record.push_back(T->getNumArgs());
156 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
157 Writer.AddTypeRef(T->getArgType(I), Record);
158 Record.push_back(T->isVariadic());
159 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000160 Record.push_back(T->hasExceptionSpec());
161 Record.push_back(T->hasAnyExceptionSpec());
162 Record.push_back(T->getNumExceptions());
163 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
164 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165 Code = pch::TYPE_FUNCTION_PROTO;
166}
167
John McCallb96ec562009-12-04 22:46:56 +0000168#if 0
169// For when we want it....
170void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_UNRESOLVED_USING;
173}
174#endif
175
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
177 Writer.AddDeclRef(T->getDecl(), Record);
178 Code = pch::TYPE_TYPEDEF;
179}
180
181void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000182 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 Code = pch::TYPE_TYPEOF_EXPR;
184}
185
186void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
187 Writer.AddTypeRef(T->getUnderlyingType(), Record);
188 Code = pch::TYPE_TYPEOF;
189}
190
Anders Carlsson81df7b82009-06-24 19:06:50 +0000191void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
192 Writer.AddStmt(T->getUnderlyingExpr());
193 Code = pch::TYPE_DECLTYPE;
194}
195
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196void PCHTypeWriter::VisitTagType(const TagType *T) {
197 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000198 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199 "Cannot serialize in the middle of a type definition");
200}
201
202void PCHTypeWriter::VisitRecordType(const RecordType *T) {
203 VisitTagType(T);
204 Code = pch::TYPE_RECORD;
205}
206
207void PCHTypeWriter::VisitEnumType(const EnumType *T) {
208 VisitTagType(T);
209 Code = pch::TYPE_ENUM;
210}
211
John McCallfcc33b02009-09-05 00:15:47 +0000212void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
213 Writer.AddTypeRef(T->getUnderlyingType(), Record);
214 Record.push_back(T->getTagKind());
215 Code = pch::TYPE_ELABORATED;
216}
217
Mike Stump11289f42009-09-09 15:08:12 +0000218void
John McCallcebee162009-10-18 09:09:24 +0000219PCHTypeWriter::VisitSubstTemplateTypeParmType(
220 const SubstTemplateTypeParmType *T) {
221 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
222 Writer.AddTypeRef(T->getReplacementType(), Record);
223 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
224}
225
226void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227PCHTypeWriter::VisitTemplateSpecializationType(
228 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000229 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230 assert(false && "Cannot serialize template specialization types");
231}
232
233void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000234 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 assert(false && "Cannot serialize qualified name types");
236}
237
238void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
239 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000241 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
242 E = T->qual_end(); I != E; ++I)
243 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000244 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245}
246
Steve Narofffb4330f2009-06-17 22:40:22 +0000247void
248PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000249 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000250 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000251 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000252 E = T->qual_end(); I != E; ++I)
253 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000254 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255}
256
John McCall8f115c62009-10-16 21:56:05 +0000257namespace {
258
259class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
260 PCHWriter &Writer;
261 PCHWriter::RecordData &Record;
262
263public:
264 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
265 : Writer(Writer), Record(Record) { }
266
John McCall17001972009-10-18 01:05:36 +0000267#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000268#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000269 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000270#include "clang/AST/TypeLocNodes.def"
271
John McCall17001972009-10-18 01:05:36 +0000272 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
273 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000274};
275
276}
277
John McCall17001972009-10-18 01:05:36 +0000278void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
279 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000280}
John McCall17001972009-10-18 01:05:36 +0000281void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000282 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
283 if (TL.needsExtraLocalData()) {
284 Record.push_back(TL.getWrittenTypeSpec());
285 Record.push_back(TL.getWrittenSignSpec());
286 Record.push_back(TL.getWrittenWidthSpec());
287 Record.push_back(TL.hasModeAttr());
288 }
John McCall8f115c62009-10-16 21:56:05 +0000289}
John McCall17001972009-10-18 01:05:36 +0000290void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
291 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000292}
John McCall17001972009-10-18 01:05:36 +0000293void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
294 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000295}
John McCall17001972009-10-18 01:05:36 +0000296void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
297 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000298}
John McCall17001972009-10-18 01:05:36 +0000299void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
300 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000301}
John McCall17001972009-10-18 01:05:36 +0000302void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
303 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000304}
John McCall17001972009-10-18 01:05:36 +0000305void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
306 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000307}
John McCall17001972009-10-18 01:05:36 +0000308void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
309 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
310 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
311 Record.push_back(TL.getSizeExpr() ? 1 : 0);
312 if (TL.getSizeExpr())
313 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000314}
John McCall17001972009-10-18 01:05:36 +0000315void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
316 VisitArrayTypeLoc(TL);
317}
318void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
319 VisitArrayTypeLoc(TL);
320}
321void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
322 VisitArrayTypeLoc(TL);
323}
324void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
325 DependentSizedArrayTypeLoc TL) {
326 VisitArrayTypeLoc(TL);
327}
328void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
329 DependentSizedExtVectorTypeLoc TL) {
330 Writer.AddSourceLocation(TL.getNameLoc(), Record);
331}
332void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
333 Writer.AddSourceLocation(TL.getNameLoc(), Record);
334}
335void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
336 Writer.AddSourceLocation(TL.getNameLoc(), Record);
337}
338void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
340 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
341 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
342 Writer.AddDeclRef(TL.getArg(i), Record);
343}
344void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
345 VisitFunctionTypeLoc(TL);
346}
347void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
348 VisitFunctionTypeLoc(TL);
349}
John McCallb96ec562009-12-04 22:46:56 +0000350void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
351 Writer.AddSourceLocation(TL.getNameLoc(), Record);
352}
John McCall17001972009-10-18 01:05:36 +0000353void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
354 Writer.AddSourceLocation(TL.getNameLoc(), Record);
355}
356void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000357 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
358 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
359 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000360}
361void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000362 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
363 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
364 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
365 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000366}
367void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
368 Writer.AddSourceLocation(TL.getNameLoc(), Record);
369}
370void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getNameLoc(), Record);
372}
373void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getNameLoc(), Record);
375}
376void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
John McCallcebee162009-10-18 09:09:24 +0000382void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
383 SubstTemplateTypeParmTypeLoc TL) {
384 Writer.AddSourceLocation(TL.getNameLoc(), Record);
385}
John McCall17001972009-10-18 01:05:36 +0000386void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
387 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000388 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
389 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
390 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
391 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
392 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall17001972009-10-18 01:05:36 +0000393}
394void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
395 Writer.AddSourceLocation(TL.getNameLoc(), Record);
396}
397void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
398 Writer.AddSourceLocation(TL.getNameLoc(), Record);
399}
400void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000402 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
403 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
404 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
405 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000406}
John McCallfc93cf92009-10-22 22:37:11 +0000407void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getStarLoc(), Record);
409 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
410 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
411 Record.push_back(TL.hasBaseTypeAsWritten());
412 Record.push_back(TL.hasProtocolsAsWritten());
413 if (TL.hasProtocolsAsWritten())
414 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
415 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
416}
John McCall8f115c62009-10-16 21:56:05 +0000417
Chris Lattner19cea4e2009-04-22 05:57:30 +0000418//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000419// PCHWriter Implementation
420//===----------------------------------------------------------------------===//
421
Chris Lattner28fa4e62009-04-26 22:26:21 +0000422static void EmitBlockID(unsigned ID, const char *Name,
423 llvm::BitstreamWriter &Stream,
424 PCHWriter::RecordData &Record) {
425 Record.clear();
426 Record.push_back(ID);
427 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
428
429 // Emit the block name if present.
430 if (Name == 0 || Name[0] == 0) return;
431 Record.clear();
432 while (*Name)
433 Record.push_back(*Name++);
434 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
435}
436
437static void EmitRecordID(unsigned ID, const char *Name,
438 llvm::BitstreamWriter &Stream,
439 PCHWriter::RecordData &Record) {
440 Record.clear();
441 Record.push_back(ID);
442 while (*Name)
443 Record.push_back(*Name++);
444 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000445}
446
447static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
448 PCHWriter::RecordData &Record) {
449#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
450 RECORD(STMT_STOP);
451 RECORD(STMT_NULL_PTR);
452 RECORD(STMT_NULL);
453 RECORD(STMT_COMPOUND);
454 RECORD(STMT_CASE);
455 RECORD(STMT_DEFAULT);
456 RECORD(STMT_LABEL);
457 RECORD(STMT_IF);
458 RECORD(STMT_SWITCH);
459 RECORD(STMT_WHILE);
460 RECORD(STMT_DO);
461 RECORD(STMT_FOR);
462 RECORD(STMT_GOTO);
463 RECORD(STMT_INDIRECT_GOTO);
464 RECORD(STMT_CONTINUE);
465 RECORD(STMT_BREAK);
466 RECORD(STMT_RETURN);
467 RECORD(STMT_DECL);
468 RECORD(STMT_ASM);
469 RECORD(EXPR_PREDEFINED);
470 RECORD(EXPR_DECL_REF);
471 RECORD(EXPR_INTEGER_LITERAL);
472 RECORD(EXPR_FLOATING_LITERAL);
473 RECORD(EXPR_IMAGINARY_LITERAL);
474 RECORD(EXPR_STRING_LITERAL);
475 RECORD(EXPR_CHARACTER_LITERAL);
476 RECORD(EXPR_PAREN);
477 RECORD(EXPR_UNARY_OPERATOR);
478 RECORD(EXPR_SIZEOF_ALIGN_OF);
479 RECORD(EXPR_ARRAY_SUBSCRIPT);
480 RECORD(EXPR_CALL);
481 RECORD(EXPR_MEMBER);
482 RECORD(EXPR_BINARY_OPERATOR);
483 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
484 RECORD(EXPR_CONDITIONAL_OPERATOR);
485 RECORD(EXPR_IMPLICIT_CAST);
486 RECORD(EXPR_CSTYLE_CAST);
487 RECORD(EXPR_COMPOUND_LITERAL);
488 RECORD(EXPR_EXT_VECTOR_ELEMENT);
489 RECORD(EXPR_INIT_LIST);
490 RECORD(EXPR_DESIGNATED_INIT);
491 RECORD(EXPR_IMPLICIT_VALUE_INIT);
492 RECORD(EXPR_VA_ARG);
493 RECORD(EXPR_ADDR_LABEL);
494 RECORD(EXPR_STMT);
495 RECORD(EXPR_TYPES_COMPATIBLE);
496 RECORD(EXPR_CHOOSE);
497 RECORD(EXPR_GNU_NULL);
498 RECORD(EXPR_SHUFFLE_VECTOR);
499 RECORD(EXPR_BLOCK);
500 RECORD(EXPR_BLOCK_DECL_REF);
501 RECORD(EXPR_OBJC_STRING_LITERAL);
502 RECORD(EXPR_OBJC_ENCODE);
503 RECORD(EXPR_OBJC_SELECTOR_EXPR);
504 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
505 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
506 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
507 RECORD(EXPR_OBJC_KVC_REF_EXPR);
508 RECORD(EXPR_OBJC_MESSAGE_EXPR);
509 RECORD(EXPR_OBJC_SUPER_EXPR);
510 RECORD(STMT_OBJC_FOR_COLLECTION);
511 RECORD(STMT_OBJC_CATCH);
512 RECORD(STMT_OBJC_FINALLY);
513 RECORD(STMT_OBJC_AT_TRY);
514 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
515 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000516 RECORD(EXPR_CXX_OPERATOR_CALL);
517 RECORD(EXPR_CXX_CONSTRUCT);
518 RECORD(EXPR_CXX_STATIC_CAST);
519 RECORD(EXPR_CXX_DYNAMIC_CAST);
520 RECORD(EXPR_CXX_REINTERPRET_CAST);
521 RECORD(EXPR_CXX_CONST_CAST);
522 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
523 RECORD(EXPR_CXX_BOOL_LITERAL);
524 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000525#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000526}
Mike Stump11289f42009-09-09 15:08:12 +0000527
Chris Lattner28fa4e62009-04-26 22:26:21 +0000528void PCHWriter::WriteBlockInfoBlock() {
529 RecordData Record;
530 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000531
Chris Lattner64031982009-04-27 00:40:25 +0000532#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000533#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000534
Chris Lattner28fa4e62009-04-26 22:26:21 +0000535 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000536 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000537 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000538 RECORD(TYPE_OFFSET);
539 RECORD(DECL_OFFSET);
540 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000541 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000542 RECORD(IDENTIFIER_OFFSET);
543 RECORD(IDENTIFIER_TABLE);
544 RECORD(EXTERNAL_DEFINITIONS);
545 RECORD(SPECIAL_TYPES);
546 RECORD(STATISTICS);
547 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattner90073802010-02-12 00:07:30 +0000548 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000549 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
550 RECORD(SELECTOR_OFFSETS);
551 RECORD(METHOD_POOL);
552 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000553 RECORD(SOURCE_LOCATION_OFFSETS);
554 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000555 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000556 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000557 RECORD(COMMENT_RANGES);
Ted Kremenek17437132010-01-22 20:59:36 +0000558 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000559
Chris Lattner28fa4e62009-04-26 22:26:21 +0000560 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000561 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000562 RECORD(SM_SLOC_FILE_ENTRY);
563 RECORD(SM_SLOC_BUFFER_ENTRY);
564 RECORD(SM_SLOC_BUFFER_BLOB);
565 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
566 RECORD(SM_LINE_TABLE);
567 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattner28fa4e62009-04-26 22:26:21 +0000569 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000570 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000571 RECORD(PP_MACRO_OBJECT_LIKE);
572 RECORD(PP_MACRO_FUNCTION_LIKE);
573 RECORD(PP_TOKEN);
574
Douglas Gregor12bfa382009-10-17 00:13:19 +0000575 // Decls and Types block.
576 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000577 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000578 RECORD(TYPE_COMPLEX);
579 RECORD(TYPE_POINTER);
580 RECORD(TYPE_BLOCK_POINTER);
581 RECORD(TYPE_LVALUE_REFERENCE);
582 RECORD(TYPE_RVALUE_REFERENCE);
583 RECORD(TYPE_MEMBER_POINTER);
584 RECORD(TYPE_CONSTANT_ARRAY);
585 RECORD(TYPE_INCOMPLETE_ARRAY);
586 RECORD(TYPE_VARIABLE_ARRAY);
587 RECORD(TYPE_VECTOR);
588 RECORD(TYPE_EXT_VECTOR);
589 RECORD(TYPE_FUNCTION_PROTO);
590 RECORD(TYPE_FUNCTION_NO_PROTO);
591 RECORD(TYPE_TYPEDEF);
592 RECORD(TYPE_TYPEOF_EXPR);
593 RECORD(TYPE_TYPEOF);
594 RECORD(TYPE_RECORD);
595 RECORD(TYPE_ENUM);
596 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000597 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000598 RECORD(DECL_ATTR);
599 RECORD(DECL_TRANSLATION_UNIT);
600 RECORD(DECL_TYPEDEF);
601 RECORD(DECL_ENUM);
602 RECORD(DECL_RECORD);
603 RECORD(DECL_ENUM_CONSTANT);
604 RECORD(DECL_FUNCTION);
605 RECORD(DECL_OBJC_METHOD);
606 RECORD(DECL_OBJC_INTERFACE);
607 RECORD(DECL_OBJC_PROTOCOL);
608 RECORD(DECL_OBJC_IVAR);
609 RECORD(DECL_OBJC_AT_DEFS_FIELD);
610 RECORD(DECL_OBJC_CLASS);
611 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
612 RECORD(DECL_OBJC_CATEGORY);
613 RECORD(DECL_OBJC_CATEGORY_IMPL);
614 RECORD(DECL_OBJC_IMPLEMENTATION);
615 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
616 RECORD(DECL_OBJC_PROPERTY);
617 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000618 RECORD(DECL_FIELD);
619 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000620 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000621 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000622 RECORD(DECL_FILE_SCOPE_ASM);
623 RECORD(DECL_BLOCK);
624 RECORD(DECL_CONTEXT_LEXICAL);
625 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000626 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000627 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000628#undef RECORD
629#undef BLOCK
630 Stream.ExitBlock();
631}
632
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000633/// \brief Adjusts the given filename to only write out the portion of the
634/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000635///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000636/// \param Filename the file name to adjust.
637///
638/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
639/// the returned filename will be adjusted by this system root.
640///
641/// \returns either the original filename (if it needs no adjustment) or the
642/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000643static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000644adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
645 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000646
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000647 if (!isysroot)
648 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000649
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000650 // Verify that the filename and the system root have the same prefix.
651 unsigned Pos = 0;
652 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
653 if (Filename[Pos] != isysroot[Pos])
654 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000655
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000656 // We hit the end of the filename before we hit the end of the system root.
657 if (!Filename[Pos])
658 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000659
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000660 // If the file name has a '/' at the current position, skip over the '/'.
661 // We distinguish sysroot-based includes from absolute includes by the
662 // absence of '/' at the beginning of sysroot-based includes.
663 if (Filename[Pos] == '/')
664 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000666 return Filename + Pos;
667}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000668
Douglas Gregor7b71e632009-04-27 22:23:34 +0000669/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000670void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000671 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000672
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000673 // Metadata
674 const TargetInfo &Target = Context.Target;
675 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
676 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
677 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
678 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
679 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
680 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
681 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
682 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
683 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000684
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000685 RecordData Record;
686 Record.push_back(pch::METADATA);
687 Record.push_back(pch::VERSION_MAJOR);
688 Record.push_back(pch::VERSION_MINOR);
689 Record.push_back(CLANG_VERSION_MAJOR);
690 Record.push_back(CLANG_VERSION_MINOR);
691 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000692 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000693 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000694
Douglas Gregor45fe0362009-05-12 01:31:05 +0000695 // Original file name
696 SourceManager &SM = Context.getSourceManager();
697 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
698 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
699 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
700 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
701 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
702
703 llvm::sys::Path MainFilePath(MainFile->getName());
704 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000705
Douglas Gregor45fe0362009-05-12 01:31:05 +0000706 if (!MainFilePath.isAbsolute()) {
707 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000708 P.appendComponent(MainFilePath.str());
709 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000710 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000711 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000712 }
713
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000714 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000715 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000716 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000717 RecordData Record;
718 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000719 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000720 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000721
Ted Kremenek18e066f2010-01-22 22:12:47 +0000722 // Repository branch/version information.
723 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
724 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
725 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
726 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000727 Record.clear();
Ted Kremenek17437132010-01-22 20:59:36 +0000728 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000729 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
730 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000731}
732
733/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000734void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
735 RecordData Record;
736 Record.push_back(LangOpts.Trigraphs);
737 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
738 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
739 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
740 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
741 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
742 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
743 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
744 Record.push_back(LangOpts.C99); // C99 Support
745 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
746 Record.push_back(LangOpts.CPlusPlus); // C++ Support
747 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000748 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000749
Douglas Gregor55abb232009-04-10 20:39:37 +0000750 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
751 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Fariborz Jahanian45878032010-02-09 19:31:38 +0000752 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
753 // modern abi enabled.
754 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
755 // modern abi enabled.
Mike Stump11289f42009-09-09 15:08:12 +0000756
Douglas Gregor55abb232009-04-10 20:39:37 +0000757 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000758 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
759 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000760 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000761 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000762 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000763
764 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
765 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
766 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
767
Chris Lattner258172e2009-04-27 07:35:58 +0000768 // Whether static initializers are protected by locks.
769 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000770 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000771 Record.push_back(LangOpts.Blocks); // block extension to C
772 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
773 // they are unused.
774 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
775 // (modulo the platform support).
776
777 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
778 // signed integer arithmetic overflows.
779
780 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
781 // may be ripped out at any time.
782
783 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000784 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000785 // defined.
786 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
787 // opposed to __DYNAMIC__).
788 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
789
790 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
791 // used (instead of C99 semantics).
792 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000793 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
794 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000795 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
796 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000797 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000798 Record.push_back(LangOpts.getGCMode());
799 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000800 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000801 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000802 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000803 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000804 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000805 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000806}
807
Douglas Gregora7f71a92009-04-10 03:52:48 +0000808//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000809// stat cache Serialization
810//===----------------------------------------------------------------------===//
811
812namespace {
813// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000814class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000815public:
816 typedef const char * key_type;
817 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000818
Douglas Gregorc5046832009-04-27 18:38:38 +0000819 typedef std::pair<int, struct stat> data_type;
820 typedef const data_type& data_type_ref;
821
822 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000823 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000824 }
Mike Stump11289f42009-09-09 15:08:12 +0000825
826 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000827 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
828 data_type_ref Data) {
829 unsigned StrLen = strlen(path);
830 clang::io::Emit16(Out, StrLen);
831 unsigned DataLen = 1; // result value
832 if (Data.first == 0)
833 DataLen += 4 + 4 + 2 + 8 + 8;
834 clang::io::Emit8(Out, DataLen);
835 return std::make_pair(StrLen + 1, DataLen);
836 }
Mike Stump11289f42009-09-09 15:08:12 +0000837
Douglas Gregorc5046832009-04-27 18:38:38 +0000838 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
839 Out.write(path, KeyLen);
840 }
Mike Stump11289f42009-09-09 15:08:12 +0000841
Douglas Gregorc5046832009-04-27 18:38:38 +0000842 void EmitData(llvm::raw_ostream& Out, key_type_ref,
843 data_type_ref Data, unsigned DataLen) {
844 using namespace clang::io;
845 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000846
Douglas Gregorc5046832009-04-27 18:38:38 +0000847 // Result of stat()
848 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000849
Douglas Gregorc5046832009-04-27 18:38:38 +0000850 if (Data.first == 0) {
851 Emit32(Out, (uint32_t) Data.second.st_ino);
852 Emit32(Out, (uint32_t) Data.second.st_dev);
853 Emit16(Out, (uint16_t) Data.second.st_mode);
854 Emit64(Out, (uint64_t) Data.second.st_mtime);
855 Emit64(Out, (uint64_t) Data.second.st_size);
856 }
857
858 assert(Out.tell() - Start == DataLen && "Wrong data length");
859 }
860};
861} // end anonymous namespace
862
863/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000864void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
865 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000866 // Build the on-disk hash table containing information about every
867 // stat() call.
868 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
869 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000870 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000871 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000872 Stat != StatEnd; ++Stat, ++NumStatEntries) {
873 const char *Filename = Stat->first();
874 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
875 Generator.insert(Filename, Stat->second);
876 }
Mike Stump11289f42009-09-09 15:08:12 +0000877
Douglas Gregorc5046832009-04-27 18:38:38 +0000878 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000879 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000880 uint32_t BucketOffset;
881 {
882 llvm::raw_svector_ostream Out(StatCacheData);
883 // Make sure that no bucket is at offset 0
884 clang::io::Emit32(Out, 0);
885 BucketOffset = Generator.Emit(Out);
886 }
887
888 // Create a blob abbreviation
889 using namespace llvm;
890 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
891 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
892 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
893 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
895 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
896
897 // Write the stat cache
898 RecordData Record;
899 Record.push_back(pch::STAT_CACHE);
900 Record.push_back(BucketOffset);
901 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000902 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000903}
904
905//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000906// Source Manager Serialization
907//===----------------------------------------------------------------------===//
908
909/// \brief Create an abbreviation for the SLocEntry that refers to a
910/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000911static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000912 using namespace llvm;
913 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
914 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000920 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000921}
922
923/// \brief Create an abbreviation for the SLocEntry that refers to a
924/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000925static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000926 using namespace llvm;
927 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
928 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000934 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000935}
936
937/// \brief Create an abbreviation for the SLocEntry that refers to a
938/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000939static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000940 using namespace llvm;
941 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
942 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000944 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000945}
946
947/// \brief Create an abbreviation for the SLocEntry that refers to an
948/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000949static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000950 using namespace llvm;
951 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
952 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000958 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000959}
960
961/// \brief Writes the block containing the serialized form of the
962/// source manager.
963///
964/// TODO: We should probably use an on-disk hash table (stored in a
965/// blob), indexed based on the file name, so that we only create
966/// entries for files that we actually need. In the common case (no
967/// errors), we probably won't have to create file entries for any of
968/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000969void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000970 const Preprocessor &PP,
971 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000972 RecordData Record;
973
Chris Lattner0910e3b2009-04-10 17:16:57 +0000974 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000975 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000976
977 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000978 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
979 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
980 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
981 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000982
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000983 // Write the line table.
984 if (SourceMgr.hasLineTable()) {
985 LineTableInfo &LineTable = SourceMgr.getLineTable();
986
987 // Emit the file names
988 Record.push_back(LineTable.getNumFilenames());
989 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
990 // Emit the file name
991 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000992 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000993 unsigned FilenameLen = Filename? strlen(Filename) : 0;
994 Record.push_back(FilenameLen);
995 if (FilenameLen)
996 Record.insert(Record.end(), Filename, Filename + FilenameLen);
997 }
Mike Stump11289f42009-09-09 15:08:12 +0000998
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000999 // Emit the line entries
1000 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1001 L != LEnd; ++L) {
1002 // Emit the file ID
1003 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001004
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001005 // Emit the line entries
1006 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001007 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001008 LEEnd = L->second.end();
1009 LE != LEEnd; ++LE) {
1010 Record.push_back(LE->FileOffset);
1011 Record.push_back(LE->LineNo);
1012 Record.push_back(LE->FilenameID);
1013 Record.push_back((unsigned)LE->FileKind);
1014 Record.push_back(LE->IncludeOffset);
1015 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001016 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +00001017 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001018 }
1019
Douglas Gregor258ae542009-04-27 06:38:32 +00001020 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +00001021 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +00001022 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001023 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +00001024 E = HS.header_file_end();
1025 I != E; ++I) {
1026 Record.push_back(I->isImport);
1027 Record.push_back(I->DirInfo);
1028 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +00001029 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001030 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1031 Record.clear();
1032 }
1033
Douglas Gregor258ae542009-04-27 06:38:32 +00001034 // Write out the source location entry table. We skip the first
1035 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001036 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001037 RecordData PreloadSLocs;
1038 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001039 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1040 // Get this source location entry.
1041 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1042
Douglas Gregor258ae542009-04-27 06:38:32 +00001043 // Record the offset of this source-location entry.
1044 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1045
1046 // Figure out which record code to use.
1047 unsigned Code;
1048 if (SLoc->isFile()) {
1049 if (SLoc->getFile().getContentCache()->Entry)
1050 Code = pch::SM_SLOC_FILE_ENTRY;
1051 else
1052 Code = pch::SM_SLOC_BUFFER_ENTRY;
1053 } else
1054 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1055 Record.clear();
1056 Record.push_back(Code);
1057
1058 Record.push_back(SLoc->getOffset());
1059 if (SLoc->isFile()) {
1060 const SrcMgr::FileInfo &File = SLoc->getFile();
1061 Record.push_back(File.getIncludeLoc().getRawEncoding());
1062 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1063 Record.push_back(File.hasLineDirectives());
1064
1065 const SrcMgr::ContentCache *Content = File.getContentCache();
1066 if (Content->Entry) {
1067 // The source location entry is a file. The blob associated
1068 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001069
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001070 // Turn the file name into an absolute path, if it isn't already.
1071 const char *Filename = Content->Entry->getName();
1072 llvm::sys::Path FilePath(Filename, strlen(Filename));
1073 std::string FilenameStr;
1074 if (!FilePath.isAbsolute()) {
1075 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001076 P.appendComponent(FilePath.str());
1077 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001078 Filename = FilenameStr.c_str();
1079 }
Mike Stump11289f42009-09-09 15:08:12 +00001080
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001081 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001082 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001083
1084 // FIXME: For now, preload all file source locations, so that
1085 // we get the appropriate File entries in the reader. This is
1086 // a temporary measure.
1087 PreloadSLocs.push_back(SLocEntryOffsets.size());
1088 } else {
1089 // The source location entry is a buffer. The blob associated
1090 // with this entry contains the contents of the buffer.
1091
1092 // We add one to the size so that we capture the trailing NULL
1093 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1094 // the reader side).
1095 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1096 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001097 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1098 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001099 Record.clear();
1100 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1101 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001102 llvm::StringRef(Buffer->getBufferStart(),
1103 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001104
1105 if (strcmp(Name, "<built-in>") == 0)
1106 PreloadSLocs.push_back(SLocEntryOffsets.size());
1107 }
1108 } else {
1109 // The source location entry is an instantiation.
1110 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1111 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1112 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1113 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1114
1115 // Compute the token length for this macro expansion.
1116 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001117 if (I + 1 != N)
1118 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001119 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1120 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1121 }
1122 }
1123
Douglas Gregor8f45df52009-04-16 22:23:12 +00001124 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001125
1126 if (SLocEntryOffsets.empty())
1127 return;
1128
1129 // Write the source-location offsets table into the PCH block. This
1130 // table is used for lazily loading source-location information.
1131 using namespace llvm;
1132 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1133 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1135 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1137 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001138
Douglas Gregor258ae542009-04-27 06:38:32 +00001139 Record.clear();
1140 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1141 Record.push_back(SLocEntryOffsets.size());
1142 Record.push_back(SourceMgr.getNextOffset());
1143 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001144 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001145 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001146
1147 // Write the source location entry preloads array, telling the PCH
1148 // reader which source locations entries it should load eagerly.
1149 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001150}
1151
Douglas Gregorc5046832009-04-27 18:38:38 +00001152//===----------------------------------------------------------------------===//
1153// Preprocessor Serialization
1154//===----------------------------------------------------------------------===//
1155
Chris Lattnereeffaef2009-04-10 17:15:23 +00001156/// \brief Writes the block containing the serialized form of the
1157/// preprocessor.
1158///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001159void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001160 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001161
Chris Lattner0af3ba12009-04-13 01:29:17 +00001162 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1163 if (PP.getCounterValue() != 0) {
1164 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001165 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001166 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001167 }
1168
1169 // Enter the preprocessor block.
1170 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001171
Douglas Gregoreda6a892009-04-26 00:07:37 +00001172 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1173 // FIXME: use diagnostics subsystem for localization etc.
1174 if (PP.SawDateOrTime())
1175 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001176
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001177 // Loop over all the macro definitions that are live at the end of the file,
1178 // emitting each to the PP section.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001179 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1180 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001181 // FIXME: This emits macros in hash table order, we should do it in a stable
1182 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001183 MacroInfo *MI = I->second;
1184
1185 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1186 // been redefined by the header (in which case they are not isBuiltinMacro).
1187 if (MI->isBuiltinMacro())
1188 continue;
1189
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001190 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001191 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001192 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1193 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001194
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001195 unsigned Code;
1196 if (MI->isObjectLike()) {
1197 Code = pch::PP_MACRO_OBJECT_LIKE;
1198 } else {
1199 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001200
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001201 Record.push_back(MI->isC99Varargs());
1202 Record.push_back(MI->isGNUVarargs());
1203 Record.push_back(MI->getNumArgs());
1204 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1205 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001206 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001207 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001208 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001209 Record.clear();
1210
Chris Lattner2199f5b2009-04-10 18:08:30 +00001211 // Emit the tokens array.
1212 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1213 // Note that we know that the preprocessor does not have any annotation
1214 // tokens in it because they are created by the parser, and thus can't be
1215 // in a macro definition.
1216 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001217
Chris Lattner2199f5b2009-04-10 18:08:30 +00001218 Record.push_back(Tok.getLocation().getRawEncoding());
1219 Record.push_back(Tok.getLength());
1220
Chris Lattner2199f5b2009-04-10 18:08:30 +00001221 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1222 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001223 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001224
Chris Lattner2199f5b2009-04-10 18:08:30 +00001225 // FIXME: Should translate token kind to a stable encoding.
1226 Record.push_back(Tok.getKind());
1227 // FIXME: Should translate token flags to a stable encoding.
1228 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001229
Douglas Gregor8f45df52009-04-16 22:23:12 +00001230 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001231 Record.clear();
1232 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001233 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001234 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001235 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001236}
1237
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001238void PCHWriter::WriteComments(ASTContext &Context) {
1239 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001240
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001241 if (Context.Comments.empty())
1242 return;
Mike Stump11289f42009-09-09 15:08:12 +00001243
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001244 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1245 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1246 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1247 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001248
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001249 RecordData Record;
1250 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001251 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001252 (const char*)&Context.Comments[0],
1253 Context.Comments.size() * sizeof(SourceRange));
1254}
1255
Douglas Gregorc5046832009-04-27 18:38:38 +00001256//===----------------------------------------------------------------------===//
1257// Type Serialization
1258//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001259
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001260/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001261void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001262 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001263 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001264 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001265
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001266 // Record the offset for this type.
1267 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001268 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001269 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1270 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001271 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001272 }
1273
1274 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001275
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001276 // Emit the type's representation.
1277 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001278
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001279 if (T.hasLocalNonFastQualifiers()) {
1280 Qualifiers Qs = T.getLocalQualifiers();
1281 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001282 Record.push_back(Qs.getAsOpaqueValue());
1283 W.Code = pch::TYPE_EXT_QUAL;
1284 } else {
1285 switch (T->getTypeClass()) {
1286 // For all of the concrete, non-dependent types, call the
1287 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001288#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001289 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001290#define ABSTRACT_TYPE(Class, Base)
1291#define DEPENDENT_TYPE(Class, Base)
1292#include "clang/AST/TypeNodes.def"
1293
John McCall8ccfcb52009-09-24 19:53:00 +00001294 // For all of the dependent type nodes (which only occur in C++
1295 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001296#define TYPE(Class, Base)
1297#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1298#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001299 assert(false && "Cannot serialize dependent type nodes");
1300 break;
1301 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001302 }
1303
1304 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001305 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001306
1307 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001308 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001309}
1310
Douglas Gregorc5046832009-04-27 18:38:38 +00001311//===----------------------------------------------------------------------===//
1312// Declaration Serialization
1313//===----------------------------------------------------------------------===//
1314
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001315/// \brief Write the block containing all of the declaration IDs
1316/// lexically declared within the given DeclContext.
1317///
1318/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1319/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001320uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001321 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001322 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001323 return 0;
1324
Douglas Gregor8f45df52009-04-16 22:23:12 +00001325 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001326 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001327 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1328 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001329 AddDeclRef(*D, Record);
1330
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001331 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001332 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001333 return Offset;
1334}
1335
1336/// \brief Write the block containing all of the declaration IDs
1337/// visible from the given DeclContext.
1338///
1339/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1340/// bistream, or 0 if no block was written.
1341uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1342 DeclContext *DC) {
1343 if (DC->getPrimaryContext() != DC)
1344 return 0;
1345
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001346 // Since there is no name lookup into functions or methods, and we
1347 // perform name lookup for the translation unit via the
1348 // IdentifierInfo chains, don't bother to build a
1349 // visible-declarations table for these entities.
1350 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001351 return 0;
1352
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001353 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001354 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001355
1356 // Serialize the contents of the mapping used for lookup. Note that,
1357 // although we have two very different code paths, the serialized
1358 // representation is the same for both cases: a declaration name,
1359 // followed by a size, followed by references to the visible
1360 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001361 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001362 RecordData Record;
1363 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001364 if (!Map)
1365 return 0;
1366
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001367 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1368 D != DEnd; ++D) {
1369 AddDeclarationName(D->first, Record);
1370 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1371 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001372 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001373 AddDeclRef(*Result.first, Record);
1374 }
1375
1376 if (Record.size() == 0)
1377 return 0;
1378
Douglas Gregor8f45df52009-04-16 22:23:12 +00001379 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001380 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001381 return Offset;
1382}
1383
Douglas Gregorc5046832009-04-27 18:38:38 +00001384//===----------------------------------------------------------------------===//
1385// Global Method Pool and Selector Serialization
1386//===----------------------------------------------------------------------===//
1387
Douglas Gregore84a9da2009-04-20 20:36:09 +00001388namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001389// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001390class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001391 PCHWriter &Writer;
1392
1393public:
1394 typedef Selector key_type;
1395 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001396
Douglas Gregorc78d3462009-04-24 21:10:55 +00001397 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1398 typedef const data_type& data_type_ref;
1399
1400 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001401
Douglas Gregorc78d3462009-04-24 21:10:55 +00001402 static unsigned ComputeHash(Selector Sel) {
1403 unsigned N = Sel.getNumArgs();
1404 if (N == 0)
1405 ++N;
1406 unsigned R = 5381;
1407 for (unsigned I = 0; I != N; ++I)
1408 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001409 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001410 return R;
1411 }
Mike Stump11289f42009-09-09 15:08:12 +00001412
1413 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001414 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1415 data_type_ref Methods) {
1416 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1417 clang::io::Emit16(Out, KeyLen);
1418 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001419 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001420 Method = Method->Next)
1421 if (Method->Method)
1422 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001423 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001424 Method = Method->Next)
1425 if (Method->Method)
1426 DataLen += 4;
1427 clang::io::Emit16(Out, DataLen);
1428 return std::make_pair(KeyLen, DataLen);
1429 }
Mike Stump11289f42009-09-09 15:08:12 +00001430
Douglas Gregor95c13f52009-04-25 17:48:32 +00001431 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001432 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001433 assert((Start >> 32) == 0 && "Selector key offset too large");
1434 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001435 unsigned N = Sel.getNumArgs();
1436 clang::io::Emit16(Out, N);
1437 if (N == 0)
1438 N = 1;
1439 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001440 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001441 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1442 }
Mike Stump11289f42009-09-09 15:08:12 +00001443
Douglas Gregorc78d3462009-04-24 21:10:55 +00001444 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001445 data_type_ref Methods, unsigned DataLen) {
1446 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001447 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001448 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001449 Method = Method->Next)
1450 if (Method->Method)
1451 ++NumInstanceMethods;
1452
1453 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001454 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001455 Method = Method->Next)
1456 if (Method->Method)
1457 ++NumFactoryMethods;
1458
1459 clang::io::Emit16(Out, NumInstanceMethods);
1460 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001461 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001462 Method = Method->Next)
1463 if (Method->Method)
1464 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001465 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001466 Method = Method->Next)
1467 if (Method->Method)
1468 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001469
1470 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001471 }
1472};
1473} // end anonymous namespace
1474
1475/// \brief Write the method pool into the PCH file.
1476///
1477/// The method pool contains both instance and factory methods, stored
1478/// in an on-disk hash table indexed by the selector.
1479void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1480 using namespace llvm;
1481
1482 // Create and write out the blob that contains the instance and
1483 // factor method pools.
1484 bool Empty = true;
1485 {
1486 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001487
Douglas Gregorc78d3462009-04-24 21:10:55 +00001488 // Create the on-disk hash table representation. Start by
1489 // iterating through the instance method pool.
1490 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001491 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001492 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001493 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001494 InstanceEnd = SemaRef.InstanceMethodPool.end();
1495 Instance != InstanceEnd; ++Instance) {
1496 // Check whether there is a factory method with the same
1497 // selector.
1498 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1499 = SemaRef.FactoryMethodPool.find(Instance->first);
1500
1501 if (Factory == SemaRef.FactoryMethodPool.end())
1502 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001503 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001504 ObjCMethodList()));
1505 else
1506 Generator.insert(Instance->first,
1507 std::make_pair(Instance->second, Factory->second));
1508
Douglas Gregor95c13f52009-04-25 17:48:32 +00001509 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001510 Empty = false;
1511 }
1512
1513 // Now iterate through the factory method pool, to pick up any
1514 // selectors that weren't already in the instance method pool.
1515 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001516 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001517 FactoryEnd = SemaRef.FactoryMethodPool.end();
1518 Factory != FactoryEnd; ++Factory) {
1519 // Check whether there is an instance method with the same
1520 // selector. If so, there is no work to do here.
1521 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1522 = SemaRef.InstanceMethodPool.find(Factory->first);
1523
Douglas Gregor95c13f52009-04-25 17:48:32 +00001524 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001525 Generator.insert(Factory->first,
1526 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001527 ++NumSelectorsInMethodPool;
1528 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001529
1530 Empty = false;
1531 }
1532
Douglas Gregor95c13f52009-04-25 17:48:32 +00001533 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001534 return;
1535
1536 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001537 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001538 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001539 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001540 {
1541 PCHMethodPoolTrait Trait(*this);
1542 llvm::raw_svector_ostream Out(MethodPool);
1543 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001544 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001545 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001546
1547 // For every selector that we have seen but which was not
1548 // written into the hash table, write the selector itself and
1549 // record it's offset.
1550 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1551 if (SelectorOffsets[I] == 0)
1552 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001553 }
1554
1555 // Create a blob abbreviation
1556 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1557 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1561 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1562
Douglas Gregor95c13f52009-04-25 17:48:32 +00001563 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001564 RecordData Record;
1565 Record.push_back(pch::METHOD_POOL);
1566 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001567 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001568 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001569
1570 // Create a blob abbreviation for the selector table offsets.
1571 Abbrev = new BitCodeAbbrev();
1572 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1575 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1576
1577 // Write the selector offsets table.
1578 Record.clear();
1579 Record.push_back(pch::SELECTOR_OFFSETS);
1580 Record.push_back(SelectorOffsets.size());
1581 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1582 (const char *)&SelectorOffsets.front(),
1583 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001584 }
1585}
1586
Douglas Gregorc5046832009-04-27 18:38:38 +00001587//===----------------------------------------------------------------------===//
1588// Identifier Table Serialization
1589//===----------------------------------------------------------------------===//
1590
Douglas Gregorc78d3462009-04-24 21:10:55 +00001591namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001592class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001593 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001594 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001595
Douglas Gregor1d583f22009-04-28 21:18:29 +00001596 /// \brief Determines whether this is an "interesting" identifier
1597 /// that needs a full IdentifierInfo structure written into the hash
1598 /// table.
1599 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1600 return II->isPoisoned() ||
1601 II->isExtensionToken() ||
1602 II->hasMacroDefinition() ||
1603 II->getObjCOrBuiltinID() ||
1604 II->getFETokenInfo<void>();
1605 }
1606
Douglas Gregore84a9da2009-04-20 20:36:09 +00001607public:
1608 typedef const IdentifierInfo* key_type;
1609 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001610
Douglas Gregore84a9da2009-04-20 20:36:09 +00001611 typedef pch::IdentID data_type;
1612 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001613
1614 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001615 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001616
1617 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001618 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001619 }
Mike Stump11289f42009-09-09 15:08:12 +00001620
1621 std::pair<unsigned,unsigned>
1622 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001623 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001624 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001625 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1626 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001627 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001628 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001629 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001630 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001631 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1632 DEnd = IdentifierResolver::end();
1633 D != DEnd; ++D)
1634 DataLen += sizeof(pch::DeclID);
1635 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001636 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001637 // We emit the key length after the data length so that every
1638 // string is preceded by a 16-bit length. This matches the PTH
1639 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001640 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001641 return std::make_pair(KeyLen, DataLen);
1642 }
Mike Stump11289f42009-09-09 15:08:12 +00001643
1644 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001645 unsigned KeyLen) {
1646 // Record the location of the key data. This is used when generating
1647 // the mapping from persistent IDs to strings.
1648 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001649 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001650 }
Mike Stump11289f42009-09-09 15:08:12 +00001651
1652 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001653 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001654 if (!isInterestingIdentifier(II)) {
1655 clang::io::Emit32(Out, ID << 1);
1656 return;
1657 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001658
Douglas Gregor1d583f22009-04-28 21:18:29 +00001659 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001660 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001661 bool hasMacroDefinition =
1662 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001663 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001664 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001665 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1666 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1667 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1668 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001669 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001670
Douglas Gregorc3366a52009-04-21 23:56:24 +00001671 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001672 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001673
Douglas Gregora868bbd2009-04-21 22:25:48 +00001674 // Emit the declaration IDs in reverse order, because the
1675 // IdentifierResolver provides the declarations as they would be
1676 // visible (e.g., the function "stat" would come before the struct
1677 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1678 // adds declarations to the end of the list (so we need to see the
1679 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001680 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001681 IdentifierResolver::end());
1682 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1683 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001684 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001685 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001686 }
1687};
1688} // end anonymous namespace
1689
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001690/// \brief Write the identifier table into the PCH file.
1691///
1692/// The identifier table consists of a blob containing string data
1693/// (the actual identifiers themselves) and a separate "offsets" index
1694/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001695void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001696 using namespace llvm;
1697
1698 // Create and write out the blob that contains the identifier
1699 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001700 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001701 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001702
Douglas Gregore6648fb2009-04-28 20:33:11 +00001703 // Look for any identifiers that were named while processing the
1704 // headers, but are otherwise not needed. We add these to the hash
1705 // table to enable checking of the predefines buffer in the case
1706 // where the user adds new macro definitions when building the PCH
1707 // file.
1708 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1709 IDEnd = PP.getIdentifierTable().end();
1710 ID != IDEnd; ++ID)
1711 getIdentifierRef(ID->second);
1712
Douglas Gregore84a9da2009-04-20 20:36:09 +00001713 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001714 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001715 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1716 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1717 ID != IDEnd; ++ID) {
1718 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001719 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001720 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001721
Douglas Gregore84a9da2009-04-20 20:36:09 +00001722 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001723 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001724 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001725 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001726 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001727 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001728 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001729 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001730 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001731 }
1732
1733 // Create a blob abbreviation
1734 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1735 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001738 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001739
1740 // Write the identifier table
1741 RecordData Record;
1742 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001743 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001744 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001745 }
1746
1747 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001748 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1749 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1752 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1753
1754 RecordData Record;
1755 Record.push_back(pch::IDENTIFIER_OFFSET);
1756 Record.push_back(IdentifierOffsets.size());
1757 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1758 (const char *)&IdentifierOffsets.front(),
1759 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001760}
1761
Douglas Gregorc5046832009-04-27 18:38:38 +00001762//===----------------------------------------------------------------------===//
1763// General Serialization Routines
1764//===----------------------------------------------------------------------===//
1765
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001766/// \brief Write a record containing the given attributes.
1767void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1768 RecordData Record;
1769 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001770 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001771 Record.push_back(Attr->isInherited());
1772 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001773 default:
1774 assert(0 && "Does not support PCH writing for this attribute yet!");
1775 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001776 case Attr::Alias:
1777 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1778 break;
1779
1780 case Attr::Aligned:
1781 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1782 break;
1783
1784 case Attr::AlwaysInline:
1785 break;
Mike Stump11289f42009-09-09 15:08:12 +00001786
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001787 case Attr::AnalyzerNoReturn:
1788 break;
1789
1790 case Attr::Annotate:
1791 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1792 break;
1793
1794 case Attr::AsmLabel:
1795 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1796 break;
1797
Alexis Hunt54a02542009-11-25 04:20:27 +00001798 case Attr::BaseCheck:
1799 break;
1800
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001801 case Attr::Blocks:
1802 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1803 break;
1804
Eli Friedmane4310c82009-11-09 18:38:53 +00001805 case Attr::CDecl:
1806 break;
1807
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001808 case Attr::Cleanup:
1809 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1810 break;
1811
1812 case Attr::Const:
1813 break;
1814
1815 case Attr::Constructor:
1816 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1817 break;
1818
1819 case Attr::DLLExport:
1820 case Attr::DLLImport:
1821 case Attr::Deprecated:
1822 break;
1823
1824 case Attr::Destructor:
1825 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1826 break;
1827
1828 case Attr::FastCall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001829 case Attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001830 break;
1831
1832 case Attr::Format: {
1833 const FormatAttr *Format = cast<FormatAttr>(Attr);
1834 AddString(Format->getType(), Record);
1835 Record.push_back(Format->getFormatIdx());
1836 Record.push_back(Format->getFirstArg());
1837 break;
1838 }
1839
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001840 case Attr::FormatArg: {
1841 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1842 Record.push_back(Format->getFormatIdx());
1843 break;
1844 }
1845
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001846 case Attr::Sentinel : {
1847 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1848 Record.push_back(Sentinel->getSentinel());
1849 Record.push_back(Sentinel->getNullPos());
1850 break;
1851 }
Mike Stump11289f42009-09-09 15:08:12 +00001852
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001853 case Attr::GNUInline:
Alexis Hunt54a02542009-11-25 04:20:27 +00001854 case Attr::Hiding:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001855 case Attr::IBOutletKind:
Ted Kremenek06be9682010-02-17 02:37:45 +00001856 case Attr::IBActionKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001857 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001858 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001859 case Attr::NoReturn:
1860 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001861 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001862 break;
1863
1864 case Attr::NonNull: {
1865 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1866 Record.push_back(NonNull->size());
1867 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1868 break;
1869 }
1870
Ted Kremenekd9c66632010-02-18 00:05:45 +00001871 case Attr::CFReturnsNotRetained:
1872 case Attr::CFReturnsRetained:
1873 case Attr::NSReturnsNotRetained:
1874 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001875 case Attr::ObjCException:
1876 case Attr::ObjCNSObject:
1877 case Attr::Overloadable:
Alexis Hunt54a02542009-11-25 04:20:27 +00001878 case Attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001879 break;
1880
Anders Carlsson68e0b682009-08-08 18:23:56 +00001881 case Attr::PragmaPack:
1882 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001883 break;
1884
Anders Carlsson68e0b682009-08-08 18:23:56 +00001885 case Attr::Packed:
1886 break;
Mike Stump11289f42009-09-09 15:08:12 +00001887
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001888 case Attr::Pure:
1889 break;
1890
1891 case Attr::Regparm:
1892 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1893 break;
Mike Stump11289f42009-09-09 15:08:12 +00001894
Nate Begemanf2758702009-06-26 06:32:41 +00001895 case Attr::ReqdWorkGroupSize:
1896 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1897 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1898 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1899 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001900
1901 case Attr::Section:
1902 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1903 break;
1904
1905 case Attr::StdCall:
1906 case Attr::TransparentUnion:
1907 case Attr::Unavailable:
1908 case Attr::Unused:
1909 case Attr::Used:
1910 break;
1911
1912 case Attr::Visibility:
1913 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001914 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001915 break;
1916
1917 case Attr::WarnUnusedResult:
1918 case Attr::Weak:
1919 case Attr::WeakImport:
1920 break;
1921 }
1922 }
1923
Douglas Gregor8f45df52009-04-16 22:23:12 +00001924 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001925}
1926
1927void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1928 Record.push_back(Str.size());
1929 Record.insert(Record.end(), Str.begin(), Str.end());
1930}
1931
Douglas Gregore84a9da2009-04-20 20:36:09 +00001932/// \brief Note that the identifier II occurs at the given offset
1933/// within the identifier table.
1934void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001935 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001936}
1937
Douglas Gregor95c13f52009-04-25 17:48:32 +00001938/// \brief Note that the selector Sel occurs at the given offset
1939/// within the method pool/selector table.
1940void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1941 unsigned ID = SelectorIDs[Sel];
1942 assert(ID && "Unknown selector");
1943 SelectorOffsets[ID - 1] = Offset;
1944}
1945
Mike Stump11289f42009-09-09 15:08:12 +00001946PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1947 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001948 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1949 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001950
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001951void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1952 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001953 using namespace llvm;
1954
Douglas Gregor162dd022009-04-20 15:53:59 +00001955 ASTContext &Context = SemaRef.Context;
1956 Preprocessor &PP = SemaRef.PP;
1957
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001958 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001959 Stream.Emit((unsigned)'C', 8);
1960 Stream.Emit((unsigned)'P', 8);
1961 Stream.Emit((unsigned)'C', 8);
1962 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001963
Chris Lattner28fa4e62009-04-26 22:26:21 +00001964 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001965
1966 // The translation unit is the first declaration we'll emit.
1967 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001968 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001969
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001970 // Make sure that we emit IdentifierInfos (and any attached
1971 // declarations) for builtins.
1972 {
1973 IdentifierTable &Table = PP.getIdentifierTable();
1974 llvm::SmallVector<const char *, 32> BuiltinNames;
1975 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1976 Context.getLangOptions().NoBuiltin);
1977 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1978 getIdentifierRef(&Table.get(BuiltinNames[I]));
1979 }
1980
Chris Lattner0c797362009-09-08 18:19:27 +00001981 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00001982 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00001983 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001984 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00001985 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
1986 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00001987 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001988
Tanya Lattner90073802010-02-12 00:07:30 +00001989 // Build a record containing all of the static unused functions in this file.
1990 RecordData UnusedStaticFuncs;
1991 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
1992 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
1993
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001994 // Build a record containing all of the locally-scoped external
1995 // declarations in this header file. Generally, this record will be
1996 // empty.
1997 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001998 // FIXME: This is filling in the PCH file in densemap order which is
1999 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002000 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002001 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2002 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2003 TD != TDEnd; ++TD)
2004 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2005
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002006 // Build a record containing all of the ext_vector declarations.
2007 RecordData ExtVectorDecls;
2008 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2009 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2010
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002011 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002012 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00002013 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002014 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00002015 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002016 if (StatCalls && !isysroot)
2017 WriteStatCache(*StatCalls, isysroot);
2018 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00002019 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00002020 // Write the record of special types.
2021 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002022
Steve Naroffc277ad12009-07-18 15:33:26 +00002023 AddTypeRef(Context.getBuiltinVaListType(), Record);
2024 AddTypeRef(Context.getObjCIdType(), Record);
2025 AddTypeRef(Context.getObjCSelType(), Record);
2026 AddTypeRef(Context.getObjCProtoType(), Record);
2027 AddTypeRef(Context.getObjCClassType(), Record);
2028 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2029 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2030 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002031 AddTypeRef(Context.getjmp_bufType(), Record);
2032 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002033 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2034 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002035#if 0
2036 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian04b258c2009-11-25 23:07:42 +00002037 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002038#endif
Mike Stumpd0153282009-10-20 02:12:22 +00002039 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002040 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002041 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002042
Douglas Gregor1970d882009-04-26 03:49:13 +00002043 // Keep writing types and declarations until all types and
2044 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002045 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2046 WriteDeclsBlockAbbrevs();
2047 while (!DeclTypesToEmit.empty()) {
2048 DeclOrType DOT = DeclTypesToEmit.front();
2049 DeclTypesToEmit.pop();
2050 if (DOT.isType())
2051 WriteType(DOT.getType());
2052 else
2053 WriteDecl(Context, DOT.getDecl());
2054 }
2055 Stream.ExitBlock();
2056
Douglas Gregor45053152009-10-17 17:25:45 +00002057 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002058 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002059 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002060
2061 // Write the type offsets array
2062 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2063 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2066 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2067 Record.clear();
2068 Record.push_back(pch::TYPE_OFFSET);
2069 Record.push_back(TypeOffsets.size());
2070 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002071 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002072 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002073
Douglas Gregor745ed142009-04-25 18:35:21 +00002074 // Write the declaration offsets array
2075 Abbrev = new BitCodeAbbrev();
2076 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2077 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2078 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2079 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2080 Record.clear();
2081 Record.push_back(pch::DECL_OFFSET);
2082 Record.push_back(DeclOffsets.size());
2083 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002084 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002085 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002086
Douglas Gregord4df8652009-04-22 22:02:47 +00002087 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002088 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002089 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002090
2091 // Write the record containing tentative definitions.
2092 if (!TentativeDefinitions.empty())
2093 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002094
Tanya Lattner90073802010-02-12 00:07:30 +00002095 // Write the record containing unused static functions.
2096 if (!UnusedStaticFuncs.empty())
2097 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
2098
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002099 // Write the record containing locally-scoped external definitions.
2100 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002101 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002102 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002103
2104 // Write the record containing ext_vector type names.
2105 if (!ExtVectorDecls.empty())
2106 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002107
Douglas Gregor08f01292009-04-17 22:13:46 +00002108 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002109 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002110 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002111 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002112 Record.push_back(NumLexicalDeclContexts);
2113 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002114 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002115 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002116}
2117
2118void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2119 Record.push_back(Loc.getRawEncoding());
2120}
2121
2122void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2123 Record.push_back(Value.getBitWidth());
2124 unsigned N = Value.getNumWords();
2125 const uint64_t* Words = Value.getRawData();
2126 for (unsigned I = 0; I != N; ++I)
2127 Record.push_back(Words[I]);
2128}
2129
Douglas Gregor1daeb692009-04-13 18:14:40 +00002130void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2131 Record.push_back(Value.isUnsigned());
2132 AddAPInt(Value, Record);
2133}
2134
Douglas Gregore0a3a512009-04-14 21:55:33 +00002135void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2136 AddAPInt(Value.bitcastToAPInt(), Record);
2137}
2138
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002139void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002140 Record.push_back(getIdentifierRef(II));
2141}
2142
2143pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2144 if (II == 0)
2145 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002146
2147 pch::IdentID &ID = IdentifierIDs[II];
2148 if (ID == 0)
2149 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002150 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002151}
2152
Steve Naroff2ddea052009-04-23 10:39:46 +00002153void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2154 if (SelRef.getAsOpaquePtr() == 0) {
2155 Record.push_back(0);
2156 return;
2157 }
2158
2159 pch::SelectorID &SID = SelectorIDs[SelRef];
2160 if (SID == 0) {
2161 SID = SelectorIDs.size();
2162 SelVector.push_back(SelRef);
2163 }
2164 Record.push_back(SID);
2165}
2166
John McCall0ad16662009-10-29 08:12:44 +00002167void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2168 RecordData &Record) {
2169 switch (Arg.getArgument().getKind()) {
2170 case TemplateArgument::Expression:
2171 AddStmt(Arg.getLocInfo().getAsExpr());
2172 break;
2173 case TemplateArgument::Type:
John McCallbcd03502009-12-07 02:54:59 +00002174 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002175 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002176 case TemplateArgument::Template:
2177 Record.push_back(
2178 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2179 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2180 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2181 break;
John McCall0ad16662009-10-29 08:12:44 +00002182 case TemplateArgument::Null:
2183 case TemplateArgument::Integral:
2184 case TemplateArgument::Declaration:
2185 case TemplateArgument::Pack:
2186 break;
2187 }
2188}
2189
John McCallbcd03502009-12-07 02:54:59 +00002190void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2191 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002192 AddTypeRef(QualType(), Record);
2193 return;
2194 }
2195
John McCallbcd03502009-12-07 02:54:59 +00002196 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002197 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002198 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00002199 TLW.Visit(TL);
2200}
2201
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002202void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2203 if (T.isNull()) {
2204 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2205 return;
2206 }
2207
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002208 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002209 T.removeFastQualifiers();
2210
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002211 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002212 pch::TypeID &ID = TypeIDs[T];
2213 if (ID == 0) {
2214 // We haven't seen these qualifiers applied to this type before.
2215 // Assign it a new ID. This is the only time we enqueue a
2216 // qualified type, and it has no CV qualifiers.
2217 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002218 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002219 }
2220
2221 // Encode the type qualifiers in the type reference.
2222 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2223 return;
2224 }
2225
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002226 assert(!T.hasLocalQualifiers());
John McCall8ccfcb52009-09-24 19:53:00 +00002227
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002228 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002229 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002230 switch (BT->getKind()) {
2231 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2232 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2233 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2234 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2235 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2236 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2237 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2238 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002239 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002240 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2241 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2242 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2243 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2244 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2245 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2246 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002247 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002248 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2249 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2250 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002251 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002252 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2253 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002254 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2255 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002256 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2257 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002258 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002259 case BuiltinType::UndeducedAuto:
2260 assert(0 && "Should not see undeduced auto here");
2261 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002262 }
2263
John McCall8ccfcb52009-09-24 19:53:00 +00002264 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002265 return;
2266 }
2267
John McCall8ccfcb52009-09-24 19:53:00 +00002268 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002269 if (ID == 0) {
2270 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002271 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002272 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002273 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002274 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002275
2276 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002277 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002278}
2279
2280void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2281 if (D == 0) {
2282 Record.push_back(0);
2283 return;
2284 }
2285
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002286 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002287 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002288 // We haven't seen this declaration before. Give it a new ID and
2289 // enqueue it in the list of declarations to emit.
2290 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002291 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002292 }
2293
2294 Record.push_back(ID);
2295}
2296
Douglas Gregore84a9da2009-04-20 20:36:09 +00002297pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2298 if (D == 0)
2299 return 0;
2300
2301 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2302 return DeclIDs[D];
2303}
2304
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002305void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002306 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002307 Record.push_back(Name.getNameKind());
2308 switch (Name.getNameKind()) {
2309 case DeclarationName::Identifier:
2310 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2311 break;
2312
2313 case DeclarationName::ObjCZeroArgSelector:
2314 case DeclarationName::ObjCOneArgSelector:
2315 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002316 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002317 break;
2318
2319 case DeclarationName::CXXConstructorName:
2320 case DeclarationName::CXXDestructorName:
2321 case DeclarationName::CXXConversionFunctionName:
2322 AddTypeRef(Name.getCXXNameType(), Record);
2323 break;
2324
2325 case DeclarationName::CXXOperatorName:
2326 Record.push_back(Name.getCXXOverloadedOperator());
2327 break;
2328
Alexis Hunt3d221f22009-11-29 07:34:05 +00002329 case DeclarationName::CXXLiteralOperatorName:
2330 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2331 break;
2332
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002333 case DeclarationName::CXXUsingDirective:
2334 // No extra data to emit
2335 break;
2336 }
2337}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002338