blob: 45d9b1baced0d6d6642dd27811fdc32309c3011a [file] [log] [blame]
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000031#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000037#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000038#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42// Type serialization
43//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000044
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000046 class PCHTypeWriter {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047 PCHWriter &Writer;
48 PCHWriter::RecordData &Record;
49
50 public:
51 /// \brief Type code that corresponds to the record generated.
52 pch::TypeCode Code;
53
Mike Stump11289f42009-09-09 15:08:12 +000054 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000055 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
57 void VisitArrayType(const ArrayType *T);
58 void VisitFunctionType(const FunctionType *T);
59 void VisitTagType(const TagType *T);
60
61#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
62#define ABSTRACT_TYPE(Class, Base)
63#define DEPENDENT_TYPE(Class, Base)
64#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
73 Writer.AddTypeRef(T->getElementType(), Record);
74 Code = pch::TYPE_COMPLEX;
75}
76
77void PCHTypeWriter::VisitPointerType(const PointerType *T) {
78 Writer.AddTypeRef(T->getPointeeType(), Record);
79 Code = pch::TYPE_POINTER;
80}
81
82void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000083 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000084 Code = pch::TYPE_BLOCK_POINTER;
85}
86
87void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_LVALUE_REFERENCE;
90}
91
92void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_RVALUE_REFERENCE;
95}
96
97void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100 Code = pch::TYPE_MEMBER_POINTER;
101}
102
103void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
104 Writer.AddTypeRef(T->getElementType(), Record);
105 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000106 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107}
108
109void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
110 VisitArrayType(T);
111 Writer.AddAPInt(T->getSize(), Record);
112 Code = pch::TYPE_CONSTANT_ARRAY;
113}
114
115void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
116 VisitArrayType(T);
117 Code = pch::TYPE_INCOMPLETE_ARRAY;
118}
119
120void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
121 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000122 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
123 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000124 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125 Code = pch::TYPE_VARIABLE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVectorType(const VectorType *T) {
129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getNumElements());
John Thompson22334602010-02-05 00:12:22 +0000131 Record.push_back(T->isAltiVec());
132 Record.push_back(T->isPixel());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133 Code = pch::TYPE_VECTOR;
134}
135
136void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
137 VisitVectorType(T);
138 Code = pch::TYPE_EXT_VECTOR;
139}
140
141void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
142 Writer.AddTypeRef(T->getResultType(), Record);
Douglas Gregordc728752009-12-22 18:11:50 +0000143 Record.push_back(T->getNoReturnAttr());
Douglas Gregor8c940862010-01-18 17:14:39 +0000144 // FIXME: need to stabilize encoding of calling convention...
145 Record.push_back(T->getCallConv());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146}
147
148void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
149 VisitFunctionType(T);
150 Code = pch::TYPE_FUNCTION_NO_PROTO;
151}
152
153void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
154 VisitFunctionType(T);
155 Record.push_back(T->getNumArgs());
156 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
157 Writer.AddTypeRef(T->getArgType(I), Record);
158 Record.push_back(T->isVariadic());
159 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000160 Record.push_back(T->hasExceptionSpec());
161 Record.push_back(T->hasAnyExceptionSpec());
162 Record.push_back(T->getNumExceptions());
163 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
164 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165 Code = pch::TYPE_FUNCTION_PROTO;
166}
167
John McCallb96ec562009-12-04 22:46:56 +0000168#if 0
169// For when we want it....
170void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_UNRESOLVED_USING;
173}
174#endif
175
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
177 Writer.AddDeclRef(T->getDecl(), Record);
178 Code = pch::TYPE_TYPEDEF;
179}
180
181void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000182 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 Code = pch::TYPE_TYPEOF_EXPR;
184}
185
186void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
187 Writer.AddTypeRef(T->getUnderlyingType(), Record);
188 Code = pch::TYPE_TYPEOF;
189}
190
Anders Carlsson81df7b82009-06-24 19:06:50 +0000191void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
192 Writer.AddStmt(T->getUnderlyingExpr());
193 Code = pch::TYPE_DECLTYPE;
194}
195
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196void PCHTypeWriter::VisitTagType(const TagType *T) {
197 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000198 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199 "Cannot serialize in the middle of a type definition");
200}
201
202void PCHTypeWriter::VisitRecordType(const RecordType *T) {
203 VisitTagType(T);
204 Code = pch::TYPE_RECORD;
205}
206
207void PCHTypeWriter::VisitEnumType(const EnumType *T) {
208 VisitTagType(T);
209 Code = pch::TYPE_ENUM;
210}
211
John McCallfcc33b02009-09-05 00:15:47 +0000212void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
213 Writer.AddTypeRef(T->getUnderlyingType(), Record);
214 Record.push_back(T->getTagKind());
215 Code = pch::TYPE_ELABORATED;
216}
217
Mike Stump11289f42009-09-09 15:08:12 +0000218void
John McCallcebee162009-10-18 09:09:24 +0000219PCHTypeWriter::VisitSubstTemplateTypeParmType(
220 const SubstTemplateTypeParmType *T) {
221 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
222 Writer.AddTypeRef(T->getReplacementType(), Record);
223 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
224}
225
226void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227PCHTypeWriter::VisitTemplateSpecializationType(
228 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000229 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230 assert(false && "Cannot serialize template specialization types");
231}
232
233void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000234 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 assert(false && "Cannot serialize qualified name types");
236}
237
238void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
239 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000241 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
242 E = T->qual_end(); I != E; ++I)
243 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000244 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245}
246
Steve Narofffb4330f2009-06-17 22:40:22 +0000247void
248PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000249 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000250 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000251 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000252 E = T->qual_end(); I != E; ++I)
253 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000254 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255}
256
John McCall8f115c62009-10-16 21:56:05 +0000257namespace {
258
259class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
260 PCHWriter &Writer;
261 PCHWriter::RecordData &Record;
262
263public:
264 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
265 : Writer(Writer), Record(Record) { }
266
John McCall17001972009-10-18 01:05:36 +0000267#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000268#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000269 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000270#include "clang/AST/TypeLocNodes.def"
271
John McCall17001972009-10-18 01:05:36 +0000272 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
273 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000274};
275
276}
277
John McCall17001972009-10-18 01:05:36 +0000278void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
279 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000280}
John McCall17001972009-10-18 01:05:36 +0000281void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000282 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
283 if (TL.needsExtraLocalData()) {
284 Record.push_back(TL.getWrittenTypeSpec());
285 Record.push_back(TL.getWrittenSignSpec());
286 Record.push_back(TL.getWrittenWidthSpec());
287 Record.push_back(TL.hasModeAttr());
288 }
John McCall8f115c62009-10-16 21:56:05 +0000289}
John McCall17001972009-10-18 01:05:36 +0000290void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
291 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000292}
John McCall17001972009-10-18 01:05:36 +0000293void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
294 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000295}
John McCall17001972009-10-18 01:05:36 +0000296void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
297 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000298}
John McCall17001972009-10-18 01:05:36 +0000299void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
300 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000301}
John McCall17001972009-10-18 01:05:36 +0000302void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
303 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000304}
John McCall17001972009-10-18 01:05:36 +0000305void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
306 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000307}
John McCall17001972009-10-18 01:05:36 +0000308void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
309 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
310 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
311 Record.push_back(TL.getSizeExpr() ? 1 : 0);
312 if (TL.getSizeExpr())
313 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000314}
John McCall17001972009-10-18 01:05:36 +0000315void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
316 VisitArrayTypeLoc(TL);
317}
318void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
319 VisitArrayTypeLoc(TL);
320}
321void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
322 VisitArrayTypeLoc(TL);
323}
324void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
325 DependentSizedArrayTypeLoc TL) {
326 VisitArrayTypeLoc(TL);
327}
328void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
329 DependentSizedExtVectorTypeLoc TL) {
330 Writer.AddSourceLocation(TL.getNameLoc(), Record);
331}
332void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
333 Writer.AddSourceLocation(TL.getNameLoc(), Record);
334}
335void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
336 Writer.AddSourceLocation(TL.getNameLoc(), Record);
337}
338void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
340 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
341 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
342 Writer.AddDeclRef(TL.getArg(i), Record);
343}
344void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
345 VisitFunctionTypeLoc(TL);
346}
347void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
348 VisitFunctionTypeLoc(TL);
349}
John McCallb96ec562009-12-04 22:46:56 +0000350void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
351 Writer.AddSourceLocation(TL.getNameLoc(), Record);
352}
John McCall17001972009-10-18 01:05:36 +0000353void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
354 Writer.AddSourceLocation(TL.getNameLoc(), Record);
355}
356void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000357 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
358 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
359 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000360}
361void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000362 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
363 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
364 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
365 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000366}
367void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
368 Writer.AddSourceLocation(TL.getNameLoc(), Record);
369}
370void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getNameLoc(), Record);
372}
373void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getNameLoc(), Record);
375}
376void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
John McCallcebee162009-10-18 09:09:24 +0000382void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
383 SubstTemplateTypeParmTypeLoc TL) {
384 Writer.AddSourceLocation(TL.getNameLoc(), Record);
385}
John McCall17001972009-10-18 01:05:36 +0000386void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
387 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000388 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
389 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
390 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
391 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
392 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall17001972009-10-18 01:05:36 +0000393}
394void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
395 Writer.AddSourceLocation(TL.getNameLoc(), Record);
396}
397void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
398 Writer.AddSourceLocation(TL.getNameLoc(), Record);
399}
400void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000402 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
403 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
404 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
405 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000406}
John McCallfc93cf92009-10-22 22:37:11 +0000407void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getStarLoc(), Record);
409 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
410 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
411 Record.push_back(TL.hasBaseTypeAsWritten());
412 Record.push_back(TL.hasProtocolsAsWritten());
413 if (TL.hasProtocolsAsWritten())
414 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
415 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
416}
John McCall8f115c62009-10-16 21:56:05 +0000417
Chris Lattner19cea4e2009-04-22 05:57:30 +0000418//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000419// PCHWriter Implementation
420//===----------------------------------------------------------------------===//
421
Chris Lattner28fa4e62009-04-26 22:26:21 +0000422static void EmitBlockID(unsigned ID, const char *Name,
423 llvm::BitstreamWriter &Stream,
424 PCHWriter::RecordData &Record) {
425 Record.clear();
426 Record.push_back(ID);
427 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
428
429 // Emit the block name if present.
430 if (Name == 0 || Name[0] == 0) return;
431 Record.clear();
432 while (*Name)
433 Record.push_back(*Name++);
434 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
435}
436
437static void EmitRecordID(unsigned ID, const char *Name,
438 llvm::BitstreamWriter &Stream,
439 PCHWriter::RecordData &Record) {
440 Record.clear();
441 Record.push_back(ID);
442 while (*Name)
443 Record.push_back(*Name++);
444 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000445}
446
447static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
448 PCHWriter::RecordData &Record) {
449#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
450 RECORD(STMT_STOP);
451 RECORD(STMT_NULL_PTR);
452 RECORD(STMT_NULL);
453 RECORD(STMT_COMPOUND);
454 RECORD(STMT_CASE);
455 RECORD(STMT_DEFAULT);
456 RECORD(STMT_LABEL);
457 RECORD(STMT_IF);
458 RECORD(STMT_SWITCH);
459 RECORD(STMT_WHILE);
460 RECORD(STMT_DO);
461 RECORD(STMT_FOR);
462 RECORD(STMT_GOTO);
463 RECORD(STMT_INDIRECT_GOTO);
464 RECORD(STMT_CONTINUE);
465 RECORD(STMT_BREAK);
466 RECORD(STMT_RETURN);
467 RECORD(STMT_DECL);
468 RECORD(STMT_ASM);
469 RECORD(EXPR_PREDEFINED);
470 RECORD(EXPR_DECL_REF);
471 RECORD(EXPR_INTEGER_LITERAL);
472 RECORD(EXPR_FLOATING_LITERAL);
473 RECORD(EXPR_IMAGINARY_LITERAL);
474 RECORD(EXPR_STRING_LITERAL);
475 RECORD(EXPR_CHARACTER_LITERAL);
476 RECORD(EXPR_PAREN);
477 RECORD(EXPR_UNARY_OPERATOR);
478 RECORD(EXPR_SIZEOF_ALIGN_OF);
479 RECORD(EXPR_ARRAY_SUBSCRIPT);
480 RECORD(EXPR_CALL);
481 RECORD(EXPR_MEMBER);
482 RECORD(EXPR_BINARY_OPERATOR);
483 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
484 RECORD(EXPR_CONDITIONAL_OPERATOR);
485 RECORD(EXPR_IMPLICIT_CAST);
486 RECORD(EXPR_CSTYLE_CAST);
487 RECORD(EXPR_COMPOUND_LITERAL);
488 RECORD(EXPR_EXT_VECTOR_ELEMENT);
489 RECORD(EXPR_INIT_LIST);
490 RECORD(EXPR_DESIGNATED_INIT);
491 RECORD(EXPR_IMPLICIT_VALUE_INIT);
492 RECORD(EXPR_VA_ARG);
493 RECORD(EXPR_ADDR_LABEL);
494 RECORD(EXPR_STMT);
495 RECORD(EXPR_TYPES_COMPATIBLE);
496 RECORD(EXPR_CHOOSE);
497 RECORD(EXPR_GNU_NULL);
498 RECORD(EXPR_SHUFFLE_VECTOR);
499 RECORD(EXPR_BLOCK);
500 RECORD(EXPR_BLOCK_DECL_REF);
501 RECORD(EXPR_OBJC_STRING_LITERAL);
502 RECORD(EXPR_OBJC_ENCODE);
503 RECORD(EXPR_OBJC_SELECTOR_EXPR);
504 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
505 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
506 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
507 RECORD(EXPR_OBJC_KVC_REF_EXPR);
508 RECORD(EXPR_OBJC_MESSAGE_EXPR);
509 RECORD(EXPR_OBJC_SUPER_EXPR);
510 RECORD(STMT_OBJC_FOR_COLLECTION);
511 RECORD(STMT_OBJC_CATCH);
512 RECORD(STMT_OBJC_FINALLY);
513 RECORD(STMT_OBJC_AT_TRY);
514 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
515 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000516 RECORD(EXPR_CXX_OPERATOR_CALL);
517 RECORD(EXPR_CXX_CONSTRUCT);
518 RECORD(EXPR_CXX_STATIC_CAST);
519 RECORD(EXPR_CXX_DYNAMIC_CAST);
520 RECORD(EXPR_CXX_REINTERPRET_CAST);
521 RECORD(EXPR_CXX_CONST_CAST);
522 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
523 RECORD(EXPR_CXX_BOOL_LITERAL);
524 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000525#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000526}
Mike Stump11289f42009-09-09 15:08:12 +0000527
Chris Lattner28fa4e62009-04-26 22:26:21 +0000528void PCHWriter::WriteBlockInfoBlock() {
529 RecordData Record;
530 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000531
Chris Lattner64031982009-04-27 00:40:25 +0000532#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000533#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000534
Chris Lattner28fa4e62009-04-26 22:26:21 +0000535 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000536 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000537 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000538 RECORD(TYPE_OFFSET);
539 RECORD(DECL_OFFSET);
540 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000541 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000542 RECORD(IDENTIFIER_OFFSET);
543 RECORD(IDENTIFIER_TABLE);
544 RECORD(EXTERNAL_DEFINITIONS);
545 RECORD(SPECIAL_TYPES);
546 RECORD(STATISTICS);
547 RECORD(TENTATIVE_DEFINITIONS);
548 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
549 RECORD(SELECTOR_OFFSETS);
550 RECORD(METHOD_POOL);
551 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000552 RECORD(SOURCE_LOCATION_OFFSETS);
553 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000554 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000555 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000556 RECORD(COMMENT_RANGES);
Ted Kremenek17437132010-01-22 20:59:36 +0000557 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000558
Chris Lattner28fa4e62009-04-26 22:26:21 +0000559 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000560 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000561 RECORD(SM_SLOC_FILE_ENTRY);
562 RECORD(SM_SLOC_BUFFER_ENTRY);
563 RECORD(SM_SLOC_BUFFER_BLOB);
564 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
565 RECORD(SM_LINE_TABLE);
566 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner28fa4e62009-04-26 22:26:21 +0000568 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000569 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000570 RECORD(PP_MACRO_OBJECT_LIKE);
571 RECORD(PP_MACRO_FUNCTION_LIKE);
572 RECORD(PP_TOKEN);
573
Douglas Gregor12bfa382009-10-17 00:13:19 +0000574 // Decls and Types block.
575 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000576 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000577 RECORD(TYPE_COMPLEX);
578 RECORD(TYPE_POINTER);
579 RECORD(TYPE_BLOCK_POINTER);
580 RECORD(TYPE_LVALUE_REFERENCE);
581 RECORD(TYPE_RVALUE_REFERENCE);
582 RECORD(TYPE_MEMBER_POINTER);
583 RECORD(TYPE_CONSTANT_ARRAY);
584 RECORD(TYPE_INCOMPLETE_ARRAY);
585 RECORD(TYPE_VARIABLE_ARRAY);
586 RECORD(TYPE_VECTOR);
587 RECORD(TYPE_EXT_VECTOR);
588 RECORD(TYPE_FUNCTION_PROTO);
589 RECORD(TYPE_FUNCTION_NO_PROTO);
590 RECORD(TYPE_TYPEDEF);
591 RECORD(TYPE_TYPEOF_EXPR);
592 RECORD(TYPE_TYPEOF);
593 RECORD(TYPE_RECORD);
594 RECORD(TYPE_ENUM);
595 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000596 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000597 RECORD(DECL_ATTR);
598 RECORD(DECL_TRANSLATION_UNIT);
599 RECORD(DECL_TYPEDEF);
600 RECORD(DECL_ENUM);
601 RECORD(DECL_RECORD);
602 RECORD(DECL_ENUM_CONSTANT);
603 RECORD(DECL_FUNCTION);
604 RECORD(DECL_OBJC_METHOD);
605 RECORD(DECL_OBJC_INTERFACE);
606 RECORD(DECL_OBJC_PROTOCOL);
607 RECORD(DECL_OBJC_IVAR);
608 RECORD(DECL_OBJC_AT_DEFS_FIELD);
609 RECORD(DECL_OBJC_CLASS);
610 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
611 RECORD(DECL_OBJC_CATEGORY);
612 RECORD(DECL_OBJC_CATEGORY_IMPL);
613 RECORD(DECL_OBJC_IMPLEMENTATION);
614 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
615 RECORD(DECL_OBJC_PROPERTY);
616 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000617 RECORD(DECL_FIELD);
618 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000619 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000620 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000621 RECORD(DECL_FILE_SCOPE_ASM);
622 RECORD(DECL_BLOCK);
623 RECORD(DECL_CONTEXT_LEXICAL);
624 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000625 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000626 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000627#undef RECORD
628#undef BLOCK
629 Stream.ExitBlock();
630}
631
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000632/// \brief Adjusts the given filename to only write out the portion of the
633/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000634///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000635/// \param Filename the file name to adjust.
636///
637/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
638/// the returned filename will be adjusted by this system root.
639///
640/// \returns either the original filename (if it needs no adjustment) or the
641/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000642static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000643adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
644 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000645
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000646 if (!isysroot)
647 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000648
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000649 // Verify that the filename and the system root have the same prefix.
650 unsigned Pos = 0;
651 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
652 if (Filename[Pos] != isysroot[Pos])
653 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000654
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000655 // We hit the end of the filename before we hit the end of the system root.
656 if (!Filename[Pos])
657 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000658
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000659 // If the file name has a '/' at the current position, skip over the '/'.
660 // We distinguish sysroot-based includes from absolute includes by the
661 // absence of '/' at the beginning of sysroot-based includes.
662 if (Filename[Pos] == '/')
663 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000664
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000665 return Filename + Pos;
666}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000667
Douglas Gregor7b71e632009-04-27 22:23:34 +0000668/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000669void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000670 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000671
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000672 // Metadata
673 const TargetInfo &Target = Context.Target;
674 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
675 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
676 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
677 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
678 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
679 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
680 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
681 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
682 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000683
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000684 RecordData Record;
685 Record.push_back(pch::METADATA);
686 Record.push_back(pch::VERSION_MAJOR);
687 Record.push_back(pch::VERSION_MINOR);
688 Record.push_back(CLANG_VERSION_MAJOR);
689 Record.push_back(CLANG_VERSION_MINOR);
690 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000691 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000692 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000693
Douglas Gregor45fe0362009-05-12 01:31:05 +0000694 // Original file name
695 SourceManager &SM = Context.getSourceManager();
696 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
697 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
698 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
699 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
700 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
701
702 llvm::sys::Path MainFilePath(MainFile->getName());
703 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000704
Douglas Gregor45fe0362009-05-12 01:31:05 +0000705 if (!MainFilePath.isAbsolute()) {
706 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000707 P.appendComponent(MainFilePath.str());
708 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000709 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000710 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000711 }
712
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000713 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000714 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000715 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000716 RecordData Record;
717 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000718 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000719 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000720
Ted Kremenek18e066f2010-01-22 22:12:47 +0000721 // Repository branch/version information.
722 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
723 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
724 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
725 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000726 Record.clear();
Ted Kremenek17437132010-01-22 20:59:36 +0000727 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000728 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
729 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000730}
731
732/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000733void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
734 RecordData Record;
735 Record.push_back(LangOpts.Trigraphs);
736 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
737 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
738 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
739 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
740 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
741 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
742 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
743 Record.push_back(LangOpts.C99); // C99 Support
744 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
745 Record.push_back(LangOpts.CPlusPlus); // C++ Support
746 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000747 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000748
Douglas Gregor55abb232009-04-10 20:39:37 +0000749 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
750 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Fariborz Jahanian45878032010-02-09 19:31:38 +0000751 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
752 // modern abi enabled.
753 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
754 // modern abi enabled.
Mike Stump11289f42009-09-09 15:08:12 +0000755
Douglas Gregor55abb232009-04-10 20:39:37 +0000756 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000757 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
758 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000759 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000760 Record.push_back(LangOpts.Exceptions); // Support exception handling.
761
762 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
763 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
764 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
765
Chris Lattner258172e2009-04-27 07:35:58 +0000766 // Whether static initializers are protected by locks.
767 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000768 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000769 Record.push_back(LangOpts.Blocks); // block extension to C
770 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
771 // they are unused.
772 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
773 // (modulo the platform support).
774
775 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
776 // signed integer arithmetic overflows.
777
778 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
779 // may be ripped out at any time.
780
781 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000782 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000783 // defined.
784 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
785 // opposed to __DYNAMIC__).
786 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
787
788 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
789 // used (instead of C99 semantics).
790 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000791 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
792 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000793 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
794 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000795 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000796 Record.push_back(LangOpts.getGCMode());
797 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000798 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000799 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000800 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000801 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000802 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000803 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000804}
805
Douglas Gregora7f71a92009-04-10 03:52:48 +0000806//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000807// stat cache Serialization
808//===----------------------------------------------------------------------===//
809
810namespace {
811// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000812class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000813public:
814 typedef const char * key_type;
815 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000816
Douglas Gregorc5046832009-04-27 18:38:38 +0000817 typedef std::pair<int, struct stat> data_type;
818 typedef const data_type& data_type_ref;
819
820 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000821 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
824 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000825 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
826 data_type_ref Data) {
827 unsigned StrLen = strlen(path);
828 clang::io::Emit16(Out, StrLen);
829 unsigned DataLen = 1; // result value
830 if (Data.first == 0)
831 DataLen += 4 + 4 + 2 + 8 + 8;
832 clang::io::Emit8(Out, DataLen);
833 return std::make_pair(StrLen + 1, DataLen);
834 }
Mike Stump11289f42009-09-09 15:08:12 +0000835
Douglas Gregorc5046832009-04-27 18:38:38 +0000836 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
837 Out.write(path, KeyLen);
838 }
Mike Stump11289f42009-09-09 15:08:12 +0000839
Douglas Gregorc5046832009-04-27 18:38:38 +0000840 void EmitData(llvm::raw_ostream& Out, key_type_ref,
841 data_type_ref Data, unsigned DataLen) {
842 using namespace clang::io;
843 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000844
Douglas Gregorc5046832009-04-27 18:38:38 +0000845 // Result of stat()
846 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000847
Douglas Gregorc5046832009-04-27 18:38:38 +0000848 if (Data.first == 0) {
849 Emit32(Out, (uint32_t) Data.second.st_ino);
850 Emit32(Out, (uint32_t) Data.second.st_dev);
851 Emit16(Out, (uint16_t) Data.second.st_mode);
852 Emit64(Out, (uint64_t) Data.second.st_mtime);
853 Emit64(Out, (uint64_t) Data.second.st_size);
854 }
855
856 assert(Out.tell() - Start == DataLen && "Wrong data length");
857 }
858};
859} // end anonymous namespace
860
861/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000862void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
863 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000864 // Build the on-disk hash table containing information about every
865 // stat() call.
866 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
867 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000868 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000869 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000870 Stat != StatEnd; ++Stat, ++NumStatEntries) {
871 const char *Filename = Stat->first();
872 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
873 Generator.insert(Filename, Stat->second);
874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875
Douglas Gregorc5046832009-04-27 18:38:38 +0000876 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000877 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000878 uint32_t BucketOffset;
879 {
880 llvm::raw_svector_ostream Out(StatCacheData);
881 // Make sure that no bucket is at offset 0
882 clang::io::Emit32(Out, 0);
883 BucketOffset = Generator.Emit(Out);
884 }
885
886 // Create a blob abbreviation
887 using namespace llvm;
888 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
889 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
891 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
892 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
893 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
894
895 // Write the stat cache
896 RecordData Record;
897 Record.push_back(pch::STAT_CACHE);
898 Record.push_back(BucketOffset);
899 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000900 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000901}
902
903//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000904// Source Manager Serialization
905//===----------------------------------------------------------------------===//
906
907/// \brief Create an abbreviation for the SLocEntry that refers to a
908/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000909static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000910 using namespace llvm;
911 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
912 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000918 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000919}
920
921/// \brief Create an abbreviation for the SLocEntry that refers to a
922/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000923static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000924 using namespace llvm;
925 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
926 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000932 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000933}
934
935/// \brief Create an abbreviation for the SLocEntry that refers to a
936/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000937static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000938 using namespace llvm;
939 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
940 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000942 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000943}
944
945/// \brief Create an abbreviation for the SLocEntry that refers to an
946/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000947static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000948 using namespace llvm;
949 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
950 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000956 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000957}
958
959/// \brief Writes the block containing the serialized form of the
960/// source manager.
961///
962/// TODO: We should probably use an on-disk hash table (stored in a
963/// blob), indexed based on the file name, so that we only create
964/// entries for files that we actually need. In the common case (no
965/// errors), we probably won't have to create file entries for any of
966/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000967void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000968 const Preprocessor &PP,
969 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000970 RecordData Record;
971
Chris Lattner0910e3b2009-04-10 17:16:57 +0000972 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000973 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000974
975 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000976 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
977 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
978 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
979 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000980
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000981 // Write the line table.
982 if (SourceMgr.hasLineTable()) {
983 LineTableInfo &LineTable = SourceMgr.getLineTable();
984
985 // Emit the file names
986 Record.push_back(LineTable.getNumFilenames());
987 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
988 // Emit the file name
989 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000990 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000991 unsigned FilenameLen = Filename? strlen(Filename) : 0;
992 Record.push_back(FilenameLen);
993 if (FilenameLen)
994 Record.insert(Record.end(), Filename, Filename + FilenameLen);
995 }
Mike Stump11289f42009-09-09 15:08:12 +0000996
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000997 // Emit the line entries
998 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
999 L != LEnd; ++L) {
1000 // Emit the file ID
1001 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001002
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001003 // Emit the line entries
1004 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001005 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001006 LEEnd = L->second.end();
1007 LE != LEEnd; ++LE) {
1008 Record.push_back(LE->FileOffset);
1009 Record.push_back(LE->LineNo);
1010 Record.push_back(LE->FilenameID);
1011 Record.push_back((unsigned)LE->FileKind);
1012 Record.push_back(LE->IncludeOffset);
1013 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001014 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +00001015 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001016 }
1017
Douglas Gregor258ae542009-04-27 06:38:32 +00001018 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +00001019 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +00001020 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001021 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +00001022 E = HS.header_file_end();
1023 I != E; ++I) {
1024 Record.push_back(I->isImport);
1025 Record.push_back(I->DirInfo);
1026 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +00001027 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001028 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1029 Record.clear();
1030 }
1031
Douglas Gregor258ae542009-04-27 06:38:32 +00001032 // Write out the source location entry table. We skip the first
1033 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001034 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001035 RecordData PreloadSLocs;
1036 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001037 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1038 // Get this source location entry.
1039 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1040
Douglas Gregor258ae542009-04-27 06:38:32 +00001041 // Record the offset of this source-location entry.
1042 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1043
1044 // Figure out which record code to use.
1045 unsigned Code;
1046 if (SLoc->isFile()) {
1047 if (SLoc->getFile().getContentCache()->Entry)
1048 Code = pch::SM_SLOC_FILE_ENTRY;
1049 else
1050 Code = pch::SM_SLOC_BUFFER_ENTRY;
1051 } else
1052 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1053 Record.clear();
1054 Record.push_back(Code);
1055
1056 Record.push_back(SLoc->getOffset());
1057 if (SLoc->isFile()) {
1058 const SrcMgr::FileInfo &File = SLoc->getFile();
1059 Record.push_back(File.getIncludeLoc().getRawEncoding());
1060 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1061 Record.push_back(File.hasLineDirectives());
1062
1063 const SrcMgr::ContentCache *Content = File.getContentCache();
1064 if (Content->Entry) {
1065 // The source location entry is a file. The blob associated
1066 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001067
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001068 // Turn the file name into an absolute path, if it isn't already.
1069 const char *Filename = Content->Entry->getName();
1070 llvm::sys::Path FilePath(Filename, strlen(Filename));
1071 std::string FilenameStr;
1072 if (!FilePath.isAbsolute()) {
1073 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001074 P.appendComponent(FilePath.str());
1075 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001076 Filename = FilenameStr.c_str();
1077 }
Mike Stump11289f42009-09-09 15:08:12 +00001078
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001079 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001080 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001081
1082 // FIXME: For now, preload all file source locations, so that
1083 // we get the appropriate File entries in the reader. This is
1084 // a temporary measure.
1085 PreloadSLocs.push_back(SLocEntryOffsets.size());
1086 } else {
1087 // The source location entry is a buffer. The blob associated
1088 // with this entry contains the contents of the buffer.
1089
1090 // We add one to the size so that we capture the trailing NULL
1091 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1092 // the reader side).
1093 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1094 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001095 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1096 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001097 Record.clear();
1098 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1099 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001100 llvm::StringRef(Buffer->getBufferStart(),
1101 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001102
1103 if (strcmp(Name, "<built-in>") == 0)
1104 PreloadSLocs.push_back(SLocEntryOffsets.size());
1105 }
1106 } else {
1107 // The source location entry is an instantiation.
1108 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1109 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1110 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1111 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1112
1113 // Compute the token length for this macro expansion.
1114 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001115 if (I + 1 != N)
1116 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001117 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1118 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1119 }
1120 }
1121
Douglas Gregor8f45df52009-04-16 22:23:12 +00001122 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001123
1124 if (SLocEntryOffsets.empty())
1125 return;
1126
1127 // Write the source-location offsets table into the PCH block. This
1128 // table is used for lazily loading source-location information.
1129 using namespace llvm;
1130 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1131 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1135 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001136
Douglas Gregor258ae542009-04-27 06:38:32 +00001137 Record.clear();
1138 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1139 Record.push_back(SLocEntryOffsets.size());
1140 Record.push_back(SourceMgr.getNextOffset());
1141 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001142 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001143 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001144
1145 // Write the source location entry preloads array, telling the PCH
1146 // reader which source locations entries it should load eagerly.
1147 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001148}
1149
Douglas Gregorc5046832009-04-27 18:38:38 +00001150//===----------------------------------------------------------------------===//
1151// Preprocessor Serialization
1152//===----------------------------------------------------------------------===//
1153
Chris Lattnereeffaef2009-04-10 17:15:23 +00001154/// \brief Writes the block containing the serialized form of the
1155/// preprocessor.
1156///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001157void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001158 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001159
Chris Lattner0af3ba12009-04-13 01:29:17 +00001160 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1161 if (PP.getCounterValue() != 0) {
1162 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001163 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001164 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001165 }
1166
1167 // Enter the preprocessor block.
1168 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001169
Douglas Gregoreda6a892009-04-26 00:07:37 +00001170 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1171 // FIXME: use diagnostics subsystem for localization etc.
1172 if (PP.SawDateOrTime())
1173 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001175 // Loop over all the macro definitions that are live at the end of the file,
1176 // emitting each to the PP section.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001177 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1178 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001179 // FIXME: This emits macros in hash table order, we should do it in a stable
1180 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001181 MacroInfo *MI = I->second;
1182
1183 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1184 // been redefined by the header (in which case they are not isBuiltinMacro).
1185 if (MI->isBuiltinMacro())
1186 continue;
1187
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001188 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001189 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001190 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1191 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001192
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001193 unsigned Code;
1194 if (MI->isObjectLike()) {
1195 Code = pch::PP_MACRO_OBJECT_LIKE;
1196 } else {
1197 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001199 Record.push_back(MI->isC99Varargs());
1200 Record.push_back(MI->isGNUVarargs());
1201 Record.push_back(MI->getNumArgs());
1202 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1203 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001204 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001205 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001206 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001207 Record.clear();
1208
Chris Lattner2199f5b2009-04-10 18:08:30 +00001209 // Emit the tokens array.
1210 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1211 // Note that we know that the preprocessor does not have any annotation
1212 // tokens in it because they are created by the parser, and thus can't be
1213 // in a macro definition.
1214 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001215
Chris Lattner2199f5b2009-04-10 18:08:30 +00001216 Record.push_back(Tok.getLocation().getRawEncoding());
1217 Record.push_back(Tok.getLength());
1218
Chris Lattner2199f5b2009-04-10 18:08:30 +00001219 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1220 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001221 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001222
Chris Lattner2199f5b2009-04-10 18:08:30 +00001223 // FIXME: Should translate token kind to a stable encoding.
1224 Record.push_back(Tok.getKind());
1225 // FIXME: Should translate token flags to a stable encoding.
1226 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001227
Douglas Gregor8f45df52009-04-16 22:23:12 +00001228 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001229 Record.clear();
1230 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001231 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001232 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001233 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001234}
1235
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001236void PCHWriter::WriteComments(ASTContext &Context) {
1237 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001238
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001239 if (Context.Comments.empty())
1240 return;
Mike Stump11289f42009-09-09 15:08:12 +00001241
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001242 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1243 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1244 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1245 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001246
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001247 RecordData Record;
1248 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001249 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001250 (const char*)&Context.Comments[0],
1251 Context.Comments.size() * sizeof(SourceRange));
1252}
1253
Douglas Gregorc5046832009-04-27 18:38:38 +00001254//===----------------------------------------------------------------------===//
1255// Type Serialization
1256//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001257
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001258/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001259void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001260 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001261 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001262 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001263
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001264 // Record the offset for this type.
1265 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001266 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001267 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1268 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001269 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001270 }
1271
1272 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001273
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001274 // Emit the type's representation.
1275 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001276
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001277 if (T.hasLocalNonFastQualifiers()) {
1278 Qualifiers Qs = T.getLocalQualifiers();
1279 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001280 Record.push_back(Qs.getAsOpaqueValue());
1281 W.Code = pch::TYPE_EXT_QUAL;
1282 } else {
1283 switch (T->getTypeClass()) {
1284 // For all of the concrete, non-dependent types, call the
1285 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001286#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001287 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001288#define ABSTRACT_TYPE(Class, Base)
1289#define DEPENDENT_TYPE(Class, Base)
1290#include "clang/AST/TypeNodes.def"
1291
John McCall8ccfcb52009-09-24 19:53:00 +00001292 // For all of the dependent type nodes (which only occur in C++
1293 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001294#define TYPE(Class, Base)
1295#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1296#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001297 assert(false && "Cannot serialize dependent type nodes");
1298 break;
1299 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001300 }
1301
1302 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001303 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001304
1305 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001306 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001307}
1308
Douglas Gregorc5046832009-04-27 18:38:38 +00001309//===----------------------------------------------------------------------===//
1310// Declaration Serialization
1311//===----------------------------------------------------------------------===//
1312
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001313/// \brief Write the block containing all of the declaration IDs
1314/// lexically declared within the given DeclContext.
1315///
1316/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1317/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001318uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001319 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001320 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001321 return 0;
1322
Douglas Gregor8f45df52009-04-16 22:23:12 +00001323 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001324 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001325 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1326 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001327 AddDeclRef(*D, Record);
1328
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001329 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001330 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001331 return Offset;
1332}
1333
1334/// \brief Write the block containing all of the declaration IDs
1335/// visible from the given DeclContext.
1336///
1337/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1338/// bistream, or 0 if no block was written.
1339uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1340 DeclContext *DC) {
1341 if (DC->getPrimaryContext() != DC)
1342 return 0;
1343
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001344 // Since there is no name lookup into functions or methods, and we
1345 // perform name lookup for the translation unit via the
1346 // IdentifierInfo chains, don't bother to build a
1347 // visible-declarations table for these entities.
1348 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001349 return 0;
1350
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001351 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001352 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001353
1354 // Serialize the contents of the mapping used for lookup. Note that,
1355 // although we have two very different code paths, the serialized
1356 // representation is the same for both cases: a declaration name,
1357 // followed by a size, followed by references to the visible
1358 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001359 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001360 RecordData Record;
1361 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001362 if (!Map)
1363 return 0;
1364
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001365 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1366 D != DEnd; ++D) {
1367 AddDeclarationName(D->first, Record);
1368 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1369 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001370 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001371 AddDeclRef(*Result.first, Record);
1372 }
1373
1374 if (Record.size() == 0)
1375 return 0;
1376
Douglas Gregor8f45df52009-04-16 22:23:12 +00001377 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001378 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001379 return Offset;
1380}
1381
Douglas Gregorc5046832009-04-27 18:38:38 +00001382//===----------------------------------------------------------------------===//
1383// Global Method Pool and Selector Serialization
1384//===----------------------------------------------------------------------===//
1385
Douglas Gregore84a9da2009-04-20 20:36:09 +00001386namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001387// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001388class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001389 PCHWriter &Writer;
1390
1391public:
1392 typedef Selector key_type;
1393 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001394
Douglas Gregorc78d3462009-04-24 21:10:55 +00001395 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1396 typedef const data_type& data_type_ref;
1397
1398 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001399
Douglas Gregorc78d3462009-04-24 21:10:55 +00001400 static unsigned ComputeHash(Selector Sel) {
1401 unsigned N = Sel.getNumArgs();
1402 if (N == 0)
1403 ++N;
1404 unsigned R = 5381;
1405 for (unsigned I = 0; I != N; ++I)
1406 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001407 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001408 return R;
1409 }
Mike Stump11289f42009-09-09 15:08:12 +00001410
1411 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001412 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1413 data_type_ref Methods) {
1414 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1415 clang::io::Emit16(Out, KeyLen);
1416 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001417 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001418 Method = Method->Next)
1419 if (Method->Method)
1420 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001421 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001422 Method = Method->Next)
1423 if (Method->Method)
1424 DataLen += 4;
1425 clang::io::Emit16(Out, DataLen);
1426 return std::make_pair(KeyLen, DataLen);
1427 }
Mike Stump11289f42009-09-09 15:08:12 +00001428
Douglas Gregor95c13f52009-04-25 17:48:32 +00001429 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001430 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001431 assert((Start >> 32) == 0 && "Selector key offset too large");
1432 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001433 unsigned N = Sel.getNumArgs();
1434 clang::io::Emit16(Out, N);
1435 if (N == 0)
1436 N = 1;
1437 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001438 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001439 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1440 }
Mike Stump11289f42009-09-09 15:08:12 +00001441
Douglas Gregorc78d3462009-04-24 21:10:55 +00001442 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001443 data_type_ref Methods, unsigned DataLen) {
1444 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001445 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001446 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001447 Method = Method->Next)
1448 if (Method->Method)
1449 ++NumInstanceMethods;
1450
1451 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001452 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001453 Method = Method->Next)
1454 if (Method->Method)
1455 ++NumFactoryMethods;
1456
1457 clang::io::Emit16(Out, NumInstanceMethods);
1458 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001459 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001460 Method = Method->Next)
1461 if (Method->Method)
1462 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001463 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001464 Method = Method->Next)
1465 if (Method->Method)
1466 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001467
1468 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001469 }
1470};
1471} // end anonymous namespace
1472
1473/// \brief Write the method pool into the PCH file.
1474///
1475/// The method pool contains both instance and factory methods, stored
1476/// in an on-disk hash table indexed by the selector.
1477void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1478 using namespace llvm;
1479
1480 // Create and write out the blob that contains the instance and
1481 // factor method pools.
1482 bool Empty = true;
1483 {
1484 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001485
Douglas Gregorc78d3462009-04-24 21:10:55 +00001486 // Create the on-disk hash table representation. Start by
1487 // iterating through the instance method pool.
1488 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001489 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001490 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001491 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001492 InstanceEnd = SemaRef.InstanceMethodPool.end();
1493 Instance != InstanceEnd; ++Instance) {
1494 // Check whether there is a factory method with the same
1495 // selector.
1496 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1497 = SemaRef.FactoryMethodPool.find(Instance->first);
1498
1499 if (Factory == SemaRef.FactoryMethodPool.end())
1500 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001501 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001502 ObjCMethodList()));
1503 else
1504 Generator.insert(Instance->first,
1505 std::make_pair(Instance->second, Factory->second));
1506
Douglas Gregor95c13f52009-04-25 17:48:32 +00001507 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001508 Empty = false;
1509 }
1510
1511 // Now iterate through the factory method pool, to pick up any
1512 // selectors that weren't already in the instance method pool.
1513 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001514 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001515 FactoryEnd = SemaRef.FactoryMethodPool.end();
1516 Factory != FactoryEnd; ++Factory) {
1517 // Check whether there is an instance method with the same
1518 // selector. If so, there is no work to do here.
1519 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1520 = SemaRef.InstanceMethodPool.find(Factory->first);
1521
Douglas Gregor95c13f52009-04-25 17:48:32 +00001522 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001523 Generator.insert(Factory->first,
1524 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001525 ++NumSelectorsInMethodPool;
1526 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001527
1528 Empty = false;
1529 }
1530
Douglas Gregor95c13f52009-04-25 17:48:32 +00001531 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001532 return;
1533
1534 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001535 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001536 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001537 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001538 {
1539 PCHMethodPoolTrait Trait(*this);
1540 llvm::raw_svector_ostream Out(MethodPool);
1541 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001542 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001543 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001544
1545 // For every selector that we have seen but which was not
1546 // written into the hash table, write the selector itself and
1547 // record it's offset.
1548 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1549 if (SelectorOffsets[I] == 0)
1550 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001551 }
1552
1553 // Create a blob abbreviation
1554 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1555 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1559 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1560
Douglas Gregor95c13f52009-04-25 17:48:32 +00001561 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001562 RecordData Record;
1563 Record.push_back(pch::METHOD_POOL);
1564 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001565 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001566 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001567
1568 // Create a blob abbreviation for the selector table offsets.
1569 Abbrev = new BitCodeAbbrev();
1570 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1573 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1574
1575 // Write the selector offsets table.
1576 Record.clear();
1577 Record.push_back(pch::SELECTOR_OFFSETS);
1578 Record.push_back(SelectorOffsets.size());
1579 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1580 (const char *)&SelectorOffsets.front(),
1581 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001582 }
1583}
1584
Douglas Gregorc5046832009-04-27 18:38:38 +00001585//===----------------------------------------------------------------------===//
1586// Identifier Table Serialization
1587//===----------------------------------------------------------------------===//
1588
Douglas Gregorc78d3462009-04-24 21:10:55 +00001589namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001590class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001591 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001592 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001593
Douglas Gregor1d583f22009-04-28 21:18:29 +00001594 /// \brief Determines whether this is an "interesting" identifier
1595 /// that needs a full IdentifierInfo structure written into the hash
1596 /// table.
1597 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1598 return II->isPoisoned() ||
1599 II->isExtensionToken() ||
1600 II->hasMacroDefinition() ||
1601 II->getObjCOrBuiltinID() ||
1602 II->getFETokenInfo<void>();
1603 }
1604
Douglas Gregore84a9da2009-04-20 20:36:09 +00001605public:
1606 typedef const IdentifierInfo* key_type;
1607 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001608
Douglas Gregore84a9da2009-04-20 20:36:09 +00001609 typedef pch::IdentID data_type;
1610 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001611
1612 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001613 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001614
1615 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001616 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001617 }
Mike Stump11289f42009-09-09 15:08:12 +00001618
1619 std::pair<unsigned,unsigned>
1620 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001621 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001622 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001623 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1624 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001625 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001626 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001627 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001628 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001629 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1630 DEnd = IdentifierResolver::end();
1631 D != DEnd; ++D)
1632 DataLen += sizeof(pch::DeclID);
1633 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001634 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001635 // We emit the key length after the data length so that every
1636 // string is preceded by a 16-bit length. This matches the PTH
1637 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001638 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001639 return std::make_pair(KeyLen, DataLen);
1640 }
Mike Stump11289f42009-09-09 15:08:12 +00001641
1642 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001643 unsigned KeyLen) {
1644 // Record the location of the key data. This is used when generating
1645 // the mapping from persistent IDs to strings.
1646 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001647 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001648 }
Mike Stump11289f42009-09-09 15:08:12 +00001649
1650 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001651 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001652 if (!isInterestingIdentifier(II)) {
1653 clang::io::Emit32(Out, ID << 1);
1654 return;
1655 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001656
Douglas Gregor1d583f22009-04-28 21:18:29 +00001657 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001658 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001659 bool hasMacroDefinition =
1660 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001661 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001662 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001663 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1664 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1665 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1666 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001667 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001668
Douglas Gregorc3366a52009-04-21 23:56:24 +00001669 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001670 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001671
Douglas Gregora868bbd2009-04-21 22:25:48 +00001672 // Emit the declaration IDs in reverse order, because the
1673 // IdentifierResolver provides the declarations as they would be
1674 // visible (e.g., the function "stat" would come before the struct
1675 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1676 // adds declarations to the end of the list (so we need to see the
1677 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001678 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001679 IdentifierResolver::end());
1680 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1681 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001682 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001683 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001684 }
1685};
1686} // end anonymous namespace
1687
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001688/// \brief Write the identifier table into the PCH file.
1689///
1690/// The identifier table consists of a blob containing string data
1691/// (the actual identifiers themselves) and a separate "offsets" index
1692/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001693void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001694 using namespace llvm;
1695
1696 // Create and write out the blob that contains the identifier
1697 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001698 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001699 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001700
Douglas Gregore6648fb2009-04-28 20:33:11 +00001701 // Look for any identifiers that were named while processing the
1702 // headers, but are otherwise not needed. We add these to the hash
1703 // table to enable checking of the predefines buffer in the case
1704 // where the user adds new macro definitions when building the PCH
1705 // file.
1706 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1707 IDEnd = PP.getIdentifierTable().end();
1708 ID != IDEnd; ++ID)
1709 getIdentifierRef(ID->second);
1710
Douglas Gregore84a9da2009-04-20 20:36:09 +00001711 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001712 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001713 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1714 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1715 ID != IDEnd; ++ID) {
1716 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001717 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001718 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001719
Douglas Gregore84a9da2009-04-20 20:36:09 +00001720 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001721 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001722 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001723 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001724 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001725 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001726 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001727 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001728 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001729 }
1730
1731 // Create a blob abbreviation
1732 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1733 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001736 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001737
1738 // Write the identifier table
1739 RecordData Record;
1740 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001741 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001742 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001743 }
1744
1745 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001746 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1747 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1750 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1751
1752 RecordData Record;
1753 Record.push_back(pch::IDENTIFIER_OFFSET);
1754 Record.push_back(IdentifierOffsets.size());
1755 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1756 (const char *)&IdentifierOffsets.front(),
1757 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001758}
1759
Douglas Gregorc5046832009-04-27 18:38:38 +00001760//===----------------------------------------------------------------------===//
1761// General Serialization Routines
1762//===----------------------------------------------------------------------===//
1763
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001764/// \brief Write a record containing the given attributes.
1765void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1766 RecordData Record;
1767 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001768 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001769 Record.push_back(Attr->isInherited());
1770 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001771 default:
1772 assert(0 && "Does not support PCH writing for this attribute yet!");
1773 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001774 case Attr::Alias:
1775 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1776 break;
1777
1778 case Attr::Aligned:
1779 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1780 break;
1781
1782 case Attr::AlwaysInline:
1783 break;
Mike Stump11289f42009-09-09 15:08:12 +00001784
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001785 case Attr::AnalyzerNoReturn:
1786 break;
1787
1788 case Attr::Annotate:
1789 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1790 break;
1791
1792 case Attr::AsmLabel:
1793 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1794 break;
1795
Alexis Hunt54a02542009-11-25 04:20:27 +00001796 case Attr::BaseCheck:
1797 break;
1798
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001799 case Attr::Blocks:
1800 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1801 break;
1802
Eli Friedmane4310c82009-11-09 18:38:53 +00001803 case Attr::CDecl:
1804 break;
1805
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001806 case Attr::Cleanup:
1807 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1808 break;
1809
1810 case Attr::Const:
1811 break;
1812
1813 case Attr::Constructor:
1814 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1815 break;
1816
1817 case Attr::DLLExport:
1818 case Attr::DLLImport:
1819 case Attr::Deprecated:
1820 break;
1821
1822 case Attr::Destructor:
1823 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1824 break;
1825
1826 case Attr::FastCall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001827 case Attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001828 break;
1829
1830 case Attr::Format: {
1831 const FormatAttr *Format = cast<FormatAttr>(Attr);
1832 AddString(Format->getType(), Record);
1833 Record.push_back(Format->getFormatIdx());
1834 Record.push_back(Format->getFirstArg());
1835 break;
1836 }
1837
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001838 case Attr::FormatArg: {
1839 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1840 Record.push_back(Format->getFormatIdx());
1841 break;
1842 }
1843
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001844 case Attr::Sentinel : {
1845 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1846 Record.push_back(Sentinel->getSentinel());
1847 Record.push_back(Sentinel->getNullPos());
1848 break;
1849 }
Mike Stump11289f42009-09-09 15:08:12 +00001850
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001851 case Attr::GNUInline:
Alexis Hunt54a02542009-11-25 04:20:27 +00001852 case Attr::Hiding:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001853 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001854 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001855 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001856 case Attr::NoReturn:
1857 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001858 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001859 break;
1860
1861 case Attr::NonNull: {
1862 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1863 Record.push_back(NonNull->size());
1864 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1865 break;
1866 }
1867
1868 case Attr::ObjCException:
1869 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001870 case Attr::CFReturnsRetained:
1871 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001872 case Attr::Overloadable:
Alexis Hunt54a02542009-11-25 04:20:27 +00001873 case Attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001874 break;
1875
Anders Carlsson68e0b682009-08-08 18:23:56 +00001876 case Attr::PragmaPack:
1877 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001878 break;
1879
Anders Carlsson68e0b682009-08-08 18:23:56 +00001880 case Attr::Packed:
1881 break;
Mike Stump11289f42009-09-09 15:08:12 +00001882
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001883 case Attr::Pure:
1884 break;
1885
1886 case Attr::Regparm:
1887 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1888 break;
Mike Stump11289f42009-09-09 15:08:12 +00001889
Nate Begemanf2758702009-06-26 06:32:41 +00001890 case Attr::ReqdWorkGroupSize:
1891 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1892 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1893 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1894 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001895
1896 case Attr::Section:
1897 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1898 break;
1899
1900 case Attr::StdCall:
1901 case Attr::TransparentUnion:
1902 case Attr::Unavailable:
1903 case Attr::Unused:
1904 case Attr::Used:
1905 break;
1906
1907 case Attr::Visibility:
1908 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001909 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001910 break;
1911
1912 case Attr::WarnUnusedResult:
1913 case Attr::Weak:
1914 case Attr::WeakImport:
1915 break;
1916 }
1917 }
1918
Douglas Gregor8f45df52009-04-16 22:23:12 +00001919 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001920}
1921
1922void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1923 Record.push_back(Str.size());
1924 Record.insert(Record.end(), Str.begin(), Str.end());
1925}
1926
Douglas Gregore84a9da2009-04-20 20:36:09 +00001927/// \brief Note that the identifier II occurs at the given offset
1928/// within the identifier table.
1929void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001930 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001931}
1932
Douglas Gregor95c13f52009-04-25 17:48:32 +00001933/// \brief Note that the selector Sel occurs at the given offset
1934/// within the method pool/selector table.
1935void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1936 unsigned ID = SelectorIDs[Sel];
1937 assert(ID && "Unknown selector");
1938 SelectorOffsets[ID - 1] = Offset;
1939}
1940
Mike Stump11289f42009-09-09 15:08:12 +00001941PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1942 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001943 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1944 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001945
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001946void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1947 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001948 using namespace llvm;
1949
Douglas Gregor162dd022009-04-20 15:53:59 +00001950 ASTContext &Context = SemaRef.Context;
1951 Preprocessor &PP = SemaRef.PP;
1952
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001953 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001954 Stream.Emit((unsigned)'C', 8);
1955 Stream.Emit((unsigned)'P', 8);
1956 Stream.Emit((unsigned)'C', 8);
1957 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001958
Chris Lattner28fa4e62009-04-26 22:26:21 +00001959 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001960
1961 // The translation unit is the first declaration we'll emit.
1962 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001963 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001964
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001965 // Make sure that we emit IdentifierInfos (and any attached
1966 // declarations) for builtins.
1967 {
1968 IdentifierTable &Table = PP.getIdentifierTable();
1969 llvm::SmallVector<const char *, 32> BuiltinNames;
1970 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1971 Context.getLangOptions().NoBuiltin);
1972 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1973 getIdentifierRef(&Table.get(BuiltinNames[I]));
1974 }
1975
Chris Lattner0c797362009-09-08 18:19:27 +00001976 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00001977 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00001978 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001979 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00001980 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
1981 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00001982 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001983
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001984 // Build a record containing all of the locally-scoped external
1985 // declarations in this header file. Generally, this record will be
1986 // empty.
1987 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001988 // FIXME: This is filling in the PCH file in densemap order which is
1989 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001990 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001991 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1992 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1993 TD != TDEnd; ++TD)
1994 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1995
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001996 // Build a record containing all of the ext_vector declarations.
1997 RecordData ExtVectorDecls;
1998 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1999 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2000
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002001 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002002 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00002003 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002004 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00002005 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002006 if (StatCalls && !isysroot)
2007 WriteStatCache(*StatCalls, isysroot);
2008 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00002009 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00002010 // Write the record of special types.
2011 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002012
Steve Naroffc277ad12009-07-18 15:33:26 +00002013 AddTypeRef(Context.getBuiltinVaListType(), Record);
2014 AddTypeRef(Context.getObjCIdType(), Record);
2015 AddTypeRef(Context.getObjCSelType(), Record);
2016 AddTypeRef(Context.getObjCProtoType(), Record);
2017 AddTypeRef(Context.getObjCClassType(), Record);
2018 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2019 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2020 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002021 AddTypeRef(Context.getjmp_bufType(), Record);
2022 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002023 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2024 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002025#if 0
2026 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian04b258c2009-11-25 23:07:42 +00002027 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002028#endif
Mike Stumpd0153282009-10-20 02:12:22 +00002029 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002030 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002031 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002032
Douglas Gregor1970d882009-04-26 03:49:13 +00002033 // Keep writing types and declarations until all types and
2034 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002035 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2036 WriteDeclsBlockAbbrevs();
2037 while (!DeclTypesToEmit.empty()) {
2038 DeclOrType DOT = DeclTypesToEmit.front();
2039 DeclTypesToEmit.pop();
2040 if (DOT.isType())
2041 WriteType(DOT.getType());
2042 else
2043 WriteDecl(Context, DOT.getDecl());
2044 }
2045 Stream.ExitBlock();
2046
Douglas Gregor45053152009-10-17 17:25:45 +00002047 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002048 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002049 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002050
2051 // Write the type offsets array
2052 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2053 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2056 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2057 Record.clear();
2058 Record.push_back(pch::TYPE_OFFSET);
2059 Record.push_back(TypeOffsets.size());
2060 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002061 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002062 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002063
Douglas Gregor745ed142009-04-25 18:35:21 +00002064 // Write the declaration offsets array
2065 Abbrev = new BitCodeAbbrev();
2066 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2069 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2070 Record.clear();
2071 Record.push_back(pch::DECL_OFFSET);
2072 Record.push_back(DeclOffsets.size());
2073 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002074 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002075 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002076
Douglas Gregord4df8652009-04-22 22:02:47 +00002077 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002078 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002079 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002080
2081 // Write the record containing tentative definitions.
2082 if (!TentativeDefinitions.empty())
2083 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002084
2085 // Write the record containing locally-scoped external definitions.
2086 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002087 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002088 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002089
2090 // Write the record containing ext_vector type names.
2091 if (!ExtVectorDecls.empty())
2092 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002093
Douglas Gregor08f01292009-04-17 22:13:46 +00002094 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002095 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002096 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002097 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002098 Record.push_back(NumLexicalDeclContexts);
2099 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002100 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002101 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002102}
2103
2104void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2105 Record.push_back(Loc.getRawEncoding());
2106}
2107
2108void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2109 Record.push_back(Value.getBitWidth());
2110 unsigned N = Value.getNumWords();
2111 const uint64_t* Words = Value.getRawData();
2112 for (unsigned I = 0; I != N; ++I)
2113 Record.push_back(Words[I]);
2114}
2115
Douglas Gregor1daeb692009-04-13 18:14:40 +00002116void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2117 Record.push_back(Value.isUnsigned());
2118 AddAPInt(Value, Record);
2119}
2120
Douglas Gregore0a3a512009-04-14 21:55:33 +00002121void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2122 AddAPInt(Value.bitcastToAPInt(), Record);
2123}
2124
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002125void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002126 Record.push_back(getIdentifierRef(II));
2127}
2128
2129pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2130 if (II == 0)
2131 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002132
2133 pch::IdentID &ID = IdentifierIDs[II];
2134 if (ID == 0)
2135 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002136 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002137}
2138
Steve Naroff2ddea052009-04-23 10:39:46 +00002139void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2140 if (SelRef.getAsOpaquePtr() == 0) {
2141 Record.push_back(0);
2142 return;
2143 }
2144
2145 pch::SelectorID &SID = SelectorIDs[SelRef];
2146 if (SID == 0) {
2147 SID = SelectorIDs.size();
2148 SelVector.push_back(SelRef);
2149 }
2150 Record.push_back(SID);
2151}
2152
John McCall0ad16662009-10-29 08:12:44 +00002153void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2154 RecordData &Record) {
2155 switch (Arg.getArgument().getKind()) {
2156 case TemplateArgument::Expression:
2157 AddStmt(Arg.getLocInfo().getAsExpr());
2158 break;
2159 case TemplateArgument::Type:
John McCallbcd03502009-12-07 02:54:59 +00002160 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002161 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002162 case TemplateArgument::Template:
2163 Record.push_back(
2164 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2165 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2166 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2167 break;
John McCall0ad16662009-10-29 08:12:44 +00002168 case TemplateArgument::Null:
2169 case TemplateArgument::Integral:
2170 case TemplateArgument::Declaration:
2171 case TemplateArgument::Pack:
2172 break;
2173 }
2174}
2175
John McCallbcd03502009-12-07 02:54:59 +00002176void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2177 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002178 AddTypeRef(QualType(), Record);
2179 return;
2180 }
2181
John McCallbcd03502009-12-07 02:54:59 +00002182 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002183 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002184 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00002185 TLW.Visit(TL);
2186}
2187
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002188void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2189 if (T.isNull()) {
2190 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2191 return;
2192 }
2193
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002194 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002195 T.removeFastQualifiers();
2196
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002197 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002198 pch::TypeID &ID = TypeIDs[T];
2199 if (ID == 0) {
2200 // We haven't seen these qualifiers applied to this type before.
2201 // Assign it a new ID. This is the only time we enqueue a
2202 // qualified type, and it has no CV qualifiers.
2203 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002204 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002205 }
2206
2207 // Encode the type qualifiers in the type reference.
2208 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2209 return;
2210 }
2211
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002212 assert(!T.hasLocalQualifiers());
John McCall8ccfcb52009-09-24 19:53:00 +00002213
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002214 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002215 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002216 switch (BT->getKind()) {
2217 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2218 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2219 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2220 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2221 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2222 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2223 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2224 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002225 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002226 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2227 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2228 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2229 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2230 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2231 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2232 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002233 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002234 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2235 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2236 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002237 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002238 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2239 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002240 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2241 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002242 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2243 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002244 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002245 case BuiltinType::UndeducedAuto:
2246 assert(0 && "Should not see undeduced auto here");
2247 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002248 }
2249
John McCall8ccfcb52009-09-24 19:53:00 +00002250 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002251 return;
2252 }
2253
John McCall8ccfcb52009-09-24 19:53:00 +00002254 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002255 if (ID == 0) {
2256 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002257 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002258 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002259 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002260 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002261
2262 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002263 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002264}
2265
2266void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2267 if (D == 0) {
2268 Record.push_back(0);
2269 return;
2270 }
2271
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002272 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002273 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002274 // We haven't seen this declaration before. Give it a new ID and
2275 // enqueue it in the list of declarations to emit.
2276 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002277 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002278 }
2279
2280 Record.push_back(ID);
2281}
2282
Douglas Gregore84a9da2009-04-20 20:36:09 +00002283pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2284 if (D == 0)
2285 return 0;
2286
2287 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2288 return DeclIDs[D];
2289}
2290
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002291void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002292 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002293 Record.push_back(Name.getNameKind());
2294 switch (Name.getNameKind()) {
2295 case DeclarationName::Identifier:
2296 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2297 break;
2298
2299 case DeclarationName::ObjCZeroArgSelector:
2300 case DeclarationName::ObjCOneArgSelector:
2301 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002302 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002303 break;
2304
2305 case DeclarationName::CXXConstructorName:
2306 case DeclarationName::CXXDestructorName:
2307 case DeclarationName::CXXConversionFunctionName:
2308 AddTypeRef(Name.getCXXNameType(), Record);
2309 break;
2310
2311 case DeclarationName::CXXOperatorName:
2312 Record.push_back(Name.getCXXOverloadedOperator());
2313 break;
2314
Alexis Hunt3d221f22009-11-29 07:34:05 +00002315 case DeclarationName::CXXLiteralOperatorName:
2316 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2317 break;
2318
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002319 case DeclarationName::CXXUsingDirective:
2320 // No extra data to emit
2321 break;
2322 }
2323}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002324