blob: b4fbd17bd67c5345b1cae7a1fc95ec9e4d66be93 [file] [log] [blame]
Douglas Gregor2cf26342009-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 Gregore7785042009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump1eb44332009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000033#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000045
Douglas Gregor2cf26342009-04-09 22:27:44 +000046namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000047 class PCHTypeWriter {
Douglas Gregor2cf26342009-04-09 22:27:44 +000048 PCHWriter &Writer;
49 PCHWriter::RecordData &Record;
50
51 public:
52 /// \brief Type code that corresponds to the record generated.
53 pch::TypeCode Code;
54
Mike Stump1eb44332009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
58 void VisitArrayType(const ArrayType *T);
59 void VisitFunctionType(const FunctionType *T);
60 void VisitTagType(const TagType *T);
61
62#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63#define ABSTRACT_TYPE(Class, Base)
64#define DEPENDENT_TYPE(Class, Base)
65#include "clang/AST/TypeNodes.def"
66 };
67}
68
Douglas Gregor2cf26342009-04-09 22:27:44 +000069void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
Douglas Gregor2cf26342009-04-09 22:27:44 +000073void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
74 Writer.AddTypeRef(T->getElementType(), Record);
75 Code = pch::TYPE_COMPLEX;
76}
77
78void PCHTypeWriter::VisitPointerType(const PointerType *T) {
79 Writer.AddTypeRef(T->getPointeeType(), Record);
80 Code = pch::TYPE_POINTER;
81}
82
83void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000084 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000085 Code = pch::TYPE_BLOCK_POINTER;
86}
87
88void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_LVALUE_REFERENCE;
91}
92
93void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_RVALUE_REFERENCE;
96}
97
98void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000099 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101 Code = pch::TYPE_MEMBER_POINTER;
102}
103
104void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
105 Writer.AddTypeRef(T->getElementType(), Record);
106 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000107 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
110void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
111 VisitArrayType(T);
112 Writer.AddAPInt(T->getSize(), Record);
113 Code = pch::TYPE_CONSTANT_ARRAY;
114}
115
116void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
117 VisitArrayType(T);
118 Code = pch::TYPE_INCOMPLETE_ARRAY;
119}
120
121void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
122 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000123 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
124 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000125 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126 Code = pch::TYPE_VARIABLE_ARRAY;
127}
128
129void PCHTypeWriter::VisitVectorType(const VectorType *T) {
130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getNumElements());
John Thompson82287d12010-02-05 00:12:22 +0000132 Record.push_back(T->isAltiVec());
133 Record.push_back(T->isPixel());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 Code = pch::TYPE_VECTOR;
135}
136
137void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
138 VisitVectorType(T);
139 Code = pch::TYPE_EXT_VECTOR;
140}
141
142void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
143 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000144 FunctionType::ExtInfo C = T->getExtInfo();
145 Record.push_back(C.getNoReturn());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000146 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000147 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
150void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
151 VisitFunctionType(T);
152 Code = pch::TYPE_FUNCTION_NO_PROTO;
153}
154
155void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
156 VisitFunctionType(T);
157 Record.push_back(T->getNumArgs());
158 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
159 Writer.AddTypeRef(T->getArgType(I), Record);
160 Record.push_back(T->isVariadic());
161 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000162 Record.push_back(T->hasExceptionSpec());
163 Record.push_back(T->hasAnyExceptionSpec());
164 Record.push_back(T->getNumExceptions());
165 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
166 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Code = pch::TYPE_FUNCTION_PROTO;
168}
169
John McCalled976492009-12-04 22:46:56 +0000170#if 0
171// For when we want it....
172void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
173 Writer.AddDeclRef(T->getDecl(), Record);
174 Code = pch::TYPE_UNRESOLVED_USING;
175}
176#endif
177
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
179 Writer.AddDeclRef(T->getDecl(), Record);
180 Code = pch::TYPE_TYPEDEF;
181}
182
183void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000184 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000185 Code = pch::TYPE_TYPEOF_EXPR;
186}
187
188void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
189 Writer.AddTypeRef(T->getUnderlyingType(), Record);
190 Code = pch::TYPE_TYPEOF;
191}
192
Anders Carlsson395b4752009-06-24 19:06:50 +0000193void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
194 Writer.AddStmt(T->getUnderlyingExpr());
195 Code = pch::TYPE_DECLTYPE;
196}
197
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198void PCHTypeWriter::VisitTagType(const TagType *T) {
199 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000200 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201 "Cannot serialize in the middle of a type definition");
202}
203
204void PCHTypeWriter::VisitRecordType(const RecordType *T) {
205 VisitTagType(T);
206 Code = pch::TYPE_RECORD;
207}
208
209void PCHTypeWriter::VisitEnumType(const EnumType *T) {
210 VisitTagType(T);
211 Code = pch::TYPE_ENUM;
212}
213
John McCall7da24312009-09-05 00:15:47 +0000214void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
215 Writer.AddTypeRef(T->getUnderlyingType(), Record);
216 Record.push_back(T->getTagKind());
217 Code = pch::TYPE_ELABORATED;
218}
219
Mike Stump1eb44332009-09-09 15:08:12 +0000220void
John McCall49a832b2009-10-18 09:09:24 +0000221PCHTypeWriter::VisitSubstTemplateTypeParmType(
222 const SubstTemplateTypeParmType *T) {
223 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
224 Writer.AddTypeRef(T->getReplacementType(), Record);
225 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
226}
227
228void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229PCHTypeWriter::VisitTemplateSpecializationType(
230 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000231 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000232 assert(false && "Cannot serialize template specialization types");
233}
234
235void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000236 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000237 assert(false && "Cannot serialize qualified name types");
238}
239
John McCall3cb0ebd2010-03-10 03:28:59 +0000240void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
241 Writer.AddDeclRef(T->getDecl(), Record);
242 Writer.AddTypeRef(T->getUnderlyingType(), Record);
243 Code = pch::TYPE_INJECTED_CLASS_NAME;
244}
245
Douglas Gregor2cf26342009-04-09 22:27:44 +0000246void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
247 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 Record.push_back(T->getNumProtocols());
Steve Naroff446ee4e2009-05-27 16:21:00 +0000249 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
250 E = T->qual_end(); I != E; ++I)
251 Writer.AddDeclRef(*I, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000252 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253}
254
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000255void
256PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000257 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258 Record.push_back(T->getNumProtocols());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000259 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000260 E = T->qual_end(); I != E; ++I)
261 Writer.AddDeclRef(*I, Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000262 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263}
264
John McCalla1ee0c52009-10-16 21:56:05 +0000265namespace {
266
267class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
268 PCHWriter &Writer;
269 PCHWriter::RecordData &Record;
270
271public:
272 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
273 : Writer(Writer), Record(Record) { }
274
John McCall51bd8032009-10-18 01:05:36 +0000275#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000276#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000277 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000278#include "clang/AST/TypeLocNodes.def"
279
John McCall51bd8032009-10-18 01:05:36 +0000280 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
281 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000282};
283
284}
285
John McCall51bd8032009-10-18 01:05:36 +0000286void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
287 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000288}
John McCall51bd8032009-10-18 01:05:36 +0000289void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000290 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
291 if (TL.needsExtraLocalData()) {
292 Record.push_back(TL.getWrittenTypeSpec());
293 Record.push_back(TL.getWrittenSignSpec());
294 Record.push_back(TL.getWrittenWidthSpec());
295 Record.push_back(TL.hasModeAttr());
296 }
John McCalla1ee0c52009-10-16 21:56:05 +0000297}
John McCall51bd8032009-10-18 01:05:36 +0000298void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
299 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000300}
John McCall51bd8032009-10-18 01:05:36 +0000301void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
302 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000303}
John McCall51bd8032009-10-18 01:05:36 +0000304void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
305 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000306}
John McCall51bd8032009-10-18 01:05:36 +0000307void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
308 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000309}
John McCall51bd8032009-10-18 01:05:36 +0000310void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
311 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000312}
John McCall51bd8032009-10-18 01:05:36 +0000313void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
314 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000315}
John McCall51bd8032009-10-18 01:05:36 +0000316void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
317 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
318 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
319 Record.push_back(TL.getSizeExpr() ? 1 : 0);
320 if (TL.getSizeExpr())
321 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000322}
John McCall51bd8032009-10-18 01:05:36 +0000323void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
324 VisitArrayTypeLoc(TL);
325}
326void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
327 VisitArrayTypeLoc(TL);
328}
329void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
330 VisitArrayTypeLoc(TL);
331}
332void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
333 DependentSizedArrayTypeLoc TL) {
334 VisitArrayTypeLoc(TL);
335}
336void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
337 DependentSizedExtVectorTypeLoc TL) {
338 Writer.AddSourceLocation(TL.getNameLoc(), Record);
339}
340void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
341 Writer.AddSourceLocation(TL.getNameLoc(), Record);
342}
343void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
344 Writer.AddSourceLocation(TL.getNameLoc(), Record);
345}
346void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
347 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
348 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
349 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
350 Writer.AddDeclRef(TL.getArg(i), Record);
351}
352void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
353 VisitFunctionTypeLoc(TL);
354}
355void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
356 VisitFunctionTypeLoc(TL);
357}
John McCalled976492009-12-04 22:46:56 +0000358void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
359 Writer.AddSourceLocation(TL.getNameLoc(), Record);
360}
John McCall51bd8032009-10-18 01:05:36 +0000361void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
362 Writer.AddSourceLocation(TL.getNameLoc(), Record);
363}
364void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000365 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
366 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
367 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000368}
369void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000370 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
371 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
372 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
373 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000374}
375void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
376 Writer.AddSourceLocation(TL.getNameLoc(), Record);
377}
378void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
379 Writer.AddSourceLocation(TL.getNameLoc(), Record);
380}
381void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
382 Writer.AddSourceLocation(TL.getNameLoc(), Record);
383}
384void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
385 Writer.AddSourceLocation(TL.getNameLoc(), Record);
386}
387void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
388 Writer.AddSourceLocation(TL.getNameLoc(), Record);
389}
John McCall49a832b2009-10-18 09:09:24 +0000390void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
391 SubstTemplateTypeParmTypeLoc TL) {
392 Writer.AddSourceLocation(TL.getNameLoc(), Record);
393}
John McCall51bd8032009-10-18 01:05:36 +0000394void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
395 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000396 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
397 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
398 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
399 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
400 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000401}
402void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
403 Writer.AddSourceLocation(TL.getNameLoc(), Record);
404}
John McCall3cb0ebd2010-03-10 03:28:59 +0000405void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getNameLoc(), Record);
407}
John McCall51bd8032009-10-18 01:05:36 +0000408void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getNameLoc(), Record);
410}
411void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000413 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
414 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
415 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
416 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000417}
John McCall54e14c42009-10-22 22:37:11 +0000418void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
419 Writer.AddSourceLocation(TL.getStarLoc(), Record);
420 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
421 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
422 Record.push_back(TL.hasBaseTypeAsWritten());
423 Record.push_back(TL.hasProtocolsAsWritten());
424 if (TL.hasProtocolsAsWritten())
425 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
426 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
427}
John McCalla1ee0c52009-10-16 21:56:05 +0000428
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000429//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000430// PCHWriter Implementation
431//===----------------------------------------------------------------------===//
432
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000433static void EmitBlockID(unsigned ID, const char *Name,
434 llvm::BitstreamWriter &Stream,
435 PCHWriter::RecordData &Record) {
436 Record.clear();
437 Record.push_back(ID);
438 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
439
440 // Emit the block name if present.
441 if (Name == 0 || Name[0] == 0) return;
442 Record.clear();
443 while (*Name)
444 Record.push_back(*Name++);
445 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
446}
447
448static void EmitRecordID(unsigned ID, const char *Name,
449 llvm::BitstreamWriter &Stream,
450 PCHWriter::RecordData &Record) {
451 Record.clear();
452 Record.push_back(ID);
453 while (*Name)
454 Record.push_back(*Name++);
455 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000456}
457
458static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
459 PCHWriter::RecordData &Record) {
460#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
461 RECORD(STMT_STOP);
462 RECORD(STMT_NULL_PTR);
463 RECORD(STMT_NULL);
464 RECORD(STMT_COMPOUND);
465 RECORD(STMT_CASE);
466 RECORD(STMT_DEFAULT);
467 RECORD(STMT_LABEL);
468 RECORD(STMT_IF);
469 RECORD(STMT_SWITCH);
470 RECORD(STMT_WHILE);
471 RECORD(STMT_DO);
472 RECORD(STMT_FOR);
473 RECORD(STMT_GOTO);
474 RECORD(STMT_INDIRECT_GOTO);
475 RECORD(STMT_CONTINUE);
476 RECORD(STMT_BREAK);
477 RECORD(STMT_RETURN);
478 RECORD(STMT_DECL);
479 RECORD(STMT_ASM);
480 RECORD(EXPR_PREDEFINED);
481 RECORD(EXPR_DECL_REF);
482 RECORD(EXPR_INTEGER_LITERAL);
483 RECORD(EXPR_FLOATING_LITERAL);
484 RECORD(EXPR_IMAGINARY_LITERAL);
485 RECORD(EXPR_STRING_LITERAL);
486 RECORD(EXPR_CHARACTER_LITERAL);
487 RECORD(EXPR_PAREN);
488 RECORD(EXPR_UNARY_OPERATOR);
489 RECORD(EXPR_SIZEOF_ALIGN_OF);
490 RECORD(EXPR_ARRAY_SUBSCRIPT);
491 RECORD(EXPR_CALL);
492 RECORD(EXPR_MEMBER);
493 RECORD(EXPR_BINARY_OPERATOR);
494 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
495 RECORD(EXPR_CONDITIONAL_OPERATOR);
496 RECORD(EXPR_IMPLICIT_CAST);
497 RECORD(EXPR_CSTYLE_CAST);
498 RECORD(EXPR_COMPOUND_LITERAL);
499 RECORD(EXPR_EXT_VECTOR_ELEMENT);
500 RECORD(EXPR_INIT_LIST);
501 RECORD(EXPR_DESIGNATED_INIT);
502 RECORD(EXPR_IMPLICIT_VALUE_INIT);
503 RECORD(EXPR_VA_ARG);
504 RECORD(EXPR_ADDR_LABEL);
505 RECORD(EXPR_STMT);
506 RECORD(EXPR_TYPES_COMPATIBLE);
507 RECORD(EXPR_CHOOSE);
508 RECORD(EXPR_GNU_NULL);
509 RECORD(EXPR_SHUFFLE_VECTOR);
510 RECORD(EXPR_BLOCK);
511 RECORD(EXPR_BLOCK_DECL_REF);
512 RECORD(EXPR_OBJC_STRING_LITERAL);
513 RECORD(EXPR_OBJC_ENCODE);
514 RECORD(EXPR_OBJC_SELECTOR_EXPR);
515 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
516 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
517 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
518 RECORD(EXPR_OBJC_KVC_REF_EXPR);
519 RECORD(EXPR_OBJC_MESSAGE_EXPR);
520 RECORD(EXPR_OBJC_SUPER_EXPR);
521 RECORD(STMT_OBJC_FOR_COLLECTION);
522 RECORD(STMT_OBJC_CATCH);
523 RECORD(STMT_OBJC_FINALLY);
524 RECORD(STMT_OBJC_AT_TRY);
525 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
526 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000527 RECORD(EXPR_CXX_OPERATOR_CALL);
528 RECORD(EXPR_CXX_CONSTRUCT);
529 RECORD(EXPR_CXX_STATIC_CAST);
530 RECORD(EXPR_CXX_DYNAMIC_CAST);
531 RECORD(EXPR_CXX_REINTERPRET_CAST);
532 RECORD(EXPR_CXX_CONST_CAST);
533 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
534 RECORD(EXPR_CXX_BOOL_LITERAL);
535 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000536#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000537}
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000539void PCHWriter::WriteBlockInfoBlock() {
540 RecordData Record;
541 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Chris Lattner2f4efd12009-04-27 00:40:25 +0000543#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000544#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000546 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000547 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000548 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000549 RECORD(TYPE_OFFSET);
550 RECORD(DECL_OFFSET);
551 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000552 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000553 RECORD(IDENTIFIER_OFFSET);
554 RECORD(IDENTIFIER_TABLE);
555 RECORD(EXTERNAL_DEFINITIONS);
556 RECORD(SPECIAL_TYPES);
557 RECORD(STATISTICS);
558 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000559 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000560 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
561 RECORD(SELECTOR_OFFSETS);
562 RECORD(METHOD_POOL);
563 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000564 RECORD(SOURCE_LOCATION_OFFSETS);
565 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000566 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000567 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000568 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000569 RECORD(UNUSED_STATIC_FUNCS);
570 RECORD(MACRO_DEFINITION_OFFSETS);
571
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000572 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000573 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000574 RECORD(SM_SLOC_FILE_ENTRY);
575 RECORD(SM_SLOC_BUFFER_ENTRY);
576 RECORD(SM_SLOC_BUFFER_BLOB);
577 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
578 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000580 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000581 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000582 RECORD(PP_MACRO_OBJECT_LIKE);
583 RECORD(PP_MACRO_FUNCTION_LIKE);
584 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000585 RECORD(PP_MACRO_INSTANTIATION);
586 RECORD(PP_MACRO_DEFINITION);
587
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000588 // Decls and Types block.
589 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000590 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000591 RECORD(TYPE_COMPLEX);
592 RECORD(TYPE_POINTER);
593 RECORD(TYPE_BLOCK_POINTER);
594 RECORD(TYPE_LVALUE_REFERENCE);
595 RECORD(TYPE_RVALUE_REFERENCE);
596 RECORD(TYPE_MEMBER_POINTER);
597 RECORD(TYPE_CONSTANT_ARRAY);
598 RECORD(TYPE_INCOMPLETE_ARRAY);
599 RECORD(TYPE_VARIABLE_ARRAY);
600 RECORD(TYPE_VECTOR);
601 RECORD(TYPE_EXT_VECTOR);
602 RECORD(TYPE_FUNCTION_PROTO);
603 RECORD(TYPE_FUNCTION_NO_PROTO);
604 RECORD(TYPE_TYPEDEF);
605 RECORD(TYPE_TYPEOF_EXPR);
606 RECORD(TYPE_TYPEOF);
607 RECORD(TYPE_RECORD);
608 RECORD(TYPE_ENUM);
609 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000610 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000611 RECORD(DECL_ATTR);
612 RECORD(DECL_TRANSLATION_UNIT);
613 RECORD(DECL_TYPEDEF);
614 RECORD(DECL_ENUM);
615 RECORD(DECL_RECORD);
616 RECORD(DECL_ENUM_CONSTANT);
617 RECORD(DECL_FUNCTION);
618 RECORD(DECL_OBJC_METHOD);
619 RECORD(DECL_OBJC_INTERFACE);
620 RECORD(DECL_OBJC_PROTOCOL);
621 RECORD(DECL_OBJC_IVAR);
622 RECORD(DECL_OBJC_AT_DEFS_FIELD);
623 RECORD(DECL_OBJC_CLASS);
624 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
625 RECORD(DECL_OBJC_CATEGORY);
626 RECORD(DECL_OBJC_CATEGORY_IMPL);
627 RECORD(DECL_OBJC_IMPLEMENTATION);
628 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
629 RECORD(DECL_OBJC_PROPERTY);
630 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000631 RECORD(DECL_FIELD);
632 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000633 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000634 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000635 RECORD(DECL_FILE_SCOPE_ASM);
636 RECORD(DECL_BLOCK);
637 RECORD(DECL_CONTEXT_LEXICAL);
638 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000639 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000640 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000641#undef RECORD
642#undef BLOCK
643 Stream.ExitBlock();
644}
645
Douglas Gregore650c8c2009-07-07 00:12:59 +0000646/// \brief Adjusts the given filename to only write out the portion of the
647/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000648///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000649/// \param Filename the file name to adjust.
650///
651/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
652/// the returned filename will be adjusted by this system root.
653///
654/// \returns either the original filename (if it needs no adjustment) or the
655/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000656static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000657adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
658 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Douglas Gregore650c8c2009-07-07 00:12:59 +0000660 if (!isysroot)
661 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Douglas Gregore650c8c2009-07-07 00:12:59 +0000663 // Verify that the filename and the system root have the same prefix.
664 unsigned Pos = 0;
665 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
666 if (Filename[Pos] != isysroot[Pos])
667 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregore650c8c2009-07-07 00:12:59 +0000669 // We hit the end of the filename before we hit the end of the system root.
670 if (!Filename[Pos])
671 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Douglas Gregore650c8c2009-07-07 00:12:59 +0000673 // If the file name has a '/' at the current position, skip over the '/'.
674 // We distinguish sysroot-based includes from absolute includes by the
675 // absence of '/' at the beginning of sysroot-based includes.
676 if (Filename[Pos] == '/')
677 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Douglas Gregore650c8c2009-07-07 00:12:59 +0000679 return Filename + Pos;
680}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000681
Douglas Gregorab41e632009-04-27 22:23:34 +0000682/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000683void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000684 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000685
Douglas Gregore650c8c2009-07-07 00:12:59 +0000686 // Metadata
687 const TargetInfo &Target = Context.Target;
688 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
689 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
690 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
691 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
692 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
693 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
694 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
695 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
696 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Douglas Gregore650c8c2009-07-07 00:12:59 +0000698 RecordData Record;
699 Record.push_back(pch::METADATA);
700 Record.push_back(pch::VERSION_MAJOR);
701 Record.push_back(pch::VERSION_MINOR);
702 Record.push_back(CLANG_VERSION_MAJOR);
703 Record.push_back(CLANG_VERSION_MINOR);
704 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000705 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000706 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Douglas Gregorb64c1932009-05-12 01:31:05 +0000708 // Original file name
709 SourceManager &SM = Context.getSourceManager();
710 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
711 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
712 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
713 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
714 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
715
716 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000718 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000719
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000720 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000721 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000722 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000723 RecordData Record;
724 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000725 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000726 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000727
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000728 // Repository branch/version information.
729 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
730 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
731 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
732 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000733 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000734 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000735 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
736 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000737}
738
739/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000740void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
741 RecordData Record;
742 Record.push_back(LangOpts.Trigraphs);
743 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
744 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
745 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
746 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
747 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
748 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
749 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
750 Record.push_back(LangOpts.C99); // C99 Support
751 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
752 Record.push_back(LangOpts.CPlusPlus); // C++ Support
753 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000754 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000756 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
757 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000758 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000759 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000760 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000761 // modern abi enabled.
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000763 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000764 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
765 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000766 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000767 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000768 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000769
770 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
771 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
772 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
773
Chris Lattnerea5ce472009-04-27 07:35:58 +0000774 // Whether static initializers are protected by locks.
775 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000776 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000777 Record.push_back(LangOpts.Blocks); // block extension to C
778 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
779 // they are unused.
780 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
781 // (modulo the platform support).
782
783 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
784 // signed integer arithmetic overflows.
785
786 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
787 // may be ripped out at any time.
788
789 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000790 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000791 // defined.
792 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
793 // opposed to __DYNAMIC__).
794 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
795
796 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
797 // used (instead of C99 semantics).
798 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000799 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
800 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000801 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
802 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000803 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000804 Record.push_back(LangOpts.getGCMode());
805 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000806 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000807 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000808 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000809 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000810 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000811 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000812}
813
Douglas Gregor14f79002009-04-10 03:52:48 +0000814//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000815// stat cache Serialization
816//===----------------------------------------------------------------------===//
817
818namespace {
819// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000820class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000821public:
822 typedef const char * key_type;
823 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000825 typedef std::pair<int, struct stat> data_type;
826 typedef const data_type& data_type_ref;
827
828 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000829 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
832 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000833 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
834 data_type_ref Data) {
835 unsigned StrLen = strlen(path);
836 clang::io::Emit16(Out, StrLen);
837 unsigned DataLen = 1; // result value
838 if (Data.first == 0)
839 DataLen += 4 + 4 + 2 + 8 + 8;
840 clang::io::Emit8(Out, DataLen);
841 return std::make_pair(StrLen + 1, DataLen);
842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000844 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
845 Out.write(path, KeyLen);
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000848 void EmitData(llvm::raw_ostream& Out, key_type_ref,
849 data_type_ref Data, unsigned DataLen) {
850 using namespace clang::io;
851 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000853 // Result of stat()
854 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856 if (Data.first == 0) {
857 Emit32(Out, (uint32_t) Data.second.st_ino);
858 Emit32(Out, (uint32_t) Data.second.st_dev);
859 Emit16(Out, (uint16_t) Data.second.st_mode);
860 Emit64(Out, (uint64_t) Data.second.st_mtime);
861 Emit64(Out, (uint64_t) Data.second.st_size);
862 }
863
864 assert(Out.tell() - Start == DataLen && "Wrong data length");
865 }
866};
867} // end anonymous namespace
868
869/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000870void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
871 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000872 // Build the on-disk hash table containing information about every
873 // stat() call.
874 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
875 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000876 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000877 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000878 Stat != StatEnd; ++Stat, ++NumStatEntries) {
879 const char *Filename = Stat->first();
880 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
881 Generator.insert(Filename, Stat->second);
882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000884 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000885 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000886 uint32_t BucketOffset;
887 {
888 llvm::raw_svector_ostream Out(StatCacheData);
889 // Make sure that no bucket is at offset 0
890 clang::io::Emit32(Out, 0);
891 BucketOffset = Generator.Emit(Out);
892 }
893
894 // Create a blob abbreviation
895 using namespace llvm;
896 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
897 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
901 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
902
903 // Write the stat cache
904 RecordData Record;
905 Record.push_back(pch::STAT_CACHE);
906 Record.push_back(BucketOffset);
907 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000908 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000909}
910
911//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000912// Source Manager Serialization
913//===----------------------------------------------------------------------===//
914
915/// \brief Create an abbreviation for the SLocEntry that refers to a
916/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000917static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000918 using namespace llvm;
919 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
920 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000925 // FileEntry fields.
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000928 // HeaderFileInfo fields.
929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000934 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000935}
936
937/// \brief Create an abbreviation for the SLocEntry that refers to a
938/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000939static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000940 using namespace llvm;
941 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
942 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000948 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000949}
950
951/// \brief Create an abbreviation for the SLocEntry that refers to a
952/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000953static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000954 using namespace llvm;
955 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
956 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000958 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000959}
960
961/// \brief Create an abbreviation for the SLocEntry that refers to an
962/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000963static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000964 using namespace llvm;
965 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
966 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000972 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000973}
974
975/// \brief Writes the block containing the serialized form of the
976/// source manager.
977///
978/// TODO: We should probably use an on-disk hash table (stored in a
979/// blob), indexed based on the file name, so that we only create
980/// entries for files that we actually need. In the common case (no
981/// errors), we probably won't have to create file entries for any of
982/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000983void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000984 const Preprocessor &PP,
985 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000986 RecordData Record;
987
Chris Lattnerf04ad692009-04-10 17:16:57 +0000988 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000989 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000990
991 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000992 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
993 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
994 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
995 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000996
Douglas Gregorbd945002009-04-13 16:31:14 +0000997 // Write the line table.
998 if (SourceMgr.hasLineTable()) {
999 LineTableInfo &LineTable = SourceMgr.getLineTable();
1000
1001 // Emit the file names
1002 Record.push_back(LineTable.getNumFilenames());
1003 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1004 // Emit the file name
1005 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001006 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001007 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1008 Record.push_back(FilenameLen);
1009 if (FilenameLen)
1010 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1011 }
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Douglas Gregorbd945002009-04-13 16:31:14 +00001013 // Emit the line entries
1014 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1015 L != LEnd; ++L) {
1016 // Emit the file ID
1017 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Douglas Gregorbd945002009-04-13 16:31:14 +00001019 // Emit the line entries
1020 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001021 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001022 LEEnd = L->second.end();
1023 LE != LEEnd; ++LE) {
1024 Record.push_back(LE->FileOffset);
1025 Record.push_back(LE->LineNo);
1026 Record.push_back(LE->FilenameID);
1027 Record.push_back((unsigned)LE->FileKind);
1028 Record.push_back(LE->IncludeOffset);
1029 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001030 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001031 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001032 }
1033
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001034 // Write out the source location entry table. We skip the first
1035 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001036 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001037 RecordData PreloadSLocs;
1038 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001039 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1040 // Get this source location entry.
1041 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001042
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001043 // Record the offset of this source-location entry.
1044 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1045
1046 // Figure out which record code to use.
1047 unsigned Code;
1048 if (SLoc->isFile()) {
1049 if (SLoc->getFile().getContentCache()->Entry)
1050 Code = pch::SM_SLOC_FILE_ENTRY;
1051 else
1052 Code = pch::SM_SLOC_BUFFER_ENTRY;
1053 } else
1054 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1055 Record.clear();
1056 Record.push_back(Code);
1057
1058 Record.push_back(SLoc->getOffset());
1059 if (SLoc->isFile()) {
1060 const SrcMgr::FileInfo &File = SLoc->getFile();
1061 Record.push_back(File.getIncludeLoc().getRawEncoding());
1062 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1063 Record.push_back(File.hasLineDirectives());
1064
1065 const SrcMgr::ContentCache *Content = File.getContentCache();
1066 if (Content->Entry) {
1067 // The source location entry is a file. The blob associated
1068 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Douglas Gregor2d52be52010-03-21 22:49:54 +00001070 // Emit size/modification time for this file.
1071 Record.push_back(Content->Entry->getSize());
1072 Record.push_back(Content->Entry->getModificationTime());
1073
Douglas Gregor12fab312010-03-16 16:35:32 +00001074 // Emit header-search information associated with this file.
1075 HeaderFileInfo HFI;
1076 HeaderSearch &HS = PP.getHeaderSearchInfo();
1077 if (Content->Entry->getUID() < HS.header_file_size())
1078 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1079 Record.push_back(HFI.isImport);
1080 Record.push_back(HFI.DirInfo);
1081 Record.push_back(HFI.NumIncludes);
1082 AddIdentifierRef(HFI.ControllingMacro, Record);
1083
Douglas Gregore650c8c2009-07-07 00:12:59 +00001084 // Turn the file name into an absolute path, if it isn't already.
1085 const char *Filename = Content->Entry->getName();
1086 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001087 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001088 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Douglas Gregore650c8c2009-07-07 00:12:59 +00001090 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001091 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001092
1093 // FIXME: For now, preload all file source locations, so that
1094 // we get the appropriate File entries in the reader. This is
1095 // a temporary measure.
1096 PreloadSLocs.push_back(SLocEntryOffsets.size());
1097 } else {
1098 // The source location entry is a buffer. The blob associated
1099 // with this entry contains the contents of the buffer.
1100
1101 // We add one to the size so that we capture the trailing NULL
1102 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1103 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001104 const llvm::MemoryBuffer *Buffer
1105 = Content->getBuffer(PP.getDiagnostics());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001106 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001107 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1108 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001109 Record.clear();
1110 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1111 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001112 llvm::StringRef(Buffer->getBufferStart(),
1113 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001114
1115 if (strcmp(Name, "<built-in>") == 0)
1116 PreloadSLocs.push_back(SLocEntryOffsets.size());
1117 }
1118 } else {
1119 // The source location entry is an instantiation.
1120 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1121 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1122 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1123 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1124
1125 // Compute the token length for this macro expansion.
1126 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001127 if (I + 1 != N)
1128 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001129 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1130 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1131 }
1132 }
1133
Douglas Gregorc9490c02009-04-16 22:23:12 +00001134 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001135
1136 if (SLocEntryOffsets.empty())
1137 return;
1138
1139 // Write the source-location offsets table into the PCH block. This
1140 // table is used for lazily loading source-location information.
1141 using namespace llvm;
1142 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1143 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1147 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001149 Record.clear();
1150 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1151 Record.push_back(SLocEntryOffsets.size());
1152 Record.push_back(SourceMgr.getNextOffset());
1153 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001154 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001155 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001156
1157 // Write the source location entry preloads array, telling the PCH
1158 // reader which source locations entries it should load eagerly.
1159 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001160}
1161
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001162//===----------------------------------------------------------------------===//
1163// Preprocessor Serialization
1164//===----------------------------------------------------------------------===//
1165
Chris Lattner0b1fb982009-04-10 17:15:23 +00001166/// \brief Writes the block containing the serialized form of the
1167/// preprocessor.
1168///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001169void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001170 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001171
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001172 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1173 if (PP.getCounterValue() != 0) {
1174 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001175 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001176 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001177 }
1178
1179 // Enter the preprocessor block.
1180 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001182 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1183 // FIXME: use diagnostics subsystem for localization etc.
1184 if (PP.SawDateOrTime())
1185 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001187 // Loop over all the macro definitions that are live at the end of the file,
1188 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001189 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001190 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1191 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001192 // FIXME: This emits macros in hash table order, we should do it in a stable
1193 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001194 MacroInfo *MI = I->second;
1195
1196 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1197 // been redefined by the header (in which case they are not isBuiltinMacro).
1198 if (MI->isBuiltinMacro())
1199 continue;
1200
Chris Lattner7356a312009-04-11 21:15:38 +00001201 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001202 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001203 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1204 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001206 unsigned Code;
1207 if (MI->isObjectLike()) {
1208 Code = pch::PP_MACRO_OBJECT_LIKE;
1209 } else {
1210 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001212 Record.push_back(MI->isC99Varargs());
1213 Record.push_back(MI->isGNUVarargs());
1214 Record.push_back(MI->getNumArgs());
1215 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1216 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001217 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001218 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001219
1220 // If we have a detailed preprocessing record, record the macro definition
1221 // ID that corresponds to this macro.
1222 if (PPRec)
1223 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1224
Douglas Gregorc9490c02009-04-16 22:23:12 +00001225 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001226 Record.clear();
1227
Chris Lattnerdf961c22009-04-10 18:08:30 +00001228 // Emit the tokens array.
1229 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1230 // Note that we know that the preprocessor does not have any annotation
1231 // tokens in it because they are created by the parser, and thus can't be
1232 // in a macro definition.
1233 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Chris Lattnerdf961c22009-04-10 18:08:30 +00001235 Record.push_back(Tok.getLocation().getRawEncoding());
1236 Record.push_back(Tok.getLength());
1237
Chris Lattnerdf961c22009-04-10 18:08:30 +00001238 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1239 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001240 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Chris Lattnerdf961c22009-04-10 18:08:30 +00001242 // FIXME: Should translate token kind to a stable encoding.
1243 Record.push_back(Tok.getKind());
1244 // FIXME: Should translate token flags to a stable encoding.
1245 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Douglas Gregorc9490c02009-04-16 22:23:12 +00001247 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001248 Record.clear();
1249 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001250 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001251 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001252
1253 // If the preprocessor has a preprocessing record, emit it.
1254 unsigned NumPreprocessingRecords = 0;
1255 if (PPRec) {
1256 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1257 E != EEnd; ++E) {
1258 Record.clear();
1259
1260 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1261 Record.push_back(NumPreprocessingRecords++);
1262 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1263 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1264 AddIdentifierRef(MI->getName(), Record);
1265 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1266 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1267 continue;
1268 }
1269
1270 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1271 // Record this macro definition's location.
1272 pch::IdentID ID = getMacroDefinitionID(MD);
1273 if (ID != MacroDefinitionOffsets.size()) {
1274 if (ID > MacroDefinitionOffsets.size())
1275 MacroDefinitionOffsets.resize(ID + 1);
1276
1277 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1278 } else
1279 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1280
1281 Record.push_back(NumPreprocessingRecords++);
1282 Record.push_back(ID);
1283 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1284 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1285 AddIdentifierRef(MD->getName(), Record);
1286 AddSourceLocation(MD->getLocation(), Record);
1287 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1288 continue;
1289 }
1290 }
1291 }
1292
Douglas Gregorc9490c02009-04-16 22:23:12 +00001293 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001294
1295 // Write the offsets table for the preprocessing record.
1296 if (NumPreprocessingRecords > 0) {
1297 // Write the offsets table for identifier IDs.
1298 using namespace llvm;
1299 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1300 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1304 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1305
1306 Record.clear();
1307 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1308 Record.push_back(NumPreprocessingRecords);
1309 Record.push_back(MacroDefinitionOffsets.size());
1310 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1311 (const char *)&MacroDefinitionOffsets.front(),
1312 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1313 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001314}
1315
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001316//===----------------------------------------------------------------------===//
1317// Type Serialization
1318//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001319
Douglas Gregor2cf26342009-04-09 22:27:44 +00001320/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001321void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001322 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001323 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001324 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Douglas Gregor2cf26342009-04-09 22:27:44 +00001326 // Record the offset for this type.
1327 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001328 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001329 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1330 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001331 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001332 }
1333
1334 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Douglas Gregor2cf26342009-04-09 22:27:44 +00001336 // Emit the type's representation.
1337 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001338
Douglas Gregora4923eb2009-11-16 21:35:15 +00001339 if (T.hasLocalNonFastQualifiers()) {
1340 Qualifiers Qs = T.getLocalQualifiers();
1341 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001342 Record.push_back(Qs.getAsOpaqueValue());
1343 W.Code = pch::TYPE_EXT_QUAL;
1344 } else {
1345 switch (T->getTypeClass()) {
1346 // For all of the concrete, non-dependent types, call the
1347 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001348#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001349 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001350#define ABSTRACT_TYPE(Class, Base)
1351#define DEPENDENT_TYPE(Class, Base)
1352#include "clang/AST/TypeNodes.def"
1353
John McCall0953e762009-09-24 19:53:00 +00001354 // For all of the dependent type nodes (which only occur in C++
1355 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001356#define TYPE(Class, Base)
1357#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1358#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001359 assert(false && "Cannot serialize dependent type nodes");
1360 break;
1361 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001362 }
1363
1364 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001365 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001366
1367 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001368 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001369}
1370
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001371//===----------------------------------------------------------------------===//
1372// Declaration Serialization
1373//===----------------------------------------------------------------------===//
1374
Douglas Gregor2cf26342009-04-09 22:27:44 +00001375/// \brief Write the block containing all of the declaration IDs
1376/// lexically declared within the given DeclContext.
1377///
1378/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1379/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001380uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001381 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001382 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001383 return 0;
1384
Douglas Gregorc9490c02009-04-16 22:23:12 +00001385 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001386 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001387 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1388 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001389 AddDeclRef(*D, Record);
1390
Douglas Gregor25123082009-04-22 22:34:57 +00001391 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001392 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001393 return Offset;
1394}
1395
1396/// \brief Write the block containing all of the declaration IDs
1397/// visible from the given DeclContext.
1398///
1399/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1400/// bistream, or 0 if no block was written.
1401uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1402 DeclContext *DC) {
1403 if (DC->getPrimaryContext() != DC)
1404 return 0;
1405
Douglas Gregoraff22df2009-04-21 22:32:33 +00001406 // Since there is no name lookup into functions or methods, and we
1407 // perform name lookup for the translation unit via the
1408 // IdentifierInfo chains, don't bother to build a
1409 // visible-declarations table for these entities.
1410 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001411 return 0;
1412
Douglas Gregor2cf26342009-04-09 22:27:44 +00001413 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001414 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001415
1416 // Serialize the contents of the mapping used for lookup. Note that,
1417 // although we have two very different code paths, the serialized
1418 // representation is the same for both cases: a declaration name,
1419 // followed by a size, followed by references to the visible
1420 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001421 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001422 RecordData Record;
1423 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001424 if (!Map)
1425 return 0;
1426
Douglas Gregor2cf26342009-04-09 22:27:44 +00001427 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1428 D != DEnd; ++D) {
1429 AddDeclarationName(D->first, Record);
1430 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1431 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001432 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001433 AddDeclRef(*Result.first, Record);
1434 }
1435
1436 if (Record.size() == 0)
1437 return 0;
1438
Douglas Gregorc9490c02009-04-16 22:23:12 +00001439 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001440 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001441 return Offset;
1442}
1443
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001444//===----------------------------------------------------------------------===//
1445// Global Method Pool and Selector Serialization
1446//===----------------------------------------------------------------------===//
1447
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001448namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001449// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001450class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001451 PCHWriter &Writer;
1452
1453public:
1454 typedef Selector key_type;
1455 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001457 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1458 typedef const data_type& data_type_ref;
1459
1460 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001462 static unsigned ComputeHash(Selector Sel) {
1463 unsigned N = Sel.getNumArgs();
1464 if (N == 0)
1465 ++N;
1466 unsigned R = 5381;
1467 for (unsigned I = 0; I != N; ++I)
1468 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001469 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001470 return R;
1471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
1473 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001474 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1475 data_type_ref Methods) {
1476 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1477 clang::io::Emit16(Out, KeyLen);
1478 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001479 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001480 Method = Method->Next)
1481 if (Method->Method)
1482 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001483 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001484 Method = Method->Next)
1485 if (Method->Method)
1486 DataLen += 4;
1487 clang::io::Emit16(Out, DataLen);
1488 return std::make_pair(KeyLen, DataLen);
1489 }
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Douglas Gregor83941df2009-04-25 17:48:32 +00001491 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001492 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001493 assert((Start >> 32) == 0 && "Selector key offset too large");
1494 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001495 unsigned N = Sel.getNumArgs();
1496 clang::io::Emit16(Out, N);
1497 if (N == 0)
1498 N = 1;
1499 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001500 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001501 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1502 }
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001504 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001505 data_type_ref Methods, unsigned DataLen) {
1506 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001507 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001508 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001509 Method = Method->Next)
1510 if (Method->Method)
1511 ++NumInstanceMethods;
1512
1513 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001514 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001515 Method = Method->Next)
1516 if (Method->Method)
1517 ++NumFactoryMethods;
1518
1519 clang::io::Emit16(Out, NumInstanceMethods);
1520 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001521 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001522 Method = Method->Next)
1523 if (Method->Method)
1524 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001525 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001526 Method = Method->Next)
1527 if (Method->Method)
1528 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001529
1530 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001531 }
1532};
1533} // end anonymous namespace
1534
1535/// \brief Write the method pool into the PCH file.
1536///
1537/// The method pool contains both instance and factory methods, stored
1538/// in an on-disk hash table indexed by the selector.
1539void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1540 using namespace llvm;
1541
1542 // Create and write out the blob that contains the instance and
1543 // factor method pools.
1544 bool Empty = true;
1545 {
1546 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001548 // Create the on-disk hash table representation. Start by
1549 // iterating through the instance method pool.
1550 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001551 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001552 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001553 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001554 InstanceEnd = SemaRef.InstanceMethodPool.end();
1555 Instance != InstanceEnd; ++Instance) {
1556 // Check whether there is a factory method with the same
1557 // selector.
1558 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1559 = SemaRef.FactoryMethodPool.find(Instance->first);
1560
1561 if (Factory == SemaRef.FactoryMethodPool.end())
1562 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001563 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001564 ObjCMethodList()));
1565 else
1566 Generator.insert(Instance->first,
1567 std::make_pair(Instance->second, Factory->second));
1568
Douglas Gregor83941df2009-04-25 17:48:32 +00001569 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001570 Empty = false;
1571 }
1572
1573 // Now iterate through the factory method pool, to pick up any
1574 // selectors that weren't already in the instance method pool.
1575 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001576 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001577 FactoryEnd = SemaRef.FactoryMethodPool.end();
1578 Factory != FactoryEnd; ++Factory) {
1579 // Check whether there is an instance method with the same
1580 // selector. If so, there is no work to do here.
1581 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1582 = SemaRef.InstanceMethodPool.find(Factory->first);
1583
Douglas Gregor83941df2009-04-25 17:48:32 +00001584 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001585 Generator.insert(Factory->first,
1586 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001587 ++NumSelectorsInMethodPool;
1588 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001589
1590 Empty = false;
1591 }
1592
Douglas Gregor83941df2009-04-25 17:48:32 +00001593 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001594 return;
1595
1596 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001597 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001598 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001599 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600 {
1601 PCHMethodPoolTrait Trait(*this);
1602 llvm::raw_svector_ostream Out(MethodPool);
1603 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001604 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001605 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001606
1607 // For every selector that we have seen but which was not
1608 // written into the hash table, write the selector itself and
1609 // record it's offset.
1610 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1611 if (SelectorOffsets[I] == 0)
1612 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001613 }
1614
1615 // Create a blob abbreviation
1616 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1617 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1621 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1622
Douglas Gregor83941df2009-04-25 17:48:32 +00001623 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001624 RecordData Record;
1625 Record.push_back(pch::METHOD_POOL);
1626 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001627 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001628 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001629
1630 // Create a blob abbreviation for the selector table offsets.
1631 Abbrev = new BitCodeAbbrev();
1632 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1635 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1636
1637 // Write the selector offsets table.
1638 Record.clear();
1639 Record.push_back(pch::SELECTOR_OFFSETS);
1640 Record.push_back(SelectorOffsets.size());
1641 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1642 (const char *)&SelectorOffsets.front(),
1643 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001644 }
1645}
1646
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001647//===----------------------------------------------------------------------===//
1648// Identifier Table Serialization
1649//===----------------------------------------------------------------------===//
1650
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001651namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001652class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001653 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001654 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001655
Douglas Gregora92193e2009-04-28 21:18:29 +00001656 /// \brief Determines whether this is an "interesting" identifier
1657 /// that needs a full IdentifierInfo structure written into the hash
1658 /// table.
1659 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1660 return II->isPoisoned() ||
1661 II->isExtensionToken() ||
1662 II->hasMacroDefinition() ||
1663 II->getObjCOrBuiltinID() ||
1664 II->getFETokenInfo<void>();
1665 }
1666
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001667public:
1668 typedef const IdentifierInfo* key_type;
1669 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001671 typedef pch::IdentID data_type;
1672 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001673
1674 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001675 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001676
1677 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001678 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001679 }
Mike Stump1eb44332009-09-09 15:08:12 +00001680
1681 std::pair<unsigned,unsigned>
1682 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001683 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001684 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001685 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1686 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001687 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001688 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001689 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001690 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001691 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1692 DEnd = IdentifierResolver::end();
1693 D != DEnd; ++D)
1694 DataLen += sizeof(pch::DeclID);
1695 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001696 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001697 // We emit the key length after the data length so that every
1698 // string is preceded by a 16-bit length. This matches the PTH
1699 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001700 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001701 return std::make_pair(KeyLen, DataLen);
1702 }
Mike Stump1eb44332009-09-09 15:08:12 +00001703
1704 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001705 unsigned KeyLen) {
1706 // Record the location of the key data. This is used when generating
1707 // the mapping from persistent IDs to strings.
1708 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001709 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711
1712 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001713 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001714 if (!isInterestingIdentifier(II)) {
1715 clang::io::Emit32(Out, ID << 1);
1716 return;
1717 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001718
Douglas Gregora92193e2009-04-28 21:18:29 +00001719 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001720 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001721 bool hasMacroDefinition =
1722 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001723 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001724 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001725 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1726 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1727 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1728 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001729 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001730
Douglas Gregor37e26842009-04-21 23:56:24 +00001731 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001732 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001733
Douglas Gregor668c1a42009-04-21 22:25:48 +00001734 // Emit the declaration IDs in reverse order, because the
1735 // IdentifierResolver provides the declarations as they would be
1736 // visible (e.g., the function "stat" would come before the struct
1737 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1738 // adds declarations to the end of the list (so we need to see the
1739 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001740 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001741 IdentifierResolver::end());
1742 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1743 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001744 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001745 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001746 }
1747};
1748} // end anonymous namespace
1749
Douglas Gregorafaf3082009-04-11 00:14:32 +00001750/// \brief Write the identifier table into the PCH file.
1751///
1752/// The identifier table consists of a blob containing string data
1753/// (the actual identifiers themselves) and a separate "offsets" index
1754/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001755void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001756 using namespace llvm;
1757
1758 // Create and write out the blob that contains the identifier
1759 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001760 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001761 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Douglas Gregor92b059e2009-04-28 20:33:11 +00001763 // Look for any identifiers that were named while processing the
1764 // headers, but are otherwise not needed. We add these to the hash
1765 // table to enable checking of the predefines buffer in the case
1766 // where the user adds new macro definitions when building the PCH
1767 // file.
1768 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1769 IDEnd = PP.getIdentifierTable().end();
1770 ID != IDEnd; ++ID)
1771 getIdentifierRef(ID->second);
1772
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001773 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001774 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001775 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1776 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1777 ID != IDEnd; ++ID) {
1778 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001779 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001780 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001781
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001782 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001783 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001784 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001785 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001786 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001787 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001788 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001789 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001790 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001791 }
1792
1793 // Create a blob abbreviation
1794 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1795 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001798 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001799
1800 // Write the identifier table
1801 RecordData Record;
1802 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001803 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001804 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001805 }
1806
1807 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001808 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1809 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1812 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1813
1814 RecordData Record;
1815 Record.push_back(pch::IDENTIFIER_OFFSET);
1816 Record.push_back(IdentifierOffsets.size());
1817 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1818 (const char *)&IdentifierOffsets.front(),
1819 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001820}
1821
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001822//===----------------------------------------------------------------------===//
1823// General Serialization Routines
1824//===----------------------------------------------------------------------===//
1825
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001826/// \brief Write a record containing the given attributes.
1827void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1828 RecordData Record;
1829 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001830 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001831 Record.push_back(Attr->isInherited());
1832 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001833 default:
1834 assert(0 && "Does not support PCH writing for this attribute yet!");
1835 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001836 case Attr::Alias:
1837 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1838 break;
1839
1840 case Attr::Aligned:
1841 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1842 break;
1843
1844 case Attr::AlwaysInline:
1845 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001847 case Attr::AnalyzerNoReturn:
1848 break;
1849
1850 case Attr::Annotate:
1851 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1852 break;
1853
1854 case Attr::AsmLabel:
1855 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1856 break;
1857
Sean Hunt7725e672009-11-25 04:20:27 +00001858 case Attr::BaseCheck:
1859 break;
1860
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001861 case Attr::Blocks:
1862 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1863 break;
1864
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001865 case Attr::CDecl:
1866 break;
1867
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001868 case Attr::Cleanup:
1869 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1870 break;
1871
1872 case Attr::Const:
1873 break;
1874
1875 case Attr::Constructor:
1876 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1877 break;
1878
1879 case Attr::DLLExport:
1880 case Attr::DLLImport:
1881 case Attr::Deprecated:
1882 break;
1883
1884 case Attr::Destructor:
1885 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1886 break;
1887
1888 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001889 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001890 break;
1891
1892 case Attr::Format: {
1893 const FormatAttr *Format = cast<FormatAttr>(Attr);
1894 AddString(Format->getType(), Record);
1895 Record.push_back(Format->getFormatIdx());
1896 Record.push_back(Format->getFirstArg());
1897 break;
1898 }
1899
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001900 case Attr::FormatArg: {
1901 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1902 Record.push_back(Format->getFormatIdx());
1903 break;
1904 }
1905
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001906 case Attr::Sentinel : {
1907 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1908 Record.push_back(Sentinel->getSentinel());
1909 Record.push_back(Sentinel->getNullPos());
1910 break;
1911 }
Mike Stump1eb44332009-09-09 15:08:12 +00001912
Chris Lattnercf2a7212009-04-20 19:12:28 +00001913 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001914 case Attr::Hiding:
Ted Kremenekefbddd22010-02-17 02:37:45 +00001915 case Attr::IBActionKind:
Ted Kremenek47e69902010-02-18 00:05:52 +00001916 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001917 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001918 case Attr::NoDebug:
Ted Kremenek47e69902010-02-18 00:05:52 +00001919 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001920 case Attr::NoReturn:
1921 case Attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001922 break;
1923
1924 case Attr::NonNull: {
1925 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1926 Record.push_back(NonNull->size());
1927 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1928 break;
1929 }
1930
Ted Kremenek31c780d2010-02-18 00:05:45 +00001931 case Attr::CFReturnsNotRetained:
1932 case Attr::CFReturnsRetained:
1933 case Attr::NSReturnsNotRetained:
1934 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001935 case Attr::ObjCException:
1936 case Attr::ObjCNSObject:
1937 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001938 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001939 break;
1940
Anders Carlssona860e752009-08-08 18:23:56 +00001941 case Attr::PragmaPack:
1942 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001943 break;
1944
Anders Carlssona860e752009-08-08 18:23:56 +00001945 case Attr::Packed:
1946 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001948 case Attr::Pure:
1949 break;
1950
1951 case Attr::Regparm:
1952 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1953 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Nate Begeman6f3d8382009-06-26 06:32:41 +00001955 case Attr::ReqdWorkGroupSize:
1956 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1957 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1958 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1959 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001960
1961 case Attr::Section:
1962 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1963 break;
1964
1965 case Attr::StdCall:
1966 case Attr::TransparentUnion:
1967 case Attr::Unavailable:
1968 case Attr::Unused:
1969 case Attr::Used:
1970 break;
1971
1972 case Attr::Visibility:
1973 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001974 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001975 break;
1976
1977 case Attr::WarnUnusedResult:
1978 case Attr::Weak:
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001979 case Attr::WeakRef:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001980 case Attr::WeakImport:
1981 break;
1982 }
1983 }
1984
Douglas Gregorc9490c02009-04-16 22:23:12 +00001985 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001986}
1987
1988void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1989 Record.push_back(Str.size());
1990 Record.insert(Record.end(), Str.begin(), Str.end());
1991}
1992
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001993/// \brief Note that the identifier II occurs at the given offset
1994/// within the identifier table.
1995void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001996 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001997}
1998
Douglas Gregor83941df2009-04-25 17:48:32 +00001999/// \brief Note that the selector Sel occurs at the given offset
2000/// within the method pool/selector table.
2001void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2002 unsigned ID = SelectorIDs[Sel];
2003 assert(ID && "Unknown selector");
2004 SelectorOffsets[ID - 1] = Offset;
2005}
2006
Mike Stump1eb44332009-09-09 15:08:12 +00002007PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2008 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00002009 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2010 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002011
Douglas Gregore650c8c2009-07-07 00:12:59 +00002012void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2013 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002014 using namespace llvm;
2015
Douglas Gregore7785042009-04-20 15:53:59 +00002016 ASTContext &Context = SemaRef.Context;
2017 Preprocessor &PP = SemaRef.PP;
2018
Douglas Gregor2cf26342009-04-09 22:27:44 +00002019 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002020 Stream.Emit((unsigned)'C', 8);
2021 Stream.Emit((unsigned)'P', 8);
2022 Stream.Emit((unsigned)'C', 8);
2023 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002025 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002026
2027 // The translation unit is the first declaration we'll emit.
2028 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002029 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002030
Douglas Gregor2deaea32009-04-22 18:49:13 +00002031 // Make sure that we emit IdentifierInfos (and any attached
2032 // declarations) for builtins.
2033 {
2034 IdentifierTable &Table = PP.getIdentifierTable();
2035 llvm::SmallVector<const char *, 32> BuiltinNames;
2036 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2037 Context.getLangOptions().NoBuiltin);
2038 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2039 getIdentifierRef(&Table.get(BuiltinNames[I]));
2040 }
2041
Chris Lattner63d65f82009-09-08 18:19:27 +00002042 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002043 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002044 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002045 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002046 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2047 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002048 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002049
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002050 // Build a record containing all of the static unused functions in this file.
2051 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002052 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002053 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002054
Douglas Gregor14c22f22009-04-22 22:18:58 +00002055 // Build a record containing all of the locally-scoped external
2056 // declarations in this header file. Generally, this record will be
2057 // empty.
2058 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002059 // FIXME: This is filling in the PCH file in densemap order which is
2060 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002061 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002062 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2063 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2064 TD != TDEnd; ++TD)
2065 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2066
Douglas Gregorb81c1702009-04-27 20:06:05 +00002067 // Build a record containing all of the ext_vector declarations.
2068 RecordData ExtVectorDecls;
2069 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2070 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2071
Douglas Gregor2cf26342009-04-09 22:27:44 +00002072 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002073 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002074 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002075 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002076 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002077 if (StatCalls && !isysroot)
2078 WriteStatCache(*StatCalls, isysroot);
2079 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002080 // Write the record of special types.
2081 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002083 AddTypeRef(Context.getBuiltinVaListType(), Record);
2084 AddTypeRef(Context.getObjCIdType(), Record);
2085 AddTypeRef(Context.getObjCSelType(), Record);
2086 AddTypeRef(Context.getObjCProtoType(), Record);
2087 AddTypeRef(Context.getObjCClassType(), Record);
2088 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2089 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2090 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002091 AddTypeRef(Context.getjmp_bufType(), Record);
2092 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002093 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2094 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002095#if 0
2096 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +00002097 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002098#endif
Mike Stumpadaaad32009-10-20 02:12:22 +00002099 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002100 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002101 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Douglas Gregor366809a2009-04-26 03:49:13 +00002103 // Keep writing types and declarations until all types and
2104 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002105 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2106 WriteDeclsBlockAbbrevs();
2107 while (!DeclTypesToEmit.empty()) {
2108 DeclOrType DOT = DeclTypesToEmit.front();
2109 DeclTypesToEmit.pop();
2110 if (DOT.isType())
2111 WriteType(DOT.getType());
2112 else
2113 WriteDecl(Context, DOT.getDecl());
2114 }
2115 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002116
Douglas Gregor813a97b2009-10-17 17:25:45 +00002117 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002118 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002119 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002120
2121 // Write the type offsets array
2122 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2123 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2126 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2127 Record.clear();
2128 Record.push_back(pch::TYPE_OFFSET);
2129 Record.push_back(TypeOffsets.size());
2130 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002131 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002132 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002133
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002134 // Write the declaration offsets array
2135 Abbrev = new BitCodeAbbrev();
2136 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2139 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2140 Record.clear();
2141 Record.push_back(pch::DECL_OFFSET);
2142 Record.push_back(DeclOffsets.size());
2143 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002144 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002145 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002146
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002147 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002148 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002149 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002150
2151 // Write the record containing tentative definitions.
2152 if (!TentativeDefinitions.empty())
2153 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002154
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002155 // Write the record containing unused static functions.
2156 if (!UnusedStaticFuncs.empty())
2157 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002158
Douglas Gregor14c22f22009-04-22 22:18:58 +00002159 // Write the record containing locally-scoped external definitions.
2160 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002161 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002162 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002163
2164 // Write the record containing ext_vector type names.
2165 if (!ExtVectorDecls.empty())
2166 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002167
Douglas Gregor3e1af842009-04-17 22:13:46 +00002168 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002169 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002170 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002171 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002172 Record.push_back(NumLexicalDeclContexts);
2173 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002174 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002175 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002176}
2177
2178void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2179 Record.push_back(Loc.getRawEncoding());
2180}
2181
2182void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2183 Record.push_back(Value.getBitWidth());
2184 unsigned N = Value.getNumWords();
2185 const uint64_t* Words = Value.getRawData();
2186 for (unsigned I = 0; I != N; ++I)
2187 Record.push_back(Words[I]);
2188}
2189
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002190void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2191 Record.push_back(Value.isUnsigned());
2192 AddAPInt(Value, Record);
2193}
2194
Douglas Gregor17fc2232009-04-14 21:55:33 +00002195void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2196 AddAPInt(Value.bitcastToAPInt(), Record);
2197}
2198
Douglas Gregor2cf26342009-04-09 22:27:44 +00002199void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002200 Record.push_back(getIdentifierRef(II));
2201}
2202
2203pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2204 if (II == 0)
2205 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002206
2207 pch::IdentID &ID = IdentifierIDs[II];
2208 if (ID == 0)
2209 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002210 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002211}
2212
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002213pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2214 if (MD == 0)
2215 return 0;
2216
2217 pch::IdentID &ID = MacroDefinitions[MD];
2218 if (ID == 0)
2219 ID = MacroDefinitions.size();
2220 return ID;
2221}
2222
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002223void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2224 if (SelRef.getAsOpaquePtr() == 0) {
2225 Record.push_back(0);
2226 return;
2227 }
2228
2229 pch::SelectorID &SID = SelectorIDs[SelRef];
2230 if (SID == 0) {
2231 SID = SelectorIDs.size();
2232 SelVector.push_back(SelRef);
2233 }
2234 Record.push_back(SID);
2235}
2236
John McCall833ca992009-10-29 08:12:44 +00002237void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2238 RecordData &Record) {
2239 switch (Arg.getArgument().getKind()) {
2240 case TemplateArgument::Expression:
2241 AddStmt(Arg.getLocInfo().getAsExpr());
2242 break;
2243 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002244 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002245 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002246 case TemplateArgument::Template:
2247 Record.push_back(
2248 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2249 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2250 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2251 break;
John McCall833ca992009-10-29 08:12:44 +00002252 case TemplateArgument::Null:
2253 case TemplateArgument::Integral:
2254 case TemplateArgument::Declaration:
2255 case TemplateArgument::Pack:
2256 break;
2257 }
2258}
2259
John McCalla93c9342009-12-07 02:54:59 +00002260void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2261 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002262 AddTypeRef(QualType(), Record);
2263 return;
2264 }
2265
John McCalla93c9342009-12-07 02:54:59 +00002266 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002267 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002268 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002269 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002270}
2271
Douglas Gregor2cf26342009-04-09 22:27:44 +00002272void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2273 if (T.isNull()) {
2274 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2275 return;
2276 }
2277
Douglas Gregora4923eb2009-11-16 21:35:15 +00002278 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002279 T.removeFastQualifiers();
2280
Douglas Gregora4923eb2009-11-16 21:35:15 +00002281 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002282 pch::TypeID &ID = TypeIDs[T];
2283 if (ID == 0) {
2284 // We haven't seen these qualifiers applied to this type before.
2285 // Assign it a new ID. This is the only time we enqueue a
2286 // qualified type, and it has no CV qualifiers.
2287 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002288 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002289 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002290
John McCall0953e762009-09-24 19:53:00 +00002291 // Encode the type qualifiers in the type reference.
2292 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2293 return;
2294 }
2295
Douglas Gregora4923eb2009-11-16 21:35:15 +00002296 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002297
Douglas Gregor2cf26342009-04-09 22:27:44 +00002298 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002299 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002300 switch (BT->getKind()) {
2301 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2302 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2303 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2304 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2305 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2306 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2307 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2308 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002309 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002310 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2311 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2312 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2313 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2314 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2315 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2316 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002317 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002318 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2319 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2320 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002321 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002322 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2323 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002324 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2325 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002326 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2327 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002328 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002329 case BuiltinType::UndeducedAuto:
2330 assert(0 && "Should not see undeduced auto here");
2331 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002332 }
2333
John McCall0953e762009-09-24 19:53:00 +00002334 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002335 return;
2336 }
2337
John McCall0953e762009-09-24 19:53:00 +00002338 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002339 if (ID == 0) {
2340 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002341 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002342 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002343 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002344 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002345
2346 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002347 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002348}
2349
2350void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2351 if (D == 0) {
2352 Record.push_back(0);
2353 return;
2354 }
2355
Douglas Gregor8038d512009-04-10 17:25:41 +00002356 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002357 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002358 // We haven't seen this declaration before. Give it a new ID and
2359 // enqueue it in the list of declarations to emit.
2360 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002361 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002362 }
2363
2364 Record.push_back(ID);
2365}
2366
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002367pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2368 if (D == 0)
2369 return 0;
2370
2371 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2372 return DeclIDs[D];
2373}
2374
Douglas Gregor2cf26342009-04-09 22:27:44 +00002375void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002376 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002377 Record.push_back(Name.getNameKind());
2378 switch (Name.getNameKind()) {
2379 case DeclarationName::Identifier:
2380 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2381 break;
2382
2383 case DeclarationName::ObjCZeroArgSelector:
2384 case DeclarationName::ObjCOneArgSelector:
2385 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002386 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002387 break;
2388
2389 case DeclarationName::CXXConstructorName:
2390 case DeclarationName::CXXDestructorName:
2391 case DeclarationName::CXXConversionFunctionName:
2392 AddTypeRef(Name.getCXXNameType(), Record);
2393 break;
2394
2395 case DeclarationName::CXXOperatorName:
2396 Record.push_back(Name.getCXXOverloadedOperator());
2397 break;
2398
Sean Hunt3e518bd2009-11-29 07:34:05 +00002399 case DeclarationName::CXXLiteralOperatorName:
2400 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2401 break;
2402
Douglas Gregor2cf26342009-04-09 22:27:44 +00002403 case DeclarationName::CXXUsingDirective:
2404 // No extra data to emit
2405 break;
2406 }
2407}