blob: 602f9c9efba35e785bb70e335fc6a900755a4181 [file] [log] [blame]
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000031#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamWriter.h"
36#include "llvm/Support/Compiler.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 {
47 class VISIBILITY_HIDDEN PCHTypeWriter {
48 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)
64#define DEPENDENT_TYPE(Class, Base)
65#include "clang/AST/TypeNodes.def"
66 };
67}
68
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
73void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
74 Record.push_back(T->getWidth());
75 Record.push_back(T->isSigned());
76 Code = pch::TYPE_FIXED_WIDTH_INT;
77}
78
79void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
80 Writer.AddTypeRef(T->getElementType(), Record);
81 Code = pch::TYPE_COMPLEX;
82}
83
84void PCHTypeWriter::VisitPointerType(const PointerType *T) {
85 Writer.AddTypeRef(T->getPointeeType(), Record);
86 Code = pch::TYPE_POINTER;
87}
88
89void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000090 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000091 Code = pch::TYPE_BLOCK_POINTER;
92}
93
94void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
95 Writer.AddTypeRef(T->getPointeeType(), Record);
96 Code = pch::TYPE_LVALUE_REFERENCE;
97}
98
99void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
100 Writer.AddTypeRef(T->getPointeeType(), Record);
101 Code = pch::TYPE_RVALUE_REFERENCE;
102}
103
104void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000105 Writer.AddTypeRef(T->getPointeeType(), Record);
106 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Code = pch::TYPE_MEMBER_POINTER;
108}
109
110void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
111 Writer.AddTypeRef(T->getElementType(), Record);
112 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000113 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000114}
115
116void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
117 VisitArrayType(T);
118 Writer.AddAPInt(T->getSize(), Record);
119 Code = pch::TYPE_CONSTANT_ARRAY;
120}
121
122void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
123 VisitArrayType(T);
124 Code = pch::TYPE_INCOMPLETE_ARRAY;
125}
126
127void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
128 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000129 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
130 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000131 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132 Code = pch::TYPE_VARIABLE_ARRAY;
133}
134
135void PCHTypeWriter::VisitVectorType(const VectorType *T) {
136 Writer.AddTypeRef(T->getElementType(), Record);
137 Record.push_back(T->getNumElements());
138 Code = pch::TYPE_VECTOR;
139}
140
141void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
142 VisitVectorType(T);
143 Code = pch::TYPE_EXT_VECTOR;
144}
145
146void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
147 Writer.AddTypeRef(T->getResultType(), Record);
148}
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
170void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
171 Writer.AddDeclRef(T->getDecl(), Record);
172 Code = pch::TYPE_TYPEDEF;
173}
174
175void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000176 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177 Code = pch::TYPE_TYPEOF_EXPR;
178}
179
180void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
181 Writer.AddTypeRef(T->getUnderlyingType(), Record);
182 Code = pch::TYPE_TYPEOF;
183}
184
Anders Carlsson81df7b82009-06-24 19:06:50 +0000185void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
186 Writer.AddStmt(T->getUnderlyingExpr());
187 Code = pch::TYPE_DECLTYPE;
188}
189
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190void PCHTypeWriter::VisitTagType(const TagType *T) {
191 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000192 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193 "Cannot serialize in the middle of a type definition");
194}
195
196void PCHTypeWriter::VisitRecordType(const RecordType *T) {
197 VisitTagType(T);
198 Code = pch::TYPE_RECORD;
199}
200
201void PCHTypeWriter::VisitEnumType(const EnumType *T) {
202 VisitTagType(T);
203 Code = pch::TYPE_ENUM;
204}
205
John McCallfcc33b02009-09-05 00:15:47 +0000206void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
207 Writer.AddTypeRef(T->getUnderlyingType(), Record);
208 Record.push_back(T->getTagKind());
209 Code = pch::TYPE_ELABORATED;
210}
211
Mike Stump11289f42009-09-09 15:08:12 +0000212void
John McCallcebee162009-10-18 09:09:24 +0000213PCHTypeWriter::VisitSubstTemplateTypeParmType(
214 const SubstTemplateTypeParmType *T) {
215 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
216 Writer.AddTypeRef(T->getReplacementType(), Record);
217 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
218}
219
220void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000221PCHTypeWriter::VisitTemplateSpecializationType(
222 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000223 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224 assert(false && "Cannot serialize template specialization types");
225}
226
227void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000228 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229 assert(false && "Cannot serialize qualified name types");
230}
231
232void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
233 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000234 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000235 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
236 E = T->qual_end(); I != E; ++I)
237 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000238 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239}
240
Steve Narofffb4330f2009-06-17 22:40:22 +0000241void
242PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000243 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000245 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000246 E = T->qual_end(); I != E; ++I)
247 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000248 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000249}
250
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +0000251void PCHTypeWriter::VisitObjCProtocolListType(const ObjCProtocolListType *T) {
252 Writer.AddTypeRef(T->getBaseType(), Record);
253 Record.push_back(T->getNumProtocols());
254 for (ObjCProtocolListType::qual_iterator I = T->qual_begin(),
255 E = T->qual_end(); I != E; ++I)
256 Writer.AddDeclRef(*I, Record);
257 Code = pch::TYPE_OBJC_PROTOCOL_LIST;
258}
259
John McCall8f115c62009-10-16 21:56:05 +0000260namespace {
261
262class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
263 PCHWriter &Writer;
264 PCHWriter::RecordData &Record;
265
266public:
267 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
268 : Writer(Writer), Record(Record) { }
269
John McCall17001972009-10-18 01:05:36 +0000270#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000271#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000272 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000273#include "clang/AST/TypeLocNodes.def"
274
John McCall17001972009-10-18 01:05:36 +0000275 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
276 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000277};
278
279}
280
John McCall17001972009-10-18 01:05:36 +0000281void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
282 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000283}
John McCall17001972009-10-18 01:05:36 +0000284void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
285 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000286}
John McCall17001972009-10-18 01:05:36 +0000287void TypeLocWriter::VisitFixedWidthIntTypeLoc(FixedWidthIntTypeLoc TL) {
288 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000289}
John McCall17001972009-10-18 01:05:36 +0000290void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
291 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000292}
John McCall17001972009-10-18 01:05:36 +0000293void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
294 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000295}
John McCall17001972009-10-18 01:05:36 +0000296void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
297 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000298}
John McCall17001972009-10-18 01:05:36 +0000299void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
300 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000301}
John McCall17001972009-10-18 01:05:36 +0000302void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
303 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000304}
John McCall17001972009-10-18 01:05:36 +0000305void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
306 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000307}
John McCall17001972009-10-18 01:05:36 +0000308void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
309 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
310 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
311 Record.push_back(TL.getSizeExpr() ? 1 : 0);
312 if (TL.getSizeExpr())
313 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000314}
John McCall17001972009-10-18 01:05:36 +0000315void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
316 VisitArrayTypeLoc(TL);
317}
318void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
319 VisitArrayTypeLoc(TL);
320}
321void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
322 VisitArrayTypeLoc(TL);
323}
324void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
325 DependentSizedArrayTypeLoc TL) {
326 VisitArrayTypeLoc(TL);
327}
328void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
329 DependentSizedExtVectorTypeLoc TL) {
330 Writer.AddSourceLocation(TL.getNameLoc(), Record);
331}
332void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
333 Writer.AddSourceLocation(TL.getNameLoc(), Record);
334}
335void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
336 Writer.AddSourceLocation(TL.getNameLoc(), Record);
337}
338void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
339 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
340 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
341 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
342 Writer.AddDeclRef(TL.getArg(i), Record);
343}
344void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
345 VisitFunctionTypeLoc(TL);
346}
347void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
348 VisitFunctionTypeLoc(TL);
349}
350void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
351 Writer.AddSourceLocation(TL.getNameLoc(), Record);
352}
353void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
354 Writer.AddSourceLocation(TL.getNameLoc(), Record);
355}
356void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
357 Writer.AddSourceLocation(TL.getNameLoc(), Record);
358}
359void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
360 Writer.AddSourceLocation(TL.getNameLoc(), Record);
361}
362void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
363 Writer.AddSourceLocation(TL.getNameLoc(), Record);
364}
365void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
366 Writer.AddSourceLocation(TL.getNameLoc(), Record);
367}
368void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
369 Writer.AddSourceLocation(TL.getNameLoc(), Record);
370}
371void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
372 Writer.AddSourceLocation(TL.getNameLoc(), Record);
373}
John McCallcebee162009-10-18 09:09:24 +0000374void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
375 SubstTemplateTypeParmTypeLoc TL) {
376 Writer.AddSourceLocation(TL.getNameLoc(), Record);
377}
John McCall17001972009-10-18 01:05:36 +0000378void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
379 TemplateSpecializationTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getNameLoc(), Record);
381}
382void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getNameLoc(), Record);
384}
385void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getNameLoc(), Record);
387}
388void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
389 Writer.AddSourceLocation(TL.getNameLoc(), Record);
390}
391void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
392 Writer.AddSourceLocation(TL.getStarLoc(), Record);
393}
394void TypeLocWriter::VisitObjCProtocolListTypeLoc(ObjCProtocolListTypeLoc TL) {
395 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
396 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
397 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
398 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000399}
400
Chris Lattner19cea4e2009-04-22 05:57:30 +0000401//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000402// PCHWriter Implementation
403//===----------------------------------------------------------------------===//
404
Chris Lattner28fa4e62009-04-26 22:26:21 +0000405static void EmitBlockID(unsigned ID, const char *Name,
406 llvm::BitstreamWriter &Stream,
407 PCHWriter::RecordData &Record) {
408 Record.clear();
409 Record.push_back(ID);
410 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
411
412 // Emit the block name if present.
413 if (Name == 0 || Name[0] == 0) return;
414 Record.clear();
415 while (*Name)
416 Record.push_back(*Name++);
417 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
418}
419
420static void EmitRecordID(unsigned ID, const char *Name,
421 llvm::BitstreamWriter &Stream,
422 PCHWriter::RecordData &Record) {
423 Record.clear();
424 Record.push_back(ID);
425 while (*Name)
426 Record.push_back(*Name++);
427 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000428}
429
430static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
431 PCHWriter::RecordData &Record) {
432#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
433 RECORD(STMT_STOP);
434 RECORD(STMT_NULL_PTR);
435 RECORD(STMT_NULL);
436 RECORD(STMT_COMPOUND);
437 RECORD(STMT_CASE);
438 RECORD(STMT_DEFAULT);
439 RECORD(STMT_LABEL);
440 RECORD(STMT_IF);
441 RECORD(STMT_SWITCH);
442 RECORD(STMT_WHILE);
443 RECORD(STMT_DO);
444 RECORD(STMT_FOR);
445 RECORD(STMT_GOTO);
446 RECORD(STMT_INDIRECT_GOTO);
447 RECORD(STMT_CONTINUE);
448 RECORD(STMT_BREAK);
449 RECORD(STMT_RETURN);
450 RECORD(STMT_DECL);
451 RECORD(STMT_ASM);
452 RECORD(EXPR_PREDEFINED);
453 RECORD(EXPR_DECL_REF);
454 RECORD(EXPR_INTEGER_LITERAL);
455 RECORD(EXPR_FLOATING_LITERAL);
456 RECORD(EXPR_IMAGINARY_LITERAL);
457 RECORD(EXPR_STRING_LITERAL);
458 RECORD(EXPR_CHARACTER_LITERAL);
459 RECORD(EXPR_PAREN);
460 RECORD(EXPR_UNARY_OPERATOR);
461 RECORD(EXPR_SIZEOF_ALIGN_OF);
462 RECORD(EXPR_ARRAY_SUBSCRIPT);
463 RECORD(EXPR_CALL);
464 RECORD(EXPR_MEMBER);
465 RECORD(EXPR_BINARY_OPERATOR);
466 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
467 RECORD(EXPR_CONDITIONAL_OPERATOR);
468 RECORD(EXPR_IMPLICIT_CAST);
469 RECORD(EXPR_CSTYLE_CAST);
470 RECORD(EXPR_COMPOUND_LITERAL);
471 RECORD(EXPR_EXT_VECTOR_ELEMENT);
472 RECORD(EXPR_INIT_LIST);
473 RECORD(EXPR_DESIGNATED_INIT);
474 RECORD(EXPR_IMPLICIT_VALUE_INIT);
475 RECORD(EXPR_VA_ARG);
476 RECORD(EXPR_ADDR_LABEL);
477 RECORD(EXPR_STMT);
478 RECORD(EXPR_TYPES_COMPATIBLE);
479 RECORD(EXPR_CHOOSE);
480 RECORD(EXPR_GNU_NULL);
481 RECORD(EXPR_SHUFFLE_VECTOR);
482 RECORD(EXPR_BLOCK);
483 RECORD(EXPR_BLOCK_DECL_REF);
484 RECORD(EXPR_OBJC_STRING_LITERAL);
485 RECORD(EXPR_OBJC_ENCODE);
486 RECORD(EXPR_OBJC_SELECTOR_EXPR);
487 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
488 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
489 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
490 RECORD(EXPR_OBJC_KVC_REF_EXPR);
491 RECORD(EXPR_OBJC_MESSAGE_EXPR);
492 RECORD(EXPR_OBJC_SUPER_EXPR);
493 RECORD(STMT_OBJC_FOR_COLLECTION);
494 RECORD(STMT_OBJC_CATCH);
495 RECORD(STMT_OBJC_FINALLY);
496 RECORD(STMT_OBJC_AT_TRY);
497 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
498 RECORD(STMT_OBJC_AT_THROW);
499#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000500}
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattner28fa4e62009-04-26 22:26:21 +0000502void PCHWriter::WriteBlockInfoBlock() {
503 RecordData Record;
504 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000505
Chris Lattner64031982009-04-27 00:40:25 +0000506#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000507#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000508
Chris Lattner28fa4e62009-04-26 22:26:21 +0000509 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000510 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000511 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000512 RECORD(TYPE_OFFSET);
513 RECORD(DECL_OFFSET);
514 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000515 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000516 RECORD(IDENTIFIER_OFFSET);
517 RECORD(IDENTIFIER_TABLE);
518 RECORD(EXTERNAL_DEFINITIONS);
519 RECORD(SPECIAL_TYPES);
520 RECORD(STATISTICS);
521 RECORD(TENTATIVE_DEFINITIONS);
522 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
523 RECORD(SELECTOR_OFFSETS);
524 RECORD(METHOD_POOL);
525 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000526 RECORD(SOURCE_LOCATION_OFFSETS);
527 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000528 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000529 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000530 RECORD(COMMENT_RANGES);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000531 RECORD(SVN_BRANCH_REVISION);
532
Chris Lattner28fa4e62009-04-26 22:26:21 +0000533 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000534 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000535 RECORD(SM_SLOC_FILE_ENTRY);
536 RECORD(SM_SLOC_BUFFER_ENTRY);
537 RECORD(SM_SLOC_BUFFER_BLOB);
538 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
539 RECORD(SM_LINE_TABLE);
540 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000541
Chris Lattner28fa4e62009-04-26 22:26:21 +0000542 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000543 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000544 RECORD(PP_MACRO_OBJECT_LIKE);
545 RECORD(PP_MACRO_FUNCTION_LIKE);
546 RECORD(PP_TOKEN);
547
Douglas Gregor12bfa382009-10-17 00:13:19 +0000548 // Decls and Types block.
549 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000550 RECORD(TYPE_EXT_QUAL);
551 RECORD(TYPE_FIXED_WIDTH_INT);
552 RECORD(TYPE_COMPLEX);
553 RECORD(TYPE_POINTER);
554 RECORD(TYPE_BLOCK_POINTER);
555 RECORD(TYPE_LVALUE_REFERENCE);
556 RECORD(TYPE_RVALUE_REFERENCE);
557 RECORD(TYPE_MEMBER_POINTER);
558 RECORD(TYPE_CONSTANT_ARRAY);
559 RECORD(TYPE_INCOMPLETE_ARRAY);
560 RECORD(TYPE_VARIABLE_ARRAY);
561 RECORD(TYPE_VECTOR);
562 RECORD(TYPE_EXT_VECTOR);
563 RECORD(TYPE_FUNCTION_PROTO);
564 RECORD(TYPE_FUNCTION_NO_PROTO);
565 RECORD(TYPE_TYPEDEF);
566 RECORD(TYPE_TYPEOF_EXPR);
567 RECORD(TYPE_TYPEOF);
568 RECORD(TYPE_RECORD);
569 RECORD(TYPE_ENUM);
570 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000571 RECORD(TYPE_OBJC_OBJECT_POINTER);
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +0000572 RECORD(TYPE_OBJC_PROTOCOL_LIST);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000573 RECORD(DECL_ATTR);
574 RECORD(DECL_TRANSLATION_UNIT);
575 RECORD(DECL_TYPEDEF);
576 RECORD(DECL_ENUM);
577 RECORD(DECL_RECORD);
578 RECORD(DECL_ENUM_CONSTANT);
579 RECORD(DECL_FUNCTION);
580 RECORD(DECL_OBJC_METHOD);
581 RECORD(DECL_OBJC_INTERFACE);
582 RECORD(DECL_OBJC_PROTOCOL);
583 RECORD(DECL_OBJC_IVAR);
584 RECORD(DECL_OBJC_AT_DEFS_FIELD);
585 RECORD(DECL_OBJC_CLASS);
586 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
587 RECORD(DECL_OBJC_CATEGORY);
588 RECORD(DECL_OBJC_CATEGORY_IMPL);
589 RECORD(DECL_OBJC_IMPLEMENTATION);
590 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
591 RECORD(DECL_OBJC_PROPERTY);
592 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000593 RECORD(DECL_FIELD);
594 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000595 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000596 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000597 RECORD(DECL_ORIGINAL_PARM_VAR);
598 RECORD(DECL_FILE_SCOPE_ASM);
599 RECORD(DECL_BLOCK);
600 RECORD(DECL_CONTEXT_LEXICAL);
601 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000602 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000603 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000604#undef RECORD
605#undef BLOCK
606 Stream.ExitBlock();
607}
608
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000609/// \brief Adjusts the given filename to only write out the portion of the
610/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000611///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000612/// \param Filename the file name to adjust.
613///
614/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
615/// the returned filename will be adjusted by this system root.
616///
617/// \returns either the original filename (if it needs no adjustment) or the
618/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000619static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000620adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
621 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000622
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000623 if (!isysroot)
624 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000625
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000626 // Verify that the filename and the system root have the same prefix.
627 unsigned Pos = 0;
628 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
629 if (Filename[Pos] != isysroot[Pos])
630 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000631
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000632 // We hit the end of the filename before we hit the end of the system root.
633 if (!Filename[Pos])
634 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000635
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000636 // If the file name has a '/' at the current position, skip over the '/'.
637 // We distinguish sysroot-based includes from absolute includes by the
638 // absence of '/' at the beginning of sysroot-based includes.
639 if (Filename[Pos] == '/')
640 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000641
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000642 return Filename + Pos;
643}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000644
Douglas Gregor7b71e632009-04-27 22:23:34 +0000645/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000646void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000647 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000648
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000649 // Metadata
650 const TargetInfo &Target = Context.Target;
651 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
652 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
653 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
654 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
655 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
656 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
657 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
658 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
659 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000660
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000661 RecordData Record;
662 Record.push_back(pch::METADATA);
663 Record.push_back(pch::VERSION_MAJOR);
664 Record.push_back(pch::VERSION_MINOR);
665 Record.push_back(CLANG_VERSION_MAJOR);
666 Record.push_back(CLANG_VERSION_MINOR);
667 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000668 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000669 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000670
Douglas Gregor45fe0362009-05-12 01:31:05 +0000671 // Original file name
672 SourceManager &SM = Context.getSourceManager();
673 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
674 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
675 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
676 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
677 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
678
679 llvm::sys::Path MainFilePath(MainFile->getName());
680 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Douglas Gregor45fe0362009-05-12 01:31:05 +0000682 if (!MainFilePath.isAbsolute()) {
683 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000684 P.appendComponent(MainFilePath.str());
685 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000686 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000687 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000688 }
689
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000690 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000691 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000692 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000693 RecordData Record;
694 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000695 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000696 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000697
698 // Subversion branch/version information.
699 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
700 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
701 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
702 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
703 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
704 Record.clear();
705 Record.push_back(pch::SVN_BRANCH_REVISION);
706 Record.push_back(getClangSubversionRevision());
707 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000708}
709
710/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000711void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
712 RecordData Record;
713 Record.push_back(LangOpts.Trigraphs);
714 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
715 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
716 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
717 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
718 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
719 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
720 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
721 Record.push_back(LangOpts.C99); // C99 Support
722 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
723 Record.push_back(LangOpts.CPlusPlus); // C++ Support
724 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000725 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000726
Douglas Gregor55abb232009-04-10 20:39:37 +0000727 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
728 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
729 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000730
Douglas Gregor55abb232009-04-10 20:39:37 +0000731 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000732 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
733 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000734 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000735 Record.push_back(LangOpts.Exceptions); // Support exception handling.
736
737 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
738 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
739 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
740
Chris Lattner258172e2009-04-27 07:35:58 +0000741 // Whether static initializers are protected by locks.
742 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000743 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000744 Record.push_back(LangOpts.Blocks); // block extension to C
745 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
746 // they are unused.
747 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
748 // (modulo the platform support).
749
750 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
751 // signed integer arithmetic overflows.
752
753 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
754 // may be ripped out at any time.
755
756 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000757 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000758 // defined.
759 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
760 // opposed to __DYNAMIC__).
761 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
762
763 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
764 // used (instead of C99 semantics).
765 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000766 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
767 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000768 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
769 // unsigned type
Douglas Gregor55abb232009-04-10 20:39:37 +0000770 Record.push_back(LangOpts.getGCMode());
771 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000772 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000773 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000774 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000775 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000776 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000777}
778
Douglas Gregora7f71a92009-04-10 03:52:48 +0000779//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000780// stat cache Serialization
781//===----------------------------------------------------------------------===//
782
783namespace {
784// Trait used for the on-disk hash table of stat cache results.
785class VISIBILITY_HIDDEN PCHStatCacheTrait {
786public:
787 typedef const char * key_type;
788 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000789
Douglas Gregorc5046832009-04-27 18:38:38 +0000790 typedef std::pair<int, struct stat> data_type;
791 typedef const data_type& data_type_ref;
792
793 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000794 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000795 }
Mike Stump11289f42009-09-09 15:08:12 +0000796
797 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000798 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
799 data_type_ref Data) {
800 unsigned StrLen = strlen(path);
801 clang::io::Emit16(Out, StrLen);
802 unsigned DataLen = 1; // result value
803 if (Data.first == 0)
804 DataLen += 4 + 4 + 2 + 8 + 8;
805 clang::io::Emit8(Out, DataLen);
806 return std::make_pair(StrLen + 1, DataLen);
807 }
Mike Stump11289f42009-09-09 15:08:12 +0000808
Douglas Gregorc5046832009-04-27 18:38:38 +0000809 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
810 Out.write(path, KeyLen);
811 }
Mike Stump11289f42009-09-09 15:08:12 +0000812
Douglas Gregorc5046832009-04-27 18:38:38 +0000813 void EmitData(llvm::raw_ostream& Out, key_type_ref,
814 data_type_ref Data, unsigned DataLen) {
815 using namespace clang::io;
816 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000817
Douglas Gregorc5046832009-04-27 18:38:38 +0000818 // Result of stat()
819 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000820
Douglas Gregorc5046832009-04-27 18:38:38 +0000821 if (Data.first == 0) {
822 Emit32(Out, (uint32_t) Data.second.st_ino);
823 Emit32(Out, (uint32_t) Data.second.st_dev);
824 Emit16(Out, (uint16_t) Data.second.st_mode);
825 Emit64(Out, (uint64_t) Data.second.st_mtime);
826 Emit64(Out, (uint64_t) Data.second.st_size);
827 }
828
829 assert(Out.tell() - Start == DataLen && "Wrong data length");
830 }
831};
832} // end anonymous namespace
833
834/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000835void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
836 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000837 // Build the on-disk hash table containing information about every
838 // stat() call.
839 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
840 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000841 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000842 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000843 Stat != StatEnd; ++Stat, ++NumStatEntries) {
844 const char *Filename = Stat->first();
845 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
846 Generator.insert(Filename, Stat->second);
847 }
Mike Stump11289f42009-09-09 15:08:12 +0000848
Douglas Gregorc5046832009-04-27 18:38:38 +0000849 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000850 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000851 uint32_t BucketOffset;
852 {
853 llvm::raw_svector_ostream Out(StatCacheData);
854 // Make sure that no bucket is at offset 0
855 clang::io::Emit32(Out, 0);
856 BucketOffset = Generator.Emit(Out);
857 }
858
859 // Create a blob abbreviation
860 using namespace llvm;
861 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
862 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
866 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
867
868 // Write the stat cache
869 RecordData Record;
870 Record.push_back(pch::STAT_CACHE);
871 Record.push_back(BucketOffset);
872 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000873 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000874}
875
876//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000877// Source Manager Serialization
878//===----------------------------------------------------------------------===//
879
880/// \brief Create an abbreviation for the SLocEntry that refers to a
881/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000882static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000883 using namespace llvm;
884 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
885 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
889 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000891 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000892}
893
894/// \brief Create an abbreviation for the SLocEntry that refers to a
895/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000896static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000897 using namespace llvm;
898 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
899 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000905 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000906}
907
908/// \brief Create an abbreviation for the SLocEntry that refers to a
909/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000910static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000911 using namespace llvm;
912 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
913 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000915 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000916}
917
918/// \brief Create an abbreviation for the SLocEntry that refers to an
919/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000920static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000921 using namespace llvm;
922 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
923 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000929 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000930}
931
932/// \brief Writes the block containing the serialized form of the
933/// source manager.
934///
935/// TODO: We should probably use an on-disk hash table (stored in a
936/// blob), indexed based on the file name, so that we only create
937/// entries for files that we actually need. In the common case (no
938/// errors), we probably won't have to create file entries for any of
939/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000940void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000941 const Preprocessor &PP,
942 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000943 RecordData Record;
944
Chris Lattner0910e3b2009-04-10 17:16:57 +0000945 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000946 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000947
948 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000949 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
950 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
951 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
952 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000953
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000954 // Write the line table.
955 if (SourceMgr.hasLineTable()) {
956 LineTableInfo &LineTable = SourceMgr.getLineTable();
957
958 // Emit the file names
959 Record.push_back(LineTable.getNumFilenames());
960 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
961 // Emit the file name
962 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000963 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000964 unsigned FilenameLen = Filename? strlen(Filename) : 0;
965 Record.push_back(FilenameLen);
966 if (FilenameLen)
967 Record.insert(Record.end(), Filename, Filename + FilenameLen);
968 }
Mike Stump11289f42009-09-09 15:08:12 +0000969
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000970 // Emit the line entries
971 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
972 L != LEnd; ++L) {
973 // Emit the file ID
974 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000975
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000976 // Emit the line entries
977 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000978 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000979 LEEnd = L->second.end();
980 LE != LEEnd; ++LE) {
981 Record.push_back(LE->FileOffset);
982 Record.push_back(LE->LineNo);
983 Record.push_back(LE->FilenameID);
984 Record.push_back((unsigned)LE->FileKind);
985 Record.push_back(LE->IncludeOffset);
986 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000987 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +0000988 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000989 }
990
Douglas Gregor258ae542009-04-27 06:38:32 +0000991 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +0000992 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +0000993 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000994 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +0000995 E = HS.header_file_end();
996 I != E; ++I) {
997 Record.push_back(I->isImport);
998 Record.push_back(I->DirInfo);
999 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +00001000 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001001 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1002 Record.clear();
1003 }
1004
Douglas Gregor258ae542009-04-27 06:38:32 +00001005 // Write out the source location entry table. We skip the first
1006 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001007 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001008 RecordData PreloadSLocs;
1009 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +00001010 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1011 // Get this source location entry.
1012 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1013
Douglas Gregor258ae542009-04-27 06:38:32 +00001014 // Record the offset of this source-location entry.
1015 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1016
1017 // Figure out which record code to use.
1018 unsigned Code;
1019 if (SLoc->isFile()) {
1020 if (SLoc->getFile().getContentCache()->Entry)
1021 Code = pch::SM_SLOC_FILE_ENTRY;
1022 else
1023 Code = pch::SM_SLOC_BUFFER_ENTRY;
1024 } else
1025 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1026 Record.clear();
1027 Record.push_back(Code);
1028
1029 Record.push_back(SLoc->getOffset());
1030 if (SLoc->isFile()) {
1031 const SrcMgr::FileInfo &File = SLoc->getFile();
1032 Record.push_back(File.getIncludeLoc().getRawEncoding());
1033 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1034 Record.push_back(File.hasLineDirectives());
1035
1036 const SrcMgr::ContentCache *Content = File.getContentCache();
1037 if (Content->Entry) {
1038 // The source location entry is a file. The blob associated
1039 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001040
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001041 // Turn the file name into an absolute path, if it isn't already.
1042 const char *Filename = Content->Entry->getName();
1043 llvm::sys::Path FilePath(Filename, strlen(Filename));
1044 std::string FilenameStr;
1045 if (!FilePath.isAbsolute()) {
1046 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001047 P.appendComponent(FilePath.str());
1048 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001049 Filename = FilenameStr.c_str();
1050 }
Mike Stump11289f42009-09-09 15:08:12 +00001051
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001052 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001053 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001054
1055 // FIXME: For now, preload all file source locations, so that
1056 // we get the appropriate File entries in the reader. This is
1057 // a temporary measure.
1058 PreloadSLocs.push_back(SLocEntryOffsets.size());
1059 } else {
1060 // The source location entry is a buffer. The blob associated
1061 // with this entry contains the contents of the buffer.
1062
1063 // We add one to the size so that we capture the trailing NULL
1064 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1065 // the reader side).
1066 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1067 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001068 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1069 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001070 Record.clear();
1071 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1072 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001073 llvm::StringRef(Buffer->getBufferStart(),
1074 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001075
1076 if (strcmp(Name, "<built-in>") == 0)
1077 PreloadSLocs.push_back(SLocEntryOffsets.size());
1078 }
1079 } else {
1080 // The source location entry is an instantiation.
1081 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1082 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1083 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1084 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1085
1086 // Compute the token length for this macro expansion.
1087 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001088 if (I + 1 != N)
1089 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001090 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1091 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1092 }
1093 }
1094
Douglas Gregor8f45df52009-04-16 22:23:12 +00001095 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001096
1097 if (SLocEntryOffsets.empty())
1098 return;
1099
1100 // Write the source-location offsets table into the PCH block. This
1101 // table is used for lazily loading source-location information.
1102 using namespace llvm;
1103 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1104 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1107 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1108 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001109
Douglas Gregor258ae542009-04-27 06:38:32 +00001110 Record.clear();
1111 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1112 Record.push_back(SLocEntryOffsets.size());
1113 Record.push_back(SourceMgr.getNextOffset());
1114 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001115 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001116 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001117
1118 // Write the source location entry preloads array, telling the PCH
1119 // reader which source locations entries it should load eagerly.
1120 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001121}
1122
Douglas Gregorc5046832009-04-27 18:38:38 +00001123//===----------------------------------------------------------------------===//
1124// Preprocessor Serialization
1125//===----------------------------------------------------------------------===//
1126
Chris Lattnereeffaef2009-04-10 17:15:23 +00001127/// \brief Writes the block containing the serialized form of the
1128/// preprocessor.
1129///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001130void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001131 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001132
Chris Lattner0af3ba12009-04-13 01:29:17 +00001133 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1134 if (PP.getCounterValue() != 0) {
1135 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001136 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001137 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001138 }
1139
1140 // Enter the preprocessor block.
1141 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001142
Douglas Gregoreda6a892009-04-26 00:07:37 +00001143 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1144 // FIXME: use diagnostics subsystem for localization etc.
1145 if (PP.SawDateOrTime())
1146 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001147
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001148 // Loop over all the macro definitions that are live at the end of the file,
1149 // emitting each to the PP section.
Douglas Gregor45053152009-10-17 17:25:45 +00001150 // FIXME: Make sure that this sees macros defined in included PCH files.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001151 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1152 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001153 // FIXME: This emits macros in hash table order, we should do it in a stable
1154 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001155 MacroInfo *MI = I->second;
1156
1157 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1158 // been redefined by the header (in which case they are not isBuiltinMacro).
1159 if (MI->isBuiltinMacro())
1160 continue;
1161
Douglas Gregorc3366a52009-04-21 23:56:24 +00001162 // FIXME: Remove this identifier reference?
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001163 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001164 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001165 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1166 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001168 unsigned Code;
1169 if (MI->isObjectLike()) {
1170 Code = pch::PP_MACRO_OBJECT_LIKE;
1171 } else {
1172 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001173
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001174 Record.push_back(MI->isC99Varargs());
1175 Record.push_back(MI->isGNUVarargs());
1176 Record.push_back(MI->getNumArgs());
1177 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1178 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001179 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001180 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001181 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001182 Record.clear();
1183
Chris Lattner2199f5b2009-04-10 18:08:30 +00001184 // Emit the tokens array.
1185 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1186 // Note that we know that the preprocessor does not have any annotation
1187 // tokens in it because they are created by the parser, and thus can't be
1188 // in a macro definition.
1189 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001190
Chris Lattner2199f5b2009-04-10 18:08:30 +00001191 Record.push_back(Tok.getLocation().getRawEncoding());
1192 Record.push_back(Tok.getLength());
1193
Chris Lattner2199f5b2009-04-10 18:08:30 +00001194 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1195 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001196 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001197
Chris Lattner2199f5b2009-04-10 18:08:30 +00001198 // FIXME: Should translate token kind to a stable encoding.
1199 Record.push_back(Tok.getKind());
1200 // FIXME: Should translate token flags to a stable encoding.
1201 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001202
Douglas Gregor8f45df52009-04-16 22:23:12 +00001203 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001204 Record.clear();
1205 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001206 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001207 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001208 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001209}
1210
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001211void PCHWriter::WriteComments(ASTContext &Context) {
1212 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001213
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001214 if (Context.Comments.empty())
1215 return;
Mike Stump11289f42009-09-09 15:08:12 +00001216
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001217 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1218 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1219 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1220 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001221
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001222 RecordData Record;
1223 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001224 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001225 (const char*)&Context.Comments[0],
1226 Context.Comments.size() * sizeof(SourceRange));
1227}
1228
Douglas Gregorc5046832009-04-27 18:38:38 +00001229//===----------------------------------------------------------------------===//
1230// Type Serialization
1231//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001232
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001233/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001234void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001235 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001236 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001237 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001238
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001239 // Record the offset for this type.
1240 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001241 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001242 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1243 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001244 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001245 }
1246
1247 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001248
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001249 // Emit the type's representation.
1250 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001251
1252 if (T.hasNonFastQualifiers()) {
1253 Qualifiers Qs = T.getQualifiers();
1254 AddTypeRef(T.getUnqualifiedType(), Record);
1255 Record.push_back(Qs.getAsOpaqueValue());
1256 W.Code = pch::TYPE_EXT_QUAL;
1257 } else {
1258 switch (T->getTypeClass()) {
1259 // For all of the concrete, non-dependent types, call the
1260 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001261#define TYPE(Class, Base) \
John McCall8ccfcb52009-09-24 19:53:00 +00001262 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001263#define ABSTRACT_TYPE(Class, Base)
1264#define DEPENDENT_TYPE(Class, Base)
1265#include "clang/AST/TypeNodes.def"
1266
John McCall8ccfcb52009-09-24 19:53:00 +00001267 // For all of the dependent type nodes (which only occur in C++
1268 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001269#define TYPE(Class, Base)
1270#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1271#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001272 assert(false && "Cannot serialize dependent type nodes");
1273 break;
1274 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001275 }
1276
1277 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001278 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001279
1280 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001281 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001282}
1283
Douglas Gregorc5046832009-04-27 18:38:38 +00001284//===----------------------------------------------------------------------===//
1285// Declaration Serialization
1286//===----------------------------------------------------------------------===//
1287
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001288/// \brief Write the block containing all of the declaration IDs
1289/// lexically declared within the given DeclContext.
1290///
1291/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1292/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001293uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001294 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001295 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001296 return 0;
1297
Douglas Gregor8f45df52009-04-16 22:23:12 +00001298 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001299 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001300 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1301 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001302 AddDeclRef(*D, Record);
1303
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001304 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001305 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001306 return Offset;
1307}
1308
1309/// \brief Write the block containing all of the declaration IDs
1310/// visible from the given DeclContext.
1311///
1312/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1313/// bistream, or 0 if no block was written.
1314uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1315 DeclContext *DC) {
1316 if (DC->getPrimaryContext() != DC)
1317 return 0;
1318
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001319 // Since there is no name lookup into functions or methods, and we
1320 // perform name lookup for the translation unit via the
1321 // IdentifierInfo chains, don't bother to build a
1322 // visible-declarations table for these entities.
1323 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001324 return 0;
1325
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001326 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001327 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001328
1329 // Serialize the contents of the mapping used for lookup. Note that,
1330 // although we have two very different code paths, the serialized
1331 // representation is the same for both cases: a declaration name,
1332 // followed by a size, followed by references to the visible
1333 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001334 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001335 RecordData Record;
1336 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001337 if (!Map)
1338 return 0;
1339
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001340 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1341 D != DEnd; ++D) {
1342 AddDeclarationName(D->first, Record);
1343 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1344 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001345 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001346 AddDeclRef(*Result.first, Record);
1347 }
1348
1349 if (Record.size() == 0)
1350 return 0;
1351
Douglas Gregor8f45df52009-04-16 22:23:12 +00001352 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001353 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001354 return Offset;
1355}
1356
Douglas Gregorc5046832009-04-27 18:38:38 +00001357//===----------------------------------------------------------------------===//
1358// Global Method Pool and Selector Serialization
1359//===----------------------------------------------------------------------===//
1360
Douglas Gregore84a9da2009-04-20 20:36:09 +00001361namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001362// Trait used for the on-disk hash table used in the method pool.
1363class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1364 PCHWriter &Writer;
1365
1366public:
1367 typedef Selector key_type;
1368 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001369
Douglas Gregorc78d3462009-04-24 21:10:55 +00001370 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1371 typedef const data_type& data_type_ref;
1372
1373 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001374
Douglas Gregorc78d3462009-04-24 21:10:55 +00001375 static unsigned ComputeHash(Selector Sel) {
1376 unsigned N = Sel.getNumArgs();
1377 if (N == 0)
1378 ++N;
1379 unsigned R = 5381;
1380 for (unsigned I = 0; I != N; ++I)
1381 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001382 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001383 return R;
1384 }
Mike Stump11289f42009-09-09 15:08:12 +00001385
1386 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001387 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1388 data_type_ref Methods) {
1389 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1390 clang::io::Emit16(Out, KeyLen);
1391 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001392 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001393 Method = Method->Next)
1394 if (Method->Method)
1395 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001396 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001397 Method = Method->Next)
1398 if (Method->Method)
1399 DataLen += 4;
1400 clang::io::Emit16(Out, DataLen);
1401 return std::make_pair(KeyLen, DataLen);
1402 }
Mike Stump11289f42009-09-09 15:08:12 +00001403
Douglas Gregor95c13f52009-04-25 17:48:32 +00001404 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001405 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001406 assert((Start >> 32) == 0 && "Selector key offset too large");
1407 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001408 unsigned N = Sel.getNumArgs();
1409 clang::io::Emit16(Out, N);
1410 if (N == 0)
1411 N = 1;
1412 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001413 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001414 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1415 }
Mike Stump11289f42009-09-09 15:08:12 +00001416
Douglas Gregorc78d3462009-04-24 21:10:55 +00001417 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001418 data_type_ref Methods, unsigned DataLen) {
1419 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001420 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001421 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001422 Method = Method->Next)
1423 if (Method->Method)
1424 ++NumInstanceMethods;
1425
1426 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001427 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001428 Method = Method->Next)
1429 if (Method->Method)
1430 ++NumFactoryMethods;
1431
1432 clang::io::Emit16(Out, NumInstanceMethods);
1433 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001434 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001435 Method = Method->Next)
1436 if (Method->Method)
1437 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001438 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001439 Method = Method->Next)
1440 if (Method->Method)
1441 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001442
1443 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001444 }
1445};
1446} // end anonymous namespace
1447
1448/// \brief Write the method pool into the PCH file.
1449///
1450/// The method pool contains both instance and factory methods, stored
1451/// in an on-disk hash table indexed by the selector.
1452void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1453 using namespace llvm;
1454
1455 // Create and write out the blob that contains the instance and
1456 // factor method pools.
1457 bool Empty = true;
1458 {
1459 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001460
Douglas Gregorc78d3462009-04-24 21:10:55 +00001461 // Create the on-disk hash table representation. Start by
1462 // iterating through the instance method pool.
1463 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001464 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001465 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001466 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001467 InstanceEnd = SemaRef.InstanceMethodPool.end();
1468 Instance != InstanceEnd; ++Instance) {
1469 // Check whether there is a factory method with the same
1470 // selector.
1471 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1472 = SemaRef.FactoryMethodPool.find(Instance->first);
1473
1474 if (Factory == SemaRef.FactoryMethodPool.end())
1475 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001476 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001477 ObjCMethodList()));
1478 else
1479 Generator.insert(Instance->first,
1480 std::make_pair(Instance->second, Factory->second));
1481
Douglas Gregor95c13f52009-04-25 17:48:32 +00001482 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001483 Empty = false;
1484 }
1485
1486 // Now iterate through the factory method pool, to pick up any
1487 // selectors that weren't already in the instance method pool.
1488 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001489 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001490 FactoryEnd = SemaRef.FactoryMethodPool.end();
1491 Factory != FactoryEnd; ++Factory) {
1492 // Check whether there is an instance method with the same
1493 // selector. If so, there is no work to do here.
1494 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1495 = SemaRef.InstanceMethodPool.find(Factory->first);
1496
Douglas Gregor95c13f52009-04-25 17:48:32 +00001497 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001498 Generator.insert(Factory->first,
1499 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001500 ++NumSelectorsInMethodPool;
1501 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001502
1503 Empty = false;
1504 }
1505
Douglas Gregor95c13f52009-04-25 17:48:32 +00001506 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001507 return;
1508
1509 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001510 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001511 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001512 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001513 {
1514 PCHMethodPoolTrait Trait(*this);
1515 llvm::raw_svector_ostream Out(MethodPool);
1516 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001517 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001518 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001519
1520 // For every selector that we have seen but which was not
1521 // written into the hash table, write the selector itself and
1522 // record it's offset.
1523 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1524 if (SelectorOffsets[I] == 0)
1525 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001526 }
1527
1528 // Create a blob abbreviation
1529 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1530 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1534 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1535
Douglas Gregor95c13f52009-04-25 17:48:32 +00001536 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001537 RecordData Record;
1538 Record.push_back(pch::METHOD_POOL);
1539 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001540 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001541 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001542
1543 // Create a blob abbreviation for the selector table offsets.
1544 Abbrev = new BitCodeAbbrev();
1545 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1548 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1549
1550 // Write the selector offsets table.
1551 Record.clear();
1552 Record.push_back(pch::SELECTOR_OFFSETS);
1553 Record.push_back(SelectorOffsets.size());
1554 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1555 (const char *)&SelectorOffsets.front(),
1556 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001557 }
1558}
1559
Douglas Gregorc5046832009-04-27 18:38:38 +00001560//===----------------------------------------------------------------------===//
1561// Identifier Table Serialization
1562//===----------------------------------------------------------------------===//
1563
Douglas Gregorc78d3462009-04-24 21:10:55 +00001564namespace {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001565class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1566 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001567 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001568
Douglas Gregor1d583f22009-04-28 21:18:29 +00001569 /// \brief Determines whether this is an "interesting" identifier
1570 /// that needs a full IdentifierInfo structure written into the hash
1571 /// table.
1572 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1573 return II->isPoisoned() ||
1574 II->isExtensionToken() ||
1575 II->hasMacroDefinition() ||
1576 II->getObjCOrBuiltinID() ||
1577 II->getFETokenInfo<void>();
1578 }
1579
Douglas Gregore84a9da2009-04-20 20:36:09 +00001580public:
1581 typedef const IdentifierInfo* key_type;
1582 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001583
Douglas Gregore84a9da2009-04-20 20:36:09 +00001584 typedef pch::IdentID data_type;
1585 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001586
1587 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001588 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001589
1590 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001591 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001592 }
Mike Stump11289f42009-09-09 15:08:12 +00001593
1594 std::pair<unsigned,unsigned>
1595 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001596 pch::IdentID ID) {
1597 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001598 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1599 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001600 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001601 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001602 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001603 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001604 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1605 DEnd = IdentifierResolver::end();
1606 D != DEnd; ++D)
1607 DataLen += sizeof(pch::DeclID);
1608 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001609 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001610 // We emit the key length after the data length so that every
1611 // string is preceded by a 16-bit length. This matches the PTH
1612 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001613 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001614 return std::make_pair(KeyLen, DataLen);
1615 }
Mike Stump11289f42009-09-09 15:08:12 +00001616
1617 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001618 unsigned KeyLen) {
1619 // Record the location of the key data. This is used when generating
1620 // the mapping from persistent IDs to strings.
1621 Writer.SetIdentifierOffset(II, Out.tell());
1622 Out.write(II->getName(), KeyLen);
1623 }
Mike Stump11289f42009-09-09 15:08:12 +00001624
1625 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001626 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001627 if (!isInterestingIdentifier(II)) {
1628 clang::io::Emit32(Out, ID << 1);
1629 return;
1630 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001631
Douglas Gregor1d583f22009-04-28 21:18:29 +00001632 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001633 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001634 bool hasMacroDefinition =
1635 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001636 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001637 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001638 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001639 Bits = (Bits << 1) | II->isExtensionToken();
1640 Bits = (Bits << 1) | II->isPoisoned();
1641 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregorb9256522009-04-28 21:32:13 +00001642 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001643
Douglas Gregorc3366a52009-04-21 23:56:24 +00001644 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001645 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001646
Douglas Gregora868bbd2009-04-21 22:25:48 +00001647 // Emit the declaration IDs in reverse order, because the
1648 // IdentifierResolver provides the declarations as they would be
1649 // visible (e.g., the function "stat" would come before the struct
1650 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1651 // adds declarations to the end of the list (so we need to see the
1652 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001653 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001654 IdentifierResolver::end());
1655 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1656 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001657 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001658 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001659 }
1660};
1661} // end anonymous namespace
1662
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001663/// \brief Write the identifier table into the PCH file.
1664///
1665/// The identifier table consists of a blob containing string data
1666/// (the actual identifiers themselves) and a separate "offsets" index
1667/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001668void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001669 using namespace llvm;
1670
1671 // Create and write out the blob that contains the identifier
1672 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001673 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001674 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001675
Douglas Gregore6648fb2009-04-28 20:33:11 +00001676 // Look for any identifiers that were named while processing the
1677 // headers, but are otherwise not needed. We add these to the hash
1678 // table to enable checking of the predefines buffer in the case
1679 // where the user adds new macro definitions when building the PCH
1680 // file.
1681 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1682 IDEnd = PP.getIdentifierTable().end();
1683 ID != IDEnd; ++ID)
1684 getIdentifierRef(ID->second);
1685
Douglas Gregore84a9da2009-04-20 20:36:09 +00001686 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001687 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001688 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1689 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1690 ID != IDEnd; ++ID) {
1691 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001692 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001693 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001694
Douglas Gregore84a9da2009-04-20 20:36:09 +00001695 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001696 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001697 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001698 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001699 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001700 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001701 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001702 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001703 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001704 }
1705
1706 // Create a blob abbreviation
1707 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1708 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001710 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001711 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001712
1713 // Write the identifier table
1714 RecordData Record;
1715 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001716 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001717 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001718 }
1719
1720 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001721 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1722 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1723 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1724 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1725 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1726
1727 RecordData Record;
1728 Record.push_back(pch::IDENTIFIER_OFFSET);
1729 Record.push_back(IdentifierOffsets.size());
1730 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1731 (const char *)&IdentifierOffsets.front(),
1732 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001733}
1734
Douglas Gregorc5046832009-04-27 18:38:38 +00001735//===----------------------------------------------------------------------===//
1736// General Serialization Routines
1737//===----------------------------------------------------------------------===//
1738
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001739/// \brief Write a record containing the given attributes.
1740void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1741 RecordData Record;
1742 for (; Attr; Attr = Attr->getNext()) {
1743 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1744 Record.push_back(Attr->isInherited());
1745 switch (Attr->getKind()) {
1746 case Attr::Alias:
1747 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1748 break;
1749
1750 case Attr::Aligned:
1751 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1752 break;
1753
1754 case Attr::AlwaysInline:
1755 break;
Mike Stump11289f42009-09-09 15:08:12 +00001756
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001757 case Attr::AnalyzerNoReturn:
1758 break;
1759
1760 case Attr::Annotate:
1761 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1762 break;
1763
1764 case Attr::AsmLabel:
1765 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1766 break;
1767
1768 case Attr::Blocks:
1769 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1770 break;
1771
1772 case Attr::Cleanup:
1773 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1774 break;
1775
1776 case Attr::Const:
1777 break;
1778
1779 case Attr::Constructor:
1780 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1781 break;
1782
1783 case Attr::DLLExport:
1784 case Attr::DLLImport:
1785 case Attr::Deprecated:
1786 break;
1787
1788 case Attr::Destructor:
1789 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1790 break;
1791
1792 case Attr::FastCall:
1793 break;
1794
1795 case Attr::Format: {
1796 const FormatAttr *Format = cast<FormatAttr>(Attr);
1797 AddString(Format->getType(), Record);
1798 Record.push_back(Format->getFormatIdx());
1799 Record.push_back(Format->getFirstArg());
1800 break;
1801 }
1802
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001803 case Attr::FormatArg: {
1804 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1805 Record.push_back(Format->getFormatIdx());
1806 break;
1807 }
1808
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001809 case Attr::Sentinel : {
1810 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1811 Record.push_back(Sentinel->getSentinel());
1812 Record.push_back(Sentinel->getNullPos());
1813 break;
1814 }
Mike Stump11289f42009-09-09 15:08:12 +00001815
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001816 case Attr::GNUInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001817 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001818 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001819 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001820 case Attr::NoReturn:
1821 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001822 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001823 break;
1824
1825 case Attr::NonNull: {
1826 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1827 Record.push_back(NonNull->size());
1828 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1829 break;
1830 }
1831
1832 case Attr::ObjCException:
1833 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001834 case Attr::CFReturnsRetained:
1835 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001836 case Attr::Overloadable:
1837 break;
1838
Anders Carlsson68e0b682009-08-08 18:23:56 +00001839 case Attr::PragmaPack:
1840 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001841 break;
1842
Anders Carlsson68e0b682009-08-08 18:23:56 +00001843 case Attr::Packed:
1844 break;
Mike Stump11289f42009-09-09 15:08:12 +00001845
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001846 case Attr::Pure:
1847 break;
1848
1849 case Attr::Regparm:
1850 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1851 break;
Mike Stump11289f42009-09-09 15:08:12 +00001852
Nate Begemanf2758702009-06-26 06:32:41 +00001853 case Attr::ReqdWorkGroupSize:
1854 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1855 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1856 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1857 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001858
1859 case Attr::Section:
1860 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1861 break;
1862
1863 case Attr::StdCall:
1864 case Attr::TransparentUnion:
1865 case Attr::Unavailable:
1866 case Attr::Unused:
1867 case Attr::Used:
1868 break;
1869
1870 case Attr::Visibility:
1871 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001872 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001873 break;
1874
1875 case Attr::WarnUnusedResult:
1876 case Attr::Weak:
1877 case Attr::WeakImport:
1878 break;
1879 }
1880 }
1881
Douglas Gregor8f45df52009-04-16 22:23:12 +00001882 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001883}
1884
1885void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1886 Record.push_back(Str.size());
1887 Record.insert(Record.end(), Str.begin(), Str.end());
1888}
1889
Douglas Gregore84a9da2009-04-20 20:36:09 +00001890/// \brief Note that the identifier II occurs at the given offset
1891/// within the identifier table.
1892void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001893 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001894}
1895
Douglas Gregor95c13f52009-04-25 17:48:32 +00001896/// \brief Note that the selector Sel occurs at the given offset
1897/// within the method pool/selector table.
1898void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1899 unsigned ID = SelectorIDs[Sel];
1900 assert(ID && "Unknown selector");
1901 SelectorOffsets[ID - 1] = Offset;
1902}
1903
Mike Stump11289f42009-09-09 15:08:12 +00001904PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1905 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001906 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1907 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001908
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001909void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1910 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001911 using namespace llvm;
1912
Douglas Gregor162dd022009-04-20 15:53:59 +00001913 ASTContext &Context = SemaRef.Context;
1914 Preprocessor &PP = SemaRef.PP;
1915
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001916 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001917 Stream.Emit((unsigned)'C', 8);
1918 Stream.Emit((unsigned)'P', 8);
1919 Stream.Emit((unsigned)'C', 8);
1920 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001921
Chris Lattner28fa4e62009-04-26 22:26:21 +00001922 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001923
1924 // The translation unit is the first declaration we'll emit.
1925 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001926 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001927
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001928 // Make sure that we emit IdentifierInfos (and any attached
1929 // declarations) for builtins.
1930 {
1931 IdentifierTable &Table = PP.getIdentifierTable();
1932 llvm::SmallVector<const char *, 32> BuiltinNames;
1933 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1934 Context.getLangOptions().NoBuiltin);
1935 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1936 getIdentifierRef(&Table.get(BuiltinNames[I]));
1937 }
1938
Chris Lattner0c797362009-09-08 18:19:27 +00001939 // Build a record containing all of the tentative definitions in this file, in
1940 // TentativeDefinitionList order. Generally, this record will be empty for
1941 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001942 RecordData TentativeDefinitions;
Chris Lattner0c797362009-09-08 18:19:27 +00001943 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1944 VarDecl *VD =
1945 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1946 if (VD) AddDeclRef(VD, TentativeDefinitions);
1947 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001948
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001949 // Build a record containing all of the locally-scoped external
1950 // declarations in this header file. Generally, this record will be
1951 // empty.
1952 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001953 // FIXME: This is filling in the PCH file in densemap order which is
1954 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001955 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001956 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1957 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1958 TD != TDEnd; ++TD)
1959 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1960
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001961 // Build a record containing all of the ext_vector declarations.
1962 RecordData ExtVectorDecls;
1963 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1964 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1965
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001966 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001967 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001968 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001969 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001970 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001971 if (StatCalls && !isysroot)
1972 WriteStatCache(*StatCalls, isysroot);
1973 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00001974 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001975 // Write the record of special types.
1976 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001977
Steve Naroffc277ad12009-07-18 15:33:26 +00001978 AddTypeRef(Context.getBuiltinVaListType(), Record);
1979 AddTypeRef(Context.getObjCIdType(), Record);
1980 AddTypeRef(Context.getObjCSelType(), Record);
1981 AddTypeRef(Context.getObjCProtoType(), Record);
1982 AddTypeRef(Context.getObjCClassType(), Record);
1983 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1984 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1985 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00001986 AddTypeRef(Context.getjmp_bufType(), Record);
1987 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001988 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1989 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00001990 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00001991
Douglas Gregor1970d882009-04-26 03:49:13 +00001992 // Keep writing types and declarations until all types and
1993 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00001994 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
1995 WriteDeclsBlockAbbrevs();
1996 while (!DeclTypesToEmit.empty()) {
1997 DeclOrType DOT = DeclTypesToEmit.front();
1998 DeclTypesToEmit.pop();
1999 if (DOT.isType())
2000 WriteType(DOT.getType());
2001 else
2002 WriteDecl(Context, DOT.getDecl());
2003 }
2004 Stream.ExitBlock();
2005
Douglas Gregor45053152009-10-17 17:25:45 +00002006 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002007 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002008 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002009
2010 // Write the type offsets array
2011 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2012 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2015 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2016 Record.clear();
2017 Record.push_back(pch::TYPE_OFFSET);
2018 Record.push_back(TypeOffsets.size());
2019 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002020 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002021 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002022
Douglas Gregor745ed142009-04-25 18:35:21 +00002023 // Write the declaration offsets array
2024 Abbrev = new BitCodeAbbrev();
2025 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2028 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2029 Record.clear();
2030 Record.push_back(pch::DECL_OFFSET);
2031 Record.push_back(DeclOffsets.size());
2032 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002033 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002034 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002035
Douglas Gregord4df8652009-04-22 22:02:47 +00002036 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002037 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002038 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002039
2040 // Write the record containing tentative definitions.
2041 if (!TentativeDefinitions.empty())
2042 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002043
2044 // Write the record containing locally-scoped external definitions.
2045 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002046 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002047 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002048
2049 // Write the record containing ext_vector type names.
2050 if (!ExtVectorDecls.empty())
2051 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002052
Douglas Gregor08f01292009-04-17 22:13:46 +00002053 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002054 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002055 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002056 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002057 Record.push_back(NumLexicalDeclContexts);
2058 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002059 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002060 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002061}
2062
2063void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2064 Record.push_back(Loc.getRawEncoding());
2065}
2066
2067void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2068 Record.push_back(Value.getBitWidth());
2069 unsigned N = Value.getNumWords();
2070 const uint64_t* Words = Value.getRawData();
2071 for (unsigned I = 0; I != N; ++I)
2072 Record.push_back(Words[I]);
2073}
2074
Douglas Gregor1daeb692009-04-13 18:14:40 +00002075void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2076 Record.push_back(Value.isUnsigned());
2077 AddAPInt(Value, Record);
2078}
2079
Douglas Gregore0a3a512009-04-14 21:55:33 +00002080void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2081 AddAPInt(Value.bitcastToAPInt(), Record);
2082}
2083
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002084void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002085 Record.push_back(getIdentifierRef(II));
2086}
2087
2088pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2089 if (II == 0)
2090 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002091
2092 pch::IdentID &ID = IdentifierIDs[II];
2093 if (ID == 0)
2094 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002095 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002096}
2097
Steve Naroff2ddea052009-04-23 10:39:46 +00002098void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2099 if (SelRef.getAsOpaquePtr() == 0) {
2100 Record.push_back(0);
2101 return;
2102 }
2103
2104 pch::SelectorID &SID = SelectorIDs[SelRef];
2105 if (SID == 0) {
2106 SID = SelectorIDs.size();
2107 SelVector.push_back(SelRef);
2108 }
2109 Record.push_back(SID);
2110}
2111
John McCall8f115c62009-10-16 21:56:05 +00002112void PCHWriter::AddDeclaratorInfo(DeclaratorInfo *DInfo, RecordData &Record) {
2113 if (DInfo == 0) {
2114 AddTypeRef(QualType(), Record);
2115 return;
2116 }
2117
John McCall17001972009-10-18 01:05:36 +00002118 AddTypeRef(DInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002119 TypeLocWriter TLW(*this, Record);
2120 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2121 TLW.Visit(TL);
2122}
2123
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002124void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2125 if (T.isNull()) {
2126 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2127 return;
2128 }
2129
John McCall8ccfcb52009-09-24 19:53:00 +00002130 unsigned FastQuals = T.getFastQualifiers();
2131 T.removeFastQualifiers();
2132
2133 if (T.hasNonFastQualifiers()) {
2134 pch::TypeID &ID = TypeIDs[T];
2135 if (ID == 0) {
2136 // We haven't seen these qualifiers applied to this type before.
2137 // Assign it a new ID. This is the only time we enqueue a
2138 // qualified type, and it has no CV qualifiers.
2139 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002140 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002141 }
2142
2143 // Encode the type qualifiers in the type reference.
2144 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2145 return;
2146 }
2147
2148 assert(!T.hasQualifiers());
2149
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002150 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002151 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002152 switch (BT->getKind()) {
2153 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2154 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2155 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2156 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2157 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2158 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2159 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2160 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002161 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002162 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2163 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2164 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2165 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2166 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2167 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2168 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002169 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002170 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2171 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2172 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002173 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002174 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2175 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002176 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2177 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002178 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2179 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002180 case BuiltinType::UndeducedAuto:
2181 assert(0 && "Should not see undeduced auto here");
2182 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002183 }
2184
John McCall8ccfcb52009-09-24 19:53:00 +00002185 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002186 return;
2187 }
2188
John McCall8ccfcb52009-09-24 19:53:00 +00002189 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002190 if (ID == 0) {
2191 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002192 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002193 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002194 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002195 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002196
2197 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002198 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002199}
2200
2201void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2202 if (D == 0) {
2203 Record.push_back(0);
2204 return;
2205 }
2206
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002207 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002208 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002209 // We haven't seen this declaration before. Give it a new ID and
2210 // enqueue it in the list of declarations to emit.
2211 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002212 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002213 }
2214
2215 Record.push_back(ID);
2216}
2217
Douglas Gregore84a9da2009-04-20 20:36:09 +00002218pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2219 if (D == 0)
2220 return 0;
2221
2222 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2223 return DeclIDs[D];
2224}
2225
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002226void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002227 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002228 Record.push_back(Name.getNameKind());
2229 switch (Name.getNameKind()) {
2230 case DeclarationName::Identifier:
2231 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2232 break;
2233
2234 case DeclarationName::ObjCZeroArgSelector:
2235 case DeclarationName::ObjCOneArgSelector:
2236 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002237 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002238 break;
2239
2240 case DeclarationName::CXXConstructorName:
2241 case DeclarationName::CXXDestructorName:
2242 case DeclarationName::CXXConversionFunctionName:
2243 AddTypeRef(Name.getCXXNameType(), Record);
2244 break;
2245
2246 case DeclarationName::CXXOperatorName:
2247 Record.push_back(Name.getCXXOverloadedOperator());
2248 break;
2249
2250 case DeclarationName::CXXUsingDirective:
2251 // No extra data to emit
2252 break;
2253 }
2254}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002255