blob: f919148a7748567366e1ea9e80d85ab30c26e50d [file] [log] [blame]
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000031#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000037#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000038#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42// Type serialization
43//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000044
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000046 class PCHTypeWriter {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047 PCHWriter &Writer;
48 PCHWriter::RecordData &Record;
49
50 public:
51 /// \brief Type code that corresponds to the record generated.
52 pch::TypeCode Code;
53
Mike Stump11289f42009-09-09 15:08:12 +000054 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000055 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
57 void VisitArrayType(const ArrayType *T);
58 void VisitFunctionType(const FunctionType *T);
59 void VisitTagType(const TagType *T);
60
61#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
62#define ABSTRACT_TYPE(Class, Base)
63#define DEPENDENT_TYPE(Class, Base)
64#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
72void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
73 Record.push_back(T->getWidth());
74 Record.push_back(T->isSigned());
75 Code = pch::TYPE_FIXED_WIDTH_INT;
76}
77
78void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
79 Writer.AddTypeRef(T->getElementType(), Record);
80 Code = pch::TYPE_COMPLEX;
81}
82
83void PCHTypeWriter::VisitPointerType(const PointerType *T) {
84 Writer.AddTypeRef(T->getPointeeType(), Record);
85 Code = pch::TYPE_POINTER;
86}
87
88void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000089 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090 Code = pch::TYPE_BLOCK_POINTER;
91}
92
93void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_LVALUE_REFERENCE;
96}
97
98void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
99 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Code = pch::TYPE_RVALUE_REFERENCE;
101}
102
103void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
105 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106 Code = pch::TYPE_MEMBER_POINTER;
107}
108
109void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
110 Writer.AddTypeRef(T->getElementType(), Record);
111 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000112 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
115void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
116 VisitArrayType(T);
117 Writer.AddAPInt(T->getSize(), Record);
118 Code = pch::TYPE_CONSTANT_ARRAY;
119}
120
121void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
122 VisitArrayType(T);
123 Code = pch::TYPE_INCOMPLETE_ARRAY;
124}
125
126void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
127 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000128 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
129 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000130 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131 Code = pch::TYPE_VARIABLE_ARRAY;
132}
133
134void PCHTypeWriter::VisitVectorType(const VectorType *T) {
135 Writer.AddTypeRef(T->getElementType(), Record);
136 Record.push_back(T->getNumElements());
137 Code = pch::TYPE_VECTOR;
138}
139
140void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
141 VisitVectorType(T);
142 Code = pch::TYPE_EXT_VECTOR;
143}
144
145void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
146 Writer.AddTypeRef(T->getResultType(), Record);
147}
148
149void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
150 VisitFunctionType(T);
151 Code = pch::TYPE_FUNCTION_NO_PROTO;
152}
153
154void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
155 VisitFunctionType(T);
156 Record.push_back(T->getNumArgs());
157 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
158 Writer.AddTypeRef(T->getArgType(I), Record);
159 Record.push_back(T->isVariadic());
160 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000161 Record.push_back(T->hasExceptionSpec());
162 Record.push_back(T->hasAnyExceptionSpec());
163 Record.push_back(T->getNumExceptions());
164 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
165 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166 Code = pch::TYPE_FUNCTION_PROTO;
167}
168
169void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
170 Writer.AddDeclRef(T->getDecl(), Record);
171 Code = pch::TYPE_TYPEDEF;
172}
173
174void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000175 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176 Code = pch::TYPE_TYPEOF_EXPR;
177}
178
179void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
180 Writer.AddTypeRef(T->getUnderlyingType(), Record);
181 Code = pch::TYPE_TYPEOF;
182}
183
Anders Carlsson81df7b82009-06-24 19:06:50 +0000184void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
185 Writer.AddStmt(T->getUnderlyingExpr());
186 Code = pch::TYPE_DECLTYPE;
187}
188
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000189void PCHTypeWriter::VisitTagType(const TagType *T) {
190 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000191 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192 "Cannot serialize in the middle of a type definition");
193}
194
195void PCHTypeWriter::VisitRecordType(const RecordType *T) {
196 VisitTagType(T);
197 Code = pch::TYPE_RECORD;
198}
199
200void PCHTypeWriter::VisitEnumType(const EnumType *T) {
201 VisitTagType(T);
202 Code = pch::TYPE_ENUM;
203}
204
John McCallfcc33b02009-09-05 00:15:47 +0000205void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
206 Writer.AddTypeRef(T->getUnderlyingType(), Record);
207 Record.push_back(T->getTagKind());
208 Code = pch::TYPE_ELABORATED;
209}
210
Mike Stump11289f42009-09-09 15:08:12 +0000211void
John McCallcebee162009-10-18 09:09:24 +0000212PCHTypeWriter::VisitSubstTemplateTypeParmType(
213 const SubstTemplateTypeParmType *T) {
214 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
215 Writer.AddTypeRef(T->getReplacementType(), Record);
216 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
217}
218
219void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220PCHTypeWriter::VisitTemplateSpecializationType(
221 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000222 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223 assert(false && "Cannot serialize template specialization types");
224}
225
226void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000227 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228 assert(false && "Cannot serialize qualified name types");
229}
230
231void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
232 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000233 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000234 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
235 E = T->qual_end(); I != E; ++I)
236 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000237 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000238}
239
Steve Narofffb4330f2009-06-17 22:40:22 +0000240void
241PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000242 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000243 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000244 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000245 E = T->qual_end(); I != E; ++I)
246 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000247 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248}
249
John McCall8f115c62009-10-16 21:56:05 +0000250namespace {
251
252class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
253 PCHWriter &Writer;
254 PCHWriter::RecordData &Record;
255
256public:
257 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
258 : Writer(Writer), Record(Record) { }
259
John McCall17001972009-10-18 01:05:36 +0000260#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000261#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000262 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000263#include "clang/AST/TypeLocNodes.def"
264
John McCall17001972009-10-18 01:05:36 +0000265 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
266 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000267};
268
269}
270
John McCall17001972009-10-18 01:05:36 +0000271void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
272 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000273}
John McCall17001972009-10-18 01:05:36 +0000274void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
275 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000276}
John McCall17001972009-10-18 01:05:36 +0000277void TypeLocWriter::VisitFixedWidthIntTypeLoc(FixedWidthIntTypeLoc TL) {
278 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000279}
John McCall17001972009-10-18 01:05:36 +0000280void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
281 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000282}
John McCall17001972009-10-18 01:05:36 +0000283void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
284 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000285}
John McCall17001972009-10-18 01:05:36 +0000286void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
287 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000288}
John McCall17001972009-10-18 01:05:36 +0000289void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
290 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000291}
John McCall17001972009-10-18 01:05:36 +0000292void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
293 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000294}
John McCall17001972009-10-18 01:05:36 +0000295void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
296 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000297}
John McCall17001972009-10-18 01:05:36 +0000298void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
299 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
300 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
301 Record.push_back(TL.getSizeExpr() ? 1 : 0);
302 if (TL.getSizeExpr())
303 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000304}
John McCall17001972009-10-18 01:05:36 +0000305void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
306 VisitArrayTypeLoc(TL);
307}
308void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
309 VisitArrayTypeLoc(TL);
310}
311void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
312 VisitArrayTypeLoc(TL);
313}
314void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
315 DependentSizedArrayTypeLoc TL) {
316 VisitArrayTypeLoc(TL);
317}
318void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
319 DependentSizedExtVectorTypeLoc TL) {
320 Writer.AddSourceLocation(TL.getNameLoc(), Record);
321}
322void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
323 Writer.AddSourceLocation(TL.getNameLoc(), Record);
324}
325void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
326 Writer.AddSourceLocation(TL.getNameLoc(), Record);
327}
328void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
329 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
330 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
331 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
332 Writer.AddDeclRef(TL.getArg(i), Record);
333}
334void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
335 VisitFunctionTypeLoc(TL);
336}
337void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
338 VisitFunctionTypeLoc(TL);
339}
340void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
341 Writer.AddSourceLocation(TL.getNameLoc(), Record);
342}
343void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
344 Writer.AddSourceLocation(TL.getNameLoc(), Record);
345}
346void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
347 Writer.AddSourceLocation(TL.getNameLoc(), Record);
348}
349void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
350 Writer.AddSourceLocation(TL.getNameLoc(), Record);
351}
352void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
353 Writer.AddSourceLocation(TL.getNameLoc(), Record);
354}
355void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
356 Writer.AddSourceLocation(TL.getNameLoc(), Record);
357}
358void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
359 Writer.AddSourceLocation(TL.getNameLoc(), Record);
360}
361void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
362 Writer.AddSourceLocation(TL.getNameLoc(), Record);
363}
John McCallcebee162009-10-18 09:09:24 +0000364void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
365 SubstTemplateTypeParmTypeLoc TL) {
366 Writer.AddSourceLocation(TL.getNameLoc(), Record);
367}
John McCall17001972009-10-18 01:05:36 +0000368void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
369 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000370 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
371 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
372 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
373 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
374 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall17001972009-10-18 01:05:36 +0000375}
376void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
382void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000384 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
385 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
386 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
387 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000388}
John McCallfc93cf92009-10-22 22:37:11 +0000389void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
390 Writer.AddSourceLocation(TL.getStarLoc(), Record);
391 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
392 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
393 Record.push_back(TL.hasBaseTypeAsWritten());
394 Record.push_back(TL.hasProtocolsAsWritten());
395 if (TL.hasProtocolsAsWritten())
396 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
397 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
398}
John McCall8f115c62009-10-16 21:56:05 +0000399
Chris Lattner19cea4e2009-04-22 05:57:30 +0000400//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000401// PCHWriter Implementation
402//===----------------------------------------------------------------------===//
403
Chris Lattner28fa4e62009-04-26 22:26:21 +0000404static void EmitBlockID(unsigned ID, const char *Name,
405 llvm::BitstreamWriter &Stream,
406 PCHWriter::RecordData &Record) {
407 Record.clear();
408 Record.push_back(ID);
409 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
410
411 // Emit the block name if present.
412 if (Name == 0 || Name[0] == 0) return;
413 Record.clear();
414 while (*Name)
415 Record.push_back(*Name++);
416 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
417}
418
419static void EmitRecordID(unsigned ID, const char *Name,
420 llvm::BitstreamWriter &Stream,
421 PCHWriter::RecordData &Record) {
422 Record.clear();
423 Record.push_back(ID);
424 while (*Name)
425 Record.push_back(*Name++);
426 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000427}
428
429static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
430 PCHWriter::RecordData &Record) {
431#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
432 RECORD(STMT_STOP);
433 RECORD(STMT_NULL_PTR);
434 RECORD(STMT_NULL);
435 RECORD(STMT_COMPOUND);
436 RECORD(STMT_CASE);
437 RECORD(STMT_DEFAULT);
438 RECORD(STMT_LABEL);
439 RECORD(STMT_IF);
440 RECORD(STMT_SWITCH);
441 RECORD(STMT_WHILE);
442 RECORD(STMT_DO);
443 RECORD(STMT_FOR);
444 RECORD(STMT_GOTO);
445 RECORD(STMT_INDIRECT_GOTO);
446 RECORD(STMT_CONTINUE);
447 RECORD(STMT_BREAK);
448 RECORD(STMT_RETURN);
449 RECORD(STMT_DECL);
450 RECORD(STMT_ASM);
451 RECORD(EXPR_PREDEFINED);
452 RECORD(EXPR_DECL_REF);
453 RECORD(EXPR_INTEGER_LITERAL);
454 RECORD(EXPR_FLOATING_LITERAL);
455 RECORD(EXPR_IMAGINARY_LITERAL);
456 RECORD(EXPR_STRING_LITERAL);
457 RECORD(EXPR_CHARACTER_LITERAL);
458 RECORD(EXPR_PAREN);
459 RECORD(EXPR_UNARY_OPERATOR);
460 RECORD(EXPR_SIZEOF_ALIGN_OF);
461 RECORD(EXPR_ARRAY_SUBSCRIPT);
462 RECORD(EXPR_CALL);
463 RECORD(EXPR_MEMBER);
464 RECORD(EXPR_BINARY_OPERATOR);
465 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
466 RECORD(EXPR_CONDITIONAL_OPERATOR);
467 RECORD(EXPR_IMPLICIT_CAST);
468 RECORD(EXPR_CSTYLE_CAST);
469 RECORD(EXPR_COMPOUND_LITERAL);
470 RECORD(EXPR_EXT_VECTOR_ELEMENT);
471 RECORD(EXPR_INIT_LIST);
472 RECORD(EXPR_DESIGNATED_INIT);
473 RECORD(EXPR_IMPLICIT_VALUE_INIT);
474 RECORD(EXPR_VA_ARG);
475 RECORD(EXPR_ADDR_LABEL);
476 RECORD(EXPR_STMT);
477 RECORD(EXPR_TYPES_COMPATIBLE);
478 RECORD(EXPR_CHOOSE);
479 RECORD(EXPR_GNU_NULL);
480 RECORD(EXPR_SHUFFLE_VECTOR);
481 RECORD(EXPR_BLOCK);
482 RECORD(EXPR_BLOCK_DECL_REF);
483 RECORD(EXPR_OBJC_STRING_LITERAL);
484 RECORD(EXPR_OBJC_ENCODE);
485 RECORD(EXPR_OBJC_SELECTOR_EXPR);
486 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
487 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
488 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
489 RECORD(EXPR_OBJC_KVC_REF_EXPR);
490 RECORD(EXPR_OBJC_MESSAGE_EXPR);
491 RECORD(EXPR_OBJC_SUPER_EXPR);
492 RECORD(STMT_OBJC_FOR_COLLECTION);
493 RECORD(STMT_OBJC_CATCH);
494 RECORD(STMT_OBJC_FINALLY);
495 RECORD(STMT_OBJC_AT_TRY);
496 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
497 RECORD(STMT_OBJC_AT_THROW);
498#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000499}
Mike Stump11289f42009-09-09 15:08:12 +0000500
Chris Lattner28fa4e62009-04-26 22:26:21 +0000501void PCHWriter::WriteBlockInfoBlock() {
502 RecordData Record;
503 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattner64031982009-04-27 00:40:25 +0000505#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000506#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner28fa4e62009-04-26 22:26:21 +0000508 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000509 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000510 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000511 RECORD(TYPE_OFFSET);
512 RECORD(DECL_OFFSET);
513 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000514 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000515 RECORD(IDENTIFIER_OFFSET);
516 RECORD(IDENTIFIER_TABLE);
517 RECORD(EXTERNAL_DEFINITIONS);
518 RECORD(SPECIAL_TYPES);
519 RECORD(STATISTICS);
520 RECORD(TENTATIVE_DEFINITIONS);
521 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
522 RECORD(SELECTOR_OFFSETS);
523 RECORD(METHOD_POOL);
524 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000525 RECORD(SOURCE_LOCATION_OFFSETS);
526 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000527 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000528 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000529 RECORD(COMMENT_RANGES);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000530 RECORD(SVN_BRANCH_REVISION);
531
Chris Lattner28fa4e62009-04-26 22:26:21 +0000532 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000533 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000534 RECORD(SM_SLOC_FILE_ENTRY);
535 RECORD(SM_SLOC_BUFFER_ENTRY);
536 RECORD(SM_SLOC_BUFFER_BLOB);
537 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
538 RECORD(SM_LINE_TABLE);
539 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000540
Chris Lattner28fa4e62009-04-26 22:26:21 +0000541 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000542 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000543 RECORD(PP_MACRO_OBJECT_LIKE);
544 RECORD(PP_MACRO_FUNCTION_LIKE);
545 RECORD(PP_TOKEN);
546
Douglas Gregor12bfa382009-10-17 00:13:19 +0000547 // Decls and Types block.
548 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000549 RECORD(TYPE_EXT_QUAL);
550 RECORD(TYPE_FIXED_WIDTH_INT);
551 RECORD(TYPE_COMPLEX);
552 RECORD(TYPE_POINTER);
553 RECORD(TYPE_BLOCK_POINTER);
554 RECORD(TYPE_LVALUE_REFERENCE);
555 RECORD(TYPE_RVALUE_REFERENCE);
556 RECORD(TYPE_MEMBER_POINTER);
557 RECORD(TYPE_CONSTANT_ARRAY);
558 RECORD(TYPE_INCOMPLETE_ARRAY);
559 RECORD(TYPE_VARIABLE_ARRAY);
560 RECORD(TYPE_VECTOR);
561 RECORD(TYPE_EXT_VECTOR);
562 RECORD(TYPE_FUNCTION_PROTO);
563 RECORD(TYPE_FUNCTION_NO_PROTO);
564 RECORD(TYPE_TYPEDEF);
565 RECORD(TYPE_TYPEOF_EXPR);
566 RECORD(TYPE_TYPEOF);
567 RECORD(TYPE_RECORD);
568 RECORD(TYPE_ENUM);
569 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000570 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000571 RECORD(DECL_ATTR);
572 RECORD(DECL_TRANSLATION_UNIT);
573 RECORD(DECL_TYPEDEF);
574 RECORD(DECL_ENUM);
575 RECORD(DECL_RECORD);
576 RECORD(DECL_ENUM_CONSTANT);
577 RECORD(DECL_FUNCTION);
578 RECORD(DECL_OBJC_METHOD);
579 RECORD(DECL_OBJC_INTERFACE);
580 RECORD(DECL_OBJC_PROTOCOL);
581 RECORD(DECL_OBJC_IVAR);
582 RECORD(DECL_OBJC_AT_DEFS_FIELD);
583 RECORD(DECL_OBJC_CLASS);
584 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
585 RECORD(DECL_OBJC_CATEGORY);
586 RECORD(DECL_OBJC_CATEGORY_IMPL);
587 RECORD(DECL_OBJC_IMPLEMENTATION);
588 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
589 RECORD(DECL_OBJC_PROPERTY);
590 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000591 RECORD(DECL_FIELD);
592 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000593 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000594 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000595 RECORD(DECL_FILE_SCOPE_ASM);
596 RECORD(DECL_BLOCK);
597 RECORD(DECL_CONTEXT_LEXICAL);
598 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000599 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000600 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000601#undef RECORD
602#undef BLOCK
603 Stream.ExitBlock();
604}
605
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000606/// \brief Adjusts the given filename to only write out the portion of the
607/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000608///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000609/// \param Filename the file name to adjust.
610///
611/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
612/// the returned filename will be adjusted by this system root.
613///
614/// \returns either the original filename (if it needs no adjustment) or the
615/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000616static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000617adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
618 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000619
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000620 if (!isysroot)
621 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000622
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000623 // Verify that the filename and the system root have the same prefix.
624 unsigned Pos = 0;
625 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
626 if (Filename[Pos] != isysroot[Pos])
627 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000628
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000629 // We hit the end of the filename before we hit the end of the system root.
630 if (!Filename[Pos])
631 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000632
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000633 // If the file name has a '/' at the current position, skip over the '/'.
634 // We distinguish sysroot-based includes from absolute includes by the
635 // absence of '/' at the beginning of sysroot-based includes.
636 if (Filename[Pos] == '/')
637 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000638
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000639 return Filename + Pos;
640}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000641
Douglas Gregor7b71e632009-04-27 22:23:34 +0000642/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000643void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000644 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000645
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000646 // Metadata
647 const TargetInfo &Target = Context.Target;
648 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
649 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
650 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
651 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
652 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
653 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
654 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
655 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
656 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000657
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000658 RecordData Record;
659 Record.push_back(pch::METADATA);
660 Record.push_back(pch::VERSION_MAJOR);
661 Record.push_back(pch::VERSION_MINOR);
662 Record.push_back(CLANG_VERSION_MAJOR);
663 Record.push_back(CLANG_VERSION_MINOR);
664 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000665 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000666 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000667
Douglas Gregor45fe0362009-05-12 01:31:05 +0000668 // Original file name
669 SourceManager &SM = Context.getSourceManager();
670 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
671 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
672 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
673 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
674 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
675
676 llvm::sys::Path MainFilePath(MainFile->getName());
677 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Douglas Gregor45fe0362009-05-12 01:31:05 +0000679 if (!MainFilePath.isAbsolute()) {
680 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000681 P.appendComponent(MainFilePath.str());
682 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000683 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000684 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000685 }
686
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000687 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000688 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000689 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000690 RecordData Record;
691 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000692 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000693 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000694
695 // Subversion branch/version information.
696 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
697 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
698 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
699 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
700 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
701 Record.clear();
702 Record.push_back(pch::SVN_BRANCH_REVISION);
703 Record.push_back(getClangSubversionRevision());
704 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000705}
706
707/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000708void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
709 RecordData Record;
710 Record.push_back(LangOpts.Trigraphs);
711 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
712 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
713 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
714 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
715 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
716 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
717 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
718 Record.push_back(LangOpts.C99); // C99 Support
719 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
720 Record.push_back(LangOpts.CPlusPlus); // C++ Support
721 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000722 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000723
Douglas Gregor55abb232009-04-10 20:39:37 +0000724 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
725 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
726 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000727
Douglas Gregor55abb232009-04-10 20:39:37 +0000728 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000729 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
730 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000731 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000732 Record.push_back(LangOpts.Exceptions); // Support exception handling.
733
734 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
735 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
736 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
737
Chris Lattner258172e2009-04-27 07:35:58 +0000738 // Whether static initializers are protected by locks.
739 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000740 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000741 Record.push_back(LangOpts.Blocks); // block extension to C
742 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
743 // they are unused.
744 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
745 // (modulo the platform support).
746
747 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
748 // signed integer arithmetic overflows.
749
750 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
751 // may be ripped out at any time.
752
753 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000754 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000755 // defined.
756 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
757 // opposed to __DYNAMIC__).
758 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
759
760 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
761 // used (instead of C99 semantics).
762 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000763 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
764 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000765 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
766 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000767 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000768 Record.push_back(LangOpts.getGCMode());
769 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000770 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000771 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000772 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000773 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000774 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000775}
776
Douglas Gregora7f71a92009-04-10 03:52:48 +0000777//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000778// stat cache Serialization
779//===----------------------------------------------------------------------===//
780
781namespace {
782// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000783class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000784public:
785 typedef const char * key_type;
786 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000787
Douglas Gregorc5046832009-04-27 18:38:38 +0000788 typedef std::pair<int, struct stat> data_type;
789 typedef const data_type& data_type_ref;
790
791 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000792 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000793 }
Mike Stump11289f42009-09-09 15:08:12 +0000794
795 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000796 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
797 data_type_ref Data) {
798 unsigned StrLen = strlen(path);
799 clang::io::Emit16(Out, StrLen);
800 unsigned DataLen = 1; // result value
801 if (Data.first == 0)
802 DataLen += 4 + 4 + 2 + 8 + 8;
803 clang::io::Emit8(Out, DataLen);
804 return std::make_pair(StrLen + 1, DataLen);
805 }
Mike Stump11289f42009-09-09 15:08:12 +0000806
Douglas Gregorc5046832009-04-27 18:38:38 +0000807 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
808 Out.write(path, KeyLen);
809 }
Mike Stump11289f42009-09-09 15:08:12 +0000810
Douglas Gregorc5046832009-04-27 18:38:38 +0000811 void EmitData(llvm::raw_ostream& Out, key_type_ref,
812 data_type_ref Data, unsigned DataLen) {
813 using namespace clang::io;
814 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000815
Douglas Gregorc5046832009-04-27 18:38:38 +0000816 // Result of stat()
817 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000818
Douglas Gregorc5046832009-04-27 18:38:38 +0000819 if (Data.first == 0) {
820 Emit32(Out, (uint32_t) Data.second.st_ino);
821 Emit32(Out, (uint32_t) Data.second.st_dev);
822 Emit16(Out, (uint16_t) Data.second.st_mode);
823 Emit64(Out, (uint64_t) Data.second.st_mtime);
824 Emit64(Out, (uint64_t) Data.second.st_size);
825 }
826
827 assert(Out.tell() - Start == DataLen && "Wrong data length");
828 }
829};
830} // end anonymous namespace
831
832/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000833void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
834 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000835 // Build the on-disk hash table containing information about every
836 // stat() call.
837 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
838 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000839 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000840 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000841 Stat != StatEnd; ++Stat, ++NumStatEntries) {
842 const char *Filename = Stat->first();
843 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
844 Generator.insert(Filename, Stat->second);
845 }
Mike Stump11289f42009-09-09 15:08:12 +0000846
Douglas Gregorc5046832009-04-27 18:38:38 +0000847 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000848 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000849 uint32_t BucketOffset;
850 {
851 llvm::raw_svector_ostream Out(StatCacheData);
852 // Make sure that no bucket is at offset 0
853 clang::io::Emit32(Out, 0);
854 BucketOffset = Generator.Emit(Out);
855 }
856
857 // Create a blob abbreviation
858 using namespace llvm;
859 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
860 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
861 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
862 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
864 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
865
866 // Write the stat cache
867 RecordData Record;
868 Record.push_back(pch::STAT_CACHE);
869 Record.push_back(BucketOffset);
870 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000871 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000872}
873
874//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000875// Source Manager Serialization
876//===----------------------------------------------------------------------===//
877
878/// \brief Create an abbreviation for the SLocEntry that refers to a
879/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000880static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000881 using namespace llvm;
882 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
883 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000889 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000890}
891
892/// \brief Create an abbreviation for the SLocEntry that refers to a
893/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000894static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000895 using namespace llvm;
896 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
897 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000903 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000904}
905
906/// \brief Create an abbreviation for the SLocEntry that refers to a
907/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000908static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000909 using namespace llvm;
910 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
911 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000913 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000914}
915
916/// \brief Create an abbreviation for the SLocEntry that refers to an
917/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000918static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000919 using namespace llvm;
920 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
921 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000927 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000928}
929
930/// \brief Writes the block containing the serialized form of the
931/// source manager.
932///
933/// TODO: We should probably use an on-disk hash table (stored in a
934/// blob), indexed based on the file name, so that we only create
935/// entries for files that we actually need. In the common case (no
936/// errors), we probably won't have to create file entries for any of
937/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000938void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000939 const Preprocessor &PP,
940 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000941 RecordData Record;
942
Chris Lattner0910e3b2009-04-10 17:16:57 +0000943 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000944 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000945
946 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000947 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
948 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
949 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
950 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000951
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000952 // Write the line table.
953 if (SourceMgr.hasLineTable()) {
954 LineTableInfo &LineTable = SourceMgr.getLineTable();
955
956 // Emit the file names
957 Record.push_back(LineTable.getNumFilenames());
958 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
959 // Emit the file name
960 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000961 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000962 unsigned FilenameLen = Filename? strlen(Filename) : 0;
963 Record.push_back(FilenameLen);
964 if (FilenameLen)
965 Record.insert(Record.end(), Filename, Filename + FilenameLen);
966 }
Mike Stump11289f42009-09-09 15:08:12 +0000967
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000968 // Emit the line entries
969 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
970 L != LEnd; ++L) {
971 // Emit the file ID
972 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000973
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000974 // Emit the line entries
975 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000976 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000977 LEEnd = L->second.end();
978 LE != LEEnd; ++LE) {
979 Record.push_back(LE->FileOffset);
980 Record.push_back(LE->LineNo);
981 Record.push_back(LE->FilenameID);
982 Record.push_back((unsigned)LE->FileKind);
983 Record.push_back(LE->IncludeOffset);
984 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000985 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +0000986 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000987 }
988
Douglas Gregor258ae542009-04-27 06:38:32 +0000989 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +0000990 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +0000991 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000992 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +0000993 E = HS.header_file_end();
994 I != E; ++I) {
995 Record.push_back(I->isImport);
996 Record.push_back(I->DirInfo);
997 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +0000998 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +0000999 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1000 Record.clear();
1001 }
1002
Douglas Gregor258ae542009-04-27 06:38:32 +00001003 // Write out the source location entry table. We skip the first
1004 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001005 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001006 RecordData PreloadSLocs;
1007 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001008 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1009 // Get this source location entry.
1010 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1011
Douglas Gregor258ae542009-04-27 06:38:32 +00001012 // Record the offset of this source-location entry.
1013 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1014
1015 // Figure out which record code to use.
1016 unsigned Code;
1017 if (SLoc->isFile()) {
1018 if (SLoc->getFile().getContentCache()->Entry)
1019 Code = pch::SM_SLOC_FILE_ENTRY;
1020 else
1021 Code = pch::SM_SLOC_BUFFER_ENTRY;
1022 } else
1023 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1024 Record.clear();
1025 Record.push_back(Code);
1026
1027 Record.push_back(SLoc->getOffset());
1028 if (SLoc->isFile()) {
1029 const SrcMgr::FileInfo &File = SLoc->getFile();
1030 Record.push_back(File.getIncludeLoc().getRawEncoding());
1031 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1032 Record.push_back(File.hasLineDirectives());
1033
1034 const SrcMgr::ContentCache *Content = File.getContentCache();
1035 if (Content->Entry) {
1036 // The source location entry is a file. The blob associated
1037 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001038
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001039 // Turn the file name into an absolute path, if it isn't already.
1040 const char *Filename = Content->Entry->getName();
1041 llvm::sys::Path FilePath(Filename, strlen(Filename));
1042 std::string FilenameStr;
1043 if (!FilePath.isAbsolute()) {
1044 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001045 P.appendComponent(FilePath.str());
1046 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001047 Filename = FilenameStr.c_str();
1048 }
Mike Stump11289f42009-09-09 15:08:12 +00001049
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001050 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001051 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001052
1053 // FIXME: For now, preload all file source locations, so that
1054 // we get the appropriate File entries in the reader. This is
1055 // a temporary measure.
1056 PreloadSLocs.push_back(SLocEntryOffsets.size());
1057 } else {
1058 // The source location entry is a buffer. The blob associated
1059 // with this entry contains the contents of the buffer.
1060
1061 // We add one to the size so that we capture the trailing NULL
1062 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1063 // the reader side).
1064 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1065 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001066 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1067 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001068 Record.clear();
1069 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1070 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001071 llvm::StringRef(Buffer->getBufferStart(),
1072 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001073
1074 if (strcmp(Name, "<built-in>") == 0)
1075 PreloadSLocs.push_back(SLocEntryOffsets.size());
1076 }
1077 } else {
1078 // The source location entry is an instantiation.
1079 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1080 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1081 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1082 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1083
1084 // Compute the token length for this macro expansion.
1085 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001086 if (I + 1 != N)
1087 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001088 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1089 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1090 }
1091 }
1092
Douglas Gregor8f45df52009-04-16 22:23:12 +00001093 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001094
1095 if (SLocEntryOffsets.empty())
1096 return;
1097
1098 // Write the source-location offsets table into the PCH block. This
1099 // table is used for lazily loading source-location information.
1100 using namespace llvm;
1101 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1102 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1106 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001107
Douglas Gregor258ae542009-04-27 06:38:32 +00001108 Record.clear();
1109 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1110 Record.push_back(SLocEntryOffsets.size());
1111 Record.push_back(SourceMgr.getNextOffset());
1112 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001113 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001114 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001115
1116 // Write the source location entry preloads array, telling the PCH
1117 // reader which source locations entries it should load eagerly.
1118 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001119}
1120
Douglas Gregorc5046832009-04-27 18:38:38 +00001121//===----------------------------------------------------------------------===//
1122// Preprocessor Serialization
1123//===----------------------------------------------------------------------===//
1124
Chris Lattnereeffaef2009-04-10 17:15:23 +00001125/// \brief Writes the block containing the serialized form of the
1126/// preprocessor.
1127///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001128void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001129 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001130
Chris Lattner0af3ba12009-04-13 01:29:17 +00001131 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1132 if (PP.getCounterValue() != 0) {
1133 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001134 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001135 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001136 }
1137
1138 // Enter the preprocessor block.
1139 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001140
Douglas Gregoreda6a892009-04-26 00:07:37 +00001141 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1142 // FIXME: use diagnostics subsystem for localization etc.
1143 if (PP.SawDateOrTime())
1144 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001145
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001146 // Loop over all the macro definitions that are live at the end of the file,
1147 // emitting each to the PP section.
Douglas Gregor45053152009-10-17 17:25:45 +00001148 // FIXME: Make sure that this sees macros defined in included PCH files.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001149 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1150 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001151 // FIXME: This emits macros in hash table order, we should do it in a stable
1152 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001153 MacroInfo *MI = I->second;
1154
1155 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1156 // been redefined by the header (in which case they are not isBuiltinMacro).
1157 if (MI->isBuiltinMacro())
1158 continue;
1159
Douglas Gregorc3366a52009-04-21 23:56:24 +00001160 // FIXME: Remove this identifier reference?
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001161 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001162 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001163 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1164 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001165
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001166 unsigned Code;
1167 if (MI->isObjectLike()) {
1168 Code = pch::PP_MACRO_OBJECT_LIKE;
1169 } else {
1170 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001171
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001172 Record.push_back(MI->isC99Varargs());
1173 Record.push_back(MI->isGNUVarargs());
1174 Record.push_back(MI->getNumArgs());
1175 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1176 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001177 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001178 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001179 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001180 Record.clear();
1181
Chris Lattner2199f5b2009-04-10 18:08:30 +00001182 // Emit the tokens array.
1183 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1184 // Note that we know that the preprocessor does not have any annotation
1185 // tokens in it because they are created by the parser, and thus can't be
1186 // in a macro definition.
1187 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001188
Chris Lattner2199f5b2009-04-10 18:08:30 +00001189 Record.push_back(Tok.getLocation().getRawEncoding());
1190 Record.push_back(Tok.getLength());
1191
Chris Lattner2199f5b2009-04-10 18:08:30 +00001192 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1193 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001194 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattner2199f5b2009-04-10 18:08:30 +00001196 // FIXME: Should translate token kind to a stable encoding.
1197 Record.push_back(Tok.getKind());
1198 // FIXME: Should translate token flags to a stable encoding.
1199 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001200
Douglas Gregor8f45df52009-04-16 22:23:12 +00001201 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001202 Record.clear();
1203 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001204 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001205 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001206 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001207}
1208
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001209void PCHWriter::WriteComments(ASTContext &Context) {
1210 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001211
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001212 if (Context.Comments.empty())
1213 return;
Mike Stump11289f42009-09-09 15:08:12 +00001214
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001215 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1216 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1217 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1218 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001219
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001220 RecordData Record;
1221 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001222 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001223 (const char*)&Context.Comments[0],
1224 Context.Comments.size() * sizeof(SourceRange));
1225}
1226
Douglas Gregorc5046832009-04-27 18:38:38 +00001227//===----------------------------------------------------------------------===//
1228// Type Serialization
1229//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001230
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001231/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001232void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001233 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001234 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001235 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001236
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001237 // Record the offset for this type.
1238 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001239 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001240 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1241 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001242 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001243 }
1244
1245 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001246
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001247 // Emit the type's representation.
1248 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001249
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001250 if (T.hasLocalNonFastQualifiers()) {
1251 Qualifiers Qs = T.getLocalQualifiers();
1252 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001253 Record.push_back(Qs.getAsOpaqueValue());
1254 W.Code = pch::TYPE_EXT_QUAL;
1255 } else {
1256 switch (T->getTypeClass()) {
1257 // For all of the concrete, non-dependent types, call the
1258 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001259#define TYPE(Class, Base) \
John McCall8ccfcb52009-09-24 19:53:00 +00001260 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001261#define ABSTRACT_TYPE(Class, Base)
1262#define DEPENDENT_TYPE(Class, Base)
1263#include "clang/AST/TypeNodes.def"
1264
John McCall8ccfcb52009-09-24 19:53:00 +00001265 // For all of the dependent type nodes (which only occur in C++
1266 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001267#define TYPE(Class, Base)
1268#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1269#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001270 assert(false && "Cannot serialize dependent type nodes");
1271 break;
1272 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001273 }
1274
1275 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001276 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001277
1278 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001279 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001280}
1281
Douglas Gregorc5046832009-04-27 18:38:38 +00001282//===----------------------------------------------------------------------===//
1283// Declaration Serialization
1284//===----------------------------------------------------------------------===//
1285
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001286/// \brief Write the block containing all of the declaration IDs
1287/// lexically declared within the given DeclContext.
1288///
1289/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1290/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001291uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001292 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001293 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001294 return 0;
1295
Douglas Gregor8f45df52009-04-16 22:23:12 +00001296 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001297 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001298 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1299 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001300 AddDeclRef(*D, Record);
1301
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001302 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001303 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001304 return Offset;
1305}
1306
1307/// \brief Write the block containing all of the declaration IDs
1308/// visible from the given DeclContext.
1309///
1310/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1311/// bistream, or 0 if no block was written.
1312uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1313 DeclContext *DC) {
1314 if (DC->getPrimaryContext() != DC)
1315 return 0;
1316
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001317 // Since there is no name lookup into functions or methods, and we
1318 // perform name lookup for the translation unit via the
1319 // IdentifierInfo chains, don't bother to build a
1320 // visible-declarations table for these entities.
1321 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001322 return 0;
1323
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001324 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001325 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001326
1327 // Serialize the contents of the mapping used for lookup. Note that,
1328 // although we have two very different code paths, the serialized
1329 // representation is the same for both cases: a declaration name,
1330 // followed by a size, followed by references to the visible
1331 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001332 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001333 RecordData Record;
1334 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001335 if (!Map)
1336 return 0;
1337
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001338 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1339 D != DEnd; ++D) {
1340 AddDeclarationName(D->first, Record);
1341 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1342 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001343 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001344 AddDeclRef(*Result.first, Record);
1345 }
1346
1347 if (Record.size() == 0)
1348 return 0;
1349
Douglas Gregor8f45df52009-04-16 22:23:12 +00001350 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001351 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001352 return Offset;
1353}
1354
Douglas Gregorc5046832009-04-27 18:38:38 +00001355//===----------------------------------------------------------------------===//
1356// Global Method Pool and Selector Serialization
1357//===----------------------------------------------------------------------===//
1358
Douglas Gregore84a9da2009-04-20 20:36:09 +00001359namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001360// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001361class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001362 PCHWriter &Writer;
1363
1364public:
1365 typedef Selector key_type;
1366 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001367
Douglas Gregorc78d3462009-04-24 21:10:55 +00001368 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1369 typedef const data_type& data_type_ref;
1370
1371 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001372
Douglas Gregorc78d3462009-04-24 21:10:55 +00001373 static unsigned ComputeHash(Selector Sel) {
1374 unsigned N = Sel.getNumArgs();
1375 if (N == 0)
1376 ++N;
1377 unsigned R = 5381;
1378 for (unsigned I = 0; I != N; ++I)
1379 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001380 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001381 return R;
1382 }
Mike Stump11289f42009-09-09 15:08:12 +00001383
1384 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001385 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1386 data_type_ref Methods) {
1387 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1388 clang::io::Emit16(Out, KeyLen);
1389 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001390 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001391 Method = Method->Next)
1392 if (Method->Method)
1393 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001394 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001395 Method = Method->Next)
1396 if (Method->Method)
1397 DataLen += 4;
1398 clang::io::Emit16(Out, DataLen);
1399 return std::make_pair(KeyLen, DataLen);
1400 }
Mike Stump11289f42009-09-09 15:08:12 +00001401
Douglas Gregor95c13f52009-04-25 17:48:32 +00001402 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001403 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001404 assert((Start >> 32) == 0 && "Selector key offset too large");
1405 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001406 unsigned N = Sel.getNumArgs();
1407 clang::io::Emit16(Out, N);
1408 if (N == 0)
1409 N = 1;
1410 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001411 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001412 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1413 }
Mike Stump11289f42009-09-09 15:08:12 +00001414
Douglas Gregorc78d3462009-04-24 21:10:55 +00001415 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001416 data_type_ref Methods, unsigned DataLen) {
1417 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001418 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001419 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001420 Method = Method->Next)
1421 if (Method->Method)
1422 ++NumInstanceMethods;
1423
1424 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001425 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001426 Method = Method->Next)
1427 if (Method->Method)
1428 ++NumFactoryMethods;
1429
1430 clang::io::Emit16(Out, NumInstanceMethods);
1431 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001432 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001433 Method = Method->Next)
1434 if (Method->Method)
1435 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001436 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001437 Method = Method->Next)
1438 if (Method->Method)
1439 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001440
1441 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001442 }
1443};
1444} // end anonymous namespace
1445
1446/// \brief Write the method pool into the PCH file.
1447///
1448/// The method pool contains both instance and factory methods, stored
1449/// in an on-disk hash table indexed by the selector.
1450void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1451 using namespace llvm;
1452
1453 // Create and write out the blob that contains the instance and
1454 // factor method pools.
1455 bool Empty = true;
1456 {
1457 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001458
Douglas Gregorc78d3462009-04-24 21:10:55 +00001459 // Create the on-disk hash table representation. Start by
1460 // iterating through the instance method pool.
1461 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001462 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001463 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001464 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001465 InstanceEnd = SemaRef.InstanceMethodPool.end();
1466 Instance != InstanceEnd; ++Instance) {
1467 // Check whether there is a factory method with the same
1468 // selector.
1469 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1470 = SemaRef.FactoryMethodPool.find(Instance->first);
1471
1472 if (Factory == SemaRef.FactoryMethodPool.end())
1473 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001474 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001475 ObjCMethodList()));
1476 else
1477 Generator.insert(Instance->first,
1478 std::make_pair(Instance->second, Factory->second));
1479
Douglas Gregor95c13f52009-04-25 17:48:32 +00001480 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001481 Empty = false;
1482 }
1483
1484 // Now iterate through the factory method pool, to pick up any
1485 // selectors that weren't already in the instance method pool.
1486 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001487 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001488 FactoryEnd = SemaRef.FactoryMethodPool.end();
1489 Factory != FactoryEnd; ++Factory) {
1490 // Check whether there is an instance method with the same
1491 // selector. If so, there is no work to do here.
1492 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1493 = SemaRef.InstanceMethodPool.find(Factory->first);
1494
Douglas Gregor95c13f52009-04-25 17:48:32 +00001495 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001496 Generator.insert(Factory->first,
1497 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001498 ++NumSelectorsInMethodPool;
1499 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001500
1501 Empty = false;
1502 }
1503
Douglas Gregor95c13f52009-04-25 17:48:32 +00001504 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001505 return;
1506
1507 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001508 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001509 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001510 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001511 {
1512 PCHMethodPoolTrait Trait(*this);
1513 llvm::raw_svector_ostream Out(MethodPool);
1514 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001515 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001516 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001517
1518 // For every selector that we have seen but which was not
1519 // written into the hash table, write the selector itself and
1520 // record it's offset.
1521 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1522 if (SelectorOffsets[I] == 0)
1523 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001524 }
1525
1526 // Create a blob abbreviation
1527 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1528 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1532 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1533
Douglas Gregor95c13f52009-04-25 17:48:32 +00001534 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001535 RecordData Record;
1536 Record.push_back(pch::METHOD_POOL);
1537 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001538 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001539 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001540
1541 // Create a blob abbreviation for the selector table offsets.
1542 Abbrev = new BitCodeAbbrev();
1543 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1546 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1547
1548 // Write the selector offsets table.
1549 Record.clear();
1550 Record.push_back(pch::SELECTOR_OFFSETS);
1551 Record.push_back(SelectorOffsets.size());
1552 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1553 (const char *)&SelectorOffsets.front(),
1554 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001555 }
1556}
1557
Douglas Gregorc5046832009-04-27 18:38:38 +00001558//===----------------------------------------------------------------------===//
1559// Identifier Table Serialization
1560//===----------------------------------------------------------------------===//
1561
Douglas Gregorc78d3462009-04-24 21:10:55 +00001562namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001563class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001564 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001565 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001566
Douglas Gregor1d583f22009-04-28 21:18:29 +00001567 /// \brief Determines whether this is an "interesting" identifier
1568 /// that needs a full IdentifierInfo structure written into the hash
1569 /// table.
1570 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1571 return II->isPoisoned() ||
1572 II->isExtensionToken() ||
1573 II->hasMacroDefinition() ||
1574 II->getObjCOrBuiltinID() ||
1575 II->getFETokenInfo<void>();
1576 }
1577
Douglas Gregore84a9da2009-04-20 20:36:09 +00001578public:
1579 typedef const IdentifierInfo* key_type;
1580 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001581
Douglas Gregore84a9da2009-04-20 20:36:09 +00001582 typedef pch::IdentID data_type;
1583 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001584
1585 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001586 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001587
1588 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001589 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001590 }
Mike Stump11289f42009-09-09 15:08:12 +00001591
1592 std::pair<unsigned,unsigned>
1593 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001594 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001595 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001596 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1597 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001598 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001599 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001600 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001601 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001602 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1603 DEnd = IdentifierResolver::end();
1604 D != DEnd; ++D)
1605 DataLen += sizeof(pch::DeclID);
1606 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001607 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001608 // We emit the key length after the data length so that every
1609 // string is preceded by a 16-bit length. This matches the PTH
1610 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001611 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001612 return std::make_pair(KeyLen, DataLen);
1613 }
Mike Stump11289f42009-09-09 15:08:12 +00001614
1615 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001616 unsigned KeyLen) {
1617 // Record the location of the key data. This is used when generating
1618 // the mapping from persistent IDs to strings.
1619 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001620 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001621 }
Mike Stump11289f42009-09-09 15:08:12 +00001622
1623 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001624 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001625 if (!isInterestingIdentifier(II)) {
1626 clang::io::Emit32(Out, ID << 1);
1627 return;
1628 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001629
Douglas Gregor1d583f22009-04-28 21:18:29 +00001630 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001631 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001632 bool hasMacroDefinition =
1633 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001634 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001635 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001636 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001637 Bits = (Bits << 1) | II->isExtensionToken();
1638 Bits = (Bits << 1) | II->isPoisoned();
1639 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregorb9256522009-04-28 21:32:13 +00001640 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001641
Douglas Gregorc3366a52009-04-21 23:56:24 +00001642 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001643 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001644
Douglas Gregora868bbd2009-04-21 22:25:48 +00001645 // Emit the declaration IDs in reverse order, because the
1646 // IdentifierResolver provides the declarations as they would be
1647 // visible (e.g., the function "stat" would come before the struct
1648 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1649 // adds declarations to the end of the list (so we need to see the
1650 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001651 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001652 IdentifierResolver::end());
1653 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1654 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001655 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001656 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001657 }
1658};
1659} // end anonymous namespace
1660
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001661/// \brief Write the identifier table into the PCH file.
1662///
1663/// The identifier table consists of a blob containing string data
1664/// (the actual identifiers themselves) and a separate "offsets" index
1665/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001666void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001667 using namespace llvm;
1668
1669 // Create and write out the blob that contains the identifier
1670 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001671 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001672 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001673
Douglas Gregore6648fb2009-04-28 20:33:11 +00001674 // Look for any identifiers that were named while processing the
1675 // headers, but are otherwise not needed. We add these to the hash
1676 // table to enable checking of the predefines buffer in the case
1677 // where the user adds new macro definitions when building the PCH
1678 // file.
1679 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1680 IDEnd = PP.getIdentifierTable().end();
1681 ID != IDEnd; ++ID)
1682 getIdentifierRef(ID->second);
1683
Douglas Gregore84a9da2009-04-20 20:36:09 +00001684 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001685 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001686 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1687 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1688 ID != IDEnd; ++ID) {
1689 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001690 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001691 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001692
Douglas Gregore84a9da2009-04-20 20:36:09 +00001693 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001694 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001695 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001696 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001697 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001698 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001699 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001700 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001701 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001702 }
1703
1704 // Create a blob abbreviation
1705 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1706 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001707 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001708 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001709 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001710
1711 // Write the identifier table
1712 RecordData Record;
1713 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001714 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001715 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001716 }
1717
1718 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001719 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1720 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1721 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1722 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1723 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1724
1725 RecordData Record;
1726 Record.push_back(pch::IDENTIFIER_OFFSET);
1727 Record.push_back(IdentifierOffsets.size());
1728 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1729 (const char *)&IdentifierOffsets.front(),
1730 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001731}
1732
Douglas Gregorc5046832009-04-27 18:38:38 +00001733//===----------------------------------------------------------------------===//
1734// General Serialization Routines
1735//===----------------------------------------------------------------------===//
1736
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001737/// \brief Write a record containing the given attributes.
1738void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1739 RecordData Record;
1740 for (; Attr; Attr = Attr->getNext()) {
1741 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1742 Record.push_back(Attr->isInherited());
1743 switch (Attr->getKind()) {
1744 case Attr::Alias:
1745 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1746 break;
1747
1748 case Attr::Aligned:
1749 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1750 break;
1751
1752 case Attr::AlwaysInline:
1753 break;
Mike Stump11289f42009-09-09 15:08:12 +00001754
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001755 case Attr::AnalyzerNoReturn:
1756 break;
1757
1758 case Attr::Annotate:
1759 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1760 break;
1761
1762 case Attr::AsmLabel:
1763 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1764 break;
1765
Alexis Hunt54a02542009-11-25 04:20:27 +00001766 case Attr::BaseCheck:
1767 break;
1768
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001769 case Attr::Blocks:
1770 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1771 break;
1772
Eli Friedmane4310c82009-11-09 18:38:53 +00001773 case Attr::CDecl:
1774 break;
1775
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001776 case Attr::Cleanup:
1777 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1778 break;
1779
1780 case Attr::Const:
1781 break;
1782
1783 case Attr::Constructor:
1784 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1785 break;
1786
1787 case Attr::DLLExport:
1788 case Attr::DLLImport:
1789 case Attr::Deprecated:
1790 break;
1791
1792 case Attr::Destructor:
1793 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1794 break;
1795
1796 case Attr::FastCall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001797 case Attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001798 break;
1799
1800 case Attr::Format: {
1801 const FormatAttr *Format = cast<FormatAttr>(Attr);
1802 AddString(Format->getType(), Record);
1803 Record.push_back(Format->getFormatIdx());
1804 Record.push_back(Format->getFirstArg());
1805 break;
1806 }
1807
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001808 case Attr::FormatArg: {
1809 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1810 Record.push_back(Format->getFormatIdx());
1811 break;
1812 }
1813
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001814 case Attr::Sentinel : {
1815 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1816 Record.push_back(Sentinel->getSentinel());
1817 Record.push_back(Sentinel->getNullPos());
1818 break;
1819 }
Mike Stump11289f42009-09-09 15:08:12 +00001820
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001821 case Attr::GNUInline:
Alexis Hunt54a02542009-11-25 04:20:27 +00001822 case Attr::Hiding:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001823 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001824 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001825 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001826 case Attr::NoReturn:
1827 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001828 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001829 break;
1830
1831 case Attr::NonNull: {
1832 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1833 Record.push_back(NonNull->size());
1834 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1835 break;
1836 }
1837
1838 case Attr::ObjCException:
1839 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001840 case Attr::CFReturnsRetained:
1841 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001842 case Attr::Overloadable:
Alexis Hunt54a02542009-11-25 04:20:27 +00001843 case Attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001844 break;
1845
Anders Carlsson68e0b682009-08-08 18:23:56 +00001846 case Attr::PragmaPack:
1847 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001848 break;
1849
Anders Carlsson68e0b682009-08-08 18:23:56 +00001850 case Attr::Packed:
1851 break;
Mike Stump11289f42009-09-09 15:08:12 +00001852
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001853 case Attr::Pure:
1854 break;
1855
1856 case Attr::Regparm:
1857 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1858 break;
Mike Stump11289f42009-09-09 15:08:12 +00001859
Nate Begemanf2758702009-06-26 06:32:41 +00001860 case Attr::ReqdWorkGroupSize:
1861 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1862 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1863 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1864 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001865
1866 case Attr::Section:
1867 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1868 break;
1869
1870 case Attr::StdCall:
1871 case Attr::TransparentUnion:
1872 case Attr::Unavailable:
1873 case Attr::Unused:
1874 case Attr::Used:
1875 break;
1876
1877 case Attr::Visibility:
1878 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001879 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001880 break;
1881
1882 case Attr::WarnUnusedResult:
1883 case Attr::Weak:
1884 case Attr::WeakImport:
1885 break;
1886 }
1887 }
1888
Douglas Gregor8f45df52009-04-16 22:23:12 +00001889 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001890}
1891
1892void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1893 Record.push_back(Str.size());
1894 Record.insert(Record.end(), Str.begin(), Str.end());
1895}
1896
Douglas Gregore84a9da2009-04-20 20:36:09 +00001897/// \brief Note that the identifier II occurs at the given offset
1898/// within the identifier table.
1899void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001900 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001901}
1902
Douglas Gregor95c13f52009-04-25 17:48:32 +00001903/// \brief Note that the selector Sel occurs at the given offset
1904/// within the method pool/selector table.
1905void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1906 unsigned ID = SelectorIDs[Sel];
1907 assert(ID && "Unknown selector");
1908 SelectorOffsets[ID - 1] = Offset;
1909}
1910
Mike Stump11289f42009-09-09 15:08:12 +00001911PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1912 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001913 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1914 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001915
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001916void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1917 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001918 using namespace llvm;
1919
Douglas Gregor162dd022009-04-20 15:53:59 +00001920 ASTContext &Context = SemaRef.Context;
1921 Preprocessor &PP = SemaRef.PP;
1922
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001923 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001924 Stream.Emit((unsigned)'C', 8);
1925 Stream.Emit((unsigned)'P', 8);
1926 Stream.Emit((unsigned)'C', 8);
1927 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001928
Chris Lattner28fa4e62009-04-26 22:26:21 +00001929 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001930
1931 // The translation unit is the first declaration we'll emit.
1932 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001933 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001934
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001935 // Make sure that we emit IdentifierInfos (and any attached
1936 // declarations) for builtins.
1937 {
1938 IdentifierTable &Table = PP.getIdentifierTable();
1939 llvm::SmallVector<const char *, 32> BuiltinNames;
1940 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1941 Context.getLangOptions().NoBuiltin);
1942 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1943 getIdentifierRef(&Table.get(BuiltinNames[I]));
1944 }
1945
Chris Lattner0c797362009-09-08 18:19:27 +00001946 // Build a record containing all of the tentative definitions in this file, in
1947 // TentativeDefinitionList order. Generally, this record will be empty for
1948 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001949 RecordData TentativeDefinitions;
Chris Lattner0c797362009-09-08 18:19:27 +00001950 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1951 VarDecl *VD =
1952 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1953 if (VD) AddDeclRef(VD, TentativeDefinitions);
1954 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001955
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001956 // Build a record containing all of the locally-scoped external
1957 // declarations in this header file. Generally, this record will be
1958 // empty.
1959 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001960 // FIXME: This is filling in the PCH file in densemap order which is
1961 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001962 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001963 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1964 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1965 TD != TDEnd; ++TD)
1966 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1967
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001968 // Build a record containing all of the ext_vector declarations.
1969 RecordData ExtVectorDecls;
1970 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1971 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1972
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001973 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001974 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001975 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001976 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001977 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001978 if (StatCalls && !isysroot)
1979 WriteStatCache(*StatCalls, isysroot);
1980 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00001981 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001982 // Write the record of special types.
1983 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001984
Steve Naroffc277ad12009-07-18 15:33:26 +00001985 AddTypeRef(Context.getBuiltinVaListType(), Record);
1986 AddTypeRef(Context.getObjCIdType(), Record);
1987 AddTypeRef(Context.getObjCSelType(), Record);
1988 AddTypeRef(Context.getObjCProtoType(), Record);
1989 AddTypeRef(Context.getObjCClassType(), Record);
1990 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1991 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1992 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00001993 AddTypeRef(Context.getjmp_bufType(), Record);
1994 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001995 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1996 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00001997#if 0
1998 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian04b258c2009-11-25 23:07:42 +00001999 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002000#endif
Mike Stumpd0153282009-10-20 02:12:22 +00002001 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002002 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002003 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002004
Douglas Gregor1970d882009-04-26 03:49:13 +00002005 // Keep writing types and declarations until all types and
2006 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002007 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2008 WriteDeclsBlockAbbrevs();
2009 while (!DeclTypesToEmit.empty()) {
2010 DeclOrType DOT = DeclTypesToEmit.front();
2011 DeclTypesToEmit.pop();
2012 if (DOT.isType())
2013 WriteType(DOT.getType());
2014 else
2015 WriteDecl(Context, DOT.getDecl());
2016 }
2017 Stream.ExitBlock();
2018
Douglas Gregor45053152009-10-17 17:25:45 +00002019 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002020 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002021 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002022
2023 // Write the type offsets array
2024 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2025 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2028 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2029 Record.clear();
2030 Record.push_back(pch::TYPE_OFFSET);
2031 Record.push_back(TypeOffsets.size());
2032 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002033 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002034 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002035
Douglas Gregor745ed142009-04-25 18:35:21 +00002036 // Write the declaration offsets array
2037 Abbrev = new BitCodeAbbrev();
2038 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2041 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2042 Record.clear();
2043 Record.push_back(pch::DECL_OFFSET);
2044 Record.push_back(DeclOffsets.size());
2045 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002046 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002047 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002048
Douglas Gregord4df8652009-04-22 22:02:47 +00002049 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002050 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002051 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002052
2053 // Write the record containing tentative definitions.
2054 if (!TentativeDefinitions.empty())
2055 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002056
2057 // Write the record containing locally-scoped external definitions.
2058 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002059 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002060 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002061
2062 // Write the record containing ext_vector type names.
2063 if (!ExtVectorDecls.empty())
2064 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002065
Douglas Gregor08f01292009-04-17 22:13:46 +00002066 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002067 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002068 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002069 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002070 Record.push_back(NumLexicalDeclContexts);
2071 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002072 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002073 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002074}
2075
2076void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2077 Record.push_back(Loc.getRawEncoding());
2078}
2079
2080void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2081 Record.push_back(Value.getBitWidth());
2082 unsigned N = Value.getNumWords();
2083 const uint64_t* Words = Value.getRawData();
2084 for (unsigned I = 0; I != N; ++I)
2085 Record.push_back(Words[I]);
2086}
2087
Douglas Gregor1daeb692009-04-13 18:14:40 +00002088void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2089 Record.push_back(Value.isUnsigned());
2090 AddAPInt(Value, Record);
2091}
2092
Douglas Gregore0a3a512009-04-14 21:55:33 +00002093void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2094 AddAPInt(Value.bitcastToAPInt(), Record);
2095}
2096
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002097void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002098 Record.push_back(getIdentifierRef(II));
2099}
2100
2101pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2102 if (II == 0)
2103 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002104
2105 pch::IdentID &ID = IdentifierIDs[II];
2106 if (ID == 0)
2107 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002108 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002109}
2110
Steve Naroff2ddea052009-04-23 10:39:46 +00002111void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2112 if (SelRef.getAsOpaquePtr() == 0) {
2113 Record.push_back(0);
2114 return;
2115 }
2116
2117 pch::SelectorID &SID = SelectorIDs[SelRef];
2118 if (SID == 0) {
2119 SID = SelectorIDs.size();
2120 SelVector.push_back(SelRef);
2121 }
2122 Record.push_back(SID);
2123}
2124
John McCall0ad16662009-10-29 08:12:44 +00002125void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2126 RecordData &Record) {
2127 switch (Arg.getArgument().getKind()) {
2128 case TemplateArgument::Expression:
2129 AddStmt(Arg.getLocInfo().getAsExpr());
2130 break;
2131 case TemplateArgument::Type:
2132 AddDeclaratorInfo(Arg.getLocInfo().getAsDeclaratorInfo(), Record);
2133 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002134 case TemplateArgument::Template:
2135 Record.push_back(
2136 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2137 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2138 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2139 break;
John McCall0ad16662009-10-29 08:12:44 +00002140 case TemplateArgument::Null:
2141 case TemplateArgument::Integral:
2142 case TemplateArgument::Declaration:
2143 case TemplateArgument::Pack:
2144 break;
2145 }
2146}
2147
John McCall8f115c62009-10-16 21:56:05 +00002148void PCHWriter::AddDeclaratorInfo(DeclaratorInfo *DInfo, RecordData &Record) {
2149 if (DInfo == 0) {
2150 AddTypeRef(QualType(), Record);
2151 return;
2152 }
2153
John McCall17001972009-10-18 01:05:36 +00002154 AddTypeRef(DInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002155 TypeLocWriter TLW(*this, Record);
2156 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2157 TLW.Visit(TL);
2158}
2159
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002160void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2161 if (T.isNull()) {
2162 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2163 return;
2164 }
2165
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002166 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002167 T.removeFastQualifiers();
2168
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002169 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002170 pch::TypeID &ID = TypeIDs[T];
2171 if (ID == 0) {
2172 // We haven't seen these qualifiers applied to this type before.
2173 // Assign it a new ID. This is the only time we enqueue a
2174 // qualified type, and it has no CV qualifiers.
2175 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002176 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002177 }
2178
2179 // Encode the type qualifiers in the type reference.
2180 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2181 return;
2182 }
2183
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002184 assert(!T.hasLocalQualifiers());
John McCall8ccfcb52009-09-24 19:53:00 +00002185
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002186 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002187 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002188 switch (BT->getKind()) {
2189 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2190 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2191 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2192 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2193 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2194 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2195 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2196 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002197 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002198 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2199 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2200 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2201 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2202 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2203 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2204 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002205 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002206 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2207 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2208 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002209 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002210 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2211 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002212 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2213 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002214 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2215 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002216 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002217 case BuiltinType::UndeducedAuto:
2218 assert(0 && "Should not see undeduced auto here");
2219 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002220 }
2221
John McCall8ccfcb52009-09-24 19:53:00 +00002222 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002223 return;
2224 }
2225
John McCall8ccfcb52009-09-24 19:53:00 +00002226 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002227 if (ID == 0) {
2228 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002229 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002230 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002231 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002232 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002233
2234 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002235 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002236}
2237
2238void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2239 if (D == 0) {
2240 Record.push_back(0);
2241 return;
2242 }
2243
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002244 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002245 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002246 // We haven't seen this declaration before. Give it a new ID and
2247 // enqueue it in the list of declarations to emit.
2248 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002249 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002250 }
2251
2252 Record.push_back(ID);
2253}
2254
Douglas Gregore84a9da2009-04-20 20:36:09 +00002255pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2256 if (D == 0)
2257 return 0;
2258
2259 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2260 return DeclIDs[D];
2261}
2262
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002263void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002264 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002265 Record.push_back(Name.getNameKind());
2266 switch (Name.getNameKind()) {
2267 case DeclarationName::Identifier:
2268 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2269 break;
2270
2271 case DeclarationName::ObjCZeroArgSelector:
2272 case DeclarationName::ObjCOneArgSelector:
2273 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002274 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002275 break;
2276
2277 case DeclarationName::CXXConstructorName:
2278 case DeclarationName::CXXDestructorName:
2279 case DeclarationName::CXXConversionFunctionName:
2280 AddTypeRef(Name.getCXXNameType(), Record);
2281 break;
2282
2283 case DeclarationName::CXXOperatorName:
2284 Record.push_back(Name.getCXXOverloadedOperator());
2285 break;
2286
2287 case DeclarationName::CXXUsingDirective:
2288 // No extra data to emit
2289 break;
2290 }
2291}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002292