blob: a0bd9ec2a7ac294e65a3fc3d4050f0e22295c9c5 [file] [log] [blame]
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001//===--- PCHWriter.cpp - Precompiled Headers Writer -----------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas 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"
Douglas Gregoraae92242010-03-19 21:51:54 +000024#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000033#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000037#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000038#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000039#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// Type serialization
44//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000045
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +000047 class PCHTypeWriter {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048 PCHWriter &Writer;
49 PCHWriter::RecordData &Record;
50
51 public:
52 /// \brief Type code that corresponds to the record generated.
53 pch::TypeCode Code;
54
Mike Stump11289f42009-09-09 15:08:12 +000055 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000056 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000057
58 void VisitArrayType(const ArrayType *T);
59 void VisitFunctionType(const FunctionType *T);
60 void VisitTagType(const TagType *T);
61
62#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000064#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());
John Thompson22334602010-02-05 00:12:22 +0000131 Record.push_back(T->isAltiVec());
132 Record.push_back(T->isPixel());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133 Code = pch::TYPE_VECTOR;
134}
135
136void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
137 VisitVectorType(T);
138 Code = pch::TYPE_EXT_VECTOR;
139}
140
141void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
142 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000143 FunctionType::ExtInfo C = T->getExtInfo();
144 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000145 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000146 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000147 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148}
149
150void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
151 VisitFunctionType(T);
152 Code = pch::TYPE_FUNCTION_NO_PROTO;
153}
154
155void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
156 VisitFunctionType(T);
157 Record.push_back(T->getNumArgs());
158 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
159 Writer.AddTypeRef(T->getArgType(I), Record);
160 Record.push_back(T->isVariadic());
161 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000162 Record.push_back(T->hasExceptionSpec());
163 Record.push_back(T->hasAnyExceptionSpec());
164 Record.push_back(T->getNumExceptions());
165 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
166 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167 Code = pch::TYPE_FUNCTION_PROTO;
168}
169
John McCallb96ec562009-12-04 22:46:56 +0000170void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_UNRESOLVED_USING;
173}
John McCallb96ec562009-12-04 22:46:56 +0000174
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
176 Writer.AddDeclRef(T->getDecl(), Record);
177 Code = pch::TYPE_TYPEDEF;
178}
179
180void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000181 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000182 Code = pch::TYPE_TYPEOF_EXPR;
183}
184
185void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
186 Writer.AddTypeRef(T->getUnderlyingType(), Record);
187 Code = pch::TYPE_TYPEOF;
188}
189
Anders Carlsson81df7b82009-06-24 19:06:50 +0000190void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
191 Writer.AddStmt(T->getUnderlyingExpr());
192 Code = pch::TYPE_DECLTYPE;
193}
194
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195void PCHTypeWriter::VisitTagType(const TagType *T) {
196 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000197 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198 "Cannot serialize in the middle of a type definition");
199}
200
201void PCHTypeWriter::VisitRecordType(const RecordType *T) {
202 VisitTagType(T);
203 Code = pch::TYPE_RECORD;
204}
205
206void PCHTypeWriter::VisitEnumType(const EnumType *T) {
207 VisitTagType(T);
208 Code = pch::TYPE_ENUM;
209}
210
Mike Stump11289f42009-09-09 15:08:12 +0000211void
John McCallcebee162009-10-18 09:09:24 +0000212PCHTypeWriter::VisitSubstTemplateTypeParmType(
213 const SubstTemplateTypeParmType *T) {
214 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
215 Writer.AddTypeRef(T->getReplacementType(), Record);
216 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
217}
218
219void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220PCHTypeWriter::VisitTemplateSpecializationType(
221 const TemplateSpecializationType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000222 Writer.AddTemplateName(T->getTemplateName(), Record);
223 Record.push_back(T->getNumArgs());
224 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
225 ArgI != ArgE; ++ArgI)
226 Writer.AddTemplateArgument(*ArgI, Record);
227 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
228}
229
230void
231PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000232 // FIXME: Serialize this type (C++ only)
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000233 assert(false && "Cannot serialize dependent sized array types");
234}
235
236void
237PCHTypeWriter::VisitDependentSizedExtVectorType(
238 const DependentSizedExtVectorType *T) {
239 // FIXME: Serialize this type (C++ only)
240 assert(false && "Cannot serialize dependent sized extended vector types");
241}
242
243void
244PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
245 Record.push_back(T->getDepth());
246 Record.push_back(T->getIndex());
247 Record.push_back(T->isParameterPack());
248 Writer.AddIdentifierRef(T->getName(), Record);
249 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
250}
251
252void
253PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) {
254 // FIXME: Serialize this type (C++ only)
255 assert(false && "Cannot serialize dependent name types");
256}
257
258void
259PCHTypeWriter::VisitDependentTemplateSpecializationType(
260 const DependentTemplateSpecializationType *T) {
261 // FIXME: Serialize this type (C++ only)
262 assert(false && "Cannot serialize dependent template specialization types");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Abramo Bagnara6150c882010-05-11 21:36:43 +0000265void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
266 Writer.AddTypeRef(T->getNamedType(), Record);
267 Record.push_back(T->getKeyword());
268 // FIXME: Serialize the qualifier (C++ only)
269 assert(T->getQualifier() == 0 && "Cannot serialize qualified name types");
270 Code = pch::TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000271}
272
John McCalle78aac42010-03-10 03:28:59 +0000273void PCHTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
274 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000275 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCalle78aac42010-03-10 03:28:59 +0000276 Code = pch::TYPE_INJECTED_CLASS_NAME;
277}
278
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000279void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
280 Writer.AddDeclRef(T->getDecl(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000281 Code = pch::TYPE_OBJC_INTERFACE;
282}
283
284void PCHTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
285 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000286 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000287 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000288 E = T->qual_end(); I != E; ++I)
289 Writer.AddDeclRef(*I, Record);
John McCall8b07ec22010-05-15 11:32:37 +0000290 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000291}
292
Steve Narofffb4330f2009-06-17 22:40:22 +0000293void
294PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000295 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000296 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000297}
298
John McCall8f115c62009-10-16 21:56:05 +0000299namespace {
300
301class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
302 PCHWriter &Writer;
303 PCHWriter::RecordData &Record;
304
305public:
306 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
307 : Writer(Writer), Record(Record) { }
308
John McCall17001972009-10-18 01:05:36 +0000309#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000310#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000311 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000312#include "clang/AST/TypeLocNodes.def"
313
John McCall17001972009-10-18 01:05:36 +0000314 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
315 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000316};
317
318}
319
John McCall17001972009-10-18 01:05:36 +0000320void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
321 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000322}
John McCall17001972009-10-18 01:05:36 +0000323void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000324 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
325 if (TL.needsExtraLocalData()) {
326 Record.push_back(TL.getWrittenTypeSpec());
327 Record.push_back(TL.getWrittenSignSpec());
328 Record.push_back(TL.getWrittenWidthSpec());
329 Record.push_back(TL.hasModeAttr());
330 }
John McCall8f115c62009-10-16 21:56:05 +0000331}
John McCall17001972009-10-18 01:05:36 +0000332void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
333 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000334}
John McCall17001972009-10-18 01:05:36 +0000335void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
336 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000337}
John McCall17001972009-10-18 01:05:36 +0000338void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000340}
John McCall17001972009-10-18 01:05:36 +0000341void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
342 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000343}
John McCall17001972009-10-18 01:05:36 +0000344void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
345 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000346}
John McCall17001972009-10-18 01:05:36 +0000347void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
348 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000349}
John McCall17001972009-10-18 01:05:36 +0000350void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
351 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
352 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
353 Record.push_back(TL.getSizeExpr() ? 1 : 0);
354 if (TL.getSizeExpr())
355 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000356}
John McCall17001972009-10-18 01:05:36 +0000357void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
358 VisitArrayTypeLoc(TL);
359}
360void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
361 VisitArrayTypeLoc(TL);
362}
363void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
364 VisitArrayTypeLoc(TL);
365}
366void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
367 DependentSizedArrayTypeLoc TL) {
368 VisitArrayTypeLoc(TL);
369}
370void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
371 DependentSizedExtVectorTypeLoc TL) {
372 Writer.AddSourceLocation(TL.getNameLoc(), Record);
373}
374void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
375 Writer.AddSourceLocation(TL.getNameLoc(), Record);
376}
377void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
378 Writer.AddSourceLocation(TL.getNameLoc(), Record);
379}
380void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
381 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
382 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
383 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
384 Writer.AddDeclRef(TL.getArg(i), Record);
385}
386void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
387 VisitFunctionTypeLoc(TL);
388}
389void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
390 VisitFunctionTypeLoc(TL);
391}
John McCallb96ec562009-12-04 22:46:56 +0000392void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
393 Writer.AddSourceLocation(TL.getNameLoc(), Record);
394}
John McCall17001972009-10-18 01:05:36 +0000395void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
396 Writer.AddSourceLocation(TL.getNameLoc(), Record);
397}
398void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000399 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
400 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
401 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000402}
403void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000404 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
405 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
406 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
407 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000408}
409void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
411}
412void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getNameLoc(), Record);
414}
415void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
416 Writer.AddSourceLocation(TL.getNameLoc(), Record);
417}
John McCall17001972009-10-18 01:05:36 +0000418void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
419 Writer.AddSourceLocation(TL.getNameLoc(), Record);
420}
John McCallcebee162009-10-18 09:09:24 +0000421void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
422 SubstTemplateTypeParmTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getNameLoc(), Record);
424}
John McCall17001972009-10-18 01:05:36 +0000425void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
426 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000427 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
428 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
429 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
430 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
431 Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
John McCall17001972009-10-18 01:05:36 +0000432}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000433void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000434 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
435 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000436}
John McCalle78aac42010-03-10 03:28:59 +0000437void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
439}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000440void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000441 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
442 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
John McCallc392f372010-06-11 00:33:02 +0000445void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
446 DependentTemplateSpecializationTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
448 Writer.AddSourceRange(TL.getQualifierRange(), Record);
449 Writer.AddSourceLocation(TL.getNameLoc(), Record);
450 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
451 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
452 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
453 Writer.AddTemplateArgumentLoc(TL.getArgLoc(I), Record);
454}
John McCall17001972009-10-18 01:05:36 +0000455void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000457}
458void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
459 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000460 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
461 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
462 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
463 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000464}
John McCallfc93cf92009-10-22 22:37:11 +0000465void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000467}
John McCall8f115c62009-10-16 21:56:05 +0000468
Chris Lattner19cea4e2009-04-22 05:57:30 +0000469//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000470// PCHWriter Implementation
471//===----------------------------------------------------------------------===//
472
Chris Lattner28fa4e62009-04-26 22:26:21 +0000473static void EmitBlockID(unsigned ID, const char *Name,
474 llvm::BitstreamWriter &Stream,
475 PCHWriter::RecordData &Record) {
476 Record.clear();
477 Record.push_back(ID);
478 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
479
480 // Emit the block name if present.
481 if (Name == 0 || Name[0] == 0) return;
482 Record.clear();
483 while (*Name)
484 Record.push_back(*Name++);
485 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
486}
487
488static void EmitRecordID(unsigned ID, const char *Name,
489 llvm::BitstreamWriter &Stream,
490 PCHWriter::RecordData &Record) {
491 Record.clear();
492 Record.push_back(ID);
493 while (*Name)
494 Record.push_back(*Name++);
495 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000496}
497
498static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
499 PCHWriter::RecordData &Record) {
500#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
501 RECORD(STMT_STOP);
502 RECORD(STMT_NULL_PTR);
503 RECORD(STMT_NULL);
504 RECORD(STMT_COMPOUND);
505 RECORD(STMT_CASE);
506 RECORD(STMT_DEFAULT);
507 RECORD(STMT_LABEL);
508 RECORD(STMT_IF);
509 RECORD(STMT_SWITCH);
510 RECORD(STMT_WHILE);
511 RECORD(STMT_DO);
512 RECORD(STMT_FOR);
513 RECORD(STMT_GOTO);
514 RECORD(STMT_INDIRECT_GOTO);
515 RECORD(STMT_CONTINUE);
516 RECORD(STMT_BREAK);
517 RECORD(STMT_RETURN);
518 RECORD(STMT_DECL);
519 RECORD(STMT_ASM);
520 RECORD(EXPR_PREDEFINED);
521 RECORD(EXPR_DECL_REF);
522 RECORD(EXPR_INTEGER_LITERAL);
523 RECORD(EXPR_FLOATING_LITERAL);
524 RECORD(EXPR_IMAGINARY_LITERAL);
525 RECORD(EXPR_STRING_LITERAL);
526 RECORD(EXPR_CHARACTER_LITERAL);
527 RECORD(EXPR_PAREN);
528 RECORD(EXPR_UNARY_OPERATOR);
529 RECORD(EXPR_SIZEOF_ALIGN_OF);
530 RECORD(EXPR_ARRAY_SUBSCRIPT);
531 RECORD(EXPR_CALL);
532 RECORD(EXPR_MEMBER);
533 RECORD(EXPR_BINARY_OPERATOR);
534 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
535 RECORD(EXPR_CONDITIONAL_OPERATOR);
536 RECORD(EXPR_IMPLICIT_CAST);
537 RECORD(EXPR_CSTYLE_CAST);
538 RECORD(EXPR_COMPOUND_LITERAL);
539 RECORD(EXPR_EXT_VECTOR_ELEMENT);
540 RECORD(EXPR_INIT_LIST);
541 RECORD(EXPR_DESIGNATED_INIT);
542 RECORD(EXPR_IMPLICIT_VALUE_INIT);
543 RECORD(EXPR_VA_ARG);
544 RECORD(EXPR_ADDR_LABEL);
545 RECORD(EXPR_STMT);
546 RECORD(EXPR_TYPES_COMPATIBLE);
547 RECORD(EXPR_CHOOSE);
548 RECORD(EXPR_GNU_NULL);
549 RECORD(EXPR_SHUFFLE_VECTOR);
550 RECORD(EXPR_BLOCK);
551 RECORD(EXPR_BLOCK_DECL_REF);
552 RECORD(EXPR_OBJC_STRING_LITERAL);
553 RECORD(EXPR_OBJC_ENCODE);
554 RECORD(EXPR_OBJC_SELECTOR_EXPR);
555 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
556 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
557 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
558 RECORD(EXPR_OBJC_KVC_REF_EXPR);
559 RECORD(EXPR_OBJC_MESSAGE_EXPR);
560 RECORD(EXPR_OBJC_SUPER_EXPR);
561 RECORD(STMT_OBJC_FOR_COLLECTION);
562 RECORD(STMT_OBJC_CATCH);
563 RECORD(STMT_OBJC_FINALLY);
564 RECORD(STMT_OBJC_AT_TRY);
565 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
566 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000567 RECORD(EXPR_CXX_OPERATOR_CALL);
568 RECORD(EXPR_CXX_CONSTRUCT);
569 RECORD(EXPR_CXX_STATIC_CAST);
570 RECORD(EXPR_CXX_DYNAMIC_CAST);
571 RECORD(EXPR_CXX_REINTERPRET_CAST);
572 RECORD(EXPR_CXX_CONST_CAST);
573 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
574 RECORD(EXPR_CXX_BOOL_LITERAL);
575 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000576#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000577}
Mike Stump11289f42009-09-09 15:08:12 +0000578
Chris Lattner28fa4e62009-04-26 22:26:21 +0000579void PCHWriter::WriteBlockInfoBlock() {
580 RecordData Record;
581 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000582
Chris Lattner64031982009-04-27 00:40:25 +0000583#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000584#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000585
Chris Lattner28fa4e62009-04-26 22:26:21 +0000586 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000587 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000588 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000589 RECORD(TYPE_OFFSET);
590 RECORD(DECL_OFFSET);
591 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000592 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000593 RECORD(IDENTIFIER_OFFSET);
594 RECORD(IDENTIFIER_TABLE);
595 RECORD(EXTERNAL_DEFINITIONS);
596 RECORD(SPECIAL_TYPES);
597 RECORD(STATISTICS);
598 RECORD(TENTATIVE_DEFINITIONS);
Tanya Lattner90073802010-02-12 00:07:30 +0000599 RECORD(UNUSED_STATIC_FUNCS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000600 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
601 RECORD(SELECTOR_OFFSETS);
602 RECORD(METHOD_POOL);
603 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000604 RECORD(SOURCE_LOCATION_OFFSETS);
605 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000606 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000607 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000608 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000609 RECORD(UNUSED_STATIC_FUNCS);
610 RECORD(MACRO_DEFINITION_OFFSETS);
611
Chris Lattner28fa4e62009-04-26 22:26:21 +0000612 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000613 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000614 RECORD(SM_SLOC_FILE_ENTRY);
615 RECORD(SM_SLOC_BUFFER_ENTRY);
616 RECORD(SM_SLOC_BUFFER_BLOB);
617 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
618 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000619
Chris Lattner28fa4e62009-04-26 22:26:21 +0000620 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000621 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000622 RECORD(PP_MACRO_OBJECT_LIKE);
623 RECORD(PP_MACRO_FUNCTION_LIKE);
624 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000625 RECORD(PP_MACRO_INSTANTIATION);
626 RECORD(PP_MACRO_DEFINITION);
627
Douglas Gregor12bfa382009-10-17 00:13:19 +0000628 // Decls and Types block.
629 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000630 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000631 RECORD(TYPE_COMPLEX);
632 RECORD(TYPE_POINTER);
633 RECORD(TYPE_BLOCK_POINTER);
634 RECORD(TYPE_LVALUE_REFERENCE);
635 RECORD(TYPE_RVALUE_REFERENCE);
636 RECORD(TYPE_MEMBER_POINTER);
637 RECORD(TYPE_CONSTANT_ARRAY);
638 RECORD(TYPE_INCOMPLETE_ARRAY);
639 RECORD(TYPE_VARIABLE_ARRAY);
640 RECORD(TYPE_VECTOR);
641 RECORD(TYPE_EXT_VECTOR);
642 RECORD(TYPE_FUNCTION_PROTO);
643 RECORD(TYPE_FUNCTION_NO_PROTO);
644 RECORD(TYPE_TYPEDEF);
645 RECORD(TYPE_TYPEOF_EXPR);
646 RECORD(TYPE_TYPEOF);
647 RECORD(TYPE_RECORD);
648 RECORD(TYPE_ENUM);
649 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000650 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000651 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000652 RECORD(DECL_ATTR);
653 RECORD(DECL_TRANSLATION_UNIT);
654 RECORD(DECL_TYPEDEF);
655 RECORD(DECL_ENUM);
656 RECORD(DECL_RECORD);
657 RECORD(DECL_ENUM_CONSTANT);
658 RECORD(DECL_FUNCTION);
659 RECORD(DECL_OBJC_METHOD);
660 RECORD(DECL_OBJC_INTERFACE);
661 RECORD(DECL_OBJC_PROTOCOL);
662 RECORD(DECL_OBJC_IVAR);
663 RECORD(DECL_OBJC_AT_DEFS_FIELD);
664 RECORD(DECL_OBJC_CLASS);
665 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
666 RECORD(DECL_OBJC_CATEGORY);
667 RECORD(DECL_OBJC_CATEGORY_IMPL);
668 RECORD(DECL_OBJC_IMPLEMENTATION);
669 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
670 RECORD(DECL_OBJC_PROPERTY);
671 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000672 RECORD(DECL_FIELD);
673 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000674 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000675 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000676 RECORD(DECL_FILE_SCOPE_ASM);
677 RECORD(DECL_BLOCK);
678 RECORD(DECL_CONTEXT_LEXICAL);
679 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000680 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000681 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000682#undef RECORD
683#undef BLOCK
684 Stream.ExitBlock();
685}
686
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000687/// \brief Adjusts the given filename to only write out the portion of the
688/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000689///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000690/// \param Filename the file name to adjust.
691///
692/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
693/// the returned filename will be adjusted by this system root.
694///
695/// \returns either the original filename (if it needs no adjustment) or the
696/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000697static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000698adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
699 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000700
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000701 if (!isysroot)
702 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000703
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000704 // Verify that the filename and the system root have the same prefix.
705 unsigned Pos = 0;
706 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
707 if (Filename[Pos] != isysroot[Pos])
708 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000709
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000710 // We hit the end of the filename before we hit the end of the system root.
711 if (!Filename[Pos])
712 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000713
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000714 // If the file name has a '/' at the current position, skip over the '/'.
715 // We distinguish sysroot-based includes from absolute includes by the
716 // absence of '/' at the beginning of sysroot-based includes.
717 if (Filename[Pos] == '/')
718 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000719
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000720 return Filename + Pos;
721}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000722
Douglas Gregor7b71e632009-04-27 22:23:34 +0000723/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000724void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000725 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000726
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000727 // Metadata
728 const TargetInfo &Target = Context.Target;
729 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
730 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
731 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
732 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
733 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
734 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
735 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
736 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
737 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000738
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000739 RecordData Record;
740 Record.push_back(pch::METADATA);
741 Record.push_back(pch::VERSION_MAJOR);
742 Record.push_back(pch::VERSION_MINOR);
743 Record.push_back(CLANG_VERSION_MAJOR);
744 Record.push_back(CLANG_VERSION_MINOR);
745 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000746 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000747 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000748
Douglas Gregor45fe0362009-05-12 01:31:05 +0000749 // Original file name
750 SourceManager &SM = Context.getSourceManager();
751 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
752 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
753 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
754 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
755 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
756
757 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000758
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000759 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000760
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000761 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000762 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000763 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000764 RecordData Record;
765 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000766 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000767 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000768
Ted Kremenek18e066f2010-01-22 22:12:47 +0000769 // Repository branch/version information.
770 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
771 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
772 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
773 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000774 Record.clear();
Ted Kremenek17437132010-01-22 20:59:36 +0000775 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000776 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
777 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000778}
779
780/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000781void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
782 RecordData Record;
783 Record.push_back(LangOpts.Trigraphs);
784 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
785 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
786 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
787 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000788 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000789 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
790 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
791 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
792 Record.push_back(LangOpts.C99); // C99 Support
793 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
794 Record.push_back(LangOpts.CPlusPlus); // C++ Support
795 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000796 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000797
Douglas Gregor55abb232009-04-10 20:39:37 +0000798 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
799 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000800 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000801 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000802 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000803 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000804 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000805
Douglas Gregor55abb232009-04-10 20:39:37 +0000806 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000807 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
808 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000809 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000810 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000811 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000812
813 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
814 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
815 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
816
Chris Lattner258172e2009-04-27 07:35:58 +0000817 // Whether static initializers are protected by locks.
818 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000819 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000820 Record.push_back(LangOpts.Blocks); // block extension to C
821 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
822 // they are unused.
823 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
824 // (modulo the platform support).
825
826 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
827 // signed integer arithmetic overflows.
828
829 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
830 // may be ripped out at any time.
831
832 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000833 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000834 // defined.
835 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
836 // opposed to __DYNAMIC__).
837 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
838
839 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
840 // used (instead of C99 semantics).
841 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000842 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
843 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000844 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
845 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000846 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000847 Record.push_back(LangOpts.getGCMode());
848 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000849 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000850 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000851 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000852 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000853 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000854 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000855}
856
Douglas Gregora7f71a92009-04-10 03:52:48 +0000857//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000858// stat cache Serialization
859//===----------------------------------------------------------------------===//
860
861namespace {
862// Trait used for the on-disk hash table of stat cache results.
Benjamin Kramer16634c22009-11-28 10:07:24 +0000863class PCHStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000864public:
865 typedef const char * key_type;
866 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000867
Douglas Gregorc5046832009-04-27 18:38:38 +0000868 typedef std::pair<int, struct stat> data_type;
869 typedef const data_type& data_type_ref;
870
871 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000872 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000873 }
Mike Stump11289f42009-09-09 15:08:12 +0000874
875 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000876 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
877 data_type_ref Data) {
878 unsigned StrLen = strlen(path);
879 clang::io::Emit16(Out, StrLen);
880 unsigned DataLen = 1; // result value
881 if (Data.first == 0)
882 DataLen += 4 + 4 + 2 + 8 + 8;
883 clang::io::Emit8(Out, DataLen);
884 return std::make_pair(StrLen + 1, DataLen);
885 }
Mike Stump11289f42009-09-09 15:08:12 +0000886
Douglas Gregorc5046832009-04-27 18:38:38 +0000887 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
888 Out.write(path, KeyLen);
889 }
Mike Stump11289f42009-09-09 15:08:12 +0000890
Douglas Gregorc5046832009-04-27 18:38:38 +0000891 void EmitData(llvm::raw_ostream& Out, key_type_ref,
892 data_type_ref Data, unsigned DataLen) {
893 using namespace clang::io;
894 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000895
Douglas Gregorc5046832009-04-27 18:38:38 +0000896 // Result of stat()
897 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000898
Douglas Gregorc5046832009-04-27 18:38:38 +0000899 if (Data.first == 0) {
900 Emit32(Out, (uint32_t) Data.second.st_ino);
901 Emit32(Out, (uint32_t) Data.second.st_dev);
902 Emit16(Out, (uint16_t) Data.second.st_mode);
903 Emit64(Out, (uint64_t) Data.second.st_mtime);
904 Emit64(Out, (uint64_t) Data.second.st_size);
905 }
906
907 assert(Out.tell() - Start == DataLen && "Wrong data length");
908 }
909};
910} // end anonymous namespace
911
912/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000913void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
914 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000915 // Build the on-disk hash table containing information about every
916 // stat() call.
917 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
918 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000919 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000920 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000921 Stat != StatEnd; ++Stat, ++NumStatEntries) {
922 const char *Filename = Stat->first();
923 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
924 Generator.insert(Filename, Stat->second);
925 }
Mike Stump11289f42009-09-09 15:08:12 +0000926
Douglas Gregorc5046832009-04-27 18:38:38 +0000927 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000928 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000929 uint32_t BucketOffset;
930 {
931 llvm::raw_svector_ostream Out(StatCacheData);
932 // Make sure that no bucket is at offset 0
933 clang::io::Emit32(Out, 0);
934 BucketOffset = Generator.Emit(Out);
935 }
936
937 // Create a blob abbreviation
938 using namespace llvm;
939 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
940 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
944 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
945
946 // Write the stat cache
947 RecordData Record;
948 Record.push_back(pch::STAT_CACHE);
949 Record.push_back(BucketOffset);
950 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000951 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000952}
953
954//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000955// Source Manager Serialization
956//===----------------------------------------------------------------------===//
957
958/// \brief Create an abbreviation for the SLocEntry that refers to a
959/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000960static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000961 using namespace llvm;
962 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
963 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +0000968 // FileEntry fields.
969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000971 // HeaderFileInfo fields.
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +0000976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000977 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000978}
979
980/// \brief Create an abbreviation for the SLocEntry that refers to a
981/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000982static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000983 using namespace llvm;
984 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
985 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
986 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
987 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000991 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000992}
993
994/// \brief Create an abbreviation for the SLocEntry that refers to a
995/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000996static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000997 using namespace llvm;
998 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
999 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001001 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001002}
1003
1004/// \brief Create an abbreviation for the SLocEntry that refers to an
1005/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001006static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001007 using namespace llvm;
1008 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1009 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001015 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001016}
1017
1018/// \brief Writes the block containing the serialized form of the
1019/// source manager.
1020///
1021/// TODO: We should probably use an on-disk hash table (stored in a
1022/// blob), indexed based on the file name, so that we only create
1023/// entries for files that we actually need. In the common case (no
1024/// errors), we probably won't have to create file entries for any of
1025/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001026void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001027 const Preprocessor &PP,
1028 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001029 RecordData Record;
1030
Chris Lattner0910e3b2009-04-10 17:16:57 +00001031 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001032 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001033
1034 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001035 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1036 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1037 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1038 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001039
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001040 // Write the line table.
1041 if (SourceMgr.hasLineTable()) {
1042 LineTableInfo &LineTable = SourceMgr.getLineTable();
1043
1044 // Emit the file names
1045 Record.push_back(LineTable.getNumFilenames());
1046 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1047 // Emit the file name
1048 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001049 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001050 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1051 Record.push_back(FilenameLen);
1052 if (FilenameLen)
1053 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1054 }
Mike Stump11289f42009-09-09 15:08:12 +00001055
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001056 // Emit the line entries
1057 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1058 L != LEnd; ++L) {
1059 // Emit the file ID
1060 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001061
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001062 // Emit the line entries
1063 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001064 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001065 LEEnd = L->second.end();
1066 LE != LEEnd; ++LE) {
1067 Record.push_back(LE->FileOffset);
1068 Record.push_back(LE->LineNo);
1069 Record.push_back(LE->FilenameID);
1070 Record.push_back((unsigned)LE->FileKind);
1071 Record.push_back(LE->IncludeOffset);
1072 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001073 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +00001074 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001075 }
1076
Douglas Gregor258ae542009-04-27 06:38:32 +00001077 // Write out the source location entry table. We skip the first
1078 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001079 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001080 RecordData PreloadSLocs;
1081 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001082 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1083 // Get this source location entry.
1084 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001085
Douglas Gregor258ae542009-04-27 06:38:32 +00001086 // Record the offset of this source-location entry.
1087 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1088
1089 // Figure out which record code to use.
1090 unsigned Code;
1091 if (SLoc->isFile()) {
1092 if (SLoc->getFile().getContentCache()->Entry)
1093 Code = pch::SM_SLOC_FILE_ENTRY;
1094 else
1095 Code = pch::SM_SLOC_BUFFER_ENTRY;
1096 } else
1097 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1098 Record.clear();
1099 Record.push_back(Code);
1100
1101 Record.push_back(SLoc->getOffset());
1102 if (SLoc->isFile()) {
1103 const SrcMgr::FileInfo &File = SLoc->getFile();
1104 Record.push_back(File.getIncludeLoc().getRawEncoding());
1105 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1106 Record.push_back(File.hasLineDirectives());
1107
1108 const SrcMgr::ContentCache *Content = File.getContentCache();
1109 if (Content->Entry) {
1110 // The source location entry is a file. The blob associated
1111 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001112
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001113 // Emit size/modification time for this file.
1114 Record.push_back(Content->Entry->getSize());
1115 Record.push_back(Content->Entry->getModificationTime());
1116
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001117 // Emit header-search information associated with this file.
1118 HeaderFileInfo HFI;
1119 HeaderSearch &HS = PP.getHeaderSearchInfo();
1120 if (Content->Entry->getUID() < HS.header_file_size())
1121 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1122 Record.push_back(HFI.isImport);
1123 Record.push_back(HFI.DirInfo);
1124 Record.push_back(HFI.NumIncludes);
1125 AddIdentifierRef(HFI.ControllingMacro, Record);
1126
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001127 // Turn the file name into an absolute path, if it isn't already.
1128 const char *Filename = Content->Entry->getName();
1129 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001130 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001131 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001132
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001133 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001134 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001135
1136 // FIXME: For now, preload all file source locations, so that
1137 // we get the appropriate File entries in the reader. This is
1138 // a temporary measure.
1139 PreloadSLocs.push_back(SLocEntryOffsets.size());
1140 } else {
1141 // The source location entry is a buffer. The blob associated
1142 // with this entry contains the contents of the buffer.
1143
1144 // We add one to the size so that we capture the trailing NULL
1145 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1146 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001147 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001148 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001149 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001150 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1151 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001152 Record.clear();
1153 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1154 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001155 llvm::StringRef(Buffer->getBufferStart(),
1156 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001157
1158 if (strcmp(Name, "<built-in>") == 0)
1159 PreloadSLocs.push_back(SLocEntryOffsets.size());
1160 }
1161 } else {
1162 // The source location entry is an instantiation.
1163 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1164 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1165 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1166 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1167
1168 // Compute the token length for this macro expansion.
1169 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001170 if (I + 1 != N)
1171 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001172 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1173 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1174 }
1175 }
1176
Douglas Gregor8f45df52009-04-16 22:23:12 +00001177 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001178
1179 if (SLocEntryOffsets.empty())
1180 return;
1181
1182 // Write the source-location offsets table into the PCH block. This
1183 // table is used for lazily loading source-location information.
1184 using namespace llvm;
1185 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1186 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1190 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001191
Douglas Gregor258ae542009-04-27 06:38:32 +00001192 Record.clear();
1193 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1194 Record.push_back(SLocEntryOffsets.size());
1195 Record.push_back(SourceMgr.getNextOffset());
1196 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001197 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001198 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001199
1200 // Write the source location entry preloads array, telling the PCH
1201 // reader which source locations entries it should load eagerly.
1202 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001203}
1204
Douglas Gregorc5046832009-04-27 18:38:38 +00001205//===----------------------------------------------------------------------===//
1206// Preprocessor Serialization
1207//===----------------------------------------------------------------------===//
1208
Chris Lattnereeffaef2009-04-10 17:15:23 +00001209/// \brief Writes the block containing the serialized form of the
1210/// preprocessor.
1211///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001212void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001213 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001214
Chris Lattner0af3ba12009-04-13 01:29:17 +00001215 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1216 if (PP.getCounterValue() != 0) {
1217 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001218 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001219 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001220 }
1221
1222 // Enter the preprocessor block.
1223 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001224
Douglas Gregoreda6a892009-04-26 00:07:37 +00001225 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1226 // FIXME: use diagnostics subsystem for localization etc.
1227 if (PP.SawDateOrTime())
1228 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001229
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001230 // Loop over all the macro definitions that are live at the end of the file,
1231 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001232 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001233 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1234 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001235 // FIXME: This emits macros in hash table order, we should do it in a stable
1236 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001237 MacroInfo *MI = I->second;
1238
1239 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1240 // been redefined by the header (in which case they are not isBuiltinMacro).
1241 if (MI->isBuiltinMacro())
1242 continue;
1243
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001244 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001245 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001246 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1247 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001248
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001249 unsigned Code;
1250 if (MI->isObjectLike()) {
1251 Code = pch::PP_MACRO_OBJECT_LIKE;
1252 } else {
1253 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001254
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001255 Record.push_back(MI->isC99Varargs());
1256 Record.push_back(MI->isGNUVarargs());
1257 Record.push_back(MI->getNumArgs());
1258 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1259 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001260 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001261 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001262
1263 // If we have a detailed preprocessing record, record the macro definition
1264 // ID that corresponds to this macro.
1265 if (PPRec)
1266 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1267
Douglas Gregor8f45df52009-04-16 22:23:12 +00001268 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001269 Record.clear();
1270
Chris Lattner2199f5b2009-04-10 18:08:30 +00001271 // Emit the tokens array.
1272 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1273 // Note that we know that the preprocessor does not have any annotation
1274 // tokens in it because they are created by the parser, and thus can't be
1275 // in a macro definition.
1276 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001277
Chris Lattner2199f5b2009-04-10 18:08:30 +00001278 Record.push_back(Tok.getLocation().getRawEncoding());
1279 Record.push_back(Tok.getLength());
1280
Chris Lattner2199f5b2009-04-10 18:08:30 +00001281 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1282 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001283 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001284
Chris Lattner2199f5b2009-04-10 18:08:30 +00001285 // FIXME: Should translate token kind to a stable encoding.
1286 Record.push_back(Tok.getKind());
1287 // FIXME: Should translate token flags to a stable encoding.
1288 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001289
Douglas Gregor8f45df52009-04-16 22:23:12 +00001290 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001291 Record.clear();
1292 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001293 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001294 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001295
1296 // If the preprocessor has a preprocessing record, emit it.
1297 unsigned NumPreprocessingRecords = 0;
1298 if (PPRec) {
1299 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1300 E != EEnd; ++E) {
1301 Record.clear();
1302
1303 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1304 Record.push_back(NumPreprocessingRecords++);
1305 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1306 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1307 AddIdentifierRef(MI->getName(), Record);
1308 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1309 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1310 continue;
1311 }
1312
1313 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1314 // Record this macro definition's location.
1315 pch::IdentID ID = getMacroDefinitionID(MD);
1316 if (ID != MacroDefinitionOffsets.size()) {
1317 if (ID > MacroDefinitionOffsets.size())
1318 MacroDefinitionOffsets.resize(ID + 1);
1319
1320 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1321 } else
1322 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1323
1324 Record.push_back(NumPreprocessingRecords++);
1325 Record.push_back(ID);
1326 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1327 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1328 AddIdentifierRef(MD->getName(), Record);
1329 AddSourceLocation(MD->getLocation(), Record);
1330 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1331 continue;
1332 }
1333 }
1334 }
1335
Douglas Gregor8f45df52009-04-16 22:23:12 +00001336 Stream.ExitBlock();
Douglas Gregoraae92242010-03-19 21:51:54 +00001337
1338 // Write the offsets table for the preprocessing record.
1339 if (NumPreprocessingRecords > 0) {
1340 // Write the offsets table for identifier IDs.
1341 using namespace llvm;
1342 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1343 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1344 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1347 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1348
1349 Record.clear();
1350 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1351 Record.push_back(NumPreprocessingRecords);
1352 Record.push_back(MacroDefinitionOffsets.size());
1353 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1354 (const char *)&MacroDefinitionOffsets.front(),
1355 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1356 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001357}
1358
Douglas Gregorc5046832009-04-27 18:38:38 +00001359//===----------------------------------------------------------------------===//
1360// Type Serialization
1361//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001362
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001363/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001364void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001365 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001366 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001367 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001368
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001369 // Record the offset for this type.
1370 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001371 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001372 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1373 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001374 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001375 }
1376
1377 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001379 // Emit the type's representation.
1380 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001381
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001382 if (T.hasLocalNonFastQualifiers()) {
1383 Qualifiers Qs = T.getLocalQualifiers();
1384 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001385 Record.push_back(Qs.getAsOpaqueValue());
1386 W.Code = pch::TYPE_EXT_QUAL;
1387 } else {
1388 switch (T->getTypeClass()) {
1389 // For all of the concrete, non-dependent types, call the
1390 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001391#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001392 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001393#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001394#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001395 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001396 }
1397
1398 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001399 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001400
1401 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001402 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001403}
1404
Douglas Gregorc5046832009-04-27 18:38:38 +00001405//===----------------------------------------------------------------------===//
1406// Declaration Serialization
1407//===----------------------------------------------------------------------===//
1408
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001409/// \brief Write the block containing all of the declaration IDs
1410/// lexically declared within the given DeclContext.
1411///
1412/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1413/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001414uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001415 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001416 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001417 return 0;
1418
Douglas Gregor8f45df52009-04-16 22:23:12 +00001419 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001420 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001421 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1422 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001423 AddDeclRef(*D, Record);
1424
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001425 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001426 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001427 return Offset;
1428}
1429
1430/// \brief Write the block containing all of the declaration IDs
1431/// visible from the given DeclContext.
1432///
1433/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1434/// bistream, or 0 if no block was written.
1435uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1436 DeclContext *DC) {
1437 if (DC->getPrimaryContext() != DC)
1438 return 0;
1439
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001440 // Since there is no name lookup into functions or methods, and we
1441 // perform name lookup for the translation unit via the
1442 // IdentifierInfo chains, don't bother to build a
1443 // visible-declarations table for these entities.
1444 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001445 return 0;
1446
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001447 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001448 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001449
1450 // Serialize the contents of the mapping used for lookup. Note that,
1451 // although we have two very different code paths, the serialized
1452 // representation is the same for both cases: a declaration name,
1453 // followed by a size, followed by references to the visible
1454 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001455 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001456 RecordData Record;
1457 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001458 if (!Map)
1459 return 0;
1460
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001461 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1462 D != DEnd; ++D) {
1463 AddDeclarationName(D->first, Record);
1464 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1465 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001466 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001467 AddDeclRef(*Result.first, Record);
1468 }
1469
1470 if (Record.size() == 0)
1471 return 0;
1472
Douglas Gregor8f45df52009-04-16 22:23:12 +00001473 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001474 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001475 return Offset;
1476}
1477
Douglas Gregorc5046832009-04-27 18:38:38 +00001478//===----------------------------------------------------------------------===//
1479// Global Method Pool and Selector Serialization
1480//===----------------------------------------------------------------------===//
1481
Douglas Gregore84a9da2009-04-20 20:36:09 +00001482namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001483// Trait used for the on-disk hash table used in the method pool.
Benjamin Kramer16634c22009-11-28 10:07:24 +00001484class PCHMethodPoolTrait {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001485 PCHWriter &Writer;
1486
1487public:
1488 typedef Selector key_type;
1489 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001490
Douglas Gregorc78d3462009-04-24 21:10:55 +00001491 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1492 typedef const data_type& data_type_ref;
1493
1494 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001495
Douglas Gregorc78d3462009-04-24 21:10:55 +00001496 static unsigned ComputeHash(Selector Sel) {
1497 unsigned N = Sel.getNumArgs();
1498 if (N == 0)
1499 ++N;
1500 unsigned R = 5381;
1501 for (unsigned I = 0; I != N; ++I)
1502 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001503 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001504 return R;
1505 }
Mike Stump11289f42009-09-09 15:08:12 +00001506
1507 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001508 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1509 data_type_ref Methods) {
1510 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1511 clang::io::Emit16(Out, KeyLen);
1512 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001513 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001514 Method = Method->Next)
1515 if (Method->Method)
1516 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001517 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001518 Method = Method->Next)
1519 if (Method->Method)
1520 DataLen += 4;
1521 clang::io::Emit16(Out, DataLen);
1522 return std::make_pair(KeyLen, DataLen);
1523 }
Mike Stump11289f42009-09-09 15:08:12 +00001524
Douglas Gregor95c13f52009-04-25 17:48:32 +00001525 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001526 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001527 assert((Start >> 32) == 0 && "Selector key offset too large");
1528 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001529 unsigned N = Sel.getNumArgs();
1530 clang::io::Emit16(Out, N);
1531 if (N == 0)
1532 N = 1;
1533 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001534 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001535 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1536 }
Mike Stump11289f42009-09-09 15:08:12 +00001537
Douglas Gregorc78d3462009-04-24 21:10:55 +00001538 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001539 data_type_ref Methods, unsigned DataLen) {
1540 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001541 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001542 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001543 Method = Method->Next)
1544 if (Method->Method)
1545 ++NumInstanceMethods;
1546
1547 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001548 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001549 Method = Method->Next)
1550 if (Method->Method)
1551 ++NumFactoryMethods;
1552
1553 clang::io::Emit16(Out, NumInstanceMethods);
1554 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001555 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001556 Method = Method->Next)
1557 if (Method->Method)
1558 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001559 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001560 Method = Method->Next)
1561 if (Method->Method)
1562 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001563
1564 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001565 }
1566};
1567} // end anonymous namespace
1568
1569/// \brief Write the method pool into the PCH file.
1570///
1571/// The method pool contains both instance and factory methods, stored
1572/// in an on-disk hash table indexed by the selector.
1573void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1574 using namespace llvm;
1575
1576 // Create and write out the blob that contains the instance and
1577 // factor method pools.
1578 bool Empty = true;
1579 {
1580 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001581
Douglas Gregorc78d3462009-04-24 21:10:55 +00001582 // Create the on-disk hash table representation. Start by
1583 // iterating through the instance method pool.
1584 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001585 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001586 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001587 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001588 InstanceEnd = SemaRef.InstanceMethodPool.end();
1589 Instance != InstanceEnd; ++Instance) {
1590 // Check whether there is a factory method with the same
1591 // selector.
1592 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1593 = SemaRef.FactoryMethodPool.find(Instance->first);
1594
1595 if (Factory == SemaRef.FactoryMethodPool.end())
1596 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001597 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001598 ObjCMethodList()));
1599 else
1600 Generator.insert(Instance->first,
1601 std::make_pair(Instance->second, Factory->second));
1602
Douglas Gregor95c13f52009-04-25 17:48:32 +00001603 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001604 Empty = false;
1605 }
1606
1607 // Now iterate through the factory method pool, to pick up any
1608 // selectors that weren't already in the instance method pool.
1609 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001610 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001611 FactoryEnd = SemaRef.FactoryMethodPool.end();
1612 Factory != FactoryEnd; ++Factory) {
1613 // Check whether there is an instance method with the same
1614 // selector. If so, there is no work to do here.
1615 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1616 = SemaRef.InstanceMethodPool.find(Factory->first);
1617
Douglas Gregor95c13f52009-04-25 17:48:32 +00001618 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001619 Generator.insert(Factory->first,
1620 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001621 ++NumSelectorsInMethodPool;
1622 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001623
1624 Empty = false;
1625 }
1626
Douglas Gregor95c13f52009-04-25 17:48:32 +00001627 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001628 return;
1629
1630 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001631 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001632 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001633 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001634 {
1635 PCHMethodPoolTrait Trait(*this);
1636 llvm::raw_svector_ostream Out(MethodPool);
1637 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001638 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001639 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001640
1641 // For every selector that we have seen but which was not
1642 // written into the hash table, write the selector itself and
1643 // record it's offset.
1644 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1645 if (SelectorOffsets[I] == 0)
1646 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001647 }
1648
1649 // Create a blob abbreviation
1650 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1651 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1652 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001653 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1655 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1656
Douglas Gregor95c13f52009-04-25 17:48:32 +00001657 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001658 RecordData Record;
1659 Record.push_back(pch::METHOD_POOL);
1660 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001661 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001662 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001663
1664 // Create a blob abbreviation for the selector table offsets.
1665 Abbrev = new BitCodeAbbrev();
1666 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1667 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1668 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1669 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1670
1671 // Write the selector offsets table.
1672 Record.clear();
1673 Record.push_back(pch::SELECTOR_OFFSETS);
1674 Record.push_back(SelectorOffsets.size());
1675 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1676 (const char *)&SelectorOffsets.front(),
1677 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001678 }
1679}
1680
Douglas Gregorc5046832009-04-27 18:38:38 +00001681//===----------------------------------------------------------------------===//
1682// Identifier Table Serialization
1683//===----------------------------------------------------------------------===//
1684
Douglas Gregorc78d3462009-04-24 21:10:55 +00001685namespace {
Benjamin Kramer16634c22009-11-28 10:07:24 +00001686class PCHIdentifierTableTrait {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001687 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001688 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001689
Douglas Gregor1d583f22009-04-28 21:18:29 +00001690 /// \brief Determines whether this is an "interesting" identifier
1691 /// that needs a full IdentifierInfo structure written into the hash
1692 /// table.
1693 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1694 return II->isPoisoned() ||
1695 II->isExtensionToken() ||
1696 II->hasMacroDefinition() ||
1697 II->getObjCOrBuiltinID() ||
1698 II->getFETokenInfo<void>();
1699 }
1700
Douglas Gregore84a9da2009-04-20 20:36:09 +00001701public:
1702 typedef const IdentifierInfo* key_type;
1703 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001704
Douglas Gregore84a9da2009-04-20 20:36:09 +00001705 typedef pch::IdentID data_type;
1706 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001707
1708 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001709 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001710
1711 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001712 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001713 }
Mike Stump11289f42009-09-09 15:08:12 +00001714
1715 std::pair<unsigned,unsigned>
1716 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001717 pch::IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001718 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001719 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1720 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001721 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001722 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001723 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001724 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001725 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1726 DEnd = IdentifierResolver::end();
1727 D != DEnd; ++D)
1728 DataLen += sizeof(pch::DeclID);
1729 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001730 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001731 // We emit the key length after the data length so that every
1732 // string is preceded by a 16-bit length. This matches the PTH
1733 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001734 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001735 return std::make_pair(KeyLen, DataLen);
1736 }
Mike Stump11289f42009-09-09 15:08:12 +00001737
1738 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001739 unsigned KeyLen) {
1740 // Record the location of the key data. This is used when generating
1741 // the mapping from persistent IDs to strings.
1742 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001743 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001744 }
Mike Stump11289f42009-09-09 15:08:12 +00001745
1746 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001747 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001748 if (!isInterestingIdentifier(II)) {
1749 clang::io::Emit32(Out, ID << 1);
1750 return;
1751 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001752
Douglas Gregor1d583f22009-04-28 21:18:29 +00001753 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001754 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001755 bool hasMacroDefinition =
1756 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001757 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001758 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001759 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1760 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1761 Bits = (Bits << 1) | unsigned(II->isPoisoned());
1762 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001763 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001764
Douglas Gregorc3366a52009-04-21 23:56:24 +00001765 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001766 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001767
Douglas Gregora868bbd2009-04-21 22:25:48 +00001768 // Emit the declaration IDs in reverse order, because the
1769 // IdentifierResolver provides the declarations as they would be
1770 // visible (e.g., the function "stat" would come before the struct
1771 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1772 // adds declarations to the end of the list (so we need to see the
1773 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001774 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001775 IdentifierResolver::end());
1776 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1777 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001778 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001779 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001780 }
1781};
1782} // end anonymous namespace
1783
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001784/// \brief Write the identifier table into the PCH file.
1785///
1786/// The identifier table consists of a blob containing string data
1787/// (the actual identifiers themselves) and a separate "offsets" index
1788/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001789void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001790 using namespace llvm;
1791
1792 // Create and write out the blob that contains the identifier
1793 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001794 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001795 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001796
Douglas Gregore6648fb2009-04-28 20:33:11 +00001797 // Look for any identifiers that were named while processing the
1798 // headers, but are otherwise not needed. We add these to the hash
1799 // table to enable checking of the predefines buffer in the case
1800 // where the user adds new macro definitions when building the PCH
1801 // file.
1802 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1803 IDEnd = PP.getIdentifierTable().end();
1804 ID != IDEnd; ++ID)
1805 getIdentifierRef(ID->second);
1806
Douglas Gregore84a9da2009-04-20 20:36:09 +00001807 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001808 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001809 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1810 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1811 ID != IDEnd; ++ID) {
1812 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001813 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001814 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001815
Douglas Gregore84a9da2009-04-20 20:36:09 +00001816 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001817 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001818 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001819 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001820 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001821 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001822 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001823 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001824 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001825 }
1826
1827 // Create a blob abbreviation
1828 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1829 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001832 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001833
1834 // Write the identifier table
1835 RecordData Record;
1836 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001837 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001838 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001839 }
1840
1841 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001842 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1843 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1846 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1847
1848 RecordData Record;
1849 Record.push_back(pch::IDENTIFIER_OFFSET);
1850 Record.push_back(IdentifierOffsets.size());
1851 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1852 (const char *)&IdentifierOffsets.front(),
1853 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001854}
1855
Douglas Gregorc5046832009-04-27 18:38:38 +00001856//===----------------------------------------------------------------------===//
1857// General Serialization Routines
1858//===----------------------------------------------------------------------===//
1859
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001860/// \brief Write a record containing the given attributes.
1861void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1862 RecordData Record;
1863 for (; Attr; Attr = Attr->getNext()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001864 Record.push_back(Attr->getKind()); // FIXME: stable encoding, target attrs
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001865 Record.push_back(Attr->isInherited());
1866 switch (Attr->getKind()) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001867 default:
1868 assert(0 && "Does not support PCH writing for this attribute yet!");
1869 break;
Alexis Hunt344393e2010-06-16 23:43:53 +00001870 case attr::Alias:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001871 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1872 break;
1873
Alexis Hunt344393e2010-06-16 23:43:53 +00001874 case attr::AlignMac68k:
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00001875 break;
1876
Alexis Hunt344393e2010-06-16 23:43:53 +00001877 case attr::Aligned:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001878 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1879 break;
1880
Alexis Hunt344393e2010-06-16 23:43:53 +00001881 case attr::AlwaysInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001882 break;
Mike Stump11289f42009-09-09 15:08:12 +00001883
Alexis Hunt344393e2010-06-16 23:43:53 +00001884 case attr::AnalyzerNoReturn:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001885 break;
1886
Alexis Hunt344393e2010-06-16 23:43:53 +00001887 case attr::Annotate:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001888 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1889 break;
1890
Alexis Hunt344393e2010-06-16 23:43:53 +00001891 case attr::AsmLabel:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001892 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1893 break;
1894
Alexis Hunt344393e2010-06-16 23:43:53 +00001895 case attr::BaseCheck:
Alexis Hunt54a02542009-11-25 04:20:27 +00001896 break;
1897
Alexis Hunt344393e2010-06-16 23:43:53 +00001898 case attr::Blocks:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001899 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1900 break;
1901
Alexis Hunt344393e2010-06-16 23:43:53 +00001902 case attr::CDecl:
Eli Friedmane4310c82009-11-09 18:38:53 +00001903 break;
1904
Alexis Hunt344393e2010-06-16 23:43:53 +00001905 case attr::Cleanup:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001906 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1907 break;
1908
Alexis Hunt344393e2010-06-16 23:43:53 +00001909 case attr::Const:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001910 break;
1911
Alexis Hunt344393e2010-06-16 23:43:53 +00001912 case attr::Constructor:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001913 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1914 break;
1915
Alexis Hunt344393e2010-06-16 23:43:53 +00001916 case attr::DLLExport:
1917 case attr::DLLImport:
1918 case attr::Deprecated:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001919 break;
1920
Alexis Hunt344393e2010-06-16 23:43:53 +00001921 case attr::Destructor:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001922 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1923 break;
1924
Alexis Hunt344393e2010-06-16 23:43:53 +00001925 case attr::FastCall:
1926 case attr::Final:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001927 break;
1928
Alexis Hunt344393e2010-06-16 23:43:53 +00001929 case attr::Format: {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001930 const FormatAttr *Format = cast<FormatAttr>(Attr);
1931 AddString(Format->getType(), Record);
1932 Record.push_back(Format->getFormatIdx());
1933 Record.push_back(Format->getFirstArg());
1934 break;
1935 }
1936
Alexis Hunt344393e2010-06-16 23:43:53 +00001937 case attr::FormatArg: {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001938 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1939 Record.push_back(Format->getFormatIdx());
1940 break;
1941 }
1942
Alexis Hunt344393e2010-06-16 23:43:53 +00001943 case attr::Sentinel : {
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001944 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1945 Record.push_back(Sentinel->getSentinel());
1946 Record.push_back(Sentinel->getNullPos());
1947 break;
1948 }
Mike Stump11289f42009-09-09 15:08:12 +00001949
Alexis Hunt344393e2010-06-16 23:43:53 +00001950 case attr::GNUInline:
1951 case attr::Hiding:
1952 case attr::IBAction:
1953 case attr::IBOutlet:
1954 case attr::Malloc:
1955 case attr::NoDebug:
1956 case attr::NoInline:
1957 case attr::NoReturn:
1958 case attr::NoThrow:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001959 break;
1960
Alexis Hunt344393e2010-06-16 23:43:53 +00001961 case attr::IBOutletCollection: {
Ted Kremenek26bde772010-05-19 17:38:06 +00001962 const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr);
1963 AddDeclRef(ICA->getClass(), Record);
1964 break;
1965 }
1966
Alexis Hunt344393e2010-06-16 23:43:53 +00001967 case attr::NonNull: {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001968 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1969 Record.push_back(NonNull->size());
1970 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1971 break;
1972 }
1973
Alexis Hunt344393e2010-06-16 23:43:53 +00001974 case attr::CFReturnsNotRetained:
1975 case attr::CFReturnsRetained:
1976 case attr::NSReturnsNotRetained:
1977 case attr::NSReturnsRetained:
1978 case attr::ObjCException:
1979 case attr::ObjCNSObject:
1980 case attr::Overloadable:
1981 case attr::Override:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001982 break;
1983
Alexis Hunt344393e2010-06-16 23:43:53 +00001984 case attr::MaxFieldAlignment:
Daniel Dunbar40130442010-05-27 01:12:46 +00001985 Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001986 break;
1987
Alexis Hunt344393e2010-06-16 23:43:53 +00001988 case attr::Packed:
Anders Carlsson68e0b682009-08-08 18:23:56 +00001989 break;
Mike Stump11289f42009-09-09 15:08:12 +00001990
Alexis Hunt344393e2010-06-16 23:43:53 +00001991 case attr::Pure:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001992 break;
1993
Alexis Hunt344393e2010-06-16 23:43:53 +00001994 case attr::Regparm:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001995 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1996 break;
Mike Stump11289f42009-09-09 15:08:12 +00001997
Alexis Hunt344393e2010-06-16 23:43:53 +00001998 case attr::ReqdWorkGroupSize:
Nate Begemanf2758702009-06-26 06:32:41 +00001999 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
2000 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
2001 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
2002 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002003
Alexis Hunt344393e2010-06-16 23:43:53 +00002004 case attr::Section:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002005 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2006 break;
2007
Alexis Hunt344393e2010-06-16 23:43:53 +00002008 case attr::StdCall:
2009 case attr::TransparentUnion:
2010 case attr::Unavailable:
2011 case attr::Unused:
2012 case attr::Used:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002013 break;
2014
Alexis Hunt344393e2010-06-16 23:43:53 +00002015 case attr::Visibility:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002016 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00002017 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002018 break;
2019
Alexis Hunt344393e2010-06-16 23:43:53 +00002020 case attr::WarnUnusedResult:
2021 case attr::Weak:
2022 case attr::WeakRef:
2023 case attr::WeakImport:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002024 break;
2025 }
2026 }
2027
Douglas Gregor8f45df52009-04-16 22:23:12 +00002028 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002029}
2030
2031void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2032 Record.push_back(Str.size());
2033 Record.insert(Record.end(), Str.begin(), Str.end());
2034}
2035
Douglas Gregore84a9da2009-04-20 20:36:09 +00002036/// \brief Note that the identifier II occurs at the given offset
2037/// within the identifier table.
2038void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00002039 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002040}
2041
Douglas Gregor95c13f52009-04-25 17:48:32 +00002042/// \brief Note that the selector Sel occurs at the given offset
2043/// within the method pool/selector table.
2044void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2045 unsigned ID = SelectorIDs[Sel];
2046 assert(ID && "Unknown selector");
2047 SelectorOffsets[ID - 1] = Offset;
2048}
2049
Mike Stump11289f42009-09-09 15:08:12 +00002050PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
2051 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002052 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2053 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002054
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002055void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2056 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00002057 using namespace llvm;
2058
Douglas Gregor162dd022009-04-20 15:53:59 +00002059 ASTContext &Context = SemaRef.Context;
2060 Preprocessor &PP = SemaRef.PP;
2061
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002062 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002063 Stream.Emit((unsigned)'C', 8);
2064 Stream.Emit((unsigned)'P', 8);
2065 Stream.Emit((unsigned)'C', 8);
2066 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002067
Chris Lattner28fa4e62009-04-26 22:26:21 +00002068 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002069
2070 // The translation unit is the first declaration we'll emit.
2071 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002072 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002073
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002074 // Make sure that we emit IdentifierInfos (and any attached
2075 // declarations) for builtins.
2076 {
2077 IdentifierTable &Table = PP.getIdentifierTable();
2078 llvm::SmallVector<const char *, 32> BuiltinNames;
2079 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2080 Context.getLangOptions().NoBuiltin);
2081 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2082 getIdentifierRef(&Table.get(BuiltinNames[I]));
2083 }
2084
Chris Lattner0c797362009-09-08 18:19:27 +00002085 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002086 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002087 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002088 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002089 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2090 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002091 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002092
Tanya Lattner90073802010-02-12 00:07:30 +00002093 // Build a record containing all of the static unused functions in this file.
2094 RecordData UnusedStaticFuncs;
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002095 for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
Tanya Lattner90073802010-02-12 00:07:30 +00002096 AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002097
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002098 // Build a record containing all of the locally-scoped external
2099 // declarations in this header file. Generally, this record will be
2100 // empty.
2101 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00002102 // FIXME: This is filling in the PCH file in densemap order which is
2103 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002104 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002105 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2106 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2107 TD != TDEnd; ++TD)
2108 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2109
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002110 // Build a record containing all of the ext_vector declarations.
2111 RecordData ExtVectorDecls;
2112 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2113 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2114
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002115 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002116 RecordData Record;
Douglas Gregoraae92242010-03-19 21:51:54 +00002117 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002118 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00002119 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002120 if (StatCalls && !isysroot)
2121 WriteStatCache(*StatCalls, isysroot);
2122 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002123 // Write the record of special types.
2124 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002125
Steve Naroffc277ad12009-07-18 15:33:26 +00002126 AddTypeRef(Context.getBuiltinVaListType(), Record);
2127 AddTypeRef(Context.getObjCIdType(), Record);
2128 AddTypeRef(Context.getObjCSelType(), Record);
2129 AddTypeRef(Context.getObjCProtoType(), Record);
2130 AddTypeRef(Context.getObjCClassType(), Record);
2131 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2132 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2133 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002134 AddTypeRef(Context.getjmp_bufType(), Record);
2135 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002136 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2137 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002138 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002139 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002140 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2141 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00002142 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002143
Douglas Gregor1970d882009-04-26 03:49:13 +00002144 // Keep writing types and declarations until all types and
2145 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002146 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2147 WriteDeclsBlockAbbrevs();
2148 while (!DeclTypesToEmit.empty()) {
2149 DeclOrType DOT = DeclTypesToEmit.front();
2150 DeclTypesToEmit.pop();
2151 if (DOT.isType())
2152 WriteType(DOT.getType());
2153 else
2154 WriteDecl(Context, DOT.getDecl());
2155 }
2156 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002157
Douglas Gregor45053152009-10-17 17:25:45 +00002158 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002159 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002160 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002161
2162 // Write the type offsets array
2163 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2164 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2167 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2168 Record.clear();
2169 Record.push_back(pch::TYPE_OFFSET);
2170 Record.push_back(TypeOffsets.size());
2171 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002172 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002173 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002174
Douglas Gregor745ed142009-04-25 18:35:21 +00002175 // Write the declaration offsets array
2176 Abbrev = new BitCodeAbbrev();
2177 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2180 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2181 Record.clear();
2182 Record.push_back(pch::DECL_OFFSET);
2183 Record.push_back(DeclOffsets.size());
2184 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002185 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002186 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002187
Douglas Gregord4df8652009-04-22 22:02:47 +00002188 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002189 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002190 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002191
2192 // Write the record containing tentative definitions.
2193 if (!TentativeDefinitions.empty())
2194 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002195
Tanya Lattner90073802010-02-12 00:07:30 +00002196 // Write the record containing unused static functions.
2197 if (!UnusedStaticFuncs.empty())
2198 Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002199
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002200 // Write the record containing locally-scoped external definitions.
2201 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002202 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002203 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002204
2205 // Write the record containing ext_vector type names.
2206 if (!ExtVectorDecls.empty())
2207 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002208
Douglas Gregor08f01292009-04-17 22:13:46 +00002209 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002210 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002211 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002212 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002213 Record.push_back(NumLexicalDeclContexts);
2214 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002215 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002216 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002217}
2218
2219void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2220 Record.push_back(Loc.getRawEncoding());
2221}
2222
Chris Lattnerca025db2010-05-07 21:43:38 +00002223void PCHWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2224 AddSourceLocation(Range.getBegin(), Record);
2225 AddSourceLocation(Range.getEnd(), Record);
2226}
2227
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002228void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2229 Record.push_back(Value.getBitWidth());
2230 unsigned N = Value.getNumWords();
2231 const uint64_t* Words = Value.getRawData();
2232 for (unsigned I = 0; I != N; ++I)
2233 Record.push_back(Words[I]);
2234}
2235
Douglas Gregor1daeb692009-04-13 18:14:40 +00002236void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2237 Record.push_back(Value.isUnsigned());
2238 AddAPInt(Value, Record);
2239}
2240
Douglas Gregore0a3a512009-04-14 21:55:33 +00002241void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2242 AddAPInt(Value.bitcastToAPInt(), Record);
2243}
2244
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002245void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002246 Record.push_back(getIdentifierRef(II));
2247}
2248
2249pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2250 if (II == 0)
2251 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002252
2253 pch::IdentID &ID = IdentifierIDs[II];
2254 if (ID == 0)
2255 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002256 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002257}
2258
Douglas Gregoraae92242010-03-19 21:51:54 +00002259pch::IdentID PCHWriter::getMacroDefinitionID(MacroDefinition *MD) {
2260 if (MD == 0)
2261 return 0;
2262
2263 pch::IdentID &ID = MacroDefinitions[MD];
2264 if (ID == 0)
2265 ID = MacroDefinitions.size();
2266 return ID;
2267}
2268
Steve Naroff2ddea052009-04-23 10:39:46 +00002269void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2270 if (SelRef.getAsOpaquePtr() == 0) {
2271 Record.push_back(0);
2272 return;
2273 }
2274
2275 pch::SelectorID &SID = SelectorIDs[SelRef];
2276 if (SID == 0) {
2277 SID = SelectorIDs.size();
2278 SelVector.push_back(SelRef);
2279 }
2280 Record.push_back(SID);
2281}
2282
Chris Lattnercba86142010-05-10 00:25:06 +00002283void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2284 AddDeclRef(Temp->getDestructor(), Record);
2285}
2286
John McCall0ad16662009-10-29 08:12:44 +00002287void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2288 RecordData &Record) {
2289 switch (Arg.getArgument().getKind()) {
2290 case TemplateArgument::Expression:
2291 AddStmt(Arg.getLocInfo().getAsExpr());
2292 break;
2293 case TemplateArgument::Type:
John McCallbcd03502009-12-07 02:54:59 +00002294 AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002295 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002296 case TemplateArgument::Template:
2297 Record.push_back(
2298 Arg.getTemplateQualifierRange().getBegin().getRawEncoding());
2299 Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding());
2300 Record.push_back(Arg.getTemplateNameLoc().getRawEncoding());
2301 break;
John McCall0ad16662009-10-29 08:12:44 +00002302 case TemplateArgument::Null:
2303 case TemplateArgument::Integral:
2304 case TemplateArgument::Declaration:
2305 case TemplateArgument::Pack:
2306 break;
2307 }
2308}
2309
John McCallbcd03502009-12-07 02:54:59 +00002310void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2311 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002312 AddTypeRef(QualType(), Record);
2313 return;
2314 }
2315
John McCallbcd03502009-12-07 02:54:59 +00002316 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002317 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002318 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002319 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002320}
2321
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002322void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2323 if (T.isNull()) {
2324 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2325 return;
2326 }
2327
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002328 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00002329 T.removeFastQualifiers();
2330
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002331 if (T.hasLocalNonFastQualifiers()) {
John McCall8ccfcb52009-09-24 19:53:00 +00002332 pch::TypeID &ID = TypeIDs[T];
2333 if (ID == 0) {
2334 // We haven't seen these qualifiers applied to this type before.
2335 // Assign it a new ID. This is the only time we enqueue a
2336 // qualified type, and it has no CV qualifiers.
2337 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002338 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002339 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002340
John McCall8ccfcb52009-09-24 19:53:00 +00002341 // Encode the type qualifiers in the type reference.
2342 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2343 return;
2344 }
2345
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002346 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002347
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002348 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002349 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002350 switch (BT->getKind()) {
2351 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2352 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2353 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2354 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2355 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2356 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2357 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2358 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002359 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002360 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2361 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2362 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2363 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2364 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2365 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2366 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002367 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002368 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2369 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2370 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002371 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002372 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2373 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002374 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2375 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002376 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2377 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00002378 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002379 case BuiltinType::UndeducedAuto:
2380 assert(0 && "Should not see undeduced auto here");
2381 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002382 }
2383
John McCall8ccfcb52009-09-24 19:53:00 +00002384 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002385 return;
2386 }
2387
John McCall8ccfcb52009-09-24 19:53:00 +00002388 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002389 if (ID == 0) {
2390 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002391 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002392 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002393 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002394 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002395
2396 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002397 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002398}
2399
2400void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2401 if (D == 0) {
2402 Record.push_back(0);
2403 return;
2404 }
2405
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002406 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002407 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002408 // We haven't seen this declaration before. Give it a new ID and
2409 // enqueue it in the list of declarations to emit.
2410 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002411 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002412 }
2413
2414 Record.push_back(ID);
2415}
2416
Douglas Gregore84a9da2009-04-20 20:36:09 +00002417pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2418 if (D == 0)
2419 return 0;
2420
2421 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2422 return DeclIDs[D];
2423}
2424
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002425void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002426 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002427 Record.push_back(Name.getNameKind());
2428 switch (Name.getNameKind()) {
2429 case DeclarationName::Identifier:
2430 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2431 break;
2432
2433 case DeclarationName::ObjCZeroArgSelector:
2434 case DeclarationName::ObjCOneArgSelector:
2435 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002436 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002437 break;
2438
2439 case DeclarationName::CXXConstructorName:
2440 case DeclarationName::CXXDestructorName:
2441 case DeclarationName::CXXConversionFunctionName:
2442 AddTypeRef(Name.getCXXNameType(), Record);
2443 break;
2444
2445 case DeclarationName::CXXOperatorName:
2446 Record.push_back(Name.getCXXOverloadedOperator());
2447 break;
2448
Alexis Hunt3d221f22009-11-29 07:34:05 +00002449 case DeclarationName::CXXLiteralOperatorName:
2450 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2451 break;
2452
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002453 case DeclarationName::CXXUsingDirective:
2454 // No extra data to emit
2455 break;
2456 }
2457}
Chris Lattnerca025db2010-05-07 21:43:38 +00002458
2459void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
2460 RecordData &Record) {
2461 // Nested name specifiers usually aren't too long. I think that 8 would
2462 // typically accomodate the vast majority.
2463 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2464
2465 // Push each of the NNS's onto a stack for serialization in reverse order.
2466 while (NNS) {
2467 NestedNames.push_back(NNS);
2468 NNS = NNS->getPrefix();
2469 }
2470
2471 Record.push_back(NestedNames.size());
2472 while(!NestedNames.empty()) {
2473 NNS = NestedNames.pop_back_val();
2474 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2475 Record.push_back(Kind);
2476 switch (Kind) {
2477 case NestedNameSpecifier::Identifier:
2478 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2479 break;
2480
2481 case NestedNameSpecifier::Namespace:
2482 AddDeclRef(NNS->getAsNamespace(), Record);
2483 break;
2484
2485 case NestedNameSpecifier::TypeSpec:
2486 case NestedNameSpecifier::TypeSpecWithTemplate:
2487 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2488 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2489 break;
2490
2491 case NestedNameSpecifier::Global:
2492 // Don't need to write an associated value.
2493 break;
2494 }
2495 }
2496}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002497
2498void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
2499 TemplateName::NameKind Kind = Name.getKind();
2500 Record.push_back(Kind);
2501 switch (Kind) {
2502 case TemplateName::Template:
2503 AddDeclRef(Name.getAsTemplateDecl(), Record);
2504 break;
2505
2506 case TemplateName::OverloadedTemplate: {
2507 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2508 Record.push_back(OvT->size());
2509 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2510 I != E; ++I)
2511 AddDeclRef(*I, Record);
2512 break;
2513 }
2514
2515 case TemplateName::QualifiedTemplate: {
2516 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2517 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2518 Record.push_back(QualT->hasTemplateKeyword());
2519 AddDeclRef(QualT->getTemplateDecl(), Record);
2520 break;
2521 }
2522
2523 case TemplateName::DependentTemplate: {
2524 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2525 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2526 Record.push_back(DepT->isIdentifier());
2527 if (DepT->isIdentifier())
2528 AddIdentifierRef(DepT->getIdentifier(), Record);
2529 else
2530 Record.push_back(DepT->getOperator());
2531 break;
2532 }
2533 }
2534}
2535
2536void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg,
2537 RecordData &Record) {
2538 Record.push_back(Arg.getKind());
2539 switch (Arg.getKind()) {
2540 case TemplateArgument::Null:
2541 break;
2542 case TemplateArgument::Type:
2543 AddTypeRef(Arg.getAsType(), Record);
2544 break;
2545 case TemplateArgument::Declaration:
2546 AddDeclRef(Arg.getAsDecl(), Record);
2547 break;
2548 case TemplateArgument::Integral:
2549 AddAPSInt(*Arg.getAsIntegral(), Record);
2550 AddTypeRef(Arg.getIntegralType(), Record);
2551 break;
2552 case TemplateArgument::Template:
2553 AddTemplateName(Arg.getAsTemplate(), Record);
2554 break;
2555 case TemplateArgument::Expression:
2556 AddStmt(Arg.getAsExpr());
2557 break;
2558 case TemplateArgument::Pack:
2559 Record.push_back(Arg.pack_size());
2560 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2561 I != E; ++I)
2562 AddTemplateArgument(*I, Record);
2563 break;
2564 }
2565}