blob: a15576a82f0cea2551d4dd7d8c14eae96299284d [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
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213PCHTypeWriter::VisitTemplateSpecializationType(
214 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000215 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 assert(false && "Cannot serialize template specialization types");
217}
218
219void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000220 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000221 assert(false && "Cannot serialize qualified name types");
222}
223
224void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
225 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000227 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
228 E = T->qual_end(); I != E; ++I)
229 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000230 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000231}
232
Steve Narofffb4330f2009-06-17 22:40:22 +0000233void
234PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000235 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000237 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000238 E = T->qual_end(); I != E; ++I)
239 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000240 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241}
242
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +0000243void PCHTypeWriter::VisitObjCProtocolListType(const ObjCProtocolListType *T) {
244 Writer.AddTypeRef(T->getBaseType(), Record);
245 Record.push_back(T->getNumProtocols());
246 for (ObjCProtocolListType::qual_iterator I = T->qual_begin(),
247 E = T->qual_end(); I != E; ++I)
248 Writer.AddDeclRef(*I, Record);
249 Code = pch::TYPE_OBJC_PROTOCOL_LIST;
250}
251
John McCall8f115c62009-10-16 21:56:05 +0000252namespace {
253
254class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
255 PCHWriter &Writer;
256 PCHWriter::RecordData &Record;
257
258public:
259 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
260 : Writer(Writer), Record(Record) { }
261
John McCall17001972009-10-18 01:05:36 +0000262#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000263#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000264 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000265#include "clang/AST/TypeLocNodes.def"
266
John McCall17001972009-10-18 01:05:36 +0000267 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
268 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000269};
270
271}
272
John McCall17001972009-10-18 01:05:36 +0000273void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
274 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000275}
John McCall17001972009-10-18 01:05:36 +0000276void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
277 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000278}
John McCall17001972009-10-18 01:05:36 +0000279void TypeLocWriter::VisitFixedWidthIntTypeLoc(FixedWidthIntTypeLoc TL) {
280 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000281}
John McCall17001972009-10-18 01:05:36 +0000282void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
283 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000284}
John McCall17001972009-10-18 01:05:36 +0000285void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
286 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000287}
John McCall17001972009-10-18 01:05:36 +0000288void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
289 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000290}
John McCall17001972009-10-18 01:05:36 +0000291void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
292 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000293}
John McCall17001972009-10-18 01:05:36 +0000294void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
295 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000296}
John McCall17001972009-10-18 01:05:36 +0000297void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
298 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000299}
John McCall17001972009-10-18 01:05:36 +0000300void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
301 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
302 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
303 Record.push_back(TL.getSizeExpr() ? 1 : 0);
304 if (TL.getSizeExpr())
305 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000306}
John McCall17001972009-10-18 01:05:36 +0000307void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
308 VisitArrayTypeLoc(TL);
309}
310void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
311 VisitArrayTypeLoc(TL);
312}
313void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
314 VisitArrayTypeLoc(TL);
315}
316void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
317 DependentSizedArrayTypeLoc TL) {
318 VisitArrayTypeLoc(TL);
319}
320void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
321 DependentSizedExtVectorTypeLoc TL) {
322 Writer.AddSourceLocation(TL.getNameLoc(), Record);
323}
324void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
325 Writer.AddSourceLocation(TL.getNameLoc(), Record);
326}
327void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
328 Writer.AddSourceLocation(TL.getNameLoc(), Record);
329}
330void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
331 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
332 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
333 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
334 Writer.AddDeclRef(TL.getArg(i), Record);
335}
336void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
337 VisitFunctionTypeLoc(TL);
338}
339void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
340 VisitFunctionTypeLoc(TL);
341}
342void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
343 Writer.AddSourceLocation(TL.getNameLoc(), Record);
344}
345void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
346 Writer.AddSourceLocation(TL.getNameLoc(), Record);
347}
348void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
349 Writer.AddSourceLocation(TL.getNameLoc(), Record);
350}
351void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
352 Writer.AddSourceLocation(TL.getNameLoc(), Record);
353}
354void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
355 Writer.AddSourceLocation(TL.getNameLoc(), Record);
356}
357void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
358 Writer.AddSourceLocation(TL.getNameLoc(), Record);
359}
360void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getNameLoc(), Record);
362}
363void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getNameLoc(), Record);
365}
366void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
367 TemplateSpecializationTypeLoc TL) {
368 Writer.AddSourceLocation(TL.getNameLoc(), Record);
369}
370void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getNameLoc(), Record);
372}
373void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getNameLoc(), Record);
375}
376void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getNameLoc(), Record);
378}
379void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getStarLoc(), Record);
381}
382void TypeLocWriter::VisitObjCProtocolListTypeLoc(ObjCProtocolListTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
384 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
385 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
386 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000387}
388
Chris Lattner19cea4e2009-04-22 05:57:30 +0000389//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000390// PCHWriter Implementation
391//===----------------------------------------------------------------------===//
392
Chris Lattner28fa4e62009-04-26 22:26:21 +0000393static void EmitBlockID(unsigned ID, const char *Name,
394 llvm::BitstreamWriter &Stream,
395 PCHWriter::RecordData &Record) {
396 Record.clear();
397 Record.push_back(ID);
398 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
399
400 // Emit the block name if present.
401 if (Name == 0 || Name[0] == 0) return;
402 Record.clear();
403 while (*Name)
404 Record.push_back(*Name++);
405 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
406}
407
408static void EmitRecordID(unsigned ID, const char *Name,
409 llvm::BitstreamWriter &Stream,
410 PCHWriter::RecordData &Record) {
411 Record.clear();
412 Record.push_back(ID);
413 while (*Name)
414 Record.push_back(*Name++);
415 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000416}
417
418static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
419 PCHWriter::RecordData &Record) {
420#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
421 RECORD(STMT_STOP);
422 RECORD(STMT_NULL_PTR);
423 RECORD(STMT_NULL);
424 RECORD(STMT_COMPOUND);
425 RECORD(STMT_CASE);
426 RECORD(STMT_DEFAULT);
427 RECORD(STMT_LABEL);
428 RECORD(STMT_IF);
429 RECORD(STMT_SWITCH);
430 RECORD(STMT_WHILE);
431 RECORD(STMT_DO);
432 RECORD(STMT_FOR);
433 RECORD(STMT_GOTO);
434 RECORD(STMT_INDIRECT_GOTO);
435 RECORD(STMT_CONTINUE);
436 RECORD(STMT_BREAK);
437 RECORD(STMT_RETURN);
438 RECORD(STMT_DECL);
439 RECORD(STMT_ASM);
440 RECORD(EXPR_PREDEFINED);
441 RECORD(EXPR_DECL_REF);
442 RECORD(EXPR_INTEGER_LITERAL);
443 RECORD(EXPR_FLOATING_LITERAL);
444 RECORD(EXPR_IMAGINARY_LITERAL);
445 RECORD(EXPR_STRING_LITERAL);
446 RECORD(EXPR_CHARACTER_LITERAL);
447 RECORD(EXPR_PAREN);
448 RECORD(EXPR_UNARY_OPERATOR);
449 RECORD(EXPR_SIZEOF_ALIGN_OF);
450 RECORD(EXPR_ARRAY_SUBSCRIPT);
451 RECORD(EXPR_CALL);
452 RECORD(EXPR_MEMBER);
453 RECORD(EXPR_BINARY_OPERATOR);
454 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
455 RECORD(EXPR_CONDITIONAL_OPERATOR);
456 RECORD(EXPR_IMPLICIT_CAST);
457 RECORD(EXPR_CSTYLE_CAST);
458 RECORD(EXPR_COMPOUND_LITERAL);
459 RECORD(EXPR_EXT_VECTOR_ELEMENT);
460 RECORD(EXPR_INIT_LIST);
461 RECORD(EXPR_DESIGNATED_INIT);
462 RECORD(EXPR_IMPLICIT_VALUE_INIT);
463 RECORD(EXPR_VA_ARG);
464 RECORD(EXPR_ADDR_LABEL);
465 RECORD(EXPR_STMT);
466 RECORD(EXPR_TYPES_COMPATIBLE);
467 RECORD(EXPR_CHOOSE);
468 RECORD(EXPR_GNU_NULL);
469 RECORD(EXPR_SHUFFLE_VECTOR);
470 RECORD(EXPR_BLOCK);
471 RECORD(EXPR_BLOCK_DECL_REF);
472 RECORD(EXPR_OBJC_STRING_LITERAL);
473 RECORD(EXPR_OBJC_ENCODE);
474 RECORD(EXPR_OBJC_SELECTOR_EXPR);
475 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
476 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
477 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
478 RECORD(EXPR_OBJC_KVC_REF_EXPR);
479 RECORD(EXPR_OBJC_MESSAGE_EXPR);
480 RECORD(EXPR_OBJC_SUPER_EXPR);
481 RECORD(STMT_OBJC_FOR_COLLECTION);
482 RECORD(STMT_OBJC_CATCH);
483 RECORD(STMT_OBJC_FINALLY);
484 RECORD(STMT_OBJC_AT_TRY);
485 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
486 RECORD(STMT_OBJC_AT_THROW);
487#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000488}
Mike Stump11289f42009-09-09 15:08:12 +0000489
Chris Lattner28fa4e62009-04-26 22:26:21 +0000490void PCHWriter::WriteBlockInfoBlock() {
491 RecordData Record;
492 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattner64031982009-04-27 00:40:25 +0000494#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000495#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000496
Chris Lattner28fa4e62009-04-26 22:26:21 +0000497 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000498 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000499 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000500 RECORD(TYPE_OFFSET);
501 RECORD(DECL_OFFSET);
502 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000503 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000504 RECORD(IDENTIFIER_OFFSET);
505 RECORD(IDENTIFIER_TABLE);
506 RECORD(EXTERNAL_DEFINITIONS);
507 RECORD(SPECIAL_TYPES);
508 RECORD(STATISTICS);
509 RECORD(TENTATIVE_DEFINITIONS);
510 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
511 RECORD(SELECTOR_OFFSETS);
512 RECORD(METHOD_POOL);
513 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000514 RECORD(SOURCE_LOCATION_OFFSETS);
515 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000516 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000517 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000518 RECORD(COMMENT_RANGES);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000519 RECORD(SVN_BRANCH_REVISION);
520
Chris Lattner28fa4e62009-04-26 22:26:21 +0000521 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000522 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000523 RECORD(SM_SLOC_FILE_ENTRY);
524 RECORD(SM_SLOC_BUFFER_ENTRY);
525 RECORD(SM_SLOC_BUFFER_BLOB);
526 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
527 RECORD(SM_LINE_TABLE);
528 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000529
Chris Lattner28fa4e62009-04-26 22:26:21 +0000530 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000531 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000532 RECORD(PP_MACRO_OBJECT_LIKE);
533 RECORD(PP_MACRO_FUNCTION_LIKE);
534 RECORD(PP_TOKEN);
535
Douglas Gregor12bfa382009-10-17 00:13:19 +0000536 // Decls and Types block.
537 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000538 RECORD(TYPE_EXT_QUAL);
539 RECORD(TYPE_FIXED_WIDTH_INT);
540 RECORD(TYPE_COMPLEX);
541 RECORD(TYPE_POINTER);
542 RECORD(TYPE_BLOCK_POINTER);
543 RECORD(TYPE_LVALUE_REFERENCE);
544 RECORD(TYPE_RVALUE_REFERENCE);
545 RECORD(TYPE_MEMBER_POINTER);
546 RECORD(TYPE_CONSTANT_ARRAY);
547 RECORD(TYPE_INCOMPLETE_ARRAY);
548 RECORD(TYPE_VARIABLE_ARRAY);
549 RECORD(TYPE_VECTOR);
550 RECORD(TYPE_EXT_VECTOR);
551 RECORD(TYPE_FUNCTION_PROTO);
552 RECORD(TYPE_FUNCTION_NO_PROTO);
553 RECORD(TYPE_TYPEDEF);
554 RECORD(TYPE_TYPEOF_EXPR);
555 RECORD(TYPE_TYPEOF);
556 RECORD(TYPE_RECORD);
557 RECORD(TYPE_ENUM);
558 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000559 RECORD(TYPE_OBJC_OBJECT_POINTER);
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +0000560 RECORD(TYPE_OBJC_PROTOCOL_LIST);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000561 RECORD(DECL_ATTR);
562 RECORD(DECL_TRANSLATION_UNIT);
563 RECORD(DECL_TYPEDEF);
564 RECORD(DECL_ENUM);
565 RECORD(DECL_RECORD);
566 RECORD(DECL_ENUM_CONSTANT);
567 RECORD(DECL_FUNCTION);
568 RECORD(DECL_OBJC_METHOD);
569 RECORD(DECL_OBJC_INTERFACE);
570 RECORD(DECL_OBJC_PROTOCOL);
571 RECORD(DECL_OBJC_IVAR);
572 RECORD(DECL_OBJC_AT_DEFS_FIELD);
573 RECORD(DECL_OBJC_CLASS);
574 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
575 RECORD(DECL_OBJC_CATEGORY);
576 RECORD(DECL_OBJC_CATEGORY_IMPL);
577 RECORD(DECL_OBJC_IMPLEMENTATION);
578 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
579 RECORD(DECL_OBJC_PROPERTY);
580 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000581 RECORD(DECL_FIELD);
582 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000583 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000584 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000585 RECORD(DECL_ORIGINAL_PARM_VAR);
586 RECORD(DECL_FILE_SCOPE_ASM);
587 RECORD(DECL_BLOCK);
588 RECORD(DECL_CONTEXT_LEXICAL);
589 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000590 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000591 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000592#undef RECORD
593#undef BLOCK
594 Stream.ExitBlock();
595}
596
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000597/// \brief Adjusts the given filename to only write out the portion of the
598/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000599///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000600/// \param Filename the file name to adjust.
601///
602/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
603/// the returned filename will be adjusted by this system root.
604///
605/// \returns either the original filename (if it needs no adjustment) or the
606/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000607static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000608adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
609 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000610
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000611 if (!isysroot)
612 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000613
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000614 // Verify that the filename and the system root have the same prefix.
615 unsigned Pos = 0;
616 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
617 if (Filename[Pos] != isysroot[Pos])
618 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000619
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000620 // We hit the end of the filename before we hit the end of the system root.
621 if (!Filename[Pos])
622 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000623
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000624 // If the file name has a '/' at the current position, skip over the '/'.
625 // We distinguish sysroot-based includes from absolute includes by the
626 // absence of '/' at the beginning of sysroot-based includes.
627 if (Filename[Pos] == '/')
628 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000629
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000630 return Filename + Pos;
631}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000632
Douglas Gregor7b71e632009-04-27 22:23:34 +0000633/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000634void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000635 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000636
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000637 // Metadata
638 const TargetInfo &Target = Context.Target;
639 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
640 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
641 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
642 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
643 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
644 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
645 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
646 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
647 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000648
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000649 RecordData Record;
650 Record.push_back(pch::METADATA);
651 Record.push_back(pch::VERSION_MAJOR);
652 Record.push_back(pch::VERSION_MINOR);
653 Record.push_back(CLANG_VERSION_MAJOR);
654 Record.push_back(CLANG_VERSION_MINOR);
655 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000656 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000657 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000658
Douglas Gregor45fe0362009-05-12 01:31:05 +0000659 // Original file name
660 SourceManager &SM = Context.getSourceManager();
661 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
662 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
663 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
664 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
665 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
666
667 llvm::sys::Path MainFilePath(MainFile->getName());
668 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000669
Douglas Gregor45fe0362009-05-12 01:31:05 +0000670 if (!MainFilePath.isAbsolute()) {
671 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000672 P.appendComponent(MainFilePath.str());
673 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000674 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000675 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000676 }
677
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000678 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000679 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000680 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000681 RecordData Record;
682 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000683 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000684 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000685
686 // Subversion branch/version information.
687 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
688 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
689 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
690 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
691 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
692 Record.clear();
693 Record.push_back(pch::SVN_BRANCH_REVISION);
694 Record.push_back(getClangSubversionRevision());
695 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000696}
697
698/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000699void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
700 RecordData Record;
701 Record.push_back(LangOpts.Trigraphs);
702 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
703 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
704 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
705 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
706 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
707 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
708 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
709 Record.push_back(LangOpts.C99); // C99 Support
710 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
711 Record.push_back(LangOpts.CPlusPlus); // C++ Support
712 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000713 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000714
Douglas Gregor55abb232009-04-10 20:39:37 +0000715 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
716 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
717 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000718
Douglas Gregor55abb232009-04-10 20:39:37 +0000719 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000720 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
721 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000722 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000723 Record.push_back(LangOpts.Exceptions); // Support exception handling.
724
725 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
726 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
727 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
728
Chris Lattner258172e2009-04-27 07:35:58 +0000729 // Whether static initializers are protected by locks.
730 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000731 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000732 Record.push_back(LangOpts.Blocks); // block extension to C
733 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
734 // they are unused.
735 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
736 // (modulo the platform support).
737
738 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
739 // signed integer arithmetic overflows.
740
741 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
742 // may be ripped out at any time.
743
744 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000745 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000746 // defined.
747 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
748 // opposed to __DYNAMIC__).
749 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
750
751 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
752 // used (instead of C99 semantics).
753 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000754 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
755 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000756 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
757 // unsigned type
Douglas Gregor55abb232009-04-10 20:39:37 +0000758 Record.push_back(LangOpts.getGCMode());
759 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000760 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000761 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000762 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000763 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000764 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000765}
766
Douglas Gregora7f71a92009-04-10 03:52:48 +0000767//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000768// stat cache Serialization
769//===----------------------------------------------------------------------===//
770
771namespace {
772// Trait used for the on-disk hash table of stat cache results.
773class VISIBILITY_HIDDEN PCHStatCacheTrait {
774public:
775 typedef const char * key_type;
776 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000777
Douglas Gregorc5046832009-04-27 18:38:38 +0000778 typedef std::pair<int, struct stat> data_type;
779 typedef const data_type& data_type_ref;
780
781 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000782 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000783 }
Mike Stump11289f42009-09-09 15:08:12 +0000784
785 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000786 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
787 data_type_ref Data) {
788 unsigned StrLen = strlen(path);
789 clang::io::Emit16(Out, StrLen);
790 unsigned DataLen = 1; // result value
791 if (Data.first == 0)
792 DataLen += 4 + 4 + 2 + 8 + 8;
793 clang::io::Emit8(Out, DataLen);
794 return std::make_pair(StrLen + 1, DataLen);
795 }
Mike Stump11289f42009-09-09 15:08:12 +0000796
Douglas Gregorc5046832009-04-27 18:38:38 +0000797 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
798 Out.write(path, KeyLen);
799 }
Mike Stump11289f42009-09-09 15:08:12 +0000800
Douglas Gregorc5046832009-04-27 18:38:38 +0000801 void EmitData(llvm::raw_ostream& Out, key_type_ref,
802 data_type_ref Data, unsigned DataLen) {
803 using namespace clang::io;
804 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000805
Douglas Gregorc5046832009-04-27 18:38:38 +0000806 // Result of stat()
807 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000808
Douglas Gregorc5046832009-04-27 18:38:38 +0000809 if (Data.first == 0) {
810 Emit32(Out, (uint32_t) Data.second.st_ino);
811 Emit32(Out, (uint32_t) Data.second.st_dev);
812 Emit16(Out, (uint16_t) Data.second.st_mode);
813 Emit64(Out, (uint64_t) Data.second.st_mtime);
814 Emit64(Out, (uint64_t) Data.second.st_size);
815 }
816
817 assert(Out.tell() - Start == DataLen && "Wrong data length");
818 }
819};
820} // end anonymous namespace
821
822/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000823void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
824 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000825 // Build the on-disk hash table containing information about every
826 // stat() call.
827 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
828 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000829 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000830 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000831 Stat != StatEnd; ++Stat, ++NumStatEntries) {
832 const char *Filename = Stat->first();
833 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
834 Generator.insert(Filename, Stat->second);
835 }
Mike Stump11289f42009-09-09 15:08:12 +0000836
Douglas Gregorc5046832009-04-27 18:38:38 +0000837 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000838 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000839 uint32_t BucketOffset;
840 {
841 llvm::raw_svector_ostream Out(StatCacheData);
842 // Make sure that no bucket is at offset 0
843 clang::io::Emit32(Out, 0);
844 BucketOffset = Generator.Emit(Out);
845 }
846
847 // Create a blob abbreviation
848 using namespace llvm;
849 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
850 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
852 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
853 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
854 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
855
856 // Write the stat cache
857 RecordData Record;
858 Record.push_back(pch::STAT_CACHE);
859 Record.push_back(BucketOffset);
860 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000861 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000862}
863
864//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000865// Source Manager Serialization
866//===----------------------------------------------------------------------===//
867
868/// \brief Create an abbreviation for the SLocEntry that refers to a
869/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000870static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000871 using namespace llvm;
872 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
873 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000879 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000880}
881
882/// \brief Create an abbreviation for the SLocEntry that refers to a
883/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000884static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000885 using namespace llvm;
886 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
887 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
889 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
891 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
892 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000893 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000894}
895
896/// \brief Create an abbreviation for the SLocEntry that refers to a
897/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000898static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000899 using namespace llvm;
900 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
901 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000903 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000904}
905
906/// \brief Create an abbreviation for the SLocEntry that refers to an
907/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000908static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000909 using namespace llvm;
910 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
911 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000917 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000918}
919
920/// \brief Writes the block containing the serialized form of the
921/// source manager.
922///
923/// TODO: We should probably use an on-disk hash table (stored in a
924/// blob), indexed based on the file name, so that we only create
925/// entries for files that we actually need. In the common case (no
926/// errors), we probably won't have to create file entries for any of
927/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000928void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000929 const Preprocessor &PP,
930 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000931 RecordData Record;
932
Chris Lattner0910e3b2009-04-10 17:16:57 +0000933 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000934 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000935
936 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000937 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
938 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
939 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
940 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000941
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000942 // Write the line table.
943 if (SourceMgr.hasLineTable()) {
944 LineTableInfo &LineTable = SourceMgr.getLineTable();
945
946 // Emit the file names
947 Record.push_back(LineTable.getNumFilenames());
948 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
949 // Emit the file name
950 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000951 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000952 unsigned FilenameLen = Filename? strlen(Filename) : 0;
953 Record.push_back(FilenameLen);
954 if (FilenameLen)
955 Record.insert(Record.end(), Filename, Filename + FilenameLen);
956 }
Mike Stump11289f42009-09-09 15:08:12 +0000957
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000958 // Emit the line entries
959 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
960 L != LEnd; ++L) {
961 // Emit the file ID
962 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000963
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000964 // Emit the line entries
965 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000966 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000967 LEEnd = L->second.end();
968 LE != LEEnd; ++LE) {
969 Record.push_back(LE->FileOffset);
970 Record.push_back(LE->LineNo);
971 Record.push_back(LE->FilenameID);
972 Record.push_back((unsigned)LE->FileKind);
973 Record.push_back(LE->IncludeOffset);
974 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000975 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +0000976 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000977 }
978
Douglas Gregor258ae542009-04-27 06:38:32 +0000979 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +0000980 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +0000981 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000982 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +0000983 E = HS.header_file_end();
984 I != E; ++I) {
985 Record.push_back(I->isImport);
986 Record.push_back(I->DirInfo);
987 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +0000988 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +0000989 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
990 Record.clear();
991 }
992
Douglas Gregor258ae542009-04-27 06:38:32 +0000993 // Write out the source location entry table. We skip the first
994 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +0000995 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +0000996 RecordData PreloadSLocs;
997 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +0000998 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
999 // Get this source location entry.
1000 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1001
Douglas Gregor258ae542009-04-27 06:38:32 +00001002 // Record the offset of this source-location entry.
1003 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1004
1005 // Figure out which record code to use.
1006 unsigned Code;
1007 if (SLoc->isFile()) {
1008 if (SLoc->getFile().getContentCache()->Entry)
1009 Code = pch::SM_SLOC_FILE_ENTRY;
1010 else
1011 Code = pch::SM_SLOC_BUFFER_ENTRY;
1012 } else
1013 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1014 Record.clear();
1015 Record.push_back(Code);
1016
1017 Record.push_back(SLoc->getOffset());
1018 if (SLoc->isFile()) {
1019 const SrcMgr::FileInfo &File = SLoc->getFile();
1020 Record.push_back(File.getIncludeLoc().getRawEncoding());
1021 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1022 Record.push_back(File.hasLineDirectives());
1023
1024 const SrcMgr::ContentCache *Content = File.getContentCache();
1025 if (Content->Entry) {
1026 // The source location entry is a file. The blob associated
1027 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001028
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001029 // Turn the file name into an absolute path, if it isn't already.
1030 const char *Filename = Content->Entry->getName();
1031 llvm::sys::Path FilePath(Filename, strlen(Filename));
1032 std::string FilenameStr;
1033 if (!FilePath.isAbsolute()) {
1034 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +00001035 P.appendComponent(FilePath.str());
1036 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001037 Filename = FilenameStr.c_str();
1038 }
Mike Stump11289f42009-09-09 15:08:12 +00001039
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001040 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001041 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001042
1043 // FIXME: For now, preload all file source locations, so that
1044 // we get the appropriate File entries in the reader. This is
1045 // a temporary measure.
1046 PreloadSLocs.push_back(SLocEntryOffsets.size());
1047 } else {
1048 // The source location entry is a buffer. The blob associated
1049 // with this entry contains the contents of the buffer.
1050
1051 // We add one to the size so that we capture the trailing NULL
1052 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1053 // the reader side).
1054 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1055 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001056 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1057 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001058 Record.clear();
1059 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1060 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001061 llvm::StringRef(Buffer->getBufferStart(),
1062 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001063
1064 if (strcmp(Name, "<built-in>") == 0)
1065 PreloadSLocs.push_back(SLocEntryOffsets.size());
1066 }
1067 } else {
1068 // The source location entry is an instantiation.
1069 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1070 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1071 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1072 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1073
1074 // Compute the token length for this macro expansion.
1075 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001076 if (I + 1 != N)
1077 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001078 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1079 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1080 }
1081 }
1082
Douglas Gregor8f45df52009-04-16 22:23:12 +00001083 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001084
1085 if (SLocEntryOffsets.empty())
1086 return;
1087
1088 // Write the source-location offsets table into the PCH block. This
1089 // table is used for lazily loading source-location information.
1090 using namespace llvm;
1091 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1092 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1093 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1094 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1095 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1096 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001097
Douglas Gregor258ae542009-04-27 06:38:32 +00001098 Record.clear();
1099 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1100 Record.push_back(SLocEntryOffsets.size());
1101 Record.push_back(SourceMgr.getNextOffset());
1102 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001103 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001104 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001105
1106 // Write the source location entry preloads array, telling the PCH
1107 // reader which source locations entries it should load eagerly.
1108 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001109}
1110
Douglas Gregorc5046832009-04-27 18:38:38 +00001111//===----------------------------------------------------------------------===//
1112// Preprocessor Serialization
1113//===----------------------------------------------------------------------===//
1114
Chris Lattnereeffaef2009-04-10 17:15:23 +00001115/// \brief Writes the block containing the serialized form of the
1116/// preprocessor.
1117///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001118void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001119 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001120
Chris Lattner0af3ba12009-04-13 01:29:17 +00001121 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1122 if (PP.getCounterValue() != 0) {
1123 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001124 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001125 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001126 }
1127
1128 // Enter the preprocessor block.
1129 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001130
Douglas Gregoreda6a892009-04-26 00:07:37 +00001131 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1132 // FIXME: use diagnostics subsystem for localization etc.
1133 if (PP.SawDateOrTime())
1134 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001135
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001136 // Loop over all the macro definitions that are live at the end of the file,
1137 // emitting each to the PP section.
Douglas Gregor45053152009-10-17 17:25:45 +00001138 // FIXME: Make sure that this sees macros defined in included PCH files.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001139 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1140 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001141 // FIXME: This emits macros in hash table order, we should do it in a stable
1142 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001143 MacroInfo *MI = I->second;
1144
1145 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1146 // been redefined by the header (in which case they are not isBuiltinMacro).
1147 if (MI->isBuiltinMacro())
1148 continue;
1149
Douglas Gregorc3366a52009-04-21 23:56:24 +00001150 // FIXME: Remove this identifier reference?
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001151 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001152 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001153 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1154 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001155
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001156 unsigned Code;
1157 if (MI->isObjectLike()) {
1158 Code = pch::PP_MACRO_OBJECT_LIKE;
1159 } else {
1160 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001161
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001162 Record.push_back(MI->isC99Varargs());
1163 Record.push_back(MI->isGNUVarargs());
1164 Record.push_back(MI->getNumArgs());
1165 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1166 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001167 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001168 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001169 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001170 Record.clear();
1171
Chris Lattner2199f5b2009-04-10 18:08:30 +00001172 // Emit the tokens array.
1173 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1174 // Note that we know that the preprocessor does not have any annotation
1175 // tokens in it because they are created by the parser, and thus can't be
1176 // in a macro definition.
1177 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001178
Chris Lattner2199f5b2009-04-10 18:08:30 +00001179 Record.push_back(Tok.getLocation().getRawEncoding());
1180 Record.push_back(Tok.getLength());
1181
Chris Lattner2199f5b2009-04-10 18:08:30 +00001182 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1183 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001184 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001185
Chris Lattner2199f5b2009-04-10 18:08:30 +00001186 // FIXME: Should translate token kind to a stable encoding.
1187 Record.push_back(Tok.getKind());
1188 // FIXME: Should translate token flags to a stable encoding.
1189 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001190
Douglas Gregor8f45df52009-04-16 22:23:12 +00001191 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001192 Record.clear();
1193 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001194 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001195 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001196 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001197}
1198
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001199void PCHWriter::WriteComments(ASTContext &Context) {
1200 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001201
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001202 if (Context.Comments.empty())
1203 return;
Mike Stump11289f42009-09-09 15:08:12 +00001204
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001205 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1206 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1207 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1208 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001209
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001210 RecordData Record;
1211 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001212 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001213 (const char*)&Context.Comments[0],
1214 Context.Comments.size() * sizeof(SourceRange));
1215}
1216
Douglas Gregorc5046832009-04-27 18:38:38 +00001217//===----------------------------------------------------------------------===//
1218// Type Serialization
1219//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001220
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001221/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001222void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001223 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001224 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001225 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001226
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001227 // Record the offset for this type.
1228 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001229 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001230 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1231 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001232 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001233 }
1234
1235 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001236
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001237 // Emit the type's representation.
1238 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001239
1240 if (T.hasNonFastQualifiers()) {
1241 Qualifiers Qs = T.getQualifiers();
1242 AddTypeRef(T.getUnqualifiedType(), Record);
1243 Record.push_back(Qs.getAsOpaqueValue());
1244 W.Code = pch::TYPE_EXT_QUAL;
1245 } else {
1246 switch (T->getTypeClass()) {
1247 // For all of the concrete, non-dependent types, call the
1248 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001249#define TYPE(Class, Base) \
John McCall8ccfcb52009-09-24 19:53:00 +00001250 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001251#define ABSTRACT_TYPE(Class, Base)
1252#define DEPENDENT_TYPE(Class, Base)
1253#include "clang/AST/TypeNodes.def"
1254
John McCall8ccfcb52009-09-24 19:53:00 +00001255 // For all of the dependent type nodes (which only occur in C++
1256 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001257#define TYPE(Class, Base)
1258#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1259#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001260 assert(false && "Cannot serialize dependent type nodes");
1261 break;
1262 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001263 }
1264
1265 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001266 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001267
1268 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001269 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001270}
1271
Douglas Gregorc5046832009-04-27 18:38:38 +00001272//===----------------------------------------------------------------------===//
1273// Declaration Serialization
1274//===----------------------------------------------------------------------===//
1275
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001276/// \brief Write the block containing all of the declaration IDs
1277/// lexically declared within the given DeclContext.
1278///
1279/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1280/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001281uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001282 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001283 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001284 return 0;
1285
Douglas Gregor8f45df52009-04-16 22:23:12 +00001286 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001287 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001288 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1289 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001290 AddDeclRef(*D, Record);
1291
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001292 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001293 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001294 return Offset;
1295}
1296
1297/// \brief Write the block containing all of the declaration IDs
1298/// visible from the given DeclContext.
1299///
1300/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1301/// bistream, or 0 if no block was written.
1302uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1303 DeclContext *DC) {
1304 if (DC->getPrimaryContext() != DC)
1305 return 0;
1306
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001307 // Since there is no name lookup into functions or methods, and we
1308 // perform name lookup for the translation unit via the
1309 // IdentifierInfo chains, don't bother to build a
1310 // visible-declarations table for these entities.
1311 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001312 return 0;
1313
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001314 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001315 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001316
1317 // Serialize the contents of the mapping used for lookup. Note that,
1318 // although we have two very different code paths, the serialized
1319 // representation is the same for both cases: a declaration name,
1320 // followed by a size, followed by references to the visible
1321 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001322 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001323 RecordData Record;
1324 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001325 if (!Map)
1326 return 0;
1327
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001328 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1329 D != DEnd; ++D) {
1330 AddDeclarationName(D->first, Record);
1331 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1332 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001333 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001334 AddDeclRef(*Result.first, Record);
1335 }
1336
1337 if (Record.size() == 0)
1338 return 0;
1339
Douglas Gregor8f45df52009-04-16 22:23:12 +00001340 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001341 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001342 return Offset;
1343}
1344
Douglas Gregorc5046832009-04-27 18:38:38 +00001345//===----------------------------------------------------------------------===//
1346// Global Method Pool and Selector Serialization
1347//===----------------------------------------------------------------------===//
1348
Douglas Gregore84a9da2009-04-20 20:36:09 +00001349namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001350// Trait used for the on-disk hash table used in the method pool.
1351class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1352 PCHWriter &Writer;
1353
1354public:
1355 typedef Selector key_type;
1356 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001357
Douglas Gregorc78d3462009-04-24 21:10:55 +00001358 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1359 typedef const data_type& data_type_ref;
1360
1361 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001362
Douglas Gregorc78d3462009-04-24 21:10:55 +00001363 static unsigned ComputeHash(Selector Sel) {
1364 unsigned N = Sel.getNumArgs();
1365 if (N == 0)
1366 ++N;
1367 unsigned R = 5381;
1368 for (unsigned I = 0; I != N; ++I)
1369 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001370 R = llvm::HashString(II->getName(), R);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001371 return R;
1372 }
Mike Stump11289f42009-09-09 15:08:12 +00001373
1374 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001375 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1376 data_type_ref Methods) {
1377 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1378 clang::io::Emit16(Out, KeyLen);
1379 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001380 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001381 Method = Method->Next)
1382 if (Method->Method)
1383 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001384 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001385 Method = Method->Next)
1386 if (Method->Method)
1387 DataLen += 4;
1388 clang::io::Emit16(Out, DataLen);
1389 return std::make_pair(KeyLen, DataLen);
1390 }
Mike Stump11289f42009-09-09 15:08:12 +00001391
Douglas Gregor95c13f52009-04-25 17:48:32 +00001392 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001393 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001394 assert((Start >> 32) == 0 && "Selector key offset too large");
1395 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001396 unsigned N = Sel.getNumArgs();
1397 clang::io::Emit16(Out, N);
1398 if (N == 0)
1399 N = 1;
1400 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001401 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001402 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1403 }
Mike Stump11289f42009-09-09 15:08:12 +00001404
Douglas Gregorc78d3462009-04-24 21:10:55 +00001405 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001406 data_type_ref Methods, unsigned DataLen) {
1407 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001408 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001409 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001410 Method = Method->Next)
1411 if (Method->Method)
1412 ++NumInstanceMethods;
1413
1414 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001415 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001416 Method = Method->Next)
1417 if (Method->Method)
1418 ++NumFactoryMethods;
1419
1420 clang::io::Emit16(Out, NumInstanceMethods);
1421 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001422 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001423 Method = Method->Next)
1424 if (Method->Method)
1425 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001426 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001427 Method = Method->Next)
1428 if (Method->Method)
1429 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001430
1431 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001432 }
1433};
1434} // end anonymous namespace
1435
1436/// \brief Write the method pool into the PCH file.
1437///
1438/// The method pool contains both instance and factory methods, stored
1439/// in an on-disk hash table indexed by the selector.
1440void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1441 using namespace llvm;
1442
1443 // Create and write out the blob that contains the instance and
1444 // factor method pools.
1445 bool Empty = true;
1446 {
1447 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001448
Douglas Gregorc78d3462009-04-24 21:10:55 +00001449 // Create the on-disk hash table representation. Start by
1450 // iterating through the instance method pool.
1451 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001452 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001453 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001454 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001455 InstanceEnd = SemaRef.InstanceMethodPool.end();
1456 Instance != InstanceEnd; ++Instance) {
1457 // Check whether there is a factory method with the same
1458 // selector.
1459 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1460 = SemaRef.FactoryMethodPool.find(Instance->first);
1461
1462 if (Factory == SemaRef.FactoryMethodPool.end())
1463 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001464 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001465 ObjCMethodList()));
1466 else
1467 Generator.insert(Instance->first,
1468 std::make_pair(Instance->second, Factory->second));
1469
Douglas Gregor95c13f52009-04-25 17:48:32 +00001470 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001471 Empty = false;
1472 }
1473
1474 // Now iterate through the factory method pool, to pick up any
1475 // selectors that weren't already in the instance method pool.
1476 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001477 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001478 FactoryEnd = SemaRef.FactoryMethodPool.end();
1479 Factory != FactoryEnd; ++Factory) {
1480 // Check whether there is an instance method with the same
1481 // selector. If so, there is no work to do here.
1482 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1483 = SemaRef.InstanceMethodPool.find(Factory->first);
1484
Douglas Gregor95c13f52009-04-25 17:48:32 +00001485 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001486 Generator.insert(Factory->first,
1487 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001488 ++NumSelectorsInMethodPool;
1489 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001490
1491 Empty = false;
1492 }
1493
Douglas Gregor95c13f52009-04-25 17:48:32 +00001494 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001495 return;
1496
1497 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001498 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001499 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001500 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001501 {
1502 PCHMethodPoolTrait Trait(*this);
1503 llvm::raw_svector_ostream Out(MethodPool);
1504 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001505 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001506 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001507
1508 // For every selector that we have seen but which was not
1509 // written into the hash table, write the selector itself and
1510 // record it's offset.
1511 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1512 if (SelectorOffsets[I] == 0)
1513 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001514 }
1515
1516 // Create a blob abbreviation
1517 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1518 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001521 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1522 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1523
Douglas Gregor95c13f52009-04-25 17:48:32 +00001524 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001525 RecordData Record;
1526 Record.push_back(pch::METHOD_POOL);
1527 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001528 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001529 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001530
1531 // Create a blob abbreviation for the selector table offsets.
1532 Abbrev = new BitCodeAbbrev();
1533 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1535 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1536 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1537
1538 // Write the selector offsets table.
1539 Record.clear();
1540 Record.push_back(pch::SELECTOR_OFFSETS);
1541 Record.push_back(SelectorOffsets.size());
1542 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1543 (const char *)&SelectorOffsets.front(),
1544 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001545 }
1546}
1547
Douglas Gregorc5046832009-04-27 18:38:38 +00001548//===----------------------------------------------------------------------===//
1549// Identifier Table Serialization
1550//===----------------------------------------------------------------------===//
1551
Douglas Gregorc78d3462009-04-24 21:10:55 +00001552namespace {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001553class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1554 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001555 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001556
Douglas Gregor1d583f22009-04-28 21:18:29 +00001557 /// \brief Determines whether this is an "interesting" identifier
1558 /// that needs a full IdentifierInfo structure written into the hash
1559 /// table.
1560 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1561 return II->isPoisoned() ||
1562 II->isExtensionToken() ||
1563 II->hasMacroDefinition() ||
1564 II->getObjCOrBuiltinID() ||
1565 II->getFETokenInfo<void>();
1566 }
1567
Douglas Gregore84a9da2009-04-20 20:36:09 +00001568public:
1569 typedef const IdentifierInfo* key_type;
1570 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001571
Douglas Gregore84a9da2009-04-20 20:36:09 +00001572 typedef pch::IdentID data_type;
1573 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001574
1575 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001576 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001577
1578 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001579 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001580 }
Mike Stump11289f42009-09-09 15:08:12 +00001581
1582 std::pair<unsigned,unsigned>
1583 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001584 pch::IdentID ID) {
1585 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001586 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1587 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001588 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001589 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001590 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001591 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001592 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1593 DEnd = IdentifierResolver::end();
1594 D != DEnd; ++D)
1595 DataLen += sizeof(pch::DeclID);
1596 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001597 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001598 // We emit the key length after the data length so that every
1599 // string is preceded by a 16-bit length. This matches the PTH
1600 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001601 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001602 return std::make_pair(KeyLen, DataLen);
1603 }
Mike Stump11289f42009-09-09 15:08:12 +00001604
1605 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001606 unsigned KeyLen) {
1607 // Record the location of the key data. This is used when generating
1608 // the mapping from persistent IDs to strings.
1609 Writer.SetIdentifierOffset(II, Out.tell());
1610 Out.write(II->getName(), KeyLen);
1611 }
Mike Stump11289f42009-09-09 15:08:12 +00001612
1613 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001614 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001615 if (!isInterestingIdentifier(II)) {
1616 clang::io::Emit32(Out, ID << 1);
1617 return;
1618 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001619
Douglas Gregor1d583f22009-04-28 21:18:29 +00001620 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001621 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001622 bool hasMacroDefinition =
1623 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001624 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001625 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001626 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001627 Bits = (Bits << 1) | II->isExtensionToken();
1628 Bits = (Bits << 1) | II->isPoisoned();
1629 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregorb9256522009-04-28 21:32:13 +00001630 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001631
Douglas Gregorc3366a52009-04-21 23:56:24 +00001632 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001633 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001634
Douglas Gregora868bbd2009-04-21 22:25:48 +00001635 // Emit the declaration IDs in reverse order, because the
1636 // IdentifierResolver provides the declarations as they would be
1637 // visible (e.g., the function "stat" would come before the struct
1638 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1639 // adds declarations to the end of the list (so we need to see the
1640 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001641 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001642 IdentifierResolver::end());
1643 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1644 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001645 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001646 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001647 }
1648};
1649} // end anonymous namespace
1650
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001651/// \brief Write the identifier table into the PCH file.
1652///
1653/// The identifier table consists of a blob containing string data
1654/// (the actual identifiers themselves) and a separate "offsets" index
1655/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001656void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001657 using namespace llvm;
1658
1659 // Create and write out the blob that contains the identifier
1660 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001661 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001662 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001663
Douglas Gregore6648fb2009-04-28 20:33:11 +00001664 // Look for any identifiers that were named while processing the
1665 // headers, but are otherwise not needed. We add these to the hash
1666 // table to enable checking of the predefines buffer in the case
1667 // where the user adds new macro definitions when building the PCH
1668 // file.
1669 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1670 IDEnd = PP.getIdentifierTable().end();
1671 ID != IDEnd; ++ID)
1672 getIdentifierRef(ID->second);
1673
Douglas Gregore84a9da2009-04-20 20:36:09 +00001674 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001675 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001676 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1677 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1678 ID != IDEnd; ++ID) {
1679 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001680 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001681 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001682
Douglas Gregore84a9da2009-04-20 20:36:09 +00001683 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001684 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001685 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001686 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001687 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001688 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001689 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001690 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001691 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001692 }
1693
1694 // Create a blob abbreviation
1695 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1696 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001697 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001698 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001699 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001700
1701 // Write the identifier table
1702 RecordData Record;
1703 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001704 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001705 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001706 }
1707
1708 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001709 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1710 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1713 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1714
1715 RecordData Record;
1716 Record.push_back(pch::IDENTIFIER_OFFSET);
1717 Record.push_back(IdentifierOffsets.size());
1718 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1719 (const char *)&IdentifierOffsets.front(),
1720 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001721}
1722
Douglas Gregorc5046832009-04-27 18:38:38 +00001723//===----------------------------------------------------------------------===//
1724// General Serialization Routines
1725//===----------------------------------------------------------------------===//
1726
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001727/// \brief Write a record containing the given attributes.
1728void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1729 RecordData Record;
1730 for (; Attr; Attr = Attr->getNext()) {
1731 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1732 Record.push_back(Attr->isInherited());
1733 switch (Attr->getKind()) {
1734 case Attr::Alias:
1735 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1736 break;
1737
1738 case Attr::Aligned:
1739 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1740 break;
1741
1742 case Attr::AlwaysInline:
1743 break;
Mike Stump11289f42009-09-09 15:08:12 +00001744
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001745 case Attr::AnalyzerNoReturn:
1746 break;
1747
1748 case Attr::Annotate:
1749 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1750 break;
1751
1752 case Attr::AsmLabel:
1753 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1754 break;
1755
1756 case Attr::Blocks:
1757 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1758 break;
1759
1760 case Attr::Cleanup:
1761 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1762 break;
1763
1764 case Attr::Const:
1765 break;
1766
1767 case Attr::Constructor:
1768 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1769 break;
1770
1771 case Attr::DLLExport:
1772 case Attr::DLLImport:
1773 case Attr::Deprecated:
1774 break;
1775
1776 case Attr::Destructor:
1777 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1778 break;
1779
1780 case Attr::FastCall:
1781 break;
1782
1783 case Attr::Format: {
1784 const FormatAttr *Format = cast<FormatAttr>(Attr);
1785 AddString(Format->getType(), Record);
1786 Record.push_back(Format->getFormatIdx());
1787 Record.push_back(Format->getFirstArg());
1788 break;
1789 }
1790
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001791 case Attr::FormatArg: {
1792 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1793 Record.push_back(Format->getFormatIdx());
1794 break;
1795 }
1796
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001797 case Attr::Sentinel : {
1798 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1799 Record.push_back(Sentinel->getSentinel());
1800 Record.push_back(Sentinel->getNullPos());
1801 break;
1802 }
Mike Stump11289f42009-09-09 15:08:12 +00001803
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001804 case Attr::GNUInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001805 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001806 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001807 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001808 case Attr::NoReturn:
1809 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001810 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001811 break;
1812
1813 case Attr::NonNull: {
1814 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1815 Record.push_back(NonNull->size());
1816 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1817 break;
1818 }
1819
1820 case Attr::ObjCException:
1821 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001822 case Attr::CFReturnsRetained:
1823 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001824 case Attr::Overloadable:
1825 break;
1826
Anders Carlsson68e0b682009-08-08 18:23:56 +00001827 case Attr::PragmaPack:
1828 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001829 break;
1830
Anders Carlsson68e0b682009-08-08 18:23:56 +00001831 case Attr::Packed:
1832 break;
Mike Stump11289f42009-09-09 15:08:12 +00001833
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001834 case Attr::Pure:
1835 break;
1836
1837 case Attr::Regparm:
1838 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1839 break;
Mike Stump11289f42009-09-09 15:08:12 +00001840
Nate Begemanf2758702009-06-26 06:32:41 +00001841 case Attr::ReqdWorkGroupSize:
1842 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1843 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1844 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1845 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001846
1847 case Attr::Section:
1848 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1849 break;
1850
1851 case Attr::StdCall:
1852 case Attr::TransparentUnion:
1853 case Attr::Unavailable:
1854 case Attr::Unused:
1855 case Attr::Used:
1856 break;
1857
1858 case Attr::Visibility:
1859 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001860 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001861 break;
1862
1863 case Attr::WarnUnusedResult:
1864 case Attr::Weak:
1865 case Attr::WeakImport:
1866 break;
1867 }
1868 }
1869
Douglas Gregor8f45df52009-04-16 22:23:12 +00001870 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001871}
1872
1873void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1874 Record.push_back(Str.size());
1875 Record.insert(Record.end(), Str.begin(), Str.end());
1876}
1877
Douglas Gregore84a9da2009-04-20 20:36:09 +00001878/// \brief Note that the identifier II occurs at the given offset
1879/// within the identifier table.
1880void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001881 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001882}
1883
Douglas Gregor95c13f52009-04-25 17:48:32 +00001884/// \brief Note that the selector Sel occurs at the given offset
1885/// within the method pool/selector table.
1886void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1887 unsigned ID = SelectorIDs[Sel];
1888 assert(ID && "Unknown selector");
1889 SelectorOffsets[ID - 1] = Offset;
1890}
1891
Mike Stump11289f42009-09-09 15:08:12 +00001892PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1893 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001894 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1895 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001896
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001897void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1898 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001899 using namespace llvm;
1900
Douglas Gregor162dd022009-04-20 15:53:59 +00001901 ASTContext &Context = SemaRef.Context;
1902 Preprocessor &PP = SemaRef.PP;
1903
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001904 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001905 Stream.Emit((unsigned)'C', 8);
1906 Stream.Emit((unsigned)'P', 8);
1907 Stream.Emit((unsigned)'C', 8);
1908 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001909
Chris Lattner28fa4e62009-04-26 22:26:21 +00001910 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001911
1912 // The translation unit is the first declaration we'll emit.
1913 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001914 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001915
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001916 // Make sure that we emit IdentifierInfos (and any attached
1917 // declarations) for builtins.
1918 {
1919 IdentifierTable &Table = PP.getIdentifierTable();
1920 llvm::SmallVector<const char *, 32> BuiltinNames;
1921 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1922 Context.getLangOptions().NoBuiltin);
1923 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1924 getIdentifierRef(&Table.get(BuiltinNames[I]));
1925 }
1926
Chris Lattner0c797362009-09-08 18:19:27 +00001927 // Build a record containing all of the tentative definitions in this file, in
1928 // TentativeDefinitionList order. Generally, this record will be empty for
1929 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001930 RecordData TentativeDefinitions;
Chris Lattner0c797362009-09-08 18:19:27 +00001931 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1932 VarDecl *VD =
1933 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1934 if (VD) AddDeclRef(VD, TentativeDefinitions);
1935 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001936
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001937 // Build a record containing all of the locally-scoped external
1938 // declarations in this header file. Generally, this record will be
1939 // empty.
1940 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001941 // FIXME: This is filling in the PCH file in densemap order which is
1942 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001943 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001944 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1945 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1946 TD != TDEnd; ++TD)
1947 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1948
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001949 // Build a record containing all of the ext_vector declarations.
1950 RecordData ExtVectorDecls;
1951 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1952 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1953
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001954 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001955 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001956 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001957 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001958 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001959 if (StatCalls && !isysroot)
1960 WriteStatCache(*StatCalls, isysroot);
1961 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00001962 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001963 // Write the record of special types.
1964 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001965
Steve Naroffc277ad12009-07-18 15:33:26 +00001966 AddTypeRef(Context.getBuiltinVaListType(), Record);
1967 AddTypeRef(Context.getObjCIdType(), Record);
1968 AddTypeRef(Context.getObjCSelType(), Record);
1969 AddTypeRef(Context.getObjCProtoType(), Record);
1970 AddTypeRef(Context.getObjCClassType(), Record);
1971 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1972 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1973 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00001974 AddTypeRef(Context.getjmp_bufType(), Record);
1975 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001976 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1977 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00001978 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00001979
Douglas Gregor1970d882009-04-26 03:49:13 +00001980 // Keep writing types and declarations until all types and
1981 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00001982 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
1983 WriteDeclsBlockAbbrevs();
1984 while (!DeclTypesToEmit.empty()) {
1985 DeclOrType DOT = DeclTypesToEmit.front();
1986 DeclTypesToEmit.pop();
1987 if (DOT.isType())
1988 WriteType(DOT.getType());
1989 else
1990 WriteDecl(Context, DOT.getDecl());
1991 }
1992 Stream.ExitBlock();
1993
Douglas Gregor45053152009-10-17 17:25:45 +00001994 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001995 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001996 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00001997
1998 // Write the type offsets array
1999 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2000 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2003 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2004 Record.clear();
2005 Record.push_back(pch::TYPE_OFFSET);
2006 Record.push_back(TypeOffsets.size());
2007 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002008 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002009 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00002010
Douglas Gregor745ed142009-04-25 18:35:21 +00002011 // Write the declaration offsets array
2012 Abbrev = new BitCodeAbbrev();
2013 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2016 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2017 Record.clear();
2018 Record.push_back(pch::DECL_OFFSET);
2019 Record.push_back(DeclOffsets.size());
2020 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00002021 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00002022 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002023
Douglas Gregord4df8652009-04-22 22:02:47 +00002024 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002025 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00002026 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002027
2028 // Write the record containing tentative definitions.
2029 if (!TentativeDefinitions.empty())
2030 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002031
2032 // Write the record containing locally-scoped external definitions.
2033 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00002034 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002035 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002036
2037 // Write the record containing ext_vector type names.
2038 if (!ExtVectorDecls.empty())
2039 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002040
Douglas Gregor08f01292009-04-17 22:13:46 +00002041 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002042 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002043 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002044 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002045 Record.push_back(NumLexicalDeclContexts);
2046 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00002047 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002048 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002049}
2050
2051void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2052 Record.push_back(Loc.getRawEncoding());
2053}
2054
2055void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2056 Record.push_back(Value.getBitWidth());
2057 unsigned N = Value.getNumWords();
2058 const uint64_t* Words = Value.getRawData();
2059 for (unsigned I = 0; I != N; ++I)
2060 Record.push_back(Words[I]);
2061}
2062
Douglas Gregor1daeb692009-04-13 18:14:40 +00002063void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2064 Record.push_back(Value.isUnsigned());
2065 AddAPInt(Value, Record);
2066}
2067
Douglas Gregore0a3a512009-04-14 21:55:33 +00002068void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2069 AddAPInt(Value.bitcastToAPInt(), Record);
2070}
2071
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002072void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002073 Record.push_back(getIdentifierRef(II));
2074}
2075
2076pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2077 if (II == 0)
2078 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002079
2080 pch::IdentID &ID = IdentifierIDs[II];
2081 if (ID == 0)
2082 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002083 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002084}
2085
Steve Naroff2ddea052009-04-23 10:39:46 +00002086void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2087 if (SelRef.getAsOpaquePtr() == 0) {
2088 Record.push_back(0);
2089 return;
2090 }
2091
2092 pch::SelectorID &SID = SelectorIDs[SelRef];
2093 if (SID == 0) {
2094 SID = SelectorIDs.size();
2095 SelVector.push_back(SelRef);
2096 }
2097 Record.push_back(SID);
2098}
2099
John McCall8f115c62009-10-16 21:56:05 +00002100void PCHWriter::AddDeclaratorInfo(DeclaratorInfo *DInfo, RecordData &Record) {
2101 if (DInfo == 0) {
2102 AddTypeRef(QualType(), Record);
2103 return;
2104 }
2105
John McCall17001972009-10-18 01:05:36 +00002106 AddTypeRef(DInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002107 TypeLocWriter TLW(*this, Record);
2108 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2109 TLW.Visit(TL);
2110}
2111
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002112void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2113 if (T.isNull()) {
2114 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2115 return;
2116 }
2117
John McCall8ccfcb52009-09-24 19:53:00 +00002118 unsigned FastQuals = T.getFastQualifiers();
2119 T.removeFastQualifiers();
2120
2121 if (T.hasNonFastQualifiers()) {
2122 pch::TypeID &ID = TypeIDs[T];
2123 if (ID == 0) {
2124 // We haven't seen these qualifiers applied to this type before.
2125 // Assign it a new ID. This is the only time we enqueue a
2126 // qualified type, and it has no CV qualifiers.
2127 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002128 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002129 }
2130
2131 // Encode the type qualifiers in the type reference.
2132 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2133 return;
2134 }
2135
2136 assert(!T.hasQualifiers());
2137
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002138 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002139 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002140 switch (BT->getKind()) {
2141 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2142 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2143 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2144 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2145 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2146 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2147 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2148 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002149 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002150 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2151 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2152 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2153 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2154 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2155 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2156 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002157 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002158 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2159 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2160 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002161 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002162 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2163 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002164 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2165 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002166 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2167 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002168 case BuiltinType::UndeducedAuto:
2169 assert(0 && "Should not see undeduced auto here");
2170 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002171 }
2172
John McCall8ccfcb52009-09-24 19:53:00 +00002173 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002174 return;
2175 }
2176
John McCall8ccfcb52009-09-24 19:53:00 +00002177 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002178 if (ID == 0) {
2179 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002180 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002181 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002182 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002183 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002184
2185 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002186 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002187}
2188
2189void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2190 if (D == 0) {
2191 Record.push_back(0);
2192 return;
2193 }
2194
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002195 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002196 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002197 // We haven't seen this declaration before. Give it a new ID and
2198 // enqueue it in the list of declarations to emit.
2199 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002200 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002201 }
2202
2203 Record.push_back(ID);
2204}
2205
Douglas Gregore84a9da2009-04-20 20:36:09 +00002206pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2207 if (D == 0)
2208 return 0;
2209
2210 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2211 return DeclIDs[D];
2212}
2213
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002214void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002215 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002216 Record.push_back(Name.getNameKind());
2217 switch (Name.getNameKind()) {
2218 case DeclarationName::Identifier:
2219 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2220 break;
2221
2222 case DeclarationName::ObjCZeroArgSelector:
2223 case DeclarationName::ObjCOneArgSelector:
2224 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002225 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002226 break;
2227
2228 case DeclarationName::CXXConstructorName:
2229 case DeclarationName::CXXDestructorName:
2230 case DeclarationName::CXXConversionFunctionName:
2231 AddTypeRef(Name.getCXXNameType(), Record);
2232 break;
2233
2234 case DeclarationName::CXXOperatorName:
2235 Record.push_back(Name.getCXXOverloadedOperator());
2236 break;
2237
2238 case DeclarationName::CXXUsingDirective:
2239 // No extra data to emit
2240 break;
2241 }
2242}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002243