blob: 6887d847e9816c03dacddc769b3a601f002044cb [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) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000400 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
401 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000402}
John McCall3cb0ebd2010-03-10 03:28:59 +0000403void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getNameLoc(), Record);
405}
Douglas Gregor4714c122010-03-31 17:34:00 +0000406void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000407 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
408 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000409 Writer.AddSourceLocation(TL.getNameLoc(), Record);
410}
411void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000413}
414void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
415 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000416 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
417 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
418 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
419 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000420}
John McCall54e14c42009-10-22 22:37:11 +0000421void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
422 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000423}
John McCalla1ee0c52009-10-16 21:56:05 +0000424
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000425//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000426// PCHWriter Implementation
427//===----------------------------------------------------------------------===//
428
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000429static void EmitBlockID(unsigned ID, const char *Name,
430 llvm::BitstreamWriter &Stream,
431 PCHWriter::RecordData &Record) {
432 Record.clear();
433 Record.push_back(ID);
434 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
435
436 // Emit the block name if present.
437 if (Name == 0 || Name[0] == 0) return;
438 Record.clear();
439 while (*Name)
440 Record.push_back(*Name++);
441 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
442}
443
444static void EmitRecordID(unsigned ID, const char *Name,
445 llvm::BitstreamWriter &Stream,
446 PCHWriter::RecordData &Record) {
447 Record.clear();
448 Record.push_back(ID);
449 while (*Name)
450 Record.push_back(*Name++);
451 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000452}
453
454static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
455 PCHWriter::RecordData &Record) {
456#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
457 RECORD(STMT_STOP);
458 RECORD(STMT_NULL_PTR);
459 RECORD(STMT_NULL);
460 RECORD(STMT_COMPOUND);
461 RECORD(STMT_CASE);
462 RECORD(STMT_DEFAULT);
463 RECORD(STMT_LABEL);
464 RECORD(STMT_IF);
465 RECORD(STMT_SWITCH);
466 RECORD(STMT_WHILE);
467 RECORD(STMT_DO);
468 RECORD(STMT_FOR);
469 RECORD(STMT_GOTO);
470 RECORD(STMT_INDIRECT_GOTO);
471 RECORD(STMT_CONTINUE);
472 RECORD(STMT_BREAK);
473 RECORD(STMT_RETURN);
474 RECORD(STMT_DECL);
475 RECORD(STMT_ASM);
476 RECORD(EXPR_PREDEFINED);
477 RECORD(EXPR_DECL_REF);
478 RECORD(EXPR_INTEGER_LITERAL);
479 RECORD(EXPR_FLOATING_LITERAL);
480 RECORD(EXPR_IMAGINARY_LITERAL);
481 RECORD(EXPR_STRING_LITERAL);
482 RECORD(EXPR_CHARACTER_LITERAL);
483 RECORD(EXPR_PAREN);
484 RECORD(EXPR_UNARY_OPERATOR);
485 RECORD(EXPR_SIZEOF_ALIGN_OF);
486 RECORD(EXPR_ARRAY_SUBSCRIPT);
487 RECORD(EXPR_CALL);
488 RECORD(EXPR_MEMBER);
489 RECORD(EXPR_BINARY_OPERATOR);
490 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
491 RECORD(EXPR_CONDITIONAL_OPERATOR);
492 RECORD(EXPR_IMPLICIT_CAST);
493 RECORD(EXPR_CSTYLE_CAST);
494 RECORD(EXPR_COMPOUND_LITERAL);
495 RECORD(EXPR_EXT_VECTOR_ELEMENT);
496 RECORD(EXPR_INIT_LIST);
497 RECORD(EXPR_DESIGNATED_INIT);
498 RECORD(EXPR_IMPLICIT_VALUE_INIT);
499 RECORD(EXPR_VA_ARG);
500 RECORD(EXPR_ADDR_LABEL);
501 RECORD(EXPR_STMT);
502 RECORD(EXPR_TYPES_COMPATIBLE);
503 RECORD(EXPR_CHOOSE);
504 RECORD(EXPR_GNU_NULL);
505 RECORD(EXPR_SHUFFLE_VECTOR);
506 RECORD(EXPR_BLOCK);
507 RECORD(EXPR_BLOCK_DECL_REF);
508 RECORD(EXPR_OBJC_STRING_LITERAL);
509 RECORD(EXPR_OBJC_ENCODE);
510 RECORD(EXPR_OBJC_SELECTOR_EXPR);
511 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
512 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
513 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
514 RECORD(EXPR_OBJC_KVC_REF_EXPR);
515 RECORD(EXPR_OBJC_MESSAGE_EXPR);
516 RECORD(EXPR_OBJC_SUPER_EXPR);
517 RECORD(STMT_OBJC_FOR_COLLECTION);
518 RECORD(STMT_OBJC_CATCH);
519 RECORD(STMT_OBJC_FINALLY);
520 RECORD(STMT_OBJC_AT_TRY);
521 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
522 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000523 RECORD(EXPR_CXX_OPERATOR_CALL);
524 RECORD(EXPR_CXX_CONSTRUCT);
525 RECORD(EXPR_CXX_STATIC_CAST);
526 RECORD(EXPR_CXX_DYNAMIC_CAST);
527 RECORD(EXPR_CXX_REINTERPRET_CAST);
528 RECORD(EXPR_CXX_CONST_CAST);
529 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
530 RECORD(EXPR_CXX_BOOL_LITERAL);
531 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000532#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000533}
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000535void PCHWriter::WriteBlockInfoBlock() {
536 RecordData Record;
537 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner2f4efd12009-04-27 00:40:25 +0000539#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000540#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000542 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000543 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000544 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000545 RECORD(TYPE_OFFSET);
546 RECORD(DECL_OFFSET);
547 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000548 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000549 RECORD(IDENTIFIER_OFFSET);
550 RECORD(IDENTIFIER_TABLE);
551 RECORD(EXTERNAL_DEFINITIONS);
552 RECORD(SPECIAL_TYPES);
553 RECORD(STATISTICS);
554 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000555 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000556 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
557 RECORD(SELECTOR_OFFSETS);
558 RECORD(METHOD_POOL);
559 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000560 RECORD(SOURCE_LOCATION_OFFSETS);
561 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000562 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000563 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000564 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000565 RECORD(UNUSED_STATIC_FUNCS);
566 RECORD(MACRO_DEFINITION_OFFSETS);
567
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000568 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000569 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000570 RECORD(SM_SLOC_FILE_ENTRY);
571 RECORD(SM_SLOC_BUFFER_ENTRY);
572 RECORD(SM_SLOC_BUFFER_BLOB);
573 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
574 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000576 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000577 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000578 RECORD(PP_MACRO_OBJECT_LIKE);
579 RECORD(PP_MACRO_FUNCTION_LIKE);
580 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000581 RECORD(PP_MACRO_INSTANTIATION);
582 RECORD(PP_MACRO_DEFINITION);
583
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000584 // Decls and Types block.
585 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000586 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000587 RECORD(TYPE_COMPLEX);
588 RECORD(TYPE_POINTER);
589 RECORD(TYPE_BLOCK_POINTER);
590 RECORD(TYPE_LVALUE_REFERENCE);
591 RECORD(TYPE_RVALUE_REFERENCE);
592 RECORD(TYPE_MEMBER_POINTER);
593 RECORD(TYPE_CONSTANT_ARRAY);
594 RECORD(TYPE_INCOMPLETE_ARRAY);
595 RECORD(TYPE_VARIABLE_ARRAY);
596 RECORD(TYPE_VECTOR);
597 RECORD(TYPE_EXT_VECTOR);
598 RECORD(TYPE_FUNCTION_PROTO);
599 RECORD(TYPE_FUNCTION_NO_PROTO);
600 RECORD(TYPE_TYPEDEF);
601 RECORD(TYPE_TYPEOF_EXPR);
602 RECORD(TYPE_TYPEOF);
603 RECORD(TYPE_RECORD);
604 RECORD(TYPE_ENUM);
605 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000606 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000607 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000608 RECORD(DECL_ATTR);
609 RECORD(DECL_TRANSLATION_UNIT);
610 RECORD(DECL_TYPEDEF);
611 RECORD(DECL_ENUM);
612 RECORD(DECL_RECORD);
613 RECORD(DECL_ENUM_CONSTANT);
614 RECORD(DECL_FUNCTION);
615 RECORD(DECL_OBJC_METHOD);
616 RECORD(DECL_OBJC_INTERFACE);
617 RECORD(DECL_OBJC_PROTOCOL);
618 RECORD(DECL_OBJC_IVAR);
619 RECORD(DECL_OBJC_AT_DEFS_FIELD);
620 RECORD(DECL_OBJC_CLASS);
621 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
622 RECORD(DECL_OBJC_CATEGORY);
623 RECORD(DECL_OBJC_CATEGORY_IMPL);
624 RECORD(DECL_OBJC_IMPLEMENTATION);
625 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
626 RECORD(DECL_OBJC_PROPERTY);
627 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000628 RECORD(DECL_FIELD);
629 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000630 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000631 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000632 RECORD(DECL_FILE_SCOPE_ASM);
633 RECORD(DECL_BLOCK);
634 RECORD(DECL_CONTEXT_LEXICAL);
635 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000636 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000637 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000638#undef RECORD
639#undef BLOCK
640 Stream.ExitBlock();
641}
642
Douglas Gregore650c8c2009-07-07 00:12:59 +0000643/// \brief Adjusts the given filename to only write out the portion of the
644/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000645///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000646/// \param Filename the file name to adjust.
647///
648/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
649/// the returned filename will be adjusted by this system root.
650///
651/// \returns either the original filename (if it needs no adjustment) or the
652/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000653static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000654adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
655 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Douglas Gregore650c8c2009-07-07 00:12:59 +0000657 if (!isysroot)
658 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Douglas Gregore650c8c2009-07-07 00:12:59 +0000660 // Verify that the filename and the system root have the same prefix.
661 unsigned Pos = 0;
662 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
663 if (Filename[Pos] != isysroot[Pos])
664 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Douglas Gregore650c8c2009-07-07 00:12:59 +0000666 // We hit the end of the filename before we hit the end of the system root.
667 if (!Filename[Pos])
668 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Douglas Gregore650c8c2009-07-07 00:12:59 +0000670 // If the file name has a '/' at the current position, skip over the '/'.
671 // We distinguish sysroot-based includes from absolute includes by the
672 // absence of '/' at the beginning of sysroot-based includes.
673 if (Filename[Pos] == '/')
674 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregore650c8c2009-07-07 00:12:59 +0000676 return Filename + Pos;
677}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000678
Douglas Gregorab41e632009-04-27 22:23:34 +0000679/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000680void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000681 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000682
Douglas Gregore650c8c2009-07-07 00:12:59 +0000683 // Metadata
684 const TargetInfo &Target = Context.Target;
685 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
686 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
687 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
688 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
689 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
690 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
691 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
692 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
693 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Douglas Gregore650c8c2009-07-07 00:12:59 +0000695 RecordData Record;
696 Record.push_back(pch::METADATA);
697 Record.push_back(pch::VERSION_MAJOR);
698 Record.push_back(pch::VERSION_MINOR);
699 Record.push_back(CLANG_VERSION_MAJOR);
700 Record.push_back(CLANG_VERSION_MINOR);
701 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000702 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000703 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Douglas Gregorb64c1932009-05-12 01:31:05 +0000705 // Original file name
706 SourceManager &SM = Context.getSourceManager();
707 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
708 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
709 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
710 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
711 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
712
713 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000715 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000716
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000717 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000718 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000719 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000720 RecordData Record;
721 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000722 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000723 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000724
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000725 // Repository branch/version information.
726 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
727 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
728 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
729 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000730 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000731 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000732 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
733 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000734}
735
736/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000737void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
738 RecordData Record;
739 Record.push_back(LangOpts.Trigraphs);
740 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
741 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
742 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
743 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000744 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000745 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
746 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
747 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
748 Record.push_back(LangOpts.C99); // C99 Support
749 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
750 Record.push_back(LangOpts.CPlusPlus); // C++ Support
751 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000752 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000754 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
755 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000756 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000757 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000758 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000759 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000760 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000762 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000763 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
764 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000765 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000766 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000767 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000768
769 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
770 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
771 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
772
Chris Lattnerea5ce472009-04-27 07:35:58 +0000773 // Whether static initializers are protected by locks.
774 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000775 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000776 Record.push_back(LangOpts.Blocks); // block extension to C
777 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
778 // they are unused.
779 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
780 // (modulo the platform support).
781
782 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
783 // signed integer arithmetic overflows.
784
785 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
786 // may be ripped out at any time.
787
788 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000789 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000790 // defined.
791 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
792 // opposed to __DYNAMIC__).
793 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
794
795 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
796 // used (instead of C99 semantics).
797 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000798 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
799 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000800 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
801 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000802 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000803 Record.push_back(LangOpts.getGCMode());
804 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000805 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000806 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000807 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000808 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000809 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000810 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000811}
812
Douglas Gregor14f79002009-04-10 03:52:48 +0000813//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000814// stat cache Serialization
815//===----------------------------------------------------------------------===//
816
817namespace {
818// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramerbd218282009-11-28 10:07:24 +0000819class PCHStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000820public:
821 typedef const char * key_type;
822 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000824 typedef std::pair<int, struct stat> data_type;
825 typedef const data_type& data_type_ref;
826
827 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000828 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
831 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000832 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
833 data_type_ref Data) {
834 unsigned StrLen = strlen(path);
835 clang::io::Emit16(Out, StrLen);
836 unsigned DataLen = 1; // result value
837 if (Data.first == 0)
838 DataLen += 4 + 4 + 2 + 8 + 8;
839 clang::io::Emit8(Out, DataLen);
840 return std::make_pair(StrLen + 1, DataLen);
841 }
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000843 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
844 Out.write(path, KeyLen);
845 }
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000847 void EmitData(llvm::raw_ostream& Out, key_type_ref,
848 data_type_ref Data, unsigned DataLen) {
849 using namespace clang::io;
850 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000852 // Result of stat()
853 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000855 if (Data.first == 0) {
856 Emit32(Out, (uint32_t) Data.second.st_ino);
857 Emit32(Out, (uint32_t) Data.second.st_dev);
858 Emit16(Out, (uint16_t) Data.second.st_mode);
859 Emit64(Out, (uint64_t) Data.second.st_mtime);
860 Emit64(Out, (uint64_t) Data.second.st_size);
861 }
862
863 assert(Out.tell() - Start == DataLen && "Wrong data length");
864 }
865};
866} // end anonymous namespace
867
868/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000869void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
870 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000871 // Build the on-disk hash table containing information about every
872 // stat() call.
873 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
874 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000875 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000876 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000877 Stat != StatEnd; ++Stat, ++NumStatEntries) {
878 const char *Filename = Stat->first();
879 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
880 Generator.insert(Filename, Stat->second);
881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000883 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000884 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000885 uint32_t BucketOffset;
886 {
887 llvm::raw_svector_ostream Out(StatCacheData);
888 // Make sure that no bucket is at offset 0
889 clang::io::Emit32(Out, 0);
890 BucketOffset = Generator.Emit(Out);
891 }
892
893 // Create a blob abbreviation
894 using namespace llvm;
895 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
896 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
900 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
901
902 // Write the stat cache
903 RecordData Record;
904 Record.push_back(pch::STAT_CACHE);
905 Record.push_back(BucketOffset);
906 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000907 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000908}
909
910//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000911// Source Manager Serialization
912//===----------------------------------------------------------------------===//
913
914/// \brief Create an abbreviation for the SLocEntry that refers to a
915/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000916static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000917 using namespace llvm;
918 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
919 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000924 // FileEntry fields.
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +0000927 // HeaderFileInfo fields.
928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +0000932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000933 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000934}
935
936/// \brief Create an abbreviation for the SLocEntry that refers to a
937/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000938static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000939 using namespace llvm;
940 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
941 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000947 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000948}
949
950/// \brief Create an abbreviation for the SLocEntry that refers to a
951/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000952static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000953 using namespace llvm;
954 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
955 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000957 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000958}
959
960/// \brief Create an abbreviation for the SLocEntry that refers to an
961/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000962static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000963 using namespace llvm;
964 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
965 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000971 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000972}
973
974/// \brief Writes the block containing the serialized form of the
975/// source manager.
976///
977/// TODO: We should probably use an on-disk hash table (stored in a
978/// blob), indexed based on the file name, so that we only create
979/// entries for files that we actually need. In the common case (no
980/// errors), we probably won't have to create file entries for any of
981/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000982void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 const Preprocessor &PP,
984 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000985 RecordData Record;
986
Chris Lattnerf04ad692009-04-10 17:16:57 +0000987 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000988 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000989
990 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000991 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
992 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
993 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
994 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000995
Douglas Gregorbd945002009-04-13 16:31:14 +0000996 // Write the line table.
997 if (SourceMgr.hasLineTable()) {
998 LineTableInfo &LineTable = SourceMgr.getLineTable();
999
1000 // Emit the file names
1001 Record.push_back(LineTable.getNumFilenames());
1002 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1003 // Emit the file name
1004 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001005 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001006 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1007 Record.push_back(FilenameLen);
1008 if (FilenameLen)
1009 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Douglas Gregorbd945002009-04-13 16:31:14 +00001012 // Emit the line entries
1013 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1014 L != LEnd; ++L) {
1015 // Emit the file ID
1016 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Douglas Gregorbd945002009-04-13 16:31:14 +00001018 // Emit the line entries
1019 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001020 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001021 LEEnd = L->second.end();
1022 LE != LEEnd; ++LE) {
1023 Record.push_back(LE->FileOffset);
1024 Record.push_back(LE->LineNo);
1025 Record.push_back(LE->FilenameID);
1026 Record.push_back((unsigned)LE->FileKind);
1027 Record.push_back(LE->IncludeOffset);
1028 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001029 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001030 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001031 }
1032
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001033 // Write out the source location entry table. We skip the first
1034 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001035 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001036 RecordData PreloadSLocs;
1037 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001038 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1039 // Get this source location entry.
1040 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001041
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001042 // Record the offset of this source-location entry.
1043 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1044
1045 // Figure out which record code to use.
1046 unsigned Code;
1047 if (SLoc->isFile()) {
1048 if (SLoc->getFile().getContentCache()->Entry)
1049 Code = pch::SM_SLOC_FILE_ENTRY;
1050 else
1051 Code = pch::SM_SLOC_BUFFER_ENTRY;
1052 } else
1053 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1054 Record.clear();
1055 Record.push_back(Code);
1056
1057 Record.push_back(SLoc->getOffset());
1058 if (SLoc->isFile()) {
1059 const SrcMgr::FileInfo &File = SLoc->getFile();
1060 Record.push_back(File.getIncludeLoc().getRawEncoding());
1061 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1062 Record.push_back(File.hasLineDirectives());
1063
1064 const SrcMgr::ContentCache *Content = File.getContentCache();
1065 if (Content->Entry) {
1066 // The source location entry is a file. The blob associated
1067 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Douglas Gregor2d52be52010-03-21 22:49:54 +00001069 // Emit size/modification time for this file.
1070 Record.push_back(Content->Entry->getSize());
1071 Record.push_back(Content->Entry->getModificationTime());
1072
Douglas Gregor12fab312010-03-16 16:35:32 +00001073 // Emit header-search information associated with this file.
1074 HeaderFileInfo HFI;
1075 HeaderSearch &HS = PP.getHeaderSearchInfo();
1076 if (Content->Entry->getUID() < HS.header_file_size())
1077 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1078 Record.push_back(HFI.isImport);
1079 Record.push_back(HFI.DirInfo);
1080 Record.push_back(HFI.NumIncludes);
1081 AddIdentifierRef(HFI.ControllingMacro, Record);
1082
Douglas Gregore650c8c2009-07-07 00:12:59 +00001083 // Turn the file name into an absolute path, if it isn't already.
1084 const char *Filename = Content->Entry->getName();
1085 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001086 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001087 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Douglas Gregore650c8c2009-07-07 00:12:59 +00001089 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001090 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001091
1092 // FIXME: For now, preload all file source locations, so that
1093 // we get the appropriate File entries in the reader. This is
1094 // a temporary measure.
1095 PreloadSLocs.push_back(SLocEntryOffsets.size());
1096 } else {
1097 // The source location entry is a buffer. The blob associated
1098 // with this entry contains the contents of the buffer.
1099
1100 // We add one to the size so that we capture the trailing NULL
1101 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1102 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001103 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001104 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001105 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001106 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1107 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001108 Record.clear();
1109 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1110 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001111 llvm::StringRef(Buffer->getBufferStart(),
1112 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001113
1114 if (strcmp(Name, "<built-in>") == 0)
1115 PreloadSLocs.push_back(SLocEntryOffsets.size());
1116 }
1117 } else {
1118 // The source location entry is an instantiation.
1119 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1120 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1121 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1122 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1123
1124 // Compute the token length for this macro expansion.
1125 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001126 if (I + 1 != N)
1127 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1129 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1130 }
1131 }
1132
Douglas Gregorc9490c02009-04-16 22:23:12 +00001133 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001134
1135 if (SLocEntryOffsets.empty())
1136 return;
1137
1138 // Write the source-location offsets table into the PCH block. This
1139 // table is used for lazily loading source-location information.
1140 using namespace llvm;
1141 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1142 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1146 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001148 Record.clear();
1149 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1150 Record.push_back(SLocEntryOffsets.size());
1151 Record.push_back(SourceMgr.getNextOffset());
1152 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00001153 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +00001154 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001155
1156 // Write the source location entry preloads array, telling the PCH
1157 // reader which source locations entries it should load eagerly.
1158 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001159}
1160
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001161//===----------------------------------------------------------------------===//
1162// Preprocessor Serialization
1163//===----------------------------------------------------------------------===//
1164
Chris Lattner0b1fb982009-04-10 17:15:23 +00001165/// \brief Writes the block containing the serialized form of the
1166/// preprocessor.
1167///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001168void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001169 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001170
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001171 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1172 if (PP.getCounterValue() != 0) {
1173 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001174 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001175 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001176 }
1177
1178 // Enter the preprocessor block.
1179 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001181 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1182 // FIXME: use diagnostics subsystem for localization etc.
1183 if (PP.SawDateOrTime())
1184 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001186 // Loop over all the macro definitions that are live at the end of the file,
1187 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001188 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001189 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1190 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001191 // FIXME: This emits macros in hash table order, we should do it in a stable
1192 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001193 MacroInfo *MI = I->second;
1194
1195 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1196 // been redefined by the header (in which case they are not isBuiltinMacro).
1197 if (MI->isBuiltinMacro())
1198 continue;
1199
Chris Lattner7356a312009-04-11 21:15:38 +00001200 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001201 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001202 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1203 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001205 unsigned Code;
1206 if (MI->isObjectLike()) {
1207 Code = pch::PP_MACRO_OBJECT_LIKE;
1208 } else {
1209 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001211 Record.push_back(MI->isC99Varargs());
1212 Record.push_back(MI->isGNUVarargs());
1213 Record.push_back(MI->getNumArgs());
1214 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1215 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001216 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001217 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001218
1219 // If we have a detailed preprocessing record, record the macro definition
1220 // ID that corresponds to this macro.
1221 if (PPRec)
1222 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1223
Douglas Gregorc9490c02009-04-16 22:23:12 +00001224 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001225 Record.clear();
1226
Chris Lattnerdf961c22009-04-10 18:08:30 +00001227 // Emit the tokens array.
1228 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1229 // Note that we know that the preprocessor does not have any annotation
1230 // tokens in it because they are created by the parser, and thus can't be
1231 // in a macro definition.
1232 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Chris Lattnerdf961c22009-04-10 18:08:30 +00001234 Record.push_back(Tok.getLocation().getRawEncoding());
1235 Record.push_back(Tok.getLength());
1236
Chris Lattnerdf961c22009-04-10 18:08:30 +00001237 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1238 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001239 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Chris Lattnerdf961c22009-04-10 18:08:30 +00001241 // FIXME: Should translate token kind to a stable encoding.
1242 Record.push_back(Tok.getKind());
1243 // FIXME: Should translate token flags to a stable encoding.
1244 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Douglas Gregorc9490c02009-04-16 22:23:12 +00001246 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001247 Record.clear();
1248 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001249 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001250 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001251
1252 // If the preprocessor has a preprocessing record, emit it.
1253 unsigned NumPreprocessingRecords = 0;
1254 if (PPRec) {
1255 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1256 E != EEnd; ++E) {
1257 Record.clear();
1258
1259 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1260 Record.push_back(NumPreprocessingRecords++);
1261 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1262 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1263 AddIdentifierRef(MI->getName(), Record);
1264 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1265 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1266 continue;
1267 }
1268
1269 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1270 // Record this macro definition's location.
1271 pch::IdentID ID = getMacroDefinitionID(MD);
1272 if (ID != MacroDefinitionOffsets.size()) {
1273 if (ID > MacroDefinitionOffsets.size())
1274 MacroDefinitionOffsets.resize(ID + 1);
1275
1276 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1277 } else
1278 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1279
1280 Record.push_back(NumPreprocessingRecords++);
1281 Record.push_back(ID);
1282 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1283 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1284 AddIdentifierRef(MD->getName(), Record);
1285 AddSourceLocation(MD->getLocation(), Record);
1286 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1287 continue;
1288 }
1289 }
1290 }
1291
Douglas Gregorc9490c02009-04-16 22:23:12 +00001292 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001293
1294 // Write the offsets table for the preprocessing record.
1295 if (NumPreprocessingRecords > 0) {
1296 // Write the offsets table for identifier IDs.
1297 using namespace llvm;
1298 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1299 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1303 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1304
1305 Record.clear();
1306 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1307 Record.push_back(NumPreprocessingRecords);
1308 Record.push_back(MacroDefinitionOffsets.size());
1309 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1310 (const char *)&MacroDefinitionOffsets.front(),
1311 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1312 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001313}
1314
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001315//===----------------------------------------------------------------------===//
1316// Type Serialization
1317//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001318
Douglas Gregor2cf26342009-04-09 22:27:44 +00001319/// \brief Write the representation of a type to the PCH stream.
John McCall0953e762009-09-24 19:53:00 +00001320void PCHWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001321 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001322 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001323 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Douglas Gregor2cf26342009-04-09 22:27:44 +00001325 // Record the offset for this type.
1326 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001327 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001328 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1329 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001330 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001331 }
1332
1333 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Douglas Gregor2cf26342009-04-09 22:27:44 +00001335 // Emit the type's representation.
1336 PCHTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001337
Douglas Gregora4923eb2009-11-16 21:35:15 +00001338 if (T.hasLocalNonFastQualifiers()) {
1339 Qualifiers Qs = T.getLocalQualifiers();
1340 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001341 Record.push_back(Qs.getAsOpaqueValue());
1342 W.Code = pch::TYPE_EXT_QUAL;
1343 } else {
1344 switch (T->getTypeClass()) {
1345 // For all of the concrete, non-dependent types, call the
1346 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001347#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001348 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001349#define ABSTRACT_TYPE(Class, Base)
1350#define DEPENDENT_TYPE(Class, Base)
1351#include "clang/AST/TypeNodes.def"
1352
John McCall0953e762009-09-24 19:53:00 +00001353 // For all of the dependent type nodes (which only occur in C++
1354 // templates), produce an error.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001355#define TYPE(Class, Base)
1356#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1357#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001358 assert(false && "Cannot serialize dependent type nodes");
1359 break;
1360 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001361 }
1362
1363 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001364 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001365
1366 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001367 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001368}
1369
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001370//===----------------------------------------------------------------------===//
1371// Declaration Serialization
1372//===----------------------------------------------------------------------===//
1373
Douglas Gregor2cf26342009-04-09 22:27:44 +00001374/// \brief Write the block containing all of the declaration IDs
1375/// lexically declared within the given DeclContext.
1376///
1377/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1378/// bistream, or 0 if no block was written.
Mike Stump1eb44332009-09-09 15:08:12 +00001379uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001380 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001381 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001382 return 0;
1383
Douglas Gregorc9490c02009-04-16 22:23:12 +00001384 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001385 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001386 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1387 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001388 AddDeclRef(*D, Record);
1389
Douglas Gregor25123082009-04-22 22:34:57 +00001390 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001391 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001392 return Offset;
1393}
1394
1395/// \brief Write the block containing all of the declaration IDs
1396/// visible from the given DeclContext.
1397///
1398/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1399/// bistream, or 0 if no block was written.
1400uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1401 DeclContext *DC) {
1402 if (DC->getPrimaryContext() != DC)
1403 return 0;
1404
Douglas Gregoraff22df2009-04-21 22:32:33 +00001405 // Since there is no name lookup into functions or methods, and we
1406 // perform name lookup for the translation unit via the
1407 // IdentifierInfo chains, don't bother to build a
1408 // visible-declarations table for these entities.
1409 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001410 return 0;
1411
Douglas Gregor2cf26342009-04-09 22:27:44 +00001412 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001413 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001414
1415 // Serialize the contents of the mapping used for lookup. Note that,
1416 // although we have two very different code paths, the serialized
1417 // representation is the same for both cases: a declaration name,
1418 // followed by a size, followed by references to the visible
1419 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001420 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001421 RecordData Record;
1422 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001423 if (!Map)
1424 return 0;
1425
Douglas Gregor2cf26342009-04-09 22:27:44 +00001426 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1427 D != DEnd; ++D) {
1428 AddDeclarationName(D->first, Record);
1429 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1430 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001431 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001432 AddDeclRef(*Result.first, Record);
1433 }
1434
1435 if (Record.size() == 0)
1436 return 0;
1437
Douglas Gregorc9490c02009-04-16 22:23:12 +00001438 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001439 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001440 return Offset;
1441}
1442
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001443//===----------------------------------------------------------------------===//
1444// Global Method Pool and Selector Serialization
1445//===----------------------------------------------------------------------===//
1446
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001447namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001448// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramerbd218282009-11-28 10:07:24 +00001449class PCHMethodPoolTrait {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001450 PCHWriter &Writer;
1451
1452public:
1453 typedef Selector key_type;
1454 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001456 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1457 typedef const data_type& data_type_ref;
1458
1459 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001461 static unsigned ComputeHash(Selector Sel) {
1462 unsigned N = Sel.getNumArgs();
1463 if (N == 0)
1464 ++N;
1465 unsigned R = 5381;
1466 for (unsigned I = 0; I != N; ++I)
1467 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001468 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001469 return R;
1470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
1472 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001473 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1474 data_type_ref Methods) {
1475 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1476 clang::io::Emit16(Out, KeyLen);
1477 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump1eb44332009-09-09 15:08:12 +00001478 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001479 Method = Method->Next)
1480 if (Method->Method)
1481 DataLen += 4;
Mike Stump1eb44332009-09-09 15:08:12 +00001482 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001483 Method = Method->Next)
1484 if (Method->Method)
1485 DataLen += 4;
1486 clang::io::Emit16(Out, DataLen);
1487 return std::make_pair(KeyLen, DataLen);
1488 }
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Douglas Gregor83941df2009-04-25 17:48:32 +00001490 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001491 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001492 assert((Start >> 32) == 0 && "Selector key offset too large");
1493 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001494 unsigned N = Sel.getNumArgs();
1495 clang::io::Emit16(Out, N);
1496 if (N == 0)
1497 N = 1;
1498 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001499 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001500 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001503 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001504 data_type_ref Methods, unsigned DataLen) {
1505 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001506 unsigned NumInstanceMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001507 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001508 Method = Method->Next)
1509 if (Method->Method)
1510 ++NumInstanceMethods;
1511
1512 unsigned NumFactoryMethods = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001513 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001514 Method = Method->Next)
1515 if (Method->Method)
1516 ++NumFactoryMethods;
1517
1518 clang::io::Emit16(Out, NumInstanceMethods);
1519 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump1eb44332009-09-09 15:08:12 +00001520 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001521 Method = Method->Next)
1522 if (Method->Method)
1523 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump1eb44332009-09-09 15:08:12 +00001524 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001525 Method = Method->Next)
1526 if (Method->Method)
1527 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001528
1529 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001530 }
1531};
1532} // end anonymous namespace
1533
1534/// \brief Write the method pool into the PCH file.
1535///
1536/// The method pool contains both instance and factory methods, stored
1537/// in an on-disk hash table indexed by the selector.
1538void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1539 using namespace llvm;
1540
1541 // Create and write out the blob that contains the instance and
1542 // factor method pools.
1543 bool Empty = true;
1544 {
1545 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001547 // Create the on-disk hash table representation. Start by
1548 // iterating through the instance method pool.
1549 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001550 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001551 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001552 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001553 InstanceEnd = SemaRef.InstanceMethodPool.end();
1554 Instance != InstanceEnd; ++Instance) {
1555 // Check whether there is a factory method with the same
1556 // selector.
1557 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1558 = SemaRef.FactoryMethodPool.find(Instance->first);
1559
1560 if (Factory == SemaRef.FactoryMethodPool.end())
1561 Generator.insert(Instance->first,
Mike Stump1eb44332009-09-09 15:08:12 +00001562 std::make_pair(Instance->second,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001563 ObjCMethodList()));
1564 else
1565 Generator.insert(Instance->first,
1566 std::make_pair(Instance->second, Factory->second));
1567
Douglas Gregor83941df2009-04-25 17:48:32 +00001568 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001569 Empty = false;
1570 }
1571
1572 // Now iterate through the factory method pool, to pick up any
1573 // selectors that weren't already in the instance method pool.
1574 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump1eb44332009-09-09 15:08:12 +00001575 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001576 FactoryEnd = SemaRef.FactoryMethodPool.end();
1577 Factory != FactoryEnd; ++Factory) {
1578 // Check whether there is an instance method with the same
1579 // selector. If so, there is no work to do here.
1580 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1581 = SemaRef.InstanceMethodPool.find(Factory->first);
1582
Douglas Gregor83941df2009-04-25 17:48:32 +00001583 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001584 Generator.insert(Factory->first,
1585 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001586 ++NumSelectorsInMethodPool;
1587 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001588
1589 Empty = false;
1590 }
1591
Douglas Gregor83941df2009-04-25 17:48:32 +00001592 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001593 return;
1594
1595 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001596 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001597 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001598 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001599 {
1600 PCHMethodPoolTrait Trait(*this);
1601 llvm::raw_svector_ostream Out(MethodPool);
1602 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001603 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001604 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001605
1606 // For every selector that we have seen but which was not
1607 // written into the hash table, write the selector itself and
1608 // record it's offset.
1609 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1610 if (SelectorOffsets[I] == 0)
1611 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001612 }
1613
1614 // Create a blob abbreviation
1615 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1616 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1620 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1621
Douglas Gregor83941df2009-04-25 17:48:32 +00001622 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001623 RecordData Record;
1624 Record.push_back(pch::METHOD_POOL);
1625 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001626 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001627 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001628
1629 // Create a blob abbreviation for the selector table offsets.
1630 Abbrev = new BitCodeAbbrev();
1631 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1634 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1635
1636 // Write the selector offsets table.
1637 Record.clear();
1638 Record.push_back(pch::SELECTOR_OFFSETS);
1639 Record.push_back(SelectorOffsets.size());
1640 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1641 (const char *)&SelectorOffsets.front(),
1642 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001643 }
1644}
1645
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001646//===----------------------------------------------------------------------===//
1647// Identifier Table Serialization
1648//===----------------------------------------------------------------------===//
1649
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001650namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +00001651class PCHIdentifierTableTrait {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001652 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001653 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001654
Douglas Gregora92193e2009-04-28 21:18:29 +00001655 /// \brief Determines whether this is an "interesting" identifier
1656 /// that needs a full IdentifierInfo structure written into the hash
1657 /// table.
1658 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1659 return II->isPoisoned() ||
1660 II->isExtensionToken() ||
1661 II->hasMacroDefinition() ||
1662 II->getObjCOrBuiltinID() ||
1663 II->getFETokenInfo<void>();
1664 }
1665
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001666public:
1667 typedef const IdentifierInfo* key_type;
1668 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001670 typedef pch::IdentID data_type;
1671 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001672
1673 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001674 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001675
1676 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001677 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001678 }
Mike Stump1eb44332009-09-09 15:08:12 +00001679
1680 std::pair<unsigned,unsigned>
1681 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001682 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001683 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001684 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1685 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001686 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001687 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001688 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001689 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001690 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1691 DEnd = IdentifierResolver::end();
1692 D != DEnd; ++D)
1693 DataLen += sizeof(pch::DeclID);
1694 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001695 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001696 // We emit the key length after the data length so that every
1697 // string is preceded by a 16-bit length. This matches the PTH
1698 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001699 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001700 return std::make_pair(KeyLen, DataLen);
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702
1703 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001704 unsigned KeyLen) {
1705 // Record the location of the key data. This is used when generating
1706 // the mapping from persistent IDs to strings.
1707 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001708 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001709 }
Mike Stump1eb44332009-09-09 15:08:12 +00001710
1711 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001712 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001713 if (!isInterestingIdentifier(II)) {
1714 clang::io::Emit32(Out, ID << 1);
1715 return;
1716 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001717
Douglas Gregora92193e2009-04-28 21:18:29 +00001718 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001719 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001720 bool hasMacroDefinition =
1721 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001722 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001723 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001724 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1725 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1726 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1727 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001728 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001729
Douglas Gregor37e26842009-04-21 23:56:24 +00001730 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001731 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001732
Douglas Gregor668c1a42009-04-21 22:25:48 +00001733 // Emit the declaration IDs in reverse order, because the
1734 // IdentifierResolver provides the declarations as they would be
1735 // visible (e.g., the function "stat" would come before the struct
1736 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1737 // adds declarations to the end of the list (so we need to see the
1738 // struct "status" before the function "status").
Mike Stump1eb44332009-09-09 15:08:12 +00001739 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001740 IdentifierResolver::end());
1741 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1742 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001743 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001744 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001745 }
1746};
1747} // end anonymous namespace
1748
Douglas Gregorafaf3082009-04-11 00:14:32 +00001749/// \brief Write the identifier table into the PCH file.
1750///
1751/// The identifier table consists of a blob containing string data
1752/// (the actual identifiers themselves) and a separate "offsets" index
1753/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001754void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001755 using namespace llvm;
1756
1757 // Create and write out the blob that contains the identifier
1758 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001759 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001760 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Douglas Gregor92b059e2009-04-28 20:33:11 +00001762 // Look for any identifiers that were named while processing the
1763 // headers, but are otherwise not needed. We add these to the hash
1764 // table to enable checking of the predefines buffer in the case
1765 // where the user adds new macro definitions when building the PCH
1766 // file.
1767 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1768 IDEnd = PP.getIdentifierTable().end();
1769 ID != IDEnd; ++ID)
1770 getIdentifierRef(ID->second);
1771
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001772 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001773 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001774 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1775 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1776 ID != IDEnd; ++ID) {
1777 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001778 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001779 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001780
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001781 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001782 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001783 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001784 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001785 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001786 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001787 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001788 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001789 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001790 }
1791
1792 // Create a blob abbreviation
1793 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1794 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001797 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001798
1799 // Write the identifier table
1800 RecordData Record;
1801 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001802 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001803 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001804 }
1805
1806 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001807 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1808 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1811 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1812
1813 RecordData Record;
1814 Record.push_back(pch::IDENTIFIER_OFFSET);
1815 Record.push_back(IdentifierOffsets.size());
1816 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1817 (const char *)&IdentifierOffsets.front(),
1818 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001819}
1820
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001821//===----------------------------------------------------------------------===//
1822// General Serialization Routines
1823//===----------------------------------------------------------------------===//
1824
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001825/// \brief Write a record containing the given attributes.
1826void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1827 RecordData Record;
1828 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001829 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001830 Record.push_back(Attr->isInherited());
1831 switch (Attr->getKind()) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001832 default:
1833 assert(0 && "Does not support PCH writing for this attribute yet!");
1834 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001835 case Attr::Alias:
1836 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1837 break;
1838
1839 case Attr::Aligned:
1840 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1841 break;
1842
1843 case Attr::AlwaysInline:
1844 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001846 case Attr::AnalyzerNoReturn:
1847 break;
1848
1849 case Attr::Annotate:
1850 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1851 break;
1852
1853 case Attr::AsmLabel:
1854 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1855 break;
1856
Sean Hunt7725e672009-11-25 04:20:27 +00001857 case Attr::BaseCheck:
1858 break;
1859
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001860 case Attr::Blocks:
1861 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1862 break;
1863
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001864 case Attr::CDecl:
1865 break;
1866
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001867 case Attr::Cleanup:
1868 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1869 break;
1870
1871 case Attr::Const:
1872 break;
1873
1874 case Attr::Constructor:
1875 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1876 break;
1877
1878 case Attr::DLLExport:
1879 case Attr::DLLImport:
1880 case Attr::Deprecated:
1881 break;
1882
1883 case Attr::Destructor:
1884 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1885 break;
1886
1887 case Attr::FastCall:
Sean Huntbbd37c62009-11-21 08:43:09 +00001888 case Attr::Final:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001889 break;
1890
1891 case Attr::Format: {
1892 const FormatAttr *Format = cast<FormatAttr>(Attr);
1893 AddString(Format->getType(), Record);
1894 Record.push_back(Format->getFormatIdx());
1895 Record.push_back(Format->getFirstArg());
1896 break;
1897 }
1898
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001899 case Attr::FormatArg: {
1900 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1901 Record.push_back(Format->getFormatIdx());
1902 break;
1903 }
1904
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001905 case Attr::Sentinel : {
1906 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1907 Record.push_back(Sentinel->getSentinel());
1908 Record.push_back(Sentinel->getNullPos());
1909 break;
1910 }
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Chris Lattnercf2a7212009-04-20 19:12:28 +00001912 case Attr::GNUInline:
Sean Hunt7725e672009-11-25 04:20:27 +00001913 case Attr::Hiding:
Ted Kremenekefbddd22010-02-17 02:37:45 +00001914 case Attr::IBActionKind:
Ted Kremenek47e69902010-02-18 00:05:52 +00001915 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001916 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001917 case Attr::NoDebug:
Ted Kremenek47e69902010-02-18 00:05:52 +00001918 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001919 case Attr::NoReturn:
1920 case Attr::NoThrow:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001921 break;
1922
Ted Kremenek857e9182010-05-19 17:38:06 +00001923 case Attr::IBOutletCollectionKind: {
1924 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
1925 AddDeclRef(ICA->getClass(), Record);
1926 break;
1927 }
1928
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001929 case Attr::NonNull: {
1930 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1931 Record.push_back(NonNull->size());
1932 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1933 break;
1934 }
1935
Ted Kremenek31c780d2010-02-18 00:05:45 +00001936 case Attr::CFReturnsNotRetained:
1937 case Attr::CFReturnsRetained:
1938 case Attr::NSReturnsNotRetained:
1939 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001940 case Attr::ObjCException:
1941 case Attr::ObjCNSObject:
1942 case Attr::Overloadable:
Sean Hunt7725e672009-11-25 04:20:27 +00001943 case Attr::Override:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001944 break;
1945
Anders Carlssona860e752009-08-08 18:23:56 +00001946 case Attr::PragmaPack:
1947 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001948 break;
1949
Anders Carlssona860e752009-08-08 18:23:56 +00001950 case Attr::Packed:
1951 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001953 case Attr::Pure:
1954 break;
1955
1956 case Attr::Regparm:
1957 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1958 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Nate Begeman6f3d8382009-06-26 06:32:41 +00001960 case Attr::ReqdWorkGroupSize:
1961 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1962 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1963 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1964 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001965
1966 case Attr::Section:
1967 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1968 break;
1969
1970 case Attr::StdCall:
1971 case Attr::TransparentUnion:
1972 case Attr::Unavailable:
1973 case Attr::Unused:
1974 case Attr::Used:
1975 break;
1976
1977 case Attr::Visibility:
1978 // FIXME: stable encoding
Mike Stump1eb44332009-09-09 15:08:12 +00001979 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001980 break;
1981
1982 case Attr::WarnUnusedResult:
1983 case Attr::Weak:
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001984 case Attr::WeakRef:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001985 case Attr::WeakImport:
1986 break;
1987 }
1988 }
1989
Douglas Gregorc9490c02009-04-16 22:23:12 +00001990 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001991}
1992
1993void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1994 Record.push_back(Str.size());
1995 Record.insert(Record.end(), Str.begin(), Str.end());
1996}
1997
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001998/// \brief Note that the identifier II occurs at the given offset
1999/// within the identifier table.
2000void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002001 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002002}
2003
Douglas Gregor83941df2009-04-25 17:48:32 +00002004/// \brief Note that the selector Sel occurs at the given offset
2005/// within the method pool/selector table.
2006void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2007 unsigned ID = SelectorIDs[Sel];
2008 assert(ID && "Unknown selector");
2009 SelectorOffsets[ID - 1] = Offset;
2010}
2011
Mike Stump1eb44332009-09-09 15:08:12 +00002012PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2013 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00002014 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2015 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002016
Douglas Gregore650c8c2009-07-07 00:12:59 +00002017void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2018 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002019 using namespace llvm;
2020
Douglas Gregore7785042009-04-20 15:53:59 +00002021 ASTContext &Context = SemaRef.Context;
2022 Preprocessor &PP = SemaRef.PP;
2023
Douglas Gregor2cf26342009-04-09 22:27:44 +00002024 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002025 Stream.Emit((unsigned)'C', 8);
2026 Stream.Emit((unsigned)'P', 8);
2027 Stream.Emit((unsigned)'C', 8);
2028 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002030 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002031
2032 // The translation unit is the first declaration we'll emit.
2033 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002034 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002035
Douglas Gregor2deaea32009-04-22 18:49:13 +00002036 // Make sure that we emit IdentifierInfos (and any attached
2037 // declarations) for builtins.
2038 {
2039 IdentifierTable &Table = PP.getIdentifierTable();
2040 llvm::SmallVector<const char *, 32> BuiltinNames;
2041 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2042 Context.getLangOptions().NoBuiltin);
2043 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2044 getIdentifierRef(&Table.get(BuiltinNames[I]));
2045 }
2046
Chris Lattner63d65f82009-09-08 18:19:27 +00002047 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002048 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002049 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002050 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002051 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2052 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002053 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002054
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002055 // Build a record containing all of the static unused functions in this file.
2056 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002057 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002058 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002059
Douglas Gregor14c22f22009-04-22 22:18:58 +00002060 // Build a record containing all of the locally-scoped external
2061 // declarations in this header file. Generally, this record will be
2062 // empty.
2063 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00002064 // FIXME: This is filling in the PCH file in densemap order which is
2065 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002066 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002067 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2068 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2069 TD != TDEnd; ++TD)
2070 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2071
Douglas Gregorb81c1702009-04-27 20:06:05 +00002072 // Build a record containing all of the ext_vector declarations.
2073 RecordData ExtVectorDecls;
2074 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2075 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2076
Douglas Gregor2cf26342009-04-09 22:27:44 +00002077 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002078 RecordData Record;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002079 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002080 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002081 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002082 if (StatCalls && !isysroot)
2083 WriteStatCache(*StatCalls, isysroot);
2084 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002085 // Write the record of special types.
2086 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002087
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002088 AddTypeRef(Context.getBuiltinVaListType(), Record);
2089 AddTypeRef(Context.getObjCIdType(), Record);
2090 AddTypeRef(Context.getObjCSelType(), Record);
2091 AddTypeRef(Context.getObjCProtoType(), Record);
2092 AddTypeRef(Context.getObjCClassType(), Record);
2093 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2094 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2095 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002096 AddTypeRef(Context.getjmp_bufType(), Record);
2097 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002098 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2099 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002100 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002101 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002102 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2103 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002104 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Douglas Gregor366809a2009-04-26 03:49:13 +00002106 // Keep writing types and declarations until all types and
2107 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002108 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2109 WriteDeclsBlockAbbrevs();
2110 while (!DeclTypesToEmit.empty()) {
2111 DeclOrType DOT = DeclTypesToEmit.front();
2112 DeclTypesToEmit.pop();
2113 if (DOT.isType())
2114 WriteType(DOT.getType());
2115 else
2116 WriteDecl(Context, DOT.getDecl());
2117 }
2118 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002119
Douglas Gregor813a97b2009-10-17 17:25:45 +00002120 WritePreprocessor(PP);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002121 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002122 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002123
2124 // Write the type offsets array
2125 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2126 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2128 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2129 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2130 Record.clear();
2131 Record.push_back(pch::TYPE_OFFSET);
2132 Record.push_back(TypeOffsets.size());
2133 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002134 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002135 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump1eb44332009-09-09 15:08:12 +00002136
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002137 // Write the declaration offsets array
2138 Abbrev = new BitCodeAbbrev();
2139 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2140 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2141 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2142 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2143 Record.clear();
2144 Record.push_back(pch::DECL_OFFSET);
2145 Record.push_back(DeclOffsets.size());
2146 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump1eb44332009-09-09 15:08:12 +00002147 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00002148 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002149
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002150 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002151 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002152 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002153
2154 // Write the record containing tentative definitions.
2155 if (!TentativeDefinitions.empty())
2156 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002157
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002158 // Write the record containing unused static functions.
2159 if (!UnusedStaticFuncs.empty())
2160 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002161
Douglas Gregor14c22f22009-04-22 22:18:58 +00002162 // Write the record containing locally-scoped external definitions.
2163 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002164 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002165 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002166
2167 // Write the record containing ext_vector type names.
2168 if (!ExtVectorDecls.empty())
2169 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002170
Douglas Gregor3e1af842009-04-17 22:13:46 +00002171 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002172 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002173 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002174 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002175 Record.push_back(NumLexicalDeclContexts);
2176 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002177 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002178 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002179}
2180
2181void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2182 Record.push_back(Loc.getRawEncoding());
2183}
2184
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002185void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2186 AddSourceLocation(Range.getBegin(), Record);
2187 AddSourceLocation(Range.getEnd(), Record);
2188}
2189
Douglas Gregor2cf26342009-04-09 22:27:44 +00002190void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2191 Record.push_back(Value.getBitWidth());
2192 unsigned N = Value.getNumWords();
2193 const uint64_t* Words = Value.getRawData();
2194 for (unsigned I = 0; I != N; ++I)
2195 Record.push_back(Words[I]);
2196}
2197
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002198void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2199 Record.push_back(Value.isUnsigned());
2200 AddAPInt(Value, Record);
2201}
2202
Douglas Gregor17fc2232009-04-14 21:55:33 +00002203void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2204 AddAPInt(Value.bitcastToAPInt(), Record);
2205}
2206
Douglas Gregor2cf26342009-04-09 22:27:44 +00002207void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002208 Record.push_back(getIdentifierRef(II));
2209}
2210
2211pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2212 if (II == 0)
2213 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002214
2215 pch::IdentID &ID = IdentifierIDs[II];
2216 if (ID == 0)
2217 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002218 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002219}
2220
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002221pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2222 if (MD == 0)
2223 return 0;
2224
2225 pch::IdentID &ID = MacroDefinitions[MD];
2226 if (ID == 0)
2227 ID = MacroDefinitions.size();
2228 return ID;
2229}
2230
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002231void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2232 if (SelRef.getAsOpaquePtr() == 0) {
2233 Record.push_back(0);
2234 return;
2235 }
2236
2237 pch::SelectorID &SID = SelectorIDs[SelRef];
2238 if (SID == 0) {
2239 SID = SelectorIDs.size();
2240 SelVector.push_back(SelRef);
2241 }
2242 Record.push_back(SID);
2243}
2244
Chris Lattnerd2598362010-05-10 00:25:06 +00002245void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2246 AddDeclRef(Temp->getDestructor(), Record);
2247}
2248
John McCall833ca992009-10-29 08:12:44 +00002249void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2250 RecordData &Record) {
2251 switch (Arg.getArgument().getKind()) {
2252 case TemplateArgument::Expression:
2253 AddStmt(Arg.getLocInfo().getAsExpr());
2254 break;
2255 case TemplateArgument::Type:
John McCalla93c9342009-12-07 02:54:59 +00002256 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002257 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002258 case TemplateArgument::Template:
2259 Record.push_back(
2260 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2261 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2262 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2263 break;
John McCall833ca992009-10-29 08:12:44 +00002264 case TemplateArgument::Null:
2265 case TemplateArgument::Integral:
2266 case TemplateArgument::Declaration:
2267 case TemplateArgument::Pack:
2268 break;
2269 }
2270}
2271
John McCalla93c9342009-12-07 02:54:59 +00002272void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2273 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002274 AddTypeRef(QualType(), Record);
2275 return;
2276 }
2277
John McCalla93c9342009-12-07 02:54:59 +00002278 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002279 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002280 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002281 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002282}
2283
Douglas Gregor2cf26342009-04-09 22:27:44 +00002284void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2285 if (T.isNull()) {
2286 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2287 return;
2288 }
2289
Douglas Gregora4923eb2009-11-16 21:35:15 +00002290 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002291 T.removeFastQualifiers();
2292
Douglas Gregora4923eb2009-11-16 21:35:15 +00002293 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002294 pch::TypeID &ID = TypeIDs[T];
2295 if (ID == 0) {
2296 // We haven't seen these qualifiers applied to this type before.
2297 // Assign it a new ID. This is the only time we enqueue a
2298 // qualified type, and it has no CV qualifiers.
2299 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002300 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002301 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002302
John McCall0953e762009-09-24 19:53:00 +00002303 // Encode the type qualifiers in the type reference.
2304 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2305 return;
2306 }
2307
Douglas Gregora4923eb2009-11-16 21:35:15 +00002308 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002309
Douglas Gregor2cf26342009-04-09 22:27:44 +00002310 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002311 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002312 switch (BT->getKind()) {
2313 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2314 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2315 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2316 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2317 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2318 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2319 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2320 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002321 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002322 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2323 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2324 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2325 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2326 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2327 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2328 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002329 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002330 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2331 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2332 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002333 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002334 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2335 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002336 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2337 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002338 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2339 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002340 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002341 case BuiltinType::UndeducedAuto:
2342 assert(0 && "Should not see undeduced auto here");
2343 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002344 }
2345
John McCall0953e762009-09-24 19:53:00 +00002346 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002347 return;
2348 }
2349
John McCall0953e762009-09-24 19:53:00 +00002350 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002351 if (ID == 0) {
2352 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002353 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002354 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002355 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002356 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002357
2358 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002359 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002360}
2361
2362void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2363 if (D == 0) {
2364 Record.push_back(0);
2365 return;
2366 }
2367
Douglas Gregor8038d512009-04-10 17:25:41 +00002368 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002369 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002370 // We haven't seen this declaration before. Give it a new ID and
2371 // enqueue it in the list of declarations to emit.
2372 ID = DeclIDs.size();
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002373 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002374 }
2375
2376 Record.push_back(ID);
2377}
2378
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002379pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2380 if (D == 0)
2381 return 0;
2382
2383 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2384 return DeclIDs[D];
2385}
2386
Douglas Gregor2cf26342009-04-09 22:27:44 +00002387void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002388 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002389 Record.push_back(Name.getNameKind());
2390 switch (Name.getNameKind()) {
2391 case DeclarationName::Identifier:
2392 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2393 break;
2394
2395 case DeclarationName::ObjCZeroArgSelector:
2396 case DeclarationName::ObjCOneArgSelector:
2397 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002398 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002399 break;
2400
2401 case DeclarationName::CXXConstructorName:
2402 case DeclarationName::CXXDestructorName:
2403 case DeclarationName::CXXConversionFunctionName:
2404 AddTypeRef(Name.getCXXNameType(), Record);
2405 break;
2406
2407 case DeclarationName::CXXOperatorName:
2408 Record.push_back(Name.getCXXOverloadedOperator());
2409 break;
2410
Sean Hunt3e518bd2009-11-29 07:34:05 +00002411 case DeclarationName::CXXLiteralOperatorName:
2412 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2413 break;
2414
Douglas Gregor2cf26342009-04-09 22:27:44 +00002415 case DeclarationName::CXXUsingDirective:
2416 // No extra data to emit
2417 break;
2418 }
2419}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002420
2421void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2422 RecordData &Record) {
2423 // Nested name specifiers usually aren't too long. I think that 8 would
2424 // typically accomodate the vast majority.
2425 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2426
2427 // Push each of the NNS's onto a stack for serialization in reverse order.
2428 while (NNS) {
2429 NestedNames.push_back(NNS);
2430 NNS = NNS->getPrefix();
2431 }
2432
2433 Record.push_back(NestedNames.size());
2434 while(!NestedNames.empty()) {
2435 NNS = NestedNames.pop_back_val();
2436 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2437 Record.push_back(Kind);
2438 switch (Kind) {
2439 case NestedNameSpecifier::Identifier:
2440 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2441 break;
2442
2443 case NestedNameSpecifier::Namespace:
2444 AddDeclRef(NNS->getAsNamespace(), Record);
2445 break;
2446
2447 case NestedNameSpecifier::TypeSpec:
2448 case NestedNameSpecifier::TypeSpecWithTemplate:
2449 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2450 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2451 break;
2452
2453 case NestedNameSpecifier::Global:
2454 // Don't need to write an associated value.
2455 break;
2456 }
2457 }
2458}