blob: 84a8a450b0c238bb514b5d1745072ae95c1717db [file] [log] [blame]
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000031#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000037#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000038#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42// Type serialization
43//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000044
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000046 class PCHTypeWriter {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047 PCHWriter &Writer;
48 PCHWriter::RecordData &Record;
49
50 public:
51 /// \brief Type code that corresponds to the record generated.
52 pch::TypeCode Code;
53
Mike Stump11289f42009-09-09 15:08:12 +000054 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000055 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
57 void VisitArrayType(const ArrayType *T);
58 void VisitFunctionType(const FunctionType *T);
59 void VisitTagType(const TagType *T);
60
61#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
62#define ABSTRACT_TYPE(Class, Base)
63#define DEPENDENT_TYPE(Class, Base)
64#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
73 Writer.AddTypeRef(T->getElementType(), Record);
74 Code = pch::TYPE_COMPLEX;
75}
76
77void PCHTypeWriter::VisitPointerType(const PointerType *T) {
78 Writer.AddTypeRef(T->getPointeeType(), Record);
79 Code = pch::TYPE_POINTER;
80}
81
82void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000083 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000084 Code = pch::TYPE_BLOCK_POINTER;
85}
86
87void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
88 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_LVALUE_REFERENCE;
90}
91
92void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_RVALUE_REFERENCE;
95}
96
97void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100 Code = pch::TYPE_MEMBER_POINTER;
101}
102
103void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
104 Writer.AddTypeRef(T->getElementType(), Record);
105 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000106 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107}
108
109void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
110 VisitArrayType(T);
111 Writer.AddAPInt(T->getSize(), Record);
112 Code = pch::TYPE_CONSTANT_ARRAY;
113}
114
115void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
116 VisitArrayType(T);
117 Code = pch::TYPE_INCOMPLETE_ARRAY;
118}
119
120void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
121 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000122 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
123 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000124 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125 Code = pch::TYPE_VARIABLE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVectorType(const VectorType *T) {
129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getNumElements());
131 Code = pch::TYPE_VECTOR;
132}
133
134void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
135 VisitVectorType(T);
136 Code = pch::TYPE_EXT_VECTOR;
137}
138
139void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
140 Writer.AddTypeRef(T->getResultType(), Record);
Douglas Gregordc728752009-12-22 18:11:50 +0000141 Record.push_back(T->getNoReturnAttr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
144void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
145 VisitFunctionType(T);
146 Code = pch::TYPE_FUNCTION_NO_PROTO;
147}
148
149void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
150 VisitFunctionType(T);
151 Record.push_back(T->getNumArgs());
152 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
153 Writer.AddTypeRef(T->getArgType(I), Record);
154 Record.push_back(T->isVariadic());
155 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000156 Record.push_back(T->hasExceptionSpec());
157 Record.push_back(T->hasAnyExceptionSpec());
158 Record.push_back(T->getNumExceptions());
159 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
160 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000161 Code = pch::TYPE_FUNCTION_PROTO;
162}
163
John McCallb96ec562009-12-04 22:46:56 +0000164#if 0
165// For when we want it....
166void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
167 Writer.AddDeclRef(T->getDecl(), Record);
168 Code = pch::TYPE_UNRESOLVED_USING;
169}
170#endif
171
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000172void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
173 Writer.AddDeclRef(T->getDecl(), Record);
174 Code = pch::TYPE_TYPEDEF;
175}
176
177void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000178 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179 Code = pch::TYPE_TYPEOF_EXPR;
180}
181
182void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
183 Writer.AddTypeRef(T->getUnderlyingType(), Record);
184 Code = pch::TYPE_TYPEOF;
185}
186
Anders Carlsson81df7b82009-06-24 19:06:50 +0000187void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
188 Writer.AddStmt(T->getUnderlyingExpr());
189 Code = pch::TYPE_DECLTYPE;
190}
191
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192void PCHTypeWriter::VisitTagType(const TagType *T) {
193 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000194 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195 "Cannot serialize in the middle of a type definition");
196}
197
198void PCHTypeWriter::VisitRecordType(const RecordType *T) {
199 VisitTagType(T);
200 Code = pch::TYPE_RECORD;
201}
202
203void PCHTypeWriter::VisitEnumType(const EnumType *T) {
204 VisitTagType(T);
205 Code = pch::TYPE_ENUM;
206}
207
John McCallfcc33b02009-09-05 00:15:47 +0000208void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
209 Writer.AddTypeRef(T->getUnderlyingType(), Record);
210 Record.push_back(T->getTagKind());
211 Code = pch::TYPE_ELABORATED;
212}
213
Mike Stump11289f42009-09-09 15:08:12 +0000214void
John McCallcebee162009-10-18 09:09:24 +0000215PCHTypeWriter::VisitSubstTemplateTypeParmType(
216 const SubstTemplateTypeParmType *T) {
217 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
218 Writer.AddTypeRef(T->getReplacementType(), Record);
219 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
220}
221
222void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223PCHTypeWriter::VisitTemplateSpecializationType(
224 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000225 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226 assert(false && "Cannot serialize template specialization types");
227}
228
229void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000230 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000231 assert(false && "Cannot serialize qualified name types");
232}
233
234void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
235 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000237 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
238 E = T->qual_end(); I != E; ++I)
239 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000240 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241}
242
Steve Narofffb4330f2009-06-17 22:40:22 +0000243void
244PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000245 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000247 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000248 E = T->qual_end(); I != E; ++I)
249 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000250 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251}
252
John McCall8f115c62009-10-16 21:56:05 +0000253namespace {
254
255class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
256 PCHWriter &Writer;
257 PCHWriter::RecordData &Record;
258
259public:
260 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
261 : Writer(Writer), Record(Record) { }
262
John McCall17001972009-10-18 01:05:36 +0000263#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000264#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000265 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000266#include "clang/AST/TypeLocNodes.def"
267
John McCall17001972009-10-18 01:05:36 +0000268 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
269 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000270};
271
272}
273
John McCall17001972009-10-18 01:05:36 +0000274void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
275 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000276}
John McCall17001972009-10-18 01:05:36 +0000277void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc 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}
John McCallb96ec562009-12-04 22:46:56 +0000340void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
341 Writer.AddSourceLocation(TL.getNameLoc(), Record);
342}
John McCall17001972009-10-18 01:05:36 +0000343void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
344 Writer.AddSourceLocation(TL.getNameLoc(), Record);
345}
346void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
347 Writer.AddSourceLocation(TL.getNameLoc(), Record);
348}
349void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
350 Writer.AddSourceLocation(TL.getNameLoc(), Record);
351}
352void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
353 Writer.AddSourceLocation(TL.getNameLoc(), Record);
354}
355void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
356 Writer.AddSourceLocation(TL.getNameLoc(), Record);
357}
358void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
359 Writer.AddSourceLocation(TL.getNameLoc(), Record);
360}
361void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
362 Writer.AddSourceLocation(TL.getNameLoc(), Record);
363}
364void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
365 Writer.AddSourceLocation(TL.getNameLoc(), Record);
366}
John McCallcebee162009-10-18 09:09:24 +0000367void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
368 SubstTemplateTypeParmTypeLoc TL) {
369 Writer.AddSourceLocation(TL.getNameLoc(), Record);
370}
John McCall17001972009-10-18 01:05:36 +0000371void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
372 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000373 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
374 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
375 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
376 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
377 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall17001972009-10-18 01:05:36 +0000378}
379void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
382void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getNameLoc(), Record);
384}
385void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000387 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
388 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
389 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
390 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000391}
John McCallfc93cf92009-10-22 22:37:11 +0000392void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
393 Writer.AddSourceLocation(TL.getStarLoc(), Record);
394 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
395 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
396 Record.push_back(TL.hasBaseTypeAsWritten());
397 Record.push_back(TL.hasProtocolsAsWritten());
398 if (TL.hasProtocolsAsWritten())
399 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
400 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
401}
John McCall8f115c62009-10-16 21:56:05 +0000402
Chris Lattner19cea4e2009-04-22 05:57:30 +0000403//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000404// PCHWriter Implementation
405//===----------------------------------------------------------------------===//
406
Chris Lattner28fa4e62009-04-26 22:26:21 +0000407static void EmitBlockID(unsigned ID, const char *Name,
408 llvm::BitstreamWriter &Stream,
409 PCHWriter::RecordData &Record) {
410 Record.clear();
411 Record.push_back(ID);
412 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
413
414 // Emit the block name if present.
415 if (Name == 0 || Name[0] == 0) return;
416 Record.clear();
417 while (*Name)
418 Record.push_back(*Name++);
419 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
420}
421
422static void EmitRecordID(unsigned ID, const char *Name,
423 llvm::BitstreamWriter &Stream,
424 PCHWriter::RecordData &Record) {
425 Record.clear();
426 Record.push_back(ID);
427 while (*Name)
428 Record.push_back(*Name++);
429 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000430}
431
432static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
433 PCHWriter::RecordData &Record) {
434#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
435 RECORD(STMT_STOP);
436 RECORD(STMT_NULL_PTR);
437 RECORD(STMT_NULL);
438 RECORD(STMT_COMPOUND);
439 RECORD(STMT_CASE);
440 RECORD(STMT_DEFAULT);
441 RECORD(STMT_LABEL);
442 RECORD(STMT_IF);
443 RECORD(STMT_SWITCH);
444 RECORD(STMT_WHILE);
445 RECORD(STMT_DO);
446 RECORD(STMT_FOR);
447 RECORD(STMT_GOTO);
448 RECORD(STMT_INDIRECT_GOTO);
449 RECORD(STMT_CONTINUE);
450 RECORD(STMT_BREAK);
451 RECORD(STMT_RETURN);
452 RECORD(STMT_DECL);
453 RECORD(STMT_ASM);
454 RECORD(EXPR_PREDEFINED);
455 RECORD(EXPR_DECL_REF);
456 RECORD(EXPR_INTEGER_LITERAL);
457 RECORD(EXPR_FLOATING_LITERAL);
458 RECORD(EXPR_IMAGINARY_LITERAL);
459 RECORD(EXPR_STRING_LITERAL);
460 RECORD(EXPR_CHARACTER_LITERAL);
461 RECORD(EXPR_PAREN);
462 RECORD(EXPR_UNARY_OPERATOR);
463 RECORD(EXPR_SIZEOF_ALIGN_OF);
464 RECORD(EXPR_ARRAY_SUBSCRIPT);
465 RECORD(EXPR_CALL);
466 RECORD(EXPR_MEMBER);
467 RECORD(EXPR_BINARY_OPERATOR);
468 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
469 RECORD(EXPR_CONDITIONAL_OPERATOR);
470 RECORD(EXPR_IMPLICIT_CAST);
471 RECORD(EXPR_CSTYLE_CAST);
472 RECORD(EXPR_COMPOUND_LITERAL);
473 RECORD(EXPR_EXT_VECTOR_ELEMENT);
474 RECORD(EXPR_INIT_LIST);
475 RECORD(EXPR_DESIGNATED_INIT);
476 RECORD(EXPR_IMPLICIT_VALUE_INIT);
477 RECORD(EXPR_VA_ARG);
478 RECORD(EXPR_ADDR_LABEL);
479 RECORD(EXPR_STMT);
480 RECORD(EXPR_TYPES_COMPATIBLE);
481 RECORD(EXPR_CHOOSE);
482 RECORD(EXPR_GNU_NULL);
483 RECORD(EXPR_SHUFFLE_VECTOR);
484 RECORD(EXPR_BLOCK);
485 RECORD(EXPR_BLOCK_DECL_REF);
486 RECORD(EXPR_OBJC_STRING_LITERAL);
487 RECORD(EXPR_OBJC_ENCODE);
488 RECORD(EXPR_OBJC_SELECTOR_EXPR);
489 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
490 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
491 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
492 RECORD(EXPR_OBJC_KVC_REF_EXPR);
493 RECORD(EXPR_OBJC_MESSAGE_EXPR);
494 RECORD(EXPR_OBJC_SUPER_EXPR);
495 RECORD(STMT_OBJC_FOR_COLLECTION);
496 RECORD(STMT_OBJC_CATCH);
497 RECORD(STMT_OBJC_FINALLY);
498 RECORD(STMT_OBJC_AT_TRY);
499 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
500 RECORD(STMT_OBJC_AT_THROW);
501#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000502}
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattner28fa4e62009-04-26 22:26:21 +0000504void PCHWriter::WriteBlockInfoBlock() {
505 RecordData Record;
506 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner64031982009-04-27 00:40:25 +0000508#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000509#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000510
Chris Lattner28fa4e62009-04-26 22:26:21 +0000511 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000512 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000513 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000514 RECORD(TYPE_OFFSET);
515 RECORD(DECL_OFFSET);
516 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000517 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000518 RECORD(IDENTIFIER_OFFSET);
519 RECORD(IDENTIFIER_TABLE);
520 RECORD(EXTERNAL_DEFINITIONS);
521 RECORD(SPECIAL_TYPES);
522 RECORD(STATISTICS);
523 RECORD(TENTATIVE_DEFINITIONS);
524 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
525 RECORD(SELECTOR_OFFSETS);
526 RECORD(METHOD_POOL);
527 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000528 RECORD(SOURCE_LOCATION_OFFSETS);
529 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000530 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000531 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000532 RECORD(COMMENT_RANGES);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000533 RECORD(SVN_BRANCH_REVISION);
534
Chris Lattner28fa4e62009-04-26 22:26:21 +0000535 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000536 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000537 RECORD(SM_SLOC_FILE_ENTRY);
538 RECORD(SM_SLOC_BUFFER_ENTRY);
539 RECORD(SM_SLOC_BUFFER_BLOB);
540 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
541 RECORD(SM_LINE_TABLE);
542 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000543
Chris Lattner28fa4e62009-04-26 22:26:21 +0000544 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000545 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000546 RECORD(PP_MACRO_OBJECT_LIKE);
547 RECORD(PP_MACRO_FUNCTION_LIKE);
548 RECORD(PP_TOKEN);
549
Douglas Gregor12bfa382009-10-17 00:13:19 +0000550 // Decls and Types block.
551 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000552 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000553 RECORD(TYPE_COMPLEX);
554 RECORD(TYPE_POINTER);
555 RECORD(TYPE_BLOCK_POINTER);
556 RECORD(TYPE_LVALUE_REFERENCE);
557 RECORD(TYPE_RVALUE_REFERENCE);
558 RECORD(TYPE_MEMBER_POINTER);
559 RECORD(TYPE_CONSTANT_ARRAY);
560 RECORD(TYPE_INCOMPLETE_ARRAY);
561 RECORD(TYPE_VARIABLE_ARRAY);
562 RECORD(TYPE_VECTOR);
563 RECORD(TYPE_EXT_VECTOR);
564 RECORD(TYPE_FUNCTION_PROTO);
565 RECORD(TYPE_FUNCTION_NO_PROTO);
566 RECORD(TYPE_TYPEDEF);
567 RECORD(TYPE_TYPEOF_EXPR);
568 RECORD(TYPE_TYPEOF);
569 RECORD(TYPE_RECORD);
570 RECORD(TYPE_ENUM);
571 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000572 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000573 RECORD(DECL_ATTR);
574 RECORD(DECL_TRANSLATION_UNIT);
575 RECORD(DECL_TYPEDEF);
576 RECORD(DECL_ENUM);
577 RECORD(DECL_RECORD);
578 RECORD(DECL_ENUM_CONSTANT);
579 RECORD(DECL_FUNCTION);
580 RECORD(DECL_OBJC_METHOD);
581 RECORD(DECL_OBJC_INTERFACE);
582 RECORD(DECL_OBJC_PROTOCOL);
583 RECORD(DECL_OBJC_IVAR);
584 RECORD(DECL_OBJC_AT_DEFS_FIELD);
585 RECORD(DECL_OBJC_CLASS);
586 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
587 RECORD(DECL_OBJC_CATEGORY);
588 RECORD(DECL_OBJC_CATEGORY_IMPL);
589 RECORD(DECL_OBJC_IMPLEMENTATION);
590 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
591 RECORD(DECL_OBJC_PROPERTY);
592 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000593 RECORD(DECL_FIELD);
594 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000595 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000596 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000597 RECORD(DECL_FILE_SCOPE_ASM);
598 RECORD(DECL_BLOCK);
599 RECORD(DECL_CONTEXT_LEXICAL);
600 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000601 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000602 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000603#undef RECORD
604#undef BLOCK
605 Stream.ExitBlock();
606}
607
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000608/// \brief Adjusts the given filename to only write out the portion of the
609/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000610///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000611/// \param Filename the file name to adjust.
612///
613/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
614/// the returned filename will be adjusted by this system root.
615///
616/// \returns either the original filename (if it needs no adjustment) or the
617/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000618static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000619adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
620 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000621
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000622 if (!isysroot)
623 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000624
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000625 // Verify that the filename and the system root have the same prefix.
626 unsigned Pos = 0;
627 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
628 if (Filename[Pos] != isysroot[Pos])
629 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000630
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000631 // We hit the end of the filename before we hit the end of the system root.
632 if (!Filename[Pos])
633 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000634
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000635 // If the file name has a '/' at the current position, skip over the '/'.
636 // We distinguish sysroot-based includes from absolute includes by the
637 // absence of '/' at the beginning of sysroot-based includes.
638 if (Filename[Pos] == '/')
639 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000640
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000641 return Filename + Pos;
642}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000643
Douglas Gregor7b71e632009-04-27 22:23:34 +0000644/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000645void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000646 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000647
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000648 // Metadata
649 const TargetInfo &Target = Context.Target;
650 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
651 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
652 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
653 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
654 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
655 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
656 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
657 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
658 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000659
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000660 RecordData Record;
661 Record.push_back(pch::METADATA);
662 Record.push_back(pch::VERSION_MAJOR);
663 Record.push_back(pch::VERSION_MINOR);
664 Record.push_back(CLANG_VERSION_MAJOR);
665 Record.push_back(CLANG_VERSION_MINOR);
666 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000667 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000668 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000669
Douglas Gregor45fe0362009-05-12 01:31:05 +0000670 // Original file name
671 SourceManager &SM = Context.getSourceManager();
672 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
673 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
674 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
675 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
676 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
677
678 llvm::sys::Path MainFilePath(MainFile->getName());
679 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000680
Douglas Gregor45fe0362009-05-12 01:31:05 +0000681 if (!MainFilePath.isAbsolute()) {
682 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000683 P.appendComponent(MainFilePath.str());
684 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000685 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000686 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000687 }
688
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000689 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000690 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000691 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000692 RecordData Record;
693 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000694 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000695 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000696
697 // Subversion branch/version information.
698 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
699 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
700 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
701 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
702 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
703 Record.clear();
704 Record.push_back(pch::SVN_BRANCH_REVISION);
705 Record.push_back(getClangSubversionRevision());
706 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000707}
708
709/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000710void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
711 RecordData Record;
712 Record.push_back(LangOpts.Trigraphs);
713 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
714 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
715 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
716 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
717 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
718 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
719 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
720 Record.push_back(LangOpts.C99); // C99 Support
721 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
722 Record.push_back(LangOpts.CPlusPlus); // C++ Support
723 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000724 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000725
Douglas Gregor55abb232009-04-10 20:39:37 +0000726 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
727 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
728 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000729
Douglas Gregor55abb232009-04-10 20:39:37 +0000730 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000731 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
732 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000733 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000734 Record.push_back(LangOpts.Exceptions); // Support exception handling.
735
736 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
737 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
738 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
739
Chris Lattner258172e2009-04-27 07:35:58 +0000740 // Whether static initializers are protected by locks.
741 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000742 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000743 Record.push_back(LangOpts.Blocks); // block extension to C
744 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
745 // they are unused.
746 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
747 // (modulo the platform support).
748
749 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
750 // signed integer arithmetic overflows.
751
752 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
753 // may be ripped out at any time.
754
755 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000756 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000757 // defined.
758 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
759 // opposed to __DYNAMIC__).
760 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
761
762 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
763 // used (instead of C99 semantics).
764 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000765 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
766 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000767 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
768 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000769 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000770 Record.push_back(LangOpts.getGCMode());
771 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000772 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000773 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000774 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000775 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000776 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000777 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000778}
779
Douglas Gregora7f71a92009-04-10 03:52:48 +0000780//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000781// stat cache Serialization
782//===----------------------------------------------------------------------===//
783
784namespace {
785// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000786class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000787public:
788 typedef const char * key_type;
789 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000790
Douglas Gregorc5046832009-04-27 18:38:38 +0000791 typedef std::pair<int, struct stat> data_type;
792 typedef const data_type& data_type_ref;
793
794 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000795 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000796 }
Mike Stump11289f42009-09-09 15:08:12 +0000797
798 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000799 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
800 data_type_ref Data) {
801 unsigned StrLen = strlen(path);
802 clang::io::Emit16(Out, StrLen);
803 unsigned DataLen = 1; // result value
804 if (Data.first == 0)
805 DataLen += 4 + 4 + 2 + 8 + 8;
806 clang::io::Emit8(Out, DataLen);
807 return std::make_pair(StrLen + 1, DataLen);
808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
Douglas Gregorc5046832009-04-27 18:38:38 +0000810 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
811 Out.write(path, KeyLen);
812 }
Mike Stump11289f42009-09-09 15:08:12 +0000813
Douglas Gregorc5046832009-04-27 18:38:38 +0000814 void EmitData(llvm::raw_ostream& Out, key_type_ref,
815 data_type_ref Data, unsigned DataLen) {
816 using namespace clang::io;
817 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000818
Douglas Gregorc5046832009-04-27 18:38:38 +0000819 // Result of stat()
820 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000821
Douglas Gregorc5046832009-04-27 18:38:38 +0000822 if (Data.first == 0) {
823 Emit32(Out, (uint32_t) Data.second.st_ino);
824 Emit32(Out, (uint32_t) Data.second.st_dev);
825 Emit16(Out, (uint16_t) Data.second.st_mode);
826 Emit64(Out, (uint64_t) Data.second.st_mtime);
827 Emit64(Out, (uint64_t) Data.second.st_size);
828 }
829
830 assert(Out.tell() - Start == DataLen && "Wrong data length");
831 }
832};
833} // end anonymous namespace
834
835/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000836void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
837 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000838 // Build the on-disk hash table containing information about every
839 // stat() call.
840 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
841 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000842 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000843 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000844 Stat != StatEnd; ++Stat, ++NumStatEntries) {
845 const char *Filename = Stat->first();
846 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
847 Generator.insert(Filename, Stat->second);
848 }
Mike Stump11289f42009-09-09 15:08:12 +0000849
Douglas Gregorc5046832009-04-27 18:38:38 +0000850 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000851 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000852 uint32_t BucketOffset;
853 {
854 llvm::raw_svector_ostream Out(StatCacheData);
855 // Make sure that no bucket is at offset 0
856 clang::io::Emit32(Out, 0);
857 BucketOffset = Generator.Emit(Out);
858 }
859
860 // Create a blob abbreviation
861 using namespace llvm;
862 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
863 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
867 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
868
869 // Write the stat cache
870 RecordData Record;
871 Record.push_back(pch::STAT_CACHE);
872 Record.push_back(BucketOffset);
873 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000874 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000875}
876
877//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000878// Source Manager Serialization
879//===----------------------------------------------------------------------===//
880
881/// \brief Create an abbreviation for the SLocEntry that refers to a
882/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000883static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000884 using namespace llvm;
885 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
886 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
889 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000891 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000892 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000893}
894
895/// \brief Create an abbreviation for the SLocEntry that refers to a
896/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000897static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000898 using namespace llvm;
899 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
900 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000906 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000907}
908
909/// \brief Create an abbreviation for the SLocEntry that refers to a
910/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000911static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000912 using namespace llvm;
913 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
914 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000916 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000917}
918
919/// \brief Create an abbreviation for the SLocEntry that refers to an
920/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000921static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000922 using namespace llvm;
923 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
924 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000930 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000931}
932
933/// \brief Writes the block containing the serialized form of the
934/// source manager.
935///
936/// TODO: We should probably use an on-disk hash table (stored in a
937/// blob), indexed based on the file name, so that we only create
938/// entries for files that we actually need. In the common case (no
939/// errors), we probably won't have to create file entries for any of
940/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000941void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000942 const Preprocessor &PP,
943 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000944 RecordData Record;
945
Chris Lattner0910e3b2009-04-10 17:16:57 +0000946 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000947 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000948
949 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000950 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
951 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
952 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
953 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000954
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000955 // Write the line table.
956 if (SourceMgr.hasLineTable()) {
957 LineTableInfo &LineTable = SourceMgr.getLineTable();
958
959 // Emit the file names
960 Record.push_back(LineTable.getNumFilenames());
961 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
962 // Emit the file name
963 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000964 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000965 unsigned FilenameLen = Filename? strlen(Filename) : 0;
966 Record.push_back(FilenameLen);
967 if (FilenameLen)
968 Record.insert(Record.end(), Filename, Filename + FilenameLen);
969 }
Mike Stump11289f42009-09-09 15:08:12 +0000970
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000971 // Emit the line entries
972 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
973 L != LEnd; ++L) {
974 // Emit the file ID
975 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000976
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000977 // Emit the line entries
978 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000979 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000980 LEEnd = L->second.end();
981 LE != LEEnd; ++LE) {
982 Record.push_back(LE->FileOffset);
983 Record.push_back(LE->LineNo);
984 Record.push_back(LE->FilenameID);
985 Record.push_back((unsigned)LE->FileKind);
986 Record.push_back(LE->IncludeOffset);
987 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000988 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +0000989 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000990 }
991
Douglas Gregor258ae542009-04-27 06:38:32 +0000992 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +0000993 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +0000994 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000995 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +0000996 E = HS.header_file_end();
997 I != E; ++I) {
998 Record.push_back(I->isImport);
999 Record.push_back(I->DirInfo);
1000 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +00001001 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001002 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1003 Record.clear();
1004 }
1005
Douglas Gregor258ae542009-04-27 06:38:32 +00001006 // Write out the source location entry table. We skip the first
1007 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001008 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001009 RecordData PreloadSLocs;
1010 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001011 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1012 // Get this source location entry.
1013 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1014
Douglas Gregor258ae542009-04-27 06:38:32 +00001015 // Record the offset of this source-location entry.
1016 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1017
1018 // Figure out which record code to use.
1019 unsigned Code;
1020 if (SLoc->isFile()) {
1021 if (SLoc->getFile().getContentCache()->Entry)
1022 Code = pch::SM_SLOC_FILE_ENTRY;
1023 else
1024 Code = pch::SM_SLOC_BUFFER_ENTRY;
1025 } else
1026 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1027 Record.clear();
1028 Record.push_back(Code);
1029
1030 Record.push_back(SLoc->getOffset());
1031 if (SLoc->isFile()) {
1032 const SrcMgr::FileInfo &File = SLoc->getFile();
1033 Record.push_back(File.getIncludeLoc().getRawEncoding());
1034 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1035 Record.push_back(File.hasLineDirectives());
1036
1037 const SrcMgr::ContentCache *Content = File.getContentCache();
1038 if (Content->Entry) {
1039 // The source location entry is a file. The blob associated
1040 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001041
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001042 // Turn the file name into an absolute path, if it isn't already.
1043 const char *Filename = Content->Entry->getName();
1044 llvm::sys::Path FilePath(Filename, strlen(Filename));
1045 std::string FilenameStr;
1046 if (!FilePath.isAbsolute()) {
1047 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001048 P.appendComponent(FilePath.str());
1049 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001050 Filename = FilenameStr.c_str();
1051 }
Mike Stump11289f42009-09-09 15:08:12 +00001052
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001053 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001054 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001055
1056 // FIXME: For now, preload all file source locations, so that
1057 // we get the appropriate File entries in the reader. This is
1058 // a temporary measure.
1059 PreloadSLocs.push_back(SLocEntryOffsets.size());
1060 } else {
1061 // The source location entry is a buffer. The blob associated
1062 // with this entry contains the contents of the buffer.
1063
1064 // We add one to the size so that we capture the trailing NULL
1065 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1066 // the reader side).
1067 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1068 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001069 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1070 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001071 Record.clear();
1072 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1073 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001074 llvm::StringRef(Buffer->getBufferStart(),
1075 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001076
1077 if (strcmp(Name, "<built-in>") == 0)
1078 PreloadSLocs.push_back(SLocEntryOffsets.size());
1079 }
1080 } else {
1081 // The source location entry is an instantiation.
1082 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1083 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1084 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1085 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1086
1087 // Compute the token length for this macro expansion.
1088 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001089 if (I + 1 != N)
1090 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001091 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1092 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1093 }
1094 }
1095
Douglas Gregor8f45df52009-04-16 22:23:12 +00001096 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001097
1098 if (SLocEntryOffsets.empty())
1099 return;
1100
1101 // Write the source-location offsets table into the PCH block. This
1102 // table is used for lazily loading source-location information.
1103 using namespace llvm;
1104 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1105 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1107 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1108 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1109 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001110
Douglas Gregor258ae542009-04-27 06:38:32 +00001111 Record.clear();
1112 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1113 Record.push_back(SLocEntryOffsets.size());
1114 Record.push_back(SourceMgr.getNextOffset());
1115 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001116 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001117 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001118
1119 // Write the source location entry preloads array, telling the PCH
1120 // reader which source locations entries it should load eagerly.
1121 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001122}
1123
Douglas Gregorc5046832009-04-27 18:38:38 +00001124//===----------------------------------------------------------------------===//
1125// Preprocessor Serialization
1126//===----------------------------------------------------------------------===//
1127
Chris Lattnereeffaef2009-04-10 17:15:23 +00001128/// \brief Writes the block containing the serialized form of the
1129/// preprocessor.
1130///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001131void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001132 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001133
Chris Lattner0af3ba12009-04-13 01:29:17 +00001134 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1135 if (PP.getCounterValue() != 0) {
1136 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001137 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001138 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001139 }
1140
1141 // Enter the preprocessor block.
1142 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001143
Douglas Gregoreda6a892009-04-26 00:07:37 +00001144 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1145 // FIXME: use diagnostics subsystem for localization etc.
1146 if (PP.SawDateOrTime())
1147 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001148
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001149 // Loop over all the macro definitions that are live at the end of the file,
1150 // emitting each to the PP section.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001151 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1152 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001153 // FIXME: This emits macros in hash table order, we should do it in a stable
1154 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001155 MacroInfo *MI = I->second;
1156
1157 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1158 // been redefined by the header (in which case they are not isBuiltinMacro).
1159 if (MI->isBuiltinMacro())
1160 continue;
1161
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001162 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001163 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001164 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1165 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001166
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001167 unsigned Code;
1168 if (MI->isObjectLike()) {
1169 Code = pch::PP_MACRO_OBJECT_LIKE;
1170 } else {
1171 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001172
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001173 Record.push_back(MI->isC99Varargs());
1174 Record.push_back(MI->isGNUVarargs());
1175 Record.push_back(MI->getNumArgs());
1176 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1177 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001178 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001179 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001180 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001181 Record.clear();
1182
Chris Lattner2199f5b2009-04-10 18:08:30 +00001183 // Emit the tokens array.
1184 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1185 // Note that we know that the preprocessor does not have any annotation
1186 // tokens in it because they are created by the parser, and thus can't be
1187 // in a macro definition.
1188 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001189
Chris Lattner2199f5b2009-04-10 18:08:30 +00001190 Record.push_back(Tok.getLocation().getRawEncoding());
1191 Record.push_back(Tok.getLength());
1192
Chris Lattner2199f5b2009-04-10 18:08:30 +00001193 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1194 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001195 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001196
Chris Lattner2199f5b2009-04-10 18:08:30 +00001197 // FIXME: Should translate token kind to a stable encoding.
1198 Record.push_back(Tok.getKind());
1199 // FIXME: Should translate token flags to a stable encoding.
1200 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001201
Douglas Gregor8f45df52009-04-16 22:23:12 +00001202 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001203 Record.clear();
1204 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001205 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001206 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001207 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001208}
1209
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001210void PCHWriter::WriteComments(ASTContext &Context) {
1211 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001212
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001213 if (Context.Comments.empty())
1214 return;
Mike Stump11289f42009-09-09 15:08:12 +00001215
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001216 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1217 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1218 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1219 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001220
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001221 RecordData Record;
1222 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001223 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001224 (const char*)&Context.Comments[0],
1225 Context.Comments.size() * sizeof(SourceRange));
1226}
1227
Douglas Gregorc5046832009-04-27 18:38:38 +00001228//===----------------------------------------------------------------------===//
1229// Type Serialization
1230//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001231
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001232/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001233void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001234 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001235 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001236 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001237
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001238 // Record the offset for this type.
1239 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001240 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001241 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1242 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001243 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001244 }
1245
1246 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001247
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001248 // Emit the type's representation.
1249 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001250
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001251 if (T.hasLocalNonFastQualifiers()) {
1252 Qualifiers Qs = T.getLocalQualifiers();
1253 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001254 Record.push_back(Qs.getAsOpaqueValue());
1255 W.Code = pch::TYPE_EXT_QUAL;
1256 } else {
1257 switch (T->getTypeClass()) {
1258 // For all of the concrete, non-dependent types, call the
1259 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001260#define TYPE(Class, Base) \
John McCall8ccfcb52009-09-24 19:53:00 +00001261 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001262#define ABSTRACT_TYPE(Class, Base)
1263#define DEPENDENT_TYPE(Class, Base)
1264#include "clang/AST/TypeNodes.def"
1265
John McCall8ccfcb52009-09-24 19:53:00 +00001266 // For all of the dependent type nodes (which only occur in C++
1267 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001268#define TYPE(Class, Base)
1269#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1270#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001271 assert(false && "Cannot serialize dependent type nodes");
1272 break;
1273 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001274 }
1275
1276 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001277 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001278
1279 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001280 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001281}
1282
Douglas Gregorc5046832009-04-27 18:38:38 +00001283//===----------------------------------------------------------------------===//
1284// Declaration Serialization
1285//===----------------------------------------------------------------------===//
1286
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001287/// \brief Write the block containing all of the declaration IDs
1288/// lexically declared within the given DeclContext.
1289///
1290/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1291/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001292uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001293 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001294 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001295 return 0;
1296
Douglas Gregor8f45df52009-04-16 22:23:12 +00001297 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001298 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001299 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1300 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001301 AddDeclRef(*D, Record);
1302
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001303 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001304 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001305 return Offset;
1306}
1307
1308/// \brief Write the block containing all of the declaration IDs
1309/// visible from the given DeclContext.
1310///
1311/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1312/// bistream, or 0 if no block was written.
1313uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1314 DeclContext *DC) {
1315 if (DC->getPrimaryContext() != DC)
1316 return 0;
1317
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001318 // Since there is no name lookup into functions or methods, and we
1319 // perform name lookup for the translation unit via the
1320 // IdentifierInfo chains, don't bother to build a
1321 // visible-declarations table for these entities.
1322 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001323 return 0;
1324
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001325 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001326 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001327
1328 // Serialize the contents of the mapping used for lookup. Note that,
1329 // although we have two very different code paths, the serialized
1330 // representation is the same for both cases: a declaration name,
1331 // followed by a size, followed by references to the visible
1332 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001333 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001334 RecordData Record;
1335 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001336 if (!Map)
1337 return 0;
1338
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001339 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1340 D != DEnd; ++D) {
1341 AddDeclarationName(D->first, Record);
1342 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1343 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001344 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001345 AddDeclRef(*Result.first, Record);
1346 }
1347
1348 if (Record.size() == 0)
1349 return 0;
1350
Douglas Gregor8f45df52009-04-16 22:23:12 +00001351 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001352 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001353 return Offset;
1354}
1355
Douglas Gregorc5046832009-04-27 18:38:38 +00001356//===----------------------------------------------------------------------===//
1357// Global Method Pool and Selector Serialization
1358//===----------------------------------------------------------------------===//
1359
Douglas Gregore84a9da2009-04-20 20:36:09 +00001360namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001361// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001362class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001363 PCHWriter &Writer;
1364
1365public:
1366 typedef Selector key_type;
1367 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001368
Douglas Gregorc78d3462009-04-24 21:10:55 +00001369 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1370 typedef const data_type& data_type_ref;
1371
1372 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001373
Douglas Gregorc78d3462009-04-24 21:10:55 +00001374 static unsigned ComputeHash(Selector Sel) {
1375 unsigned N = Sel.getNumArgs();
1376 if (N == 0)
1377 ++N;
1378 unsigned R = 5381;
1379 for (unsigned I = 0; I != N; ++I)
1380 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001381 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001382 return R;
1383 }
Mike Stump11289f42009-09-09 15:08:12 +00001384
1385 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001386 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1387 data_type_ref Methods) {
1388 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1389 clang::io::Emit16(Out, KeyLen);
1390 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001391 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001392 Method = Method->Next)
1393 if (Method->Method)
1394 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001395 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001396 Method = Method->Next)
1397 if (Method->Method)
1398 DataLen += 4;
1399 clang::io::Emit16(Out, DataLen);
1400 return std::make_pair(KeyLen, DataLen);
1401 }
Mike Stump11289f42009-09-09 15:08:12 +00001402
Douglas Gregor95c13f52009-04-25 17:48:32 +00001403 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001404 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001405 assert((Start >> 32) == 0 && "Selector key offset too large");
1406 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001407 unsigned N = Sel.getNumArgs();
1408 clang::io::Emit16(Out, N);
1409 if (N == 0)
1410 N = 1;
1411 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001412 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001413 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1414 }
Mike Stump11289f42009-09-09 15:08:12 +00001415
Douglas Gregorc78d3462009-04-24 21:10:55 +00001416 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001417 data_type_ref Methods, unsigned DataLen) {
1418 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001419 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001420 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001421 Method = Method->Next)
1422 if (Method->Method)
1423 ++NumInstanceMethods;
1424
1425 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001426 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001427 Method = Method->Next)
1428 if (Method->Method)
1429 ++NumFactoryMethods;
1430
1431 clang::io::Emit16(Out, NumInstanceMethods);
1432 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001433 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001434 Method = Method->Next)
1435 if (Method->Method)
1436 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001437 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001438 Method = Method->Next)
1439 if (Method->Method)
1440 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001441
1442 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001443 }
1444};
1445} // end anonymous namespace
1446
1447/// \brief Write the method pool into the PCH file.
1448///
1449/// The method pool contains both instance and factory methods, stored
1450/// in an on-disk hash table indexed by the selector.
1451void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1452 using namespace llvm;
1453
1454 // Create and write out the blob that contains the instance and
1455 // factor method pools.
1456 bool Empty = true;
1457 {
1458 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001459
Douglas Gregorc78d3462009-04-24 21:10:55 +00001460 // Create the on-disk hash table representation. Start by
1461 // iterating through the instance method pool.
1462 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001463 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001464 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001465 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001466 InstanceEnd = SemaRef.InstanceMethodPool.end();
1467 Instance != InstanceEnd; ++Instance) {
1468 // Check whether there is a factory method with the same
1469 // selector.
1470 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1471 = SemaRef.FactoryMethodPool.find(Instance->first);
1472
1473 if (Factory == SemaRef.FactoryMethodPool.end())
1474 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001475 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001476 ObjCMethodList()));
1477 else
1478 Generator.insert(Instance->first,
1479 std::make_pair(Instance->second, Factory->second));
1480
Douglas Gregor95c13f52009-04-25 17:48:32 +00001481 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001482 Empty = false;
1483 }
1484
1485 // Now iterate through the factory method pool, to pick up any
1486 // selectors that weren't already in the instance method pool.
1487 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001488 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001489 FactoryEnd = SemaRef.FactoryMethodPool.end();
1490 Factory != FactoryEnd; ++Factory) {
1491 // Check whether there is an instance method with the same
1492 // selector. If so, there is no work to do here.
1493 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1494 = SemaRef.InstanceMethodPool.find(Factory->first);
1495
Douglas Gregor95c13f52009-04-25 17:48:32 +00001496 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001497 Generator.insert(Factory->first,
1498 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001499 ++NumSelectorsInMethodPool;
1500 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001501
1502 Empty = false;
1503 }
1504
Douglas Gregor95c13f52009-04-25 17:48:32 +00001505 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001506 return;
1507
1508 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001509 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001510 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001511 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001512 {
1513 PCHMethodPoolTrait Trait(*this);
1514 llvm::raw_svector_ostream Out(MethodPool);
1515 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001516 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001517 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001518
1519 // For every selector that we have seen but which was not
1520 // written into the hash table, write the selector itself and
1521 // record it's offset.
1522 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1523 if (SelectorOffsets[I] == 0)
1524 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001525 }
1526
1527 // Create a blob abbreviation
1528 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1529 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1533 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1534
Douglas Gregor95c13f52009-04-25 17:48:32 +00001535 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001536 RecordData Record;
1537 Record.push_back(pch::METHOD_POOL);
1538 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001539 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001540 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001541
1542 // Create a blob abbreviation for the selector table offsets.
1543 Abbrev = new BitCodeAbbrev();
1544 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1547 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1548
1549 // Write the selector offsets table.
1550 Record.clear();
1551 Record.push_back(pch::SELECTOR_OFFSETS);
1552 Record.push_back(SelectorOffsets.size());
1553 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1554 (const char *)&SelectorOffsets.front(),
1555 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001556 }
1557}
1558
Douglas Gregorc5046832009-04-27 18:38:38 +00001559//===----------------------------------------------------------------------===//
1560// Identifier Table Serialization
1561//===----------------------------------------------------------------------===//
1562
Douglas Gregorc78d3462009-04-24 21:10:55 +00001563namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001564class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001565 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001566 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001567
Douglas Gregor1d583f22009-04-28 21:18:29 +00001568 /// \brief Determines whether this is an "interesting" identifier
1569 /// that needs a full IdentifierInfo structure written into the hash
1570 /// table.
1571 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1572 return II->isPoisoned() ||
1573 II->isExtensionToken() ||
1574 II->hasMacroDefinition() ||
1575 II->getObjCOrBuiltinID() ||
1576 II->getFETokenInfo<void>();
1577 }
1578
Douglas Gregore84a9da2009-04-20 20:36:09 +00001579public:
1580 typedef const IdentifierInfo* key_type;
1581 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001582
Douglas Gregore84a9da2009-04-20 20:36:09 +00001583 typedef pch::IdentID data_type;
1584 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001585
1586 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001587 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001588
1589 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001590 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001591 }
Mike Stump11289f42009-09-09 15:08:12 +00001592
1593 std::pair<unsigned,unsigned>
1594 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001595 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001596 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001597 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1598 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001599 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001600 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001601 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001602 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001603 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1604 DEnd = IdentifierResolver::end();
1605 D != DEnd; ++D)
1606 DataLen += sizeof(pch::DeclID);
1607 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001608 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001609 // We emit the key length after the data length so that every
1610 // string is preceded by a 16-bit length. This matches the PTH
1611 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001612 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001613 return std::make_pair(KeyLen, DataLen);
1614 }
Mike Stump11289f42009-09-09 15:08:12 +00001615
1616 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001617 unsigned KeyLen) {
1618 // Record the location of the key data. This is used when generating
1619 // the mapping from persistent IDs to strings.
1620 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001621 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001622 }
Mike Stump11289f42009-09-09 15:08:12 +00001623
1624 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001625 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001626 if (!isInterestingIdentifier(II)) {
1627 clang::io::Emit32(Out, ID << 1);
1628 return;
1629 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001630
Douglas Gregor1d583f22009-04-28 21:18:29 +00001631 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001632 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001633 bool hasMacroDefinition =
1634 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001635 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001636 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001637 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1638 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1639 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1640 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001641 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001642
Douglas Gregorc3366a52009-04-21 23:56:24 +00001643 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001644 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001645
Douglas Gregora868bbd2009-04-21 22:25:48 +00001646 // Emit the declaration IDs in reverse order, because the
1647 // IdentifierResolver provides the declarations as they would be
1648 // visible (e.g., the function "stat" would come before the struct
1649 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1650 // adds declarations to the end of the list (so we need to see the
1651 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001652 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001653 IdentifierResolver::end());
1654 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1655 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001656 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001657 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001658 }
1659};
1660} // end anonymous namespace
1661
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001662/// \brief Write the identifier table into the PCH file.
1663///
1664/// The identifier table consists of a blob containing string data
1665/// (the actual identifiers themselves) and a separate "offsets" index
1666/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001667void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001668 using namespace llvm;
1669
1670 // Create and write out the blob that contains the identifier
1671 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001672 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001673 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001674
Douglas Gregore6648fb2009-04-28 20:33:11 +00001675 // Look for any identifiers that were named while processing the
1676 // headers, but are otherwise not needed. We add these to the hash
1677 // table to enable checking of the predefines buffer in the case
1678 // where the user adds new macro definitions when building the PCH
1679 // file.
1680 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1681 IDEnd = PP.getIdentifierTable().end();
1682 ID != IDEnd; ++ID)
1683 getIdentifierRef(ID->second);
1684
Douglas Gregore84a9da2009-04-20 20:36:09 +00001685 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001686 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001687 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1688 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1689 ID != IDEnd; ++ID) {
1690 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001691 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001692 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001693
Douglas Gregore84a9da2009-04-20 20:36:09 +00001694 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001695 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001696 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001697 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001698 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001699 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001700 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001701 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001702 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001703 }
1704
1705 // Create a blob abbreviation
1706 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1707 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001708 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001710 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001711
1712 // Write the identifier table
1713 RecordData Record;
1714 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001715 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001716 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001717 }
1718
1719 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001720 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1721 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1722 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1723 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1724 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1725
1726 RecordData Record;
1727 Record.push_back(pch::IDENTIFIER_OFFSET);
1728 Record.push_back(IdentifierOffsets.size());
1729 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1730 (const char *)&IdentifierOffsets.front(),
1731 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001732}
1733
Douglas Gregorc5046832009-04-27 18:38:38 +00001734//===----------------------------------------------------------------------===//
1735// General Serialization Routines
1736//===----------------------------------------------------------------------===//
1737
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001738/// \brief Write a record containing the given attributes.
1739void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1740 RecordData Record;
1741 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001742 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001743 Record.push_back(Attr->isInherited());
1744 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001745 default:
1746 assert(0 && "Does not support PCH writing for this attribute yet!");
1747 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001748 case Attr::Alias:
1749 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1750 break;
1751
1752 case Attr::Aligned:
1753 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1754 break;
1755
1756 case Attr::AlwaysInline:
1757 break;
Mike Stump11289f42009-09-09 15:08:12 +00001758
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001759 case Attr::AnalyzerNoReturn:
1760 break;
1761
1762 case Attr::Annotate:
1763 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1764 break;
1765
1766 case Attr::AsmLabel:
1767 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1768 break;
1769
Alexis Hunt54a02542009-11-25 04:20:27 +00001770 case Attr::BaseCheck:
1771 break;
1772
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001773 case Attr::Blocks:
1774 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1775 break;
1776
Eli Friedmane4310c82009-11-09 18:38:53 +00001777 case Attr::CDecl:
1778 break;
1779
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001780 case Attr::Cleanup:
1781 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1782 break;
1783
1784 case Attr::Const:
1785 break;
1786
1787 case Attr::Constructor:
1788 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1789 break;
1790
1791 case Attr::DLLExport:
1792 case Attr::DLLImport:
1793 case Attr::Deprecated:
1794 break;
1795
1796 case Attr::Destructor:
1797 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1798 break;
1799
1800 case Attr::FastCall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001801 case Attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001802 break;
1803
1804 case Attr::Format: {
1805 const FormatAttr *Format = cast<FormatAttr>(Attr);
1806 AddString(Format->getType(), Record);
1807 Record.push_back(Format->getFormatIdx());
1808 Record.push_back(Format->getFirstArg());
1809 break;
1810 }
1811
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001812 case Attr::FormatArg: {
1813 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1814 Record.push_back(Format->getFormatIdx());
1815 break;
1816 }
1817
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001818 case Attr::Sentinel : {
1819 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1820 Record.push_back(Sentinel->getSentinel());
1821 Record.push_back(Sentinel->getNullPos());
1822 break;
1823 }
Mike Stump11289f42009-09-09 15:08:12 +00001824
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001825 case Attr::GNUInline:
Alexis Hunt54a02542009-11-25 04:20:27 +00001826 case Attr::Hiding:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001827 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001828 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001829 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001830 case Attr::NoReturn:
1831 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001832 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001833 break;
1834
1835 case Attr::NonNull: {
1836 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1837 Record.push_back(NonNull->size());
1838 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1839 break;
1840 }
1841
1842 case Attr::ObjCException:
1843 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001844 case Attr::CFReturnsRetained:
1845 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001846 case Attr::Overloadable:
Alexis Hunt54a02542009-11-25 04:20:27 +00001847 case Attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001848 break;
1849
Anders Carlsson68e0b682009-08-08 18:23:56 +00001850 case Attr::PragmaPack:
1851 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001852 break;
1853
Anders Carlsson68e0b682009-08-08 18:23:56 +00001854 case Attr::Packed:
1855 break;
Mike Stump11289f42009-09-09 15:08:12 +00001856
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001857 case Attr::Pure:
1858 break;
1859
1860 case Attr::Regparm:
1861 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1862 break;
Mike Stump11289f42009-09-09 15:08:12 +00001863
Nate Begemanf2758702009-06-26 06:32:41 +00001864 case Attr::ReqdWorkGroupSize:
1865 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1866 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1867 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1868 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001869
1870 case Attr::Section:
1871 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1872 break;
1873
1874 case Attr::StdCall:
1875 case Attr::TransparentUnion:
1876 case Attr::Unavailable:
1877 case Attr::Unused:
1878 case Attr::Used:
1879 break;
1880
1881 case Attr::Visibility:
1882 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001883 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001884 break;
1885
1886 case Attr::WarnUnusedResult:
1887 case Attr::Weak:
1888 case Attr::WeakImport:
1889 break;
1890 }
1891 }
1892
Douglas Gregor8f45df52009-04-16 22:23:12 +00001893 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001894}
1895
1896void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1897 Record.push_back(Str.size());
1898 Record.insert(Record.end(), Str.begin(), Str.end());
1899}
1900
Douglas Gregore84a9da2009-04-20 20:36:09 +00001901/// \brief Note that the identifier II occurs at the given offset
1902/// within the identifier table.
1903void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001904 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001905}
1906
Douglas Gregor95c13f52009-04-25 17:48:32 +00001907/// \brief Note that the selector Sel occurs at the given offset
1908/// within the method pool/selector table.
1909void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1910 unsigned ID = SelectorIDs[Sel];
1911 assert(ID && "Unknown selector");
1912 SelectorOffsets[ID - 1] = Offset;
1913}
1914
Mike Stump11289f42009-09-09 15:08:12 +00001915PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1916 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001917 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1918 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001919
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001920void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1921 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001922 using namespace llvm;
1923
Douglas Gregor162dd022009-04-20 15:53:59 +00001924 ASTContext &Context = SemaRef.Context;
1925 Preprocessor &PP = SemaRef.PP;
1926
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001927 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001928 Stream.Emit((unsigned)'C', 8);
1929 Stream.Emit((unsigned)'P', 8);
1930 Stream.Emit((unsigned)'C', 8);
1931 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001932
Chris Lattner28fa4e62009-04-26 22:26:21 +00001933 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001934
1935 // The translation unit is the first declaration we'll emit.
1936 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001937 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001938
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001939 // Make sure that we emit IdentifierInfos (and any attached
1940 // declarations) for builtins.
1941 {
1942 IdentifierTable &Table = PP.getIdentifierTable();
1943 llvm::SmallVector<const char *, 32> BuiltinNames;
1944 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1945 Context.getLangOptions().NoBuiltin);
1946 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1947 getIdentifierRef(&Table.get(BuiltinNames[I]));
1948 }
1949
Chris Lattner0c797362009-09-08 18:19:27 +00001950 // Build a record containing all of the tentative definitions in this file, in
1951 // TentativeDefinitionList order. Generally, this record will be empty for
1952 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001953 RecordData TentativeDefinitions;
Chris Lattner0c797362009-09-08 18:19:27 +00001954 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1955 VarDecl *VD =
1956 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1957 if (VD) AddDeclRef(VD, TentativeDefinitions);
1958 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001959
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001960 // Build a record containing all of the locally-scoped external
1961 // declarations in this header file. Generally, this record will be
1962 // empty.
1963 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001964 // FIXME: This is filling in the PCH file in densemap order which is
1965 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001966 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001967 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1968 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1969 TD != TDEnd; ++TD)
1970 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1971
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001972 // Build a record containing all of the ext_vector declarations.
1973 RecordData ExtVectorDecls;
1974 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1975 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1976
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001977 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001978 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001979 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001980 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001981 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001982 if (StatCalls && !isysroot)
1983 WriteStatCache(*StatCalls, isysroot);
1984 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00001985 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001986 // Write the record of special types.
1987 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001988
Steve Naroffc277ad12009-07-18 15:33:26 +00001989 AddTypeRef(Context.getBuiltinVaListType(), Record);
1990 AddTypeRef(Context.getObjCIdType(), Record);
1991 AddTypeRef(Context.getObjCSelType(), Record);
1992 AddTypeRef(Context.getObjCProtoType(), Record);
1993 AddTypeRef(Context.getObjCClassType(), Record);
1994 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1995 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1996 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00001997 AddTypeRef(Context.getjmp_bufType(), Record);
1998 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001999 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2000 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002001#if 0
2002 // FIXME. Accommodate for this in several PCH/Indexer tests
Fariborz Jahanian04b258c2009-11-25 23:07:42 +00002003 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002004#endif
Mike Stumpd0153282009-10-20 02:12:22 +00002005 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002006 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002007 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002008
Douglas Gregor1970d882009-04-26 03:49:13 +00002009 // Keep writing types and declarations until all types and
2010 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002011 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2012 WriteDeclsBlockAbbrevs();
2013 while (!DeclTypesToEmit.empty()) {
2014 DeclOrType DOT = DeclTypesToEmit.front();
2015 DeclTypesToEmit.pop();
2016 if (DOT.isType())
2017 WriteType(DOT.getType());
2018 else
2019 WriteDecl(Context, DOT.getDecl());
2020 }
2021 Stream.ExitBlock();
2022
Douglas Gregor45053152009-10-17 17:25:45 +00002023 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002024 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002025 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002026
2027 // Write the type offsets array
2028 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2029 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2032 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2033 Record.clear();
2034 Record.push_back(pch::TYPE_OFFSET);
2035 Record.push_back(TypeOffsets.size());
2036 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002037 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002038 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002039
Douglas Gregor745ed142009-04-25 18:35:21 +00002040 // Write the declaration offsets array
2041 Abbrev = new BitCodeAbbrev();
2042 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2045 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2046 Record.clear();
2047 Record.push_back(pch::DECL_OFFSET);
2048 Record.push_back(DeclOffsets.size());
2049 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002050 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002051 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002052
Douglas Gregord4df8652009-04-22 22:02:47 +00002053 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002054 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002055 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002056
2057 // Write the record containing tentative definitions.
2058 if (!TentativeDefinitions.empty())
2059 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002060
2061 // Write the record containing locally-scoped external definitions.
2062 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002063 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002064 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002065
2066 // Write the record containing ext_vector type names.
2067 if (!ExtVectorDecls.empty())
2068 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002069
Douglas Gregor08f01292009-04-17 22:13:46 +00002070 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002071 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002072 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002073 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002074 Record.push_back(NumLexicalDeclContexts);
2075 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002076 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002077 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002078}
2079
2080void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2081 Record.push_back(Loc.getRawEncoding());
2082}
2083
2084void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2085 Record.push_back(Value.getBitWidth());
2086 unsigned N = Value.getNumWords();
2087 const uint64_t* Words = Value.getRawData();
2088 for (unsigned I = 0; I != N; ++I)
2089 Record.push_back(Words[I]);
2090}
2091
Douglas Gregor1daeb692009-04-13 18:14:40 +00002092void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2093 Record.push_back(Value.isUnsigned());
2094 AddAPInt(Value, Record);
2095}
2096
Douglas Gregore0a3a512009-04-14 21:55:33 +00002097void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2098 AddAPInt(Value.bitcastToAPInt(), Record);
2099}
2100
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002101void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002102 Record.push_back(getIdentifierRef(II));
2103}
2104
2105pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2106 if (II == 0)
2107 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002108
2109 pch::IdentID &ID = IdentifierIDs[II];
2110 if (ID == 0)
2111 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002112 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002113}
2114
Steve Naroff2ddea052009-04-23 10:39:46 +00002115void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2116 if (SelRef.getAsOpaquePtr() == 0) {
2117 Record.push_back(0);
2118 return;
2119 }
2120
2121 pch::SelectorID &SID = SelectorIDs[SelRef];
2122 if (SID == 0) {
2123 SID = SelectorIDs.size();
2124 SelVector.push_back(SelRef);
2125 }
2126 Record.push_back(SID);
2127}
2128
John McCall0ad16662009-10-29 08:12:44 +00002129void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2130 RecordData &Record) {
2131 switch (Arg.getArgument().getKind()) {
2132 case TemplateArgument::Expression:
2133 AddStmt(Arg.getLocInfo().getAsExpr());
2134 break;
2135 case TemplateArgument::Type:
John McCallbcd03502009-12-07 02:54:59 +00002136 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002137 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002138 case TemplateArgument::Template:
2139 Record.push_back(
2140 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2141 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2142 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2143 break;
John McCall0ad16662009-10-29 08:12:44 +00002144 case TemplateArgument::Null:
2145 case TemplateArgument::Integral:
2146 case TemplateArgument::Declaration:
2147 case TemplateArgument::Pack:
2148 break;
2149 }
2150}
2151
John McCallbcd03502009-12-07 02:54:59 +00002152void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2153 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002154 AddTypeRef(QualType(), Record);
2155 return;
2156 }
2157
John McCallbcd03502009-12-07 02:54:59 +00002158 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002159 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002160 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00002161 TLW.Visit(TL);
2162}
2163
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002164void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2165 if (T.isNull()) {
2166 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2167 return;
2168 }
2169
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002170 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002171 T.removeFastQualifiers();
2172
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002173 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002174 pch::TypeID &ID = TypeIDs[T];
2175 if (ID == 0) {
2176 // We haven't seen these qualifiers applied to this type before.
2177 // Assign it a new ID. This is the only time we enqueue a
2178 // qualified type, and it has no CV qualifiers.
2179 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002180 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002181 }
2182
2183 // Encode the type qualifiers in the type reference.
2184 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2185 return;
2186 }
2187
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002188 assert(!T.hasLocalQualifiers());
John McCall8ccfcb52009-09-24 19:53:00 +00002189
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002190 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002191 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002192 switch (BT->getKind()) {
2193 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2194 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2195 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2196 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2197 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2198 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2199 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2200 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002201 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002202 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2203 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2204 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2205 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2206 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2207 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2208 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002209 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002210 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2211 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2212 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002213 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002214 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2215 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002216 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2217 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002218 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2219 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002220 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002221 case BuiltinType::UndeducedAuto:
2222 assert(0 && "Should not see undeduced auto here");
2223 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002224 }
2225
John McCall8ccfcb52009-09-24 19:53:00 +00002226 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002227 return;
2228 }
2229
John McCall8ccfcb52009-09-24 19:53:00 +00002230 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002231 if (ID == 0) {
2232 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002233 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002234 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002235 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002236 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002237
2238 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002239 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002240}
2241
2242void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2243 if (D == 0) {
2244 Record.push_back(0);
2245 return;
2246 }
2247
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002248 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002249 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002250 // We haven't seen this declaration before. Give it a new ID and
2251 // enqueue it in the list of declarations to emit.
2252 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002253 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002254 }
2255
2256 Record.push_back(ID);
2257}
2258
Douglas Gregore84a9da2009-04-20 20:36:09 +00002259pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2260 if (D == 0)
2261 return 0;
2262
2263 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2264 return DeclIDs[D];
2265}
2266
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002267void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002268 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002269 Record.push_back(Name.getNameKind());
2270 switch (Name.getNameKind()) {
2271 case DeclarationName::Identifier:
2272 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2273 break;
2274
2275 case DeclarationName::ObjCZeroArgSelector:
2276 case DeclarationName::ObjCOneArgSelector:
2277 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002278 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002279 break;
2280
2281 case DeclarationName::CXXConstructorName:
2282 case DeclarationName::CXXDestructorName:
2283 case DeclarationName::CXXConversionFunctionName:
2284 AddTypeRef(Name.getCXXNameType(), Record);
2285 break;
2286
2287 case DeclarationName::CXXOperatorName:
2288 Record.push_back(Name.getCXXOverloadedOperator());
2289 break;
2290
Alexis Hunt3d221f22009-11-29 07:34:05 +00002291 case DeclarationName::CXXLiteralOperatorName:
2292 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2293 break;
2294
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002295 case DeclarationName::CXXUsingDirective:
2296 // No extra data to emit
2297 break;
2298 }
2299}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002300