blob: 88d197271d16c9d2f6164f92d638ec29718e8fb2 [file] [log] [blame]
Chris Lattnere127a0d2010-04-20 20:35:58 +00001//===--- PCHWriter.cpp - Precompiled Headers Writer -----------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
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"
John McCall31f17ec2010-04-27 00:57:59 +000066 void VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor2cf26342009-04-09 22:27:44 +000067 };
68}
69
Douglas Gregor2cf26342009-04-09 22:27:44 +000070void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
71 assert(false && "Built-in types are never serialized");
72}
73
Douglas Gregor2cf26342009-04-09 22:27:44 +000074void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
75 Writer.AddTypeRef(T->getElementType(), Record);
76 Code = pch::TYPE_COMPLEX;
77}
78
79void PCHTypeWriter::VisitPointerType(const PointerType *T) {
80 Writer.AddTypeRef(T->getPointeeType(), Record);
81 Code = pch::TYPE_POINTER;
82}
83
84void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000085 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000086 Code = pch::TYPE_BLOCK_POINTER;
87}
88
89void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
90 Writer.AddTypeRef(T->getPointeeType(), Record);
91 Code = pch::TYPE_LVALUE_REFERENCE;
92}
93
94void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
95 Writer.AddTypeRef(T->getPointeeType(), Record);
96 Code = pch::TYPE_RVALUE_REFERENCE;
97}
98
99void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000100 Writer.AddTypeRef(T->getPointeeType(), Record);
101 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Code = pch::TYPE_MEMBER_POINTER;
103}
104
105void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
106 Writer.AddTypeRef(T->getElementType(), Record);
107 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000108 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
111void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
112 VisitArrayType(T);
113 Writer.AddAPInt(T->getSize(), Record);
114 Code = pch::TYPE_CONSTANT_ARRAY;
115}
116
117void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
118 VisitArrayType(T);
119 Code = pch::TYPE_INCOMPLETE_ARRAY;
120}
121
122void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
123 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000124 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
125 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000126 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127 Code = pch::TYPE_VARIABLE_ARRAY;
128}
129
130void PCHTypeWriter::VisitVectorType(const VectorType *T) {
131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getNumElements());
John Thompson82287d12010-02-05 00:12:22 +0000133 Record.push_back(T->isAltiVec());
134 Record.push_back(T->isPixel());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135 Code = pch::TYPE_VECTOR;
136}
137
138void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
139 VisitVectorType(T);
140 Code = pch::TYPE_EXT_VECTOR;
141}
142
143void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
144 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000145 FunctionType::ExtInfo C = T->getExtInfo();
146 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000147 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000148 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000149 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000150}
151
152void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
153 VisitFunctionType(T);
154 Code = pch::TYPE_FUNCTION_NO_PROTO;
155}
156
157void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
158 VisitFunctionType(T);
159 Record.push_back(T->getNumArgs());
160 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
161 Writer.AddTypeRef(T->getArgType(I), Record);
162 Record.push_back(T->isVariadic());
163 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000164 Record.push_back(T->hasExceptionSpec());
165 Record.push_back(T->hasAnyExceptionSpec());
166 Record.push_back(T->getNumExceptions());
167 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
168 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169 Code = pch::TYPE_FUNCTION_PROTO;
170}
171
John McCalled976492009-12-04 22:46:56 +0000172#if 0
173// For when we want it....
174void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
175 Writer.AddDeclRef(T->getDecl(), Record);
176 Code = pch::TYPE_UNRESOLVED_USING;
177}
178#endif
179
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
181 Writer.AddDeclRef(T->getDecl(), Record);
182 Code = pch::TYPE_TYPEDEF;
183}
184
185void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000186 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000187 Code = pch::TYPE_TYPEOF_EXPR;
188}
189
190void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
191 Writer.AddTypeRef(T->getUnderlyingType(), Record);
192 Code = pch::TYPE_TYPEOF;
193}
194
Anders Carlsson395b4752009-06-24 19:06:50 +0000195void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
196 Writer.AddStmt(T->getUnderlyingExpr());
197 Code = pch::TYPE_DECLTYPE;
198}
199
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200void PCHTypeWriter::VisitTagType(const TagType *T) {
201 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000202 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000203 "Cannot serialize in the middle of a type definition");
204}
205
206void PCHTypeWriter::VisitRecordType(const RecordType *T) {
207 VisitTagType(T);
208 Code = pch::TYPE_RECORD;
209}
210
211void PCHTypeWriter::VisitEnumType(const EnumType *T) {
212 VisitTagType(T);
213 Code = pch::TYPE_ENUM;
214}
215
Mike Stump1eb44332009-09-09 15:08:12 +0000216void
John McCall49a832b2009-10-18 09:09:24 +0000217PCHTypeWriter::VisitSubstTemplateTypeParmType(
218 const SubstTemplateTypeParmType *T) {
219 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
220 Writer.AddTypeRef(T->getReplacementType(), Record);
221 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
222}
223
224void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225PCHTypeWriter::VisitTemplateSpecializationType(
226 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000227 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228 assert(false && "Cannot serialize template specialization types");
229}
230
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000231void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
232 Writer.AddTypeRef(T->getNamedType(), Record);
233 Record.push_back(T->getKeyword());
234 // FIXME: Serialize the qualifier (C++ only)
235 assert(T->getQualifier() == 0 && "Cannot serialize qualified name types");
236 Code = pch::TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000237}
238
John McCall3cb0ebd2010-03-10 03:28:59 +0000239void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
240 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000241 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000242 Code = pch::TYPE_INJECTED_CLASS_NAME;
243}
244
Douglas Gregor2cf26342009-04-09 22:27:44 +0000245void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
246 Writer.AddDeclRef(T->getDecl(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000247 Code = pch::TYPE_OBJC_INTERFACE;
248}
249
250void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
251 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000252 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000253 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000254 E = T->qual_end(); I != E; ++I)
255 Writer.AddDeclRef(*I, Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000256 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257}
258
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000259void
260PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000261 Writer.AddTypeRef(T->getPointeeType(), 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}
John McCall51bd8032009-10-18 01:05:36 +0000384void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
385 Writer.AddSourceLocation(TL.getNameLoc(), Record);
386}
John McCall49a832b2009-10-18 09:09:24 +0000387void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
388 SubstTemplateTypeParmTypeLoc TL) {
389 Writer.AddSourceLocation(TL.getNameLoc(), Record);
390}
John McCall51bd8032009-10-18 01:05:36 +0000391void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
392 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000393 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
394 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
395 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
396 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
397 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000398}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000399void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Daniel Dunbara63db842010-05-14 16:34:09 +0000400 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000401}
John McCall3cb0ebd2010-03-10 03:28:59 +0000402void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
403 Writer.AddSourceLocation(TL.getNameLoc(), Record);
404}
Douglas Gregor4714c122010-03-31 17:34:00 +0000405void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
John McCall51bd8032009-10-18 01:05:36 +0000406 Writer.AddSourceLocation(TL.getNameLoc(), Record);
407}
408void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000410}
411void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
412 Record.push_back(TL.hasBaseTypeAsWritten());
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);
John McCall54e14c42009-10-22 22:37:11 +0000420}
John McCalla1ee0c52009-10-16 21:56:05 +0000421
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000422//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000423// PCHWriter Implementation
424//===----------------------------------------------------------------------===//
425
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000426static void EmitBlockID(unsigned ID, const char *Name,
427 llvm::BitstreamWriter &Stream,
428 PCHWriter::RecordData &Record) {
429 Record.clear();
430 Record.push_back(ID);
431 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
432
433 // Emit the block name if present.
434 if (Name == 0 || Name[0] == 0) return;
435 Record.clear();
436 while (*Name)
437 Record.push_back(*Name++);
438 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
439}
440
441static void EmitRecordID(unsigned ID, const char *Name,
442 llvm::BitstreamWriter &Stream,
443 PCHWriter::RecordData &Record) {
444 Record.clear();
445 Record.push_back(ID);
446 while (*Name)
447 Record.push_back(*Name++);
448 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000449}
450
451static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
452 PCHWriter::RecordData &Record) {
453#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
454 RECORD(STMT_STOP);
455 RECORD(STMT_NULL_PTR);
456 RECORD(STMT_NULL);
457 RECORD(STMT_COMPOUND);
458 RECORD(STMT_CASE);
459 RECORD(STMT_DEFAULT);
460 RECORD(STMT_LABEL);
461 RECORD(STMT_IF);
462 RECORD(STMT_SWITCH);
463 RECORD(STMT_WHILE);
464 RECORD(STMT_DO);
465 RECORD(STMT_FOR);
466 RECORD(STMT_GOTO);
467 RECORD(STMT_INDIRECT_GOTO);
468 RECORD(STMT_CONTINUE);
469 RECORD(STMT_BREAK);
470 RECORD(STMT_RETURN);
471 RECORD(STMT_DECL);
472 RECORD(STMT_ASM);
473 RECORD(EXPR_PREDEFINED);
474 RECORD(EXPR_DECL_REF);
475 RECORD(EXPR_INTEGER_LITERAL);
476 RECORD(EXPR_FLOATING_LITERAL);
477 RECORD(EXPR_IMAGINARY_LITERAL);
478 RECORD(EXPR_STRING_LITERAL);
479 RECORD(EXPR_CHARACTER_LITERAL);
480 RECORD(EXPR_PAREN);
481 RECORD(EXPR_UNARY_OPERATOR);
482 RECORD(EXPR_SIZEOF_ALIGN_OF);
483 RECORD(EXPR_ARRAY_SUBSCRIPT);
484 RECORD(EXPR_CALL);
485 RECORD(EXPR_MEMBER);
486 RECORD(EXPR_BINARY_OPERATOR);
487 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
488 RECORD(EXPR_CONDITIONAL_OPERATOR);
489 RECORD(EXPR_IMPLICIT_CAST);
490 RECORD(EXPR_CSTYLE_CAST);
491 RECORD(EXPR_COMPOUND_LITERAL);
492 RECORD(EXPR_EXT_VECTOR_ELEMENT);
493 RECORD(EXPR_INIT_LIST);
494 RECORD(EXPR_DESIGNATED_INIT);
495 RECORD(EXPR_IMPLICIT_VALUE_INIT);
496 RECORD(EXPR_VA_ARG);
497 RECORD(EXPR_ADDR_LABEL);
498 RECORD(EXPR_STMT);
499 RECORD(EXPR_TYPES_COMPATIBLE);
500 RECORD(EXPR_CHOOSE);
501 RECORD(EXPR_GNU_NULL);
502 RECORD(EXPR_SHUFFLE_VECTOR);
503 RECORD(EXPR_BLOCK);
504 RECORD(EXPR_BLOCK_DECL_REF);
505 RECORD(EXPR_OBJC_STRING_LITERAL);
506 RECORD(EXPR_OBJC_ENCODE);
507 RECORD(EXPR_OBJC_SELECTOR_EXPR);
508 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
509 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
510 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
511 RECORD(EXPR_OBJC_KVC_REF_EXPR);
512 RECORD(EXPR_OBJC_MESSAGE_EXPR);
513 RECORD(EXPR_OBJC_SUPER_EXPR);
514 RECORD(STMT_OBJC_FOR_COLLECTION);
515 RECORD(STMT_OBJC_CATCH);
516 RECORD(STMT_OBJC_FINALLY);
517 RECORD(STMT_OBJC_AT_TRY);
518 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
519 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000520 RECORD(EXPR_CXX_OPERATOR_CALL);
521 RECORD(EXPR_CXX_CONSTRUCT);
522 RECORD(EXPR_CXX_STATIC_CAST);
523 RECORD(EXPR_CXX_DYNAMIC_CAST);
524 RECORD(EXPR_CXX_REINTERPRET_CAST);
525 RECORD(EXPR_CXX_CONST_CAST);
526 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
527 RECORD(EXPR_CXX_BOOL_LITERAL);
528 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000529#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000530}
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000532void PCHWriter::WriteBlockInfoBlock() {
533 RecordData Record;
534 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Chris Lattner2f4efd12009-04-27 00:40:25 +0000536#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000537#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000539 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000540 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000541 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000542 RECORD(TYPE_OFFSET);
543 RECORD(DECL_OFFSET);
544 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000545 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000546 RECORD(IDENTIFIER_OFFSET);
547 RECORD(IDENTIFIER_TABLE);
548 RECORD(EXTERNAL_DEFINITIONS);
549 RECORD(SPECIAL_TYPES);
550 RECORD(STATISTICS);
551 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000552 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000553 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
554 RECORD(SELECTOR_OFFSETS);
555 RECORD(METHOD_POOL);
556 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000557 RECORD(SOURCE_LOCATION_OFFSETS);
558 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000559 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000560 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000561 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000562 RECORD(UNUSED_STATIC_FUNCS);
563 RECORD(MACRO_DEFINITION_OFFSETS);
564
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000565 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000566 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000567 RECORD(SM_SLOC_FILE_ENTRY);
568 RECORD(SM_SLOC_BUFFER_ENTRY);
569 RECORD(SM_SLOC_BUFFER_BLOB);
570 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
571 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000573 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000574 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000575 RECORD(PP_MACRO_OBJECT_LIKE);
576 RECORD(PP_MACRO_FUNCTION_LIKE);
577 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000578 RECORD(PP_MACRO_INSTANTIATION);
579 RECORD(PP_MACRO_DEFINITION);
580
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000581 // Decls and Types block.
582 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000583 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000584 RECORD(TYPE_COMPLEX);
585 RECORD(TYPE_POINTER);
586 RECORD(TYPE_BLOCK_POINTER);
587 RECORD(TYPE_LVALUE_REFERENCE);
588 RECORD(TYPE_RVALUE_REFERENCE);
589 RECORD(TYPE_MEMBER_POINTER);
590 RECORD(TYPE_CONSTANT_ARRAY);
591 RECORD(TYPE_INCOMPLETE_ARRAY);
592 RECORD(TYPE_VARIABLE_ARRAY);
593 RECORD(TYPE_VECTOR);
594 RECORD(TYPE_EXT_VECTOR);
595 RECORD(TYPE_FUNCTION_PROTO);
596 RECORD(TYPE_FUNCTION_NO_PROTO);
597 RECORD(TYPE_TYPEDEF);
598 RECORD(TYPE_TYPEOF_EXPR);
599 RECORD(TYPE_TYPEOF);
600 RECORD(TYPE_RECORD);
601 RECORD(TYPE_ENUM);
602 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000603 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000604 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000605 RECORD(DECL_ATTR);
606 RECORD(DECL_TRANSLATION_UNIT);
607 RECORD(DECL_TYPEDEF);
608 RECORD(DECL_ENUM);
609 RECORD(DECL_RECORD);
610 RECORD(DECL_ENUM_CONSTANT);
611 RECORD(DECL_FUNCTION);
612 RECORD(DECL_OBJC_METHOD);
613 RECORD(DECL_OBJC_INTERFACE);
614 RECORD(DECL_OBJC_PROTOCOL);
615 RECORD(DECL_OBJC_IVAR);
616 RECORD(DECL_OBJC_AT_DEFS_FIELD);
617 RECORD(DECL_OBJC_CLASS);
618 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
619 RECORD(DECL_OBJC_CATEGORY);
620 RECORD(DECL_OBJC_CATEGORY_IMPL);
621 RECORD(DECL_OBJC_IMPLEMENTATION);
622 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
623 RECORD(DECL_OBJC_PROPERTY);
624 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000625 RECORD(DECL_FIELD);
626 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000627 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000628 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000629 RECORD(DECL_FILE_SCOPE_ASM);
630 RECORD(DECL_BLOCK);
631 RECORD(DECL_CONTEXT_LEXICAL);
632 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000633 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000634 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000635#undef RECORD
636#undef BLOCK
637 Stream.ExitBlock();
638}
639
Douglas Gregore650c8c2009-07-07 00:12:59 +0000640/// \brief Adjusts the given filename to only write out the portion of the
641/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000642///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000643/// \param Filename the file name to adjust.
644///
645/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
646/// the returned filename will be adjusted by this system root.
647///
648/// \returns either the original filename (if it needs no adjustment) or the
649/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000650static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000651adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
652 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Douglas Gregore650c8c2009-07-07 00:12:59 +0000654 if (!isysroot)
655 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Douglas Gregore650c8c2009-07-07 00:12:59 +0000657 // Verify that the filename and the system root have the same prefix.
658 unsigned Pos = 0;
659 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
660 if (Filename[Pos] != isysroot[Pos])
661 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Douglas Gregore650c8c2009-07-07 00:12:59 +0000663 // We hit the end of the filename before we hit the end of the system root.
664 if (!Filename[Pos])
665 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Douglas Gregore650c8c2009-07-07 00:12:59 +0000667 // If the file name has a '/' at the current position, skip over the '/'.
668 // We distinguish sysroot-based includes from absolute includes by the
669 // absence of '/' at the beginning of sysroot-based includes.
670 if (Filename[Pos] == '/')
671 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Douglas Gregore650c8c2009-07-07 00:12:59 +0000673 return Filename + Pos;
674}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000675
Douglas Gregorab41e632009-04-27 22:23:34 +0000676/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000677void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000678 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000679
Douglas Gregore650c8c2009-07-07 00:12:59 +0000680 // Metadata
681 const TargetInfo &Target = Context.Target;
682 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
683 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
684 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
685 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
686 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
687 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
688 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
689 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
690 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Douglas Gregore650c8c2009-07-07 00:12:59 +0000692 RecordData Record;
693 Record.push_back(pch::METADATA);
694 Record.push_back(pch::VERSION_MAJOR);
695 Record.push_back(pch::VERSION_MINOR);
696 Record.push_back(CLANG_VERSION_MAJOR);
697 Record.push_back(CLANG_VERSION_MINOR);
698 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000699 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000700 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Douglas Gregorb64c1932009-05-12 01:31:05 +0000702 // Original file name
703 SourceManager &SM = Context.getSourceManager();
704 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
705 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
706 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
707 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
708 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
709
710 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000712 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000713
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000714 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000715 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000716 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000717 RecordData Record;
718 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000719 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000720 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000721
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000722 // Repository branch/version information.
723 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
724 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
725 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
726 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000727 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000728 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000729 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
730 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000731}
732
733/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000734void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
735 RecordData Record;
736 Record.push_back(LangOpts.Trigraphs);
737 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
738 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
739 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
740 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000741 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000742 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
743 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
744 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
745 Record.push_back(LangOpts.C99); // C99 Support
746 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
747 Record.push_back(LangOpts.CPlusPlus); // C++ Support
748 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000749 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000751 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
752 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000753 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000754 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000755 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000756 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000757 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000759 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000760 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
761 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000762 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000763 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000764 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000765
766 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
767 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
768 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
769
Chris Lattnerea5ce472009-04-27 07:35:58 +0000770 // Whether static initializers are protected by locks.
771 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000772 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000773 Record.push_back(LangOpts.Blocks); // block extension to C
774 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
775 // they are unused.
776 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
777 // (modulo the platform support).
778
779 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
780 // signed integer arithmetic overflows.
781
782 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
783 // may be ripped out at any time.
784
785 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000786 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000787 // defined.
788 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
789 // opposed to __DYNAMIC__).
790 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
791
792 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
793 // used (instead of C99 semantics).
794 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000795 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
796 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000797 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
798 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000799 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000800 Record.push_back(LangOpts.getGCMode());
801 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000802 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000803 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000804 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000805 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000806 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000807 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000808}
809
Douglas Gregor14f79002009-04-10 03:52:48 +0000810//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000811// stat cache Serialization
812//===----------------------------------------------------------------------===//
813
814namespace {
815// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000816class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000817public:
818 typedef const char * key_type;
819 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000821 typedef std::pair<int, struct stat> data_type;
822 typedef const data_type& data_type_ref;
823
824 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000825 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
828 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000829 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
830 data_type_ref Data) {
831 unsigned StrLen = strlen(path);
832 clang::io::Emit16(Out, StrLen);
833 unsigned DataLen = 1; // result value
834 if (Data.first == 0)
835 DataLen += 4 + 4 + 2 + 8 + 8;
836 clang::io::Emit8(Out, DataLen);
837 return std::make_pair(StrLen + 1, DataLen);
838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000840 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
841 Out.write(path, KeyLen);
842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000844 void EmitData(llvm::raw_ostream& Out, key_type_ref,
845 data_type_ref Data, unsigned DataLen) {
846 using namespace clang::io;
847 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000849 // Result of stat()
850 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000852 if (Data.first == 0) {
853 Emit32(Out, (uint32_t) Data.second.st_ino);
854 Emit32(Out, (uint32_t) Data.second.st_dev);
855 Emit16(Out, (uint16_t) Data.second.st_mode);
856 Emit64(Out, (uint64_t) Data.second.st_mtime);
857 Emit64(Out, (uint64_t) Data.second.st_size);
858 }
859
860 assert(Out.tell() - Start == DataLen && "Wrong data length");
861 }
862};
863} // end anonymous namespace
864
865/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000866void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
867 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000868 // Build the on-disk hash table containing information about every
869 // stat() call.
870 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
871 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000872 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000873 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000874 Stat != StatEnd; ++Stat, ++NumStatEntries) {
875 const char *Filename = Stat->first();
876 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
877 Generator.insert(Filename, Stat->second);
878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000880 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000881 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000882 uint32_t BucketOffset;
883 {
884 llvm::raw_svector_ostream Out(StatCacheData);
885 // Make sure that no bucket is at offset 0
886 clang::io::Emit32(Out, 0);
887 BucketOffset = Generator.Emit(Out);
888 }
889
890 // Create a blob abbreviation
891 using namespace llvm;
892 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
893 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
897 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
898
899 // Write the stat cache
900 RecordData Record;
901 Record.push_back(pch::STAT_CACHE);
902 Record.push_back(BucketOffset);
903 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000904 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000905}
906
907//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000908// Source Manager Serialization
909//===----------------------------------------------------------------------===//
910
911/// \brief Create an abbreviation for the SLocEntry that refers to a
912/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000913static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000914 using namespace llvm;
915 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
916 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000921 // FileEntry fields.
922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000924 // HeaderFileInfo fields.
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000930 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000931}
932
933/// \brief Create an abbreviation for the SLocEntry that refers to a
934/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000935static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000936 using namespace llvm;
937 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
938 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000944 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000945}
946
947/// \brief Create an abbreviation for the SLocEntry that refers to a
948/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000949static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000950 using namespace llvm;
951 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
952 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000954 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000955}
956
957/// \brief Create an abbreviation for the SLocEntry that refers to an
958/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000959static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000960 using namespace llvm;
961 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
962 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000968 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000969}
970
971/// \brief Writes the block containing the serialized form of the
972/// source manager.
973///
974/// TODO: We should probably use an on-disk hash table (stored in a
975/// blob), indexed based on the file name, so that we only create
976/// entries for files that we actually need. In the common case (no
977/// errors), we probably won't have to create file entries for any of
978/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000979void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000980 const Preprocessor &PP,
981 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000982 RecordData Record;
983
Chris Lattnerf04ad692009-04-10 17:16:57 +0000984 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000985 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000986
987 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000988 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
989 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
990 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
991 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000992
Douglas Gregorbd945002009-04-13 16:31:14 +0000993 // Write the line table.
994 if (SourceMgr.hasLineTable()) {
995 LineTableInfo &LineTable = SourceMgr.getLineTable();
996
997 // Emit the file names
998 Record.push_back(LineTable.getNumFilenames());
999 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1000 // Emit the file name
1001 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001002 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001003 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1004 Record.push_back(FilenameLen);
1005 if (FilenameLen)
1006 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Douglas Gregorbd945002009-04-13 16:31:14 +00001009 // Emit the line entries
1010 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1011 L != LEnd; ++L) {
1012 // Emit the file ID
1013 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregorbd945002009-04-13 16:31:14 +00001015 // Emit the line entries
1016 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001017 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001018 LEEnd = L->second.end();
1019 LE != LEEnd; ++LE) {
1020 Record.push_back(LE->FileOffset);
1021 Record.push_back(LE->LineNo);
1022 Record.push_back(LE->FilenameID);
1023 Record.push_back((unsigned)LE->FileKind);
1024 Record.push_back(LE->IncludeOffset);
1025 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001026 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001027 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001028 }
1029
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001030 // Write out the source location entry table. We skip the first
1031 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001032 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001033 RecordData PreloadSLocs;
1034 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001035 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1036 // Get this source location entry.
1037 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001038
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001039 // Record the offset of this source-location entry.
1040 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1041
1042 // Figure out which record code to use.
1043 unsigned Code;
1044 if (SLoc->isFile()) {
1045 if (SLoc->getFile().getContentCache()->Entry)
1046 Code = pch::SM_SLOC_FILE_ENTRY;
1047 else
1048 Code = pch::SM_SLOC_BUFFER_ENTRY;
1049 } else
1050 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1051 Record.clear();
1052 Record.push_back(Code);
1053
1054 Record.push_back(SLoc->getOffset());
1055 if (SLoc->isFile()) {
1056 const SrcMgr::FileInfo &File = SLoc->getFile();
1057 Record.push_back(File.getIncludeLoc().getRawEncoding());
1058 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1059 Record.push_back(File.hasLineDirectives());
1060
1061 const SrcMgr::ContentCache *Content = File.getContentCache();
1062 if (Content->Entry) {
1063 // The source location entry is a file. The blob associated
1064 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Douglas Gregor2d52be52010-03-21 22:49:54 +00001066 // Emit size/modification time for this file.
1067 Record.push_back(Content->Entry->getSize());
1068 Record.push_back(Content->Entry->getModificationTime());
1069
Douglas Gregor12fab312010-03-16 16:35:32 +00001070 // Emit header-search information associated with this file.
1071 HeaderFileInfo HFI;
1072 HeaderSearch &HS = PP.getHeaderSearchInfo();
1073 if (Content->Entry->getUID() < HS.header_file_size())
1074 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1075 Record.push_back(HFI.isImport);
1076 Record.push_back(HFI.DirInfo);
1077 Record.push_back(HFI.NumIncludes);
1078 AddIdentifierRef(HFI.ControllingMacro, Record);
1079
Douglas Gregore650c8c2009-07-07 00:12:59 +00001080 // Turn the file name into an absolute path, if it isn't already.
1081 const char *Filename = Content->Entry->getName();
1082 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001083 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001084 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Douglas Gregore650c8c2009-07-07 00:12:59 +00001086 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001087 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001088
1089 // FIXME: For now, preload all file source locations, so that
1090 // we get the appropriate File entries in the reader. This is
1091 // a temporary measure.
1092 PreloadSLocs.push_back(SLocEntryOffsets.size());
1093 } else {
1094 // The source location entry is a buffer. The blob associated
1095 // with this entry contains the contents of the buffer.
1096
1097 // We add one to the size so that we capture the trailing NULL
1098 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1099 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001100 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001101 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001102 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001103 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1104 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001105 Record.clear();
1106 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1107 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001108 llvm::StringRef(Buffer->getBufferStart(),
1109 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001110
1111 if (strcmp(Name, "<built-in>") == 0)
1112 PreloadSLocs.push_back(SLocEntryOffsets.size());
1113 }
1114 } else {
1115 // The source location entry is an instantiation.
1116 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1117 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1118 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1119 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1120
1121 // Compute the token length for this macro expansion.
1122 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001123 if (I + 1 != N)
1124 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001125 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1126 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1127 }
1128 }
1129
Douglas Gregorc9490c02009-04-16 22:23:12 +00001130 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131
1132 if (SLocEntryOffsets.empty())
1133 return;
1134
1135 // Write the source-location offsets table into the PCH block. This
1136 // table is used for lazily loading source-location information.
1137 using namespace llvm;
1138 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1139 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1140 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1141 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1142 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1143 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001145 Record.clear();
1146 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1147 Record.push_back(SLocEntryOffsets.size());
1148 Record.push_back(SourceMgr.getNextOffset());
1149 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001150 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001151 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001152
1153 // Write the source location entry preloads array, telling the PCH
1154 // reader which source locations entries it should load eagerly.
1155 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001156}
1157
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001158//===----------------------------------------------------------------------===//
1159// Preprocessor Serialization
1160//===----------------------------------------------------------------------===//
1161
Chris Lattner0b1fb982009-04-10 17:15:23 +00001162/// \brief Writes the block containing the serialized form of the
1163/// preprocessor.
1164///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001165void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001166 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001167
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001168 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1169 if (PP.getCounterValue() != 0) {
1170 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001171 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001172 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001173 }
1174
1175 // Enter the preprocessor block.
1176 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001178 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1179 // FIXME: use diagnostics subsystem for localization etc.
1180 if (PP.SawDateOrTime())
1181 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001183 // Loop over all the macro definitions that are live at the end of the file,
1184 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001185 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001186 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1187 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001188 // FIXME: This emits macros in hash table order, we should do it in a stable
1189 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001190 MacroInfo *MI = I->second;
1191
1192 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1193 // been redefined by the header (in which case they are not isBuiltinMacro).
1194 if (MI->isBuiltinMacro())
1195 continue;
1196
Chris Lattner7356a312009-04-11 21:15:38 +00001197 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001198 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001199 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1200 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001202 unsigned Code;
1203 if (MI->isObjectLike()) {
1204 Code = pch::PP_MACRO_OBJECT_LIKE;
1205 } else {
1206 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001208 Record.push_back(MI->isC99Varargs());
1209 Record.push_back(MI->isGNUVarargs());
1210 Record.push_back(MI->getNumArgs());
1211 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1212 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001213 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001214 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001215
1216 // If we have a detailed preprocessing record, record the macro definition
1217 // ID that corresponds to this macro.
1218 if (PPRec)
1219 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1220
Douglas Gregorc9490c02009-04-16 22:23:12 +00001221 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001222 Record.clear();
1223
Chris Lattnerdf961c22009-04-10 18:08:30 +00001224 // Emit the tokens array.
1225 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1226 // Note that we know that the preprocessor does not have any annotation
1227 // tokens in it because they are created by the parser, and thus can't be
1228 // in a macro definition.
1229 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Chris Lattnerdf961c22009-04-10 18:08:30 +00001231 Record.push_back(Tok.getLocation().getRawEncoding());
1232 Record.push_back(Tok.getLength());
1233
Chris Lattnerdf961c22009-04-10 18:08:30 +00001234 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1235 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001236 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Chris Lattnerdf961c22009-04-10 18:08:30 +00001238 // FIXME: Should translate token kind to a stable encoding.
1239 Record.push_back(Tok.getKind());
1240 // FIXME: Should translate token flags to a stable encoding.
1241 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregorc9490c02009-04-16 22:23:12 +00001243 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001244 Record.clear();
1245 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001246 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001247 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001248
1249 // If the preprocessor has a preprocessing record, emit it.
1250 unsigned NumPreprocessingRecords = 0;
1251 if (PPRec) {
1252 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1253 E != EEnd; ++E) {
1254 Record.clear();
1255
1256 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1257 Record.push_back(NumPreprocessingRecords++);
1258 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1259 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1260 AddIdentifierRef(MI->getName(), Record);
1261 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1262 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1263 continue;
1264 }
1265
1266 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1267 // Record this macro definition's location.
1268 pch::IdentID ID = getMacroDefinitionID(MD);
1269 if (ID != MacroDefinitionOffsets.size()) {
1270 if (ID > MacroDefinitionOffsets.size())
1271 MacroDefinitionOffsets.resize(ID + 1);
1272
1273 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1274 } else
1275 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1276
1277 Record.push_back(NumPreprocessingRecords++);
1278 Record.push_back(ID);
1279 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1280 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1281 AddIdentifierRef(MD->getName(), Record);
1282 AddSourceLocation(MD->getLocation(), Record);
1283 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1284 continue;
1285 }
1286 }
1287 }
1288
Douglas Gregorc9490c02009-04-16 22:23:12 +00001289 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001290
1291 // Write the offsets table for the preprocessing record.
1292 if (NumPreprocessingRecords > 0) {
1293 // Write the offsets table for identifier IDs.
1294 using namespace llvm;
1295 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1296 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1300 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1301
1302 Record.clear();
1303 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1304 Record.push_back(NumPreprocessingRecords);
1305 Record.push_back(MacroDefinitionOffsets.size());
1306 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1307 (const char *)&MacroDefinitionOffsets.front(),
1308 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1309 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001310}
1311
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001312//===----------------------------------------------------------------------===//
1313// Type Serialization
1314//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001315
Douglas Gregor2cf26342009-04-09 22:27:44 +00001316/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001317void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001318 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001319 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001320 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Douglas Gregor2cf26342009-04-09 22:27:44 +00001322 // Record the offset for this type.
1323 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001324 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001325 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1326 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001327 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001328 }
1329
1330 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Douglas Gregor2cf26342009-04-09 22:27:44 +00001332 // Emit the type's representation.
1333 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001334
Douglas Gregora4923eb2009-11-16 21:35:15 +00001335 if (T.hasLocalNonFastQualifiers()) {
1336 Qualifiers Qs = T.getLocalQualifiers();
1337 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001338 Record.push_back(Qs.getAsOpaqueValue());
1339 W.Code = pch::TYPE_EXT_QUAL;
1340 } else {
1341 switch (T->getTypeClass()) {
1342 // For all of the concrete, non-dependent types, call the
1343 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001344#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001345 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001346#define ABSTRACT_TYPE(Class, Base)
1347#define DEPENDENT_TYPE(Class, Base)
1348#include "clang/AST/TypeNodes.def"
1349
John McCall0953e762009-09-24 19:53:00 +00001350 // For all of the dependent type nodes (which only occur in C++
1351 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001352#define TYPE(Class, Base)
1353#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1354#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001355 assert(false && "Cannot serialize dependent type nodes");
1356 break;
1357 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001358 }
1359
1360 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001361 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001362
1363 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001364 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001365}
1366
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001367//===----------------------------------------------------------------------===//
1368// Declaration Serialization
1369//===----------------------------------------------------------------------===//
1370
Douglas Gregor2cf26342009-04-09 22:27:44 +00001371/// \brief Write the block containing all of the declaration IDs
1372/// lexically declared within the given DeclContext.
1373///
1374/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1375/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001376uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001377 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001378 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001379 return 0;
1380
Douglas Gregorc9490c02009-04-16 22:23:12 +00001381 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001382 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001383 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1384 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001385 AddDeclRef(*D, Record);
1386
Douglas Gregor25123082009-04-22 22:34:57 +00001387 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001388 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001389 return Offset;
1390}
1391
1392/// \brief Write the block containing all of the declaration IDs
1393/// visible from the given DeclContext.
1394///
1395/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1396/// bistream, or 0 if no block was written.
1397uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1398 DeclContext *DC) {
1399 if (DC->getPrimaryContext() != DC)
1400 return 0;
1401
Douglas Gregoraff22df2009-04-21 22:32:33 +00001402 // Since there is no name lookup into functions or methods, and we
1403 // perform name lookup for the translation unit via the
1404 // IdentifierInfo chains, don't bother to build a
1405 // visible-declarations table for these entities.
1406 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001407 return 0;
1408
Douglas Gregor2cf26342009-04-09 22:27:44 +00001409 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001410 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001411
1412 // Serialize the contents of the mapping used for lookup. Note that,
1413 // although we have two very different code paths, the serialized
1414 // representation is the same for both cases: a declaration name,
1415 // followed by a size, followed by references to the visible
1416 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001417 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001418 RecordData Record;
1419 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001420 if (!Map)
1421 return 0;
1422
Douglas Gregor2cf26342009-04-09 22:27:44 +00001423 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1424 D != DEnd; ++D) {
1425 AddDeclarationName(D->first, Record);
1426 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1427 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001428 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001429 AddDeclRef(*Result.first, Record);
1430 }
1431
1432 if (Record.size() == 0)
1433 return 0;
1434
Douglas Gregorc9490c02009-04-16 22:23:12 +00001435 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001436 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001437 return Offset;
1438}
1439
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001440//===----------------------------------------------------------------------===//
1441// Global Method Pool and Selector Serialization
1442//===----------------------------------------------------------------------===//
1443
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001444namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001445// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001446class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001447 PCHWriter &Writer;
1448
1449public:
1450 typedef Selector key_type;
1451 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001453 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1454 typedef const data_type& data_type_ref;
1455
1456 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001458 static unsigned ComputeHash(Selector Sel) {
1459 unsigned N = Sel.getNumArgs();
1460 if (N == 0)
1461 ++N;
1462 unsigned R = 5381;
1463 for (unsigned I = 0; I != N; ++I)
1464 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001465 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001466 return R;
1467 }
Mike Stump1eb44332009-09-09 15:08:12 +00001468
1469 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001470 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1471 data_type_ref Methods) {
1472 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1473 clang::io::Emit16(Out, KeyLen);
1474 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001475 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001476 Method = Method->Next)
1477 if (Method->Method)
1478 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001479 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001480 Method = Method->Next)
1481 if (Method->Method)
1482 DataLen += 4;
1483 clang::io::Emit16(Out, DataLen);
1484 return std::make_pair(KeyLen, DataLen);
1485 }
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Douglas Gregor83941df2009-04-25 17:48:32 +00001487 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001488 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001489 assert((Start >> 32) == 0 && "Selector key offset too large");
1490 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001491 unsigned N = Sel.getNumArgs();
1492 clang::io::Emit16(Out, N);
1493 if (N == 0)
1494 N = 1;
1495 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001496 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001497 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1498 }
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001500 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001501 data_type_ref Methods, unsigned DataLen) {
1502 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001503 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001504 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001505 Method = Method->Next)
1506 if (Method->Method)
1507 ++NumInstanceMethods;
1508
1509 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001510 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001511 Method = Method->Next)
1512 if (Method->Method)
1513 ++NumFactoryMethods;
1514
1515 clang::io::Emit16(Out, NumInstanceMethods);
1516 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001517 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001518 Method = Method->Next)
1519 if (Method->Method)
1520 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001521 for (const ObjCMethodList *Method = &Methods.second; 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));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001525
1526 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001527 }
1528};
1529} // end anonymous namespace
1530
1531/// \brief Write the method pool into the PCH file.
1532///
1533/// The method pool contains both instance and factory methods, stored
1534/// in an on-disk hash table indexed by the selector.
1535void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1536 using namespace llvm;
1537
1538 // Create and write out the blob that contains the instance and
1539 // factor method pools.
1540 bool Empty = true;
1541 {
1542 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001544 // Create the on-disk hash table representation. Start by
1545 // iterating through the instance method pool.
1546 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001547 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001548 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001549 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001550 InstanceEnd = SemaRef.InstanceMethodPool.end();
1551 Instance != InstanceEnd; ++Instance) {
1552 // Check whether there is a factory method with the same
1553 // selector.
1554 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1555 = SemaRef.FactoryMethodPool.find(Instance->first);
1556
1557 if (Factory == SemaRef.FactoryMethodPool.end())
1558 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001559 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001560 ObjCMethodList()));
1561 else
1562 Generator.insert(Instance->first,
1563 std::make_pair(Instance->second, Factory->second));
1564
Douglas Gregor83941df2009-04-25 17:48:32 +00001565 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001566 Empty = false;
1567 }
1568
1569 // Now iterate through the factory method pool, to pick up any
1570 // selectors that weren't already in the instance method pool.
1571 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001572 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001573 FactoryEnd = SemaRef.FactoryMethodPool.end();
1574 Factory != FactoryEnd; ++Factory) {
1575 // Check whether there is an instance method with the same
1576 // selector. If so, there is no work to do here.
1577 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1578 = SemaRef.InstanceMethodPool.find(Factory->first);
1579
Douglas Gregor83941df2009-04-25 17:48:32 +00001580 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001581 Generator.insert(Factory->first,
1582 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001583 ++NumSelectorsInMethodPool;
1584 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001585
1586 Empty = false;
1587 }
1588
Douglas Gregor83941df2009-04-25 17:48:32 +00001589 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001590 return;
1591
1592 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001593 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001594 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001595 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001596 {
1597 PCHMethodPoolTrait Trait(*this);
1598 llvm::raw_svector_ostream Out(MethodPool);
1599 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001600 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001601 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001602
1603 // For every selector that we have seen but which was not
1604 // written into the hash table, write the selector itself and
1605 // record it's offset.
1606 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1607 if (SelectorOffsets[I] == 0)
1608 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001609 }
1610
1611 // Create a blob abbreviation
1612 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1613 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1617 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1618
Douglas Gregor83941df2009-04-25 17:48:32 +00001619 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001620 RecordData Record;
1621 Record.push_back(pch::METHOD_POOL);
1622 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001623 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001624 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001625
1626 // Create a blob abbreviation for the selector table offsets.
1627 Abbrev = new BitCodeAbbrev();
1628 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1631 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1632
1633 // Write the selector offsets table.
1634 Record.clear();
1635 Record.push_back(pch::SELECTOR_OFFSETS);
1636 Record.push_back(SelectorOffsets.size());
1637 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1638 (const char *)&SelectorOffsets.front(),
1639 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001640 }
1641}
1642
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001643//===----------------------------------------------------------------------===//
1644// Identifier Table Serialization
1645//===----------------------------------------------------------------------===//
1646
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001647namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001648class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001649 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001650 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001651
Douglas Gregora92193e2009-04-28 21:18:29 +00001652 /// \brief Determines whether this is an "interesting" identifier
1653 /// that needs a full IdentifierInfo structure written into the hash
1654 /// table.
1655 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1656 return II->isPoisoned() ||
1657 II->isExtensionToken() ||
1658 II->hasMacroDefinition() ||
1659 II->getObjCOrBuiltinID() ||
1660 II->getFETokenInfo<void>();
1661 }
1662
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001663public:
1664 typedef const IdentifierInfo* key_type;
1665 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001667 typedef pch::IdentID data_type;
1668 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001669
1670 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001671 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001672
1673 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001674 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001675 }
Mike Stump1eb44332009-09-09 15:08:12 +00001676
1677 std::pair<unsigned,unsigned>
1678 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001679 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001680 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001681 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1682 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001683 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001684 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001685 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001686 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001687 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1688 DEnd = IdentifierResolver::end();
1689 D != DEnd; ++D)
1690 DataLen += sizeof(pch::DeclID);
1691 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001692 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001693 // We emit the key length after the data length so that every
1694 // string is preceded by a 16-bit length. This matches the PTH
1695 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001696 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001697 return std::make_pair(KeyLen, DataLen);
1698 }
Mike Stump1eb44332009-09-09 15:08:12 +00001699
1700 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001701 unsigned KeyLen) {
1702 // Record the location of the key data. This is used when generating
1703 // the mapping from persistent IDs to strings.
1704 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001705 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001706 }
Mike Stump1eb44332009-09-09 15:08:12 +00001707
1708 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001709 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001710 if (!isInterestingIdentifier(II)) {
1711 clang::io::Emit32(Out, ID << 1);
1712 return;
1713 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001714
Douglas Gregora92193e2009-04-28 21:18:29 +00001715 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001716 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001717 bool hasMacroDefinition =
1718 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001719 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001720 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001721 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1722 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1723 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1724 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001725 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001726
Douglas Gregor37e26842009-04-21 23:56:24 +00001727 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001728 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001729
Douglas Gregor668c1a42009-04-21 22:25:48 +00001730 // Emit the declaration IDs in reverse order, because the
1731 // IdentifierResolver provides the declarations as they would be
1732 // visible (e.g., the function "stat" would come before the struct
1733 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1734 // adds declarations to the end of the list (so we need to see the
1735 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001736 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001737 IdentifierResolver::end());
1738 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1739 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001740 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001741 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001742 }
1743};
1744} // end anonymous namespace
1745
Douglas Gregorafaf3082009-04-11 00:14:32 +00001746/// \brief Write the identifier table into the PCH file.
1747///
1748/// The identifier table consists of a blob containing string data
1749/// (the actual identifiers themselves) and a separate "offsets" index
1750/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001751void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001752 using namespace llvm;
1753
1754 // Create and write out the blob that contains the identifier
1755 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001756 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001757 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Douglas Gregor92b059e2009-04-28 20:33:11 +00001759 // Look for any identifiers that were named while processing the
1760 // headers, but are otherwise not needed. We add these to the hash
1761 // table to enable checking of the predefines buffer in the case
1762 // where the user adds new macro definitions when building the PCH
1763 // file.
1764 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1765 IDEnd = PP.getIdentifierTable().end();
1766 ID != IDEnd; ++ID)
1767 getIdentifierRef(ID->second);
1768
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001769 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001770 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001771 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1772 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1773 ID != IDEnd; ++ID) {
1774 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001775 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001776 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001777
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001778 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001779 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001780 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001781 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001782 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001783 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001784 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001785 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001786 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001787 }
1788
1789 // Create a blob abbreviation
1790 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1791 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001794 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001795
1796 // Write the identifier table
1797 RecordData Record;
1798 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001799 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001800 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001801 }
1802
1803 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001804 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1805 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1808 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1809
1810 RecordData Record;
1811 Record.push_back(pch::IDENTIFIER_OFFSET);
1812 Record.push_back(IdentifierOffsets.size());
1813 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1814 (const char *)&IdentifierOffsets.front(),
1815 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001816}
1817
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001818//===----------------------------------------------------------------------===//
1819// General Serialization Routines
1820//===----------------------------------------------------------------------===//
1821
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001822/// \brief Write a record containing the given attributes.
1823void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1824 RecordData Record;
1825 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001826 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001827 Record.push_back(Attr->isInherited());
1828 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001829 default:
1830 assert(0 && "Does not support PCH writing for this attribute yet!");
1831 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001832 case Attr::Alias:
1833 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1834 break;
1835
1836 case Attr::Aligned:
1837 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1838 break;
1839
1840 case Attr::AlwaysInline:
1841 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001843 case Attr::AnalyzerNoReturn:
1844 break;
1845
1846 case Attr::Annotate:
1847 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1848 break;
1849
1850 case Attr::AsmLabel:
1851 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1852 break;
1853
Sean Hunt7725e672009-11-25 04:20:27 +00001854 case Attr::BaseCheck:
1855 break;
1856
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001857 case Attr::Blocks:
1858 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1859 break;
1860
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001861 case Attr::CDecl:
1862 break;
1863
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001864 case Attr::Cleanup:
1865 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1866 break;
1867
1868 case Attr::Const:
1869 break;
1870
1871 case Attr::Constructor:
1872 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1873 break;
1874
1875 case Attr::DLLExport:
1876 case Attr::DLLImport:
1877 case Attr::Deprecated:
1878 break;
1879
1880 case Attr::Destructor:
1881 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1882 break;
1883
1884 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001885 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001886 break;
1887
1888 case Attr::Format: {
1889 const FormatAttr *Format = cast<FormatAttr>(Attr);
1890 AddString(Format->getType(), Record);
1891 Record.push_back(Format->getFormatIdx());
1892 Record.push_back(Format->getFirstArg());
1893 break;
1894 }
1895
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001896 case Attr::FormatArg: {
1897 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1898 Record.push_back(Format->getFormatIdx());
1899 break;
1900 }
1901
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001902 case Attr::Sentinel : {
1903 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1904 Record.push_back(Sentinel->getSentinel());
1905 Record.push_back(Sentinel->getNullPos());
1906 break;
1907 }
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Chris Lattnercf2a7212009-04-20 19:12:28 +00001909 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001910 case Attr::Hiding:
Ted Kremenekefbddd22010-02-17 02:37:45 +00001911 case Attr::IBActionKind:
Ted Kremenek47e69902010-02-18 00:05:52 +00001912 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001913 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001914 case Attr::NoDebug:
Ted Kremenek47e69902010-02-18 00:05:52 +00001915 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001916 case Attr::NoReturn:
1917 case Attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001918 break;
1919
1920 case Attr::NonNull: {
1921 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1922 Record.push_back(NonNull->size());
1923 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1924 break;
1925 }
1926
Ted Kremenek31c780d2010-02-18 00:05:45 +00001927 case Attr::CFReturnsNotRetained:
1928 case Attr::CFReturnsRetained:
1929 case Attr::NSReturnsNotRetained:
1930 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001931 case Attr::ObjCException:
1932 case Attr::ObjCNSObject:
1933 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001934 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001935 break;
1936
Anders Carlssona860e752009-08-08 18:23:56 +00001937 case Attr::PragmaPack:
1938 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001939 break;
1940
Anders Carlssona860e752009-08-08 18:23:56 +00001941 case Attr::Packed:
1942 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001944 case Attr::Pure:
1945 break;
1946
1947 case Attr::Regparm:
1948 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1949 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Nate Begeman6f3d8382009-06-26 06:32:41 +00001951 case Attr::ReqdWorkGroupSize:
1952 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1953 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1954 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1955 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001956
1957 case Attr::Section:
1958 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1959 break;
1960
1961 case Attr::StdCall:
1962 case Attr::TransparentUnion:
1963 case Attr::Unavailable:
1964 case Attr::Unused:
1965 case Attr::Used:
1966 break;
1967
1968 case Attr::Visibility:
1969 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001970 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001971 break;
1972
1973 case Attr::WarnUnusedResult:
1974 case Attr::Weak:
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001975 case Attr::WeakRef:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001976 case Attr::WeakImport:
1977 break;
1978 }
1979 }
1980
Douglas Gregorc9490c02009-04-16 22:23:12 +00001981 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001982}
1983
1984void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1985 Record.push_back(Str.size());
1986 Record.insert(Record.end(), Str.begin(), Str.end());
1987}
1988
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001989/// \brief Note that the identifier II occurs at the given offset
1990/// within the identifier table.
1991void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001992 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001993}
1994
Douglas Gregor83941df2009-04-25 17:48:32 +00001995/// \brief Note that the selector Sel occurs at the given offset
1996/// within the method pool/selector table.
1997void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1998 unsigned ID = SelectorIDs[Sel];
1999 assert(ID && "Unknown selector");
2000 SelectorOffsets[ID - 1] = Offset;
2001}
2002
Mike Stump1eb44332009-09-09 15:08:12 +00002003PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2004 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00002005 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2006 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002007
Douglas Gregore650c8c2009-07-07 00:12:59 +00002008void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2009 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002010 using namespace llvm;
2011
Douglas Gregore7785042009-04-20 15:53:59 +00002012 ASTContext &Context = SemaRef.Context;
2013 Preprocessor &PP = SemaRef.PP;
2014
Douglas Gregor2cf26342009-04-09 22:27:44 +00002015 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002016 Stream.Emit((unsigned)'C', 8);
2017 Stream.Emit((unsigned)'P', 8);
2018 Stream.Emit((unsigned)'C', 8);
2019 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002021 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002022
2023 // The translation unit is the first declaration we'll emit.
2024 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002025 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002026
Douglas Gregor2deaea32009-04-22 18:49:13 +00002027 // Make sure that we emit IdentifierInfos (and any attached
2028 // declarations) for builtins.
2029 {
2030 IdentifierTable &Table = PP.getIdentifierTable();
2031 llvm::SmallVector<const char *, 32> BuiltinNames;
2032 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2033 Context.getLangOptions().NoBuiltin);
2034 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2035 getIdentifierRef(&Table.get(BuiltinNames[I]));
2036 }
2037
Chris Lattner63d65f82009-09-08 18:19:27 +00002038 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002039 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002040 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002041 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002042 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2043 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002044 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002045
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002046 // Build a record containing all of the static unused functions in this file.
2047 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002048 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002049 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002050
Douglas Gregor14c22f22009-04-22 22:18:58 +00002051 // Build a record containing all of the locally-scoped external
2052 // declarations in this header file. Generally, this record will be
2053 // empty.
2054 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002055 // FIXME: This is filling in the PCH file in densemap order which is
2056 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002057 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002058 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2059 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2060 TD != TDEnd; ++TD)
2061 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2062
Douglas Gregorb81c1702009-04-27 20:06:05 +00002063 // Build a record containing all of the ext_vector declarations.
2064 RecordData ExtVectorDecls;
2065 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2066 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2067
Douglas Gregor2cf26342009-04-09 22:27:44 +00002068 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002069 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002070 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002071 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002072 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002073 if (StatCalls && !isysroot)
2074 WriteStatCache(*StatCalls, isysroot);
2075 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002076 // Write the record of special types.
2077 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002079 AddTypeRef(Context.getBuiltinVaListType(), Record);
2080 AddTypeRef(Context.getObjCIdType(), Record);
2081 AddTypeRef(Context.getObjCSelType(), Record);
2082 AddTypeRef(Context.getObjCProtoType(), Record);
2083 AddTypeRef(Context.getObjCClassType(), Record);
2084 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2085 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2086 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002087 AddTypeRef(Context.getjmp_bufType(), Record);
2088 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002089 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2090 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002091 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002092 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002093 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2094 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002095 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002096
Douglas Gregor366809a2009-04-26 03:49:13 +00002097 // Keep writing types and declarations until all types and
2098 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002099 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2100 WriteDeclsBlockAbbrevs();
2101 while (!DeclTypesToEmit.empty()) {
2102 DeclOrType DOT = DeclTypesToEmit.front();
2103 DeclTypesToEmit.pop();
2104 if (DOT.isType())
2105 WriteType(DOT.getType());
2106 else
2107 WriteDecl(Context, DOT.getDecl());
2108 }
2109 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002110
Douglas Gregor813a97b2009-10-17 17:25:45 +00002111 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002112 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002113 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002114
2115 // Write the type offsets array
2116 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2117 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2118 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2119 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2120 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2121 Record.clear();
2122 Record.push_back(pch::TYPE_OFFSET);
2123 Record.push_back(TypeOffsets.size());
2124 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002125 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002126 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002128 // Write the declaration offsets array
2129 Abbrev = new BitCodeAbbrev();
2130 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2131 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2133 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2134 Record.clear();
2135 Record.push_back(pch::DECL_OFFSET);
2136 Record.push_back(DeclOffsets.size());
2137 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002138 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002139 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002140
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002141 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002142 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002143 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002144
2145 // Write the record containing tentative definitions.
2146 if (!TentativeDefinitions.empty())
2147 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002148
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002149 // Write the record containing unused static functions.
2150 if (!UnusedStaticFuncs.empty())
2151 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002152
Douglas Gregor14c22f22009-04-22 22:18:58 +00002153 // Write the record containing locally-scoped external definitions.
2154 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002155 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002156 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002157
2158 // Write the record containing ext_vector type names.
2159 if (!ExtVectorDecls.empty())
2160 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Douglas Gregor3e1af842009-04-17 22:13:46 +00002162 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002163 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002164 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002165 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002166 Record.push_back(NumLexicalDeclContexts);
2167 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002168 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002169 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002170}
2171
2172void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2173 Record.push_back(Loc.getRawEncoding());
2174}
2175
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002176void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2177 AddSourceLocation(Range.getBegin(), Record);
2178 AddSourceLocation(Range.getEnd(), Record);
2179}
2180
Douglas Gregor2cf26342009-04-09 22:27:44 +00002181void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2182 Record.push_back(Value.getBitWidth());
2183 unsigned N = Value.getNumWords();
2184 const uint64_t* Words = Value.getRawData();
2185 for (unsigned I = 0; I != N; ++I)
2186 Record.push_back(Words[I]);
2187}
2188
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002189void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2190 Record.push_back(Value.isUnsigned());
2191 AddAPInt(Value, Record);
2192}
2193
Douglas Gregor17fc2232009-04-14 21:55:33 +00002194void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2195 AddAPInt(Value.bitcastToAPInt(), Record);
2196}
2197
Douglas Gregor2cf26342009-04-09 22:27:44 +00002198void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002199 Record.push_back(getIdentifierRef(II));
2200}
2201
2202pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2203 if (II == 0)
2204 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002205
2206 pch::IdentID &ID = IdentifierIDs[II];
2207 if (ID == 0)
2208 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002209 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002210}
2211
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002212pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2213 if (MD == 0)
2214 return 0;
2215
2216 pch::IdentID &ID = MacroDefinitions[MD];
2217 if (ID == 0)
2218 ID = MacroDefinitions.size();
2219 return ID;
2220}
2221
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002222void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2223 if (SelRef.getAsOpaquePtr() == 0) {
2224 Record.push_back(0);
2225 return;
2226 }
2227
2228 pch::SelectorID &SID = SelectorIDs[SelRef];
2229 if (SID == 0) {
2230 SID = SelectorIDs.size();
2231 SelVector.push_back(SelRef);
2232 }
2233 Record.push_back(SID);
2234}
2235
Chris Lattnerd2598362010-05-10 00:25:06 +00002236void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2237 AddDeclRef(Temp->getDestructor(), Record);
2238}
2239
John McCall833ca992009-10-29 08:12:44 +00002240void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2241 RecordData &Record) {
2242 switch (Arg.getArgument().getKind()) {
2243 case TemplateArgument::Expression:
2244 AddStmt(Arg.getLocInfo().getAsExpr());
2245 break;
2246 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002247 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002248 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002249 case TemplateArgument::Template:
2250 Record.push_back(
2251 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2252 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2253 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2254 break;
John McCall833ca992009-10-29 08:12:44 +00002255 case TemplateArgument::Null:
2256 case TemplateArgument::Integral:
2257 case TemplateArgument::Declaration:
2258 case TemplateArgument::Pack:
2259 break;
2260 }
2261}
2262
John McCalla93c9342009-12-07 02:54:59 +00002263void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2264 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002265 AddTypeRef(QualType(), Record);
2266 return;
2267 }
2268
John McCalla93c9342009-12-07 02:54:59 +00002269 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002270 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002271 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002272 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002273}
2274
Douglas Gregor2cf26342009-04-09 22:27:44 +00002275void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2276 if (T.isNull()) {
2277 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2278 return;
2279 }
2280
Douglas Gregora4923eb2009-11-16 21:35:15 +00002281 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002282 T.removeFastQualifiers();
2283
Douglas Gregora4923eb2009-11-16 21:35:15 +00002284 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002285 pch::TypeID &ID = TypeIDs[T];
2286 if (ID == 0) {
2287 // We haven't seen these qualifiers applied to this type before.
2288 // Assign it a new ID. This is the only time we enqueue a
2289 // qualified type, and it has no CV qualifiers.
2290 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002291 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002292 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002293
John McCall0953e762009-09-24 19:53:00 +00002294 // Encode the type qualifiers in the type reference.
2295 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2296 return;
2297 }
2298
Douglas Gregora4923eb2009-11-16 21:35:15 +00002299 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002300
Douglas Gregor2cf26342009-04-09 22:27:44 +00002301 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002302 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002303 switch (BT->getKind()) {
2304 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2305 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2306 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2307 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2308 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2309 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2310 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2311 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002312 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002313 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2314 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2315 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2316 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2317 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2318 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2319 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002320 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002321 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2322 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2323 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002324 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002325 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2326 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2328 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002329 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2330 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002331 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002332 case BuiltinType::UndeducedAuto:
2333 assert(0 && "Should not see undeduced auto here");
2334 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002335 }
2336
John McCall0953e762009-09-24 19:53:00 +00002337 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002338 return;
2339 }
2340
John McCall0953e762009-09-24 19:53:00 +00002341 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002342 if (ID == 0) {
2343 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002344 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002345 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002346 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002347 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002348
2349 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002350 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002351}
2352
2353void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2354 if (D == 0) {
2355 Record.push_back(0);
2356 return;
2357 }
2358
Douglas Gregor8038d512009-04-10 17:25:41 +00002359 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002360 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002361 // We haven't seen this declaration before. Give it a new ID and
2362 // enqueue it in the list of declarations to emit.
2363 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002364 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002365 }
2366
2367 Record.push_back(ID);
2368}
2369
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002370pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2371 if (D == 0)
2372 return 0;
2373
2374 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2375 return DeclIDs[D];
2376}
2377
Douglas Gregor2cf26342009-04-09 22:27:44 +00002378void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002379 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002380 Record.push_back(Name.getNameKind());
2381 switch (Name.getNameKind()) {
2382 case DeclarationName::Identifier:
2383 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2384 break;
2385
2386 case DeclarationName::ObjCZeroArgSelector:
2387 case DeclarationName::ObjCOneArgSelector:
2388 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002389 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002390 break;
2391
2392 case DeclarationName::CXXConstructorName:
2393 case DeclarationName::CXXDestructorName:
2394 case DeclarationName::CXXConversionFunctionName:
2395 AddTypeRef(Name.getCXXNameType(), Record);
2396 break;
2397
2398 case DeclarationName::CXXOperatorName:
2399 Record.push_back(Name.getCXXOverloadedOperator());
2400 break;
2401
Sean Hunt3e518bd2009-11-29 07:34:05 +00002402 case DeclarationName::CXXLiteralOperatorName:
2403 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2404 break;
2405
Douglas Gregor2cf26342009-04-09 22:27:44 +00002406 case DeclarationName::CXXUsingDirective:
2407 // No extra data to emit
2408 break;
2409 }
2410}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002411
2412void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2413 RecordData &Record) {
2414 // Nested name specifiers usually aren't too long. I think that 8 would
2415 // typically accomodate the vast majority.
2416 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2417
2418 // Push each of the NNS's onto a stack for serialization in reverse order.
2419 while (NNS) {
2420 NestedNames.push_back(NNS);
2421 NNS = NNS->getPrefix();
2422 }
2423
2424 Record.push_back(NestedNames.size());
2425 while(!NestedNames.empty()) {
2426 NNS = NestedNames.pop_back_val();
2427 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2428 Record.push_back(Kind);
2429 switch (Kind) {
2430 case NestedNameSpecifier::Identifier:
2431 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2432 break;
2433
2434 case NestedNameSpecifier::Namespace:
2435 AddDeclRef(NNS->getAsNamespace(), Record);
2436 break;
2437
2438 case NestedNameSpecifier::TypeSpec:
2439 case NestedNameSpecifier::TypeSpecWithTemplate:
2440 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2441 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2442 break;
2443
2444 case NestedNameSpecifier::Global:
2445 // Don't need to write an associated value.
2446 break;
2447 }
2448 }
2449}