blob: b6789687d754537e5716367530529cdcd2a9f6bd [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000048#include <string.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000050using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051
Sebastian Redl3df5a082010-07-30 17:03:48 +000052template <typename T, typename Allocator>
53T *data(std::vector<T, Allocator> &v) {
54 return v.empty() ? 0 : &v.front();
55}
56template <typename T, typename Allocator>
57const T *data(const std::vector<T, Allocator> &v) {
58 return v.empty() ? 0 : &v.front();
59}
60
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061//===----------------------------------------------------------------------===//
62// Type serialization
63//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000064
Douglas Gregoref84c4b2009-04-09 22:27:44 +000065namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000066 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000067 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000068 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069
70 public:
71 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000072 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000073
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000074 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000075 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076
77 void VisitArrayType(const ArrayType *T);
78 void VisitFunctionType(const FunctionType *T);
79 void VisitTagType(const TagType *T);
80
81#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
82#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083#include "clang/AST/TypeNodes.def"
84 };
85}
86
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000087void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088 assert(false && "Built-in types are never serialized");
89}
90
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000091void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000093 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000098 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000113 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000114}
115
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000117 Writer.AddTypeRef(T->getPointeeType(), Record);
118 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000119 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000123 Writer.AddTypeRef(T->getElementType(), Record);
124 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000125 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000129 VisitArrayType(T);
130 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000131 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000135 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000136 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137}
138
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000141 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
142 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000143 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148 Writer.AddTypeRef(T->getElementType(), Record);
149 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000150 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000151 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152}
153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000156 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157}
158
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000159void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000161 FunctionType::ExtInfo C = T->getExtInfo();
162 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000163 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000164 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000165 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166}
167
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000170 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000171}
172
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000173void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174 VisitFunctionType(T);
175 Record.push_back(T->getNumArgs());
176 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
177 Writer.AddTypeRef(T->getArgType(I), Record);
178 Record.push_back(T->isVariadic());
179 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000180 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000181 Record.push_back(T->hasExceptionSpec());
182 Record.push_back(T->hasAnyExceptionSpec());
183 Record.push_back(T->getNumExceptions());
184 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
185 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000186 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000187}
188
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000189void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000190 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000191 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000192}
John McCallb96ec562009-12-04 22:46:56 +0000193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000196 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
197 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000198 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000202 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000212 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000213 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000214}
215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000217 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000219 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220 "Cannot serialize in the middle of a type definition");
221}
222
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000225 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226}
227
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000230 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000231}
232
John McCall81904512011-01-06 01:58:22 +0000233void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
234 Writer.AddTypeRef(T->getModifiedType(), Record);
235 Writer.AddTypeRef(T->getEquivalentType(), Record);
236 Record.push_back(T->getAttrKind());
237 Code = TYPE_ATTRIBUTED;
238}
239
Mike Stump11289f42009-09-09 15:08:12 +0000240void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000241ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000242 const SubstTemplateTypeParmType *T) {
243 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
244 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000245 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000246}
247
248void
Douglas Gregorada4b792011-01-14 02:55:32 +0000249ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
250 const SubstTemplateTypeParmPackType *T) {
251 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
252 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
253 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
254}
255
256void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000257ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000259 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000260 Writer.AddTemplateName(T->getTemplateName(), Record);
261 Record.push_back(T->getNumArgs());
262 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
263 ArgI != ArgE; ++ArgI)
264 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000265 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
266 : T->getCanonicalTypeInternal(),
267 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000268 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000269}
270
271void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000272ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000273 VisitArrayType(T);
274 Writer.AddStmt(T->getSizeExpr());
275 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000276 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000277}
278
279void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000280ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000281 const DependentSizedExtVectorType *T) {
282 // FIXME: Serialize this type (C++ only)
283 assert(false && "Cannot serialize dependent sized extended vector types");
284}
285
286void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000287ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000288 Record.push_back(T->getDepth());
289 Record.push_back(T->getIndex());
290 Record.push_back(T->isParameterPack());
291 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000292 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000293}
294
295void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000296ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000297 Record.push_back(T->getKeyword());
298 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
299 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000300 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
301 : T->getCanonicalTypeInternal(),
302 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000303 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000308 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000309 Record.push_back(T->getKeyword());
310 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
311 Writer.AddIdentifierRef(T->getIdentifier(), Record);
312 Record.push_back(T->getNumArgs());
313 for (DependentTemplateSpecializationType::iterator
314 I = T->begin(), E = T->end(); I != E; ++I)
315 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000316 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000317}
318
Douglas Gregord2fa7662010-12-20 02:24:11 +0000319void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
320 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000321 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
322 Record.push_back(*NumExpansions + 1);
323 else
324 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000325 Code = TYPE_PACK_EXPANSION;
326}
327
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000328void ASTTypeWriter::VisitParenType(const ParenType *T) {
329 Writer.AddTypeRef(T->getInnerType(), Record);
330 Code = TYPE_PAREN;
331}
332
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000333void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000334 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000335 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
336 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000337 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000338}
339
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000340void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000341 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000342 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000343 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000344}
345
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000346void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000347 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000349}
350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000352 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000353 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000354 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000355 E = T->qual_end(); I != E; ++I)
356 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000357 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000358}
359
Steve Narofffb4330f2009-06-17 22:40:22 +0000360void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000361ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000362 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000363 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000364}
365
John McCall8f115c62009-10-16 21:56:05 +0000366namespace {
367
368class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000369 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000370 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000371
372public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000373 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000374 : Writer(Writer), Record(Record) { }
375
John McCall17001972009-10-18 01:05:36 +0000376#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000377#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000378 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000379#include "clang/AST/TypeLocNodes.def"
380
John McCall17001972009-10-18 01:05:36 +0000381 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
382 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000383};
384
385}
386
John McCall17001972009-10-18 01:05:36 +0000387void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
388 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000389}
John McCall17001972009-10-18 01:05:36 +0000390void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000391 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
392 if (TL.needsExtraLocalData()) {
393 Record.push_back(TL.getWrittenTypeSpec());
394 Record.push_back(TL.getWrittenSignSpec());
395 Record.push_back(TL.getWrittenWidthSpec());
396 Record.push_back(TL.hasModeAttr());
397 }
John McCall8f115c62009-10-16 21:56:05 +0000398}
John McCall17001972009-10-18 01:05:36 +0000399void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
400 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000401}
John McCall17001972009-10-18 01:05:36 +0000402void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
403 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000404}
John McCall17001972009-10-18 01:05:36 +0000405void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000407}
John McCall17001972009-10-18 01:05:36 +0000408void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000410}
John McCall17001972009-10-18 01:05:36 +0000411void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000413}
John McCall17001972009-10-18 01:05:36 +0000414void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000416}
John McCall17001972009-10-18 01:05:36 +0000417void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
419 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
420 Record.push_back(TL.getSizeExpr() ? 1 : 0);
421 if (TL.getSizeExpr())
422 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000423}
John McCall17001972009-10-18 01:05:36 +0000424void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
425 VisitArrayTypeLoc(TL);
426}
427void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
428 VisitArrayTypeLoc(TL);
429}
430void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
431 VisitArrayTypeLoc(TL);
432}
433void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
434 DependentSizedArrayTypeLoc TL) {
435 VisitArrayTypeLoc(TL);
436}
437void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
438 DependentSizedExtVectorTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
440}
441void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
443}
444void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getNameLoc(), Record);
446}
447void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
449 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000450 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000451 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
452 Writer.AddDeclRef(TL.getArg(i), Record);
453}
454void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
455 VisitFunctionTypeLoc(TL);
456}
457void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
458 VisitFunctionTypeLoc(TL);
459}
John McCallb96ec562009-12-04 22:46:56 +0000460void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getNameLoc(), Record);
462}
John McCall17001972009-10-18 01:05:36 +0000463void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
464 Writer.AddSourceLocation(TL.getNameLoc(), Record);
465}
466void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000467 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
468 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
469 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000470}
471void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000472 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
473 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
474 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
475 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000476}
477void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
480void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
John McCall81904512011-01-06 01:58:22 +0000486void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
488 if (TL.hasAttrOperand()) {
489 SourceRange range = TL.getAttrOperandParensRange();
490 Writer.AddSourceLocation(range.getBegin(), Record);
491 Writer.AddSourceLocation(range.getEnd(), Record);
492 }
493 if (TL.hasAttrExprOperand()) {
494 Expr *operand = TL.getAttrExprOperand();
495 Record.push_back(operand ? 1 : 0);
496 if (operand) Writer.AddStmt(operand);
497 } else if (TL.hasAttrEnumOperand()) {
498 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
499 }
500}
John McCall17001972009-10-18 01:05:36 +0000501void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
John McCallcebee162009-10-18 09:09:24 +0000504void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
505 SubstTemplateTypeParmTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getNameLoc(), Record);
507}
Douglas Gregorada4b792011-01-14 02:55:32 +0000508void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
509 SubstTemplateTypeParmPackTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
John McCall17001972009-10-18 01:05:36 +0000512void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
513 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000514 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
515 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
516 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
517 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000518 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
519 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000520}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000521void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
523 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
524}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000525void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000526 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
527 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000528}
John McCalle78aac42010-03-10 03:28:59 +0000529void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000532void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000533 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
534 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
John McCallc392f372010-06-11 00:33:02 +0000537void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
538 DependentTemplateSpecializationTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
540 Writer.AddSourceRange(TL.getQualifierRange(), Record);
541 Writer.AddSourceLocation(TL.getNameLoc(), Record);
542 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
543 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
544 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000545 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
546 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000547}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000548void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
550}
John McCall17001972009-10-18 01:05:36 +0000551void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000553}
554void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
555 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000556 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
557 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
558 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
559 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000560}
John McCallfc93cf92009-10-22 22:37:11 +0000561void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000563}
John McCall8f115c62009-10-16 21:56:05 +0000564
Chris Lattner19cea4e2009-04-22 05:57:30 +0000565//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000566// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000567//===----------------------------------------------------------------------===//
568
Chris Lattner28fa4e62009-04-26 22:26:21 +0000569static void EmitBlockID(unsigned ID, const char *Name,
570 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000571 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000572 Record.clear();
573 Record.push_back(ID);
574 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
575
576 // Emit the block name if present.
577 if (Name == 0 || Name[0] == 0) return;
578 Record.clear();
579 while (*Name)
580 Record.push_back(*Name++);
581 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
582}
583
584static void EmitRecordID(unsigned ID, const char *Name,
585 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000586 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000587 Record.clear();
588 Record.push_back(ID);
589 while (*Name)
590 Record.push_back(*Name++);
591 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000592}
593
594static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000595 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000596#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000597 RECORD(STMT_STOP);
598 RECORD(STMT_NULL_PTR);
599 RECORD(STMT_NULL);
600 RECORD(STMT_COMPOUND);
601 RECORD(STMT_CASE);
602 RECORD(STMT_DEFAULT);
603 RECORD(STMT_LABEL);
604 RECORD(STMT_IF);
605 RECORD(STMT_SWITCH);
606 RECORD(STMT_WHILE);
607 RECORD(STMT_DO);
608 RECORD(STMT_FOR);
609 RECORD(STMT_GOTO);
610 RECORD(STMT_INDIRECT_GOTO);
611 RECORD(STMT_CONTINUE);
612 RECORD(STMT_BREAK);
613 RECORD(STMT_RETURN);
614 RECORD(STMT_DECL);
615 RECORD(STMT_ASM);
616 RECORD(EXPR_PREDEFINED);
617 RECORD(EXPR_DECL_REF);
618 RECORD(EXPR_INTEGER_LITERAL);
619 RECORD(EXPR_FLOATING_LITERAL);
620 RECORD(EXPR_IMAGINARY_LITERAL);
621 RECORD(EXPR_STRING_LITERAL);
622 RECORD(EXPR_CHARACTER_LITERAL);
623 RECORD(EXPR_PAREN);
624 RECORD(EXPR_UNARY_OPERATOR);
625 RECORD(EXPR_SIZEOF_ALIGN_OF);
626 RECORD(EXPR_ARRAY_SUBSCRIPT);
627 RECORD(EXPR_CALL);
628 RECORD(EXPR_MEMBER);
629 RECORD(EXPR_BINARY_OPERATOR);
630 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
631 RECORD(EXPR_CONDITIONAL_OPERATOR);
632 RECORD(EXPR_IMPLICIT_CAST);
633 RECORD(EXPR_CSTYLE_CAST);
634 RECORD(EXPR_COMPOUND_LITERAL);
635 RECORD(EXPR_EXT_VECTOR_ELEMENT);
636 RECORD(EXPR_INIT_LIST);
637 RECORD(EXPR_DESIGNATED_INIT);
638 RECORD(EXPR_IMPLICIT_VALUE_INIT);
639 RECORD(EXPR_VA_ARG);
640 RECORD(EXPR_ADDR_LABEL);
641 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000642 RECORD(EXPR_CHOOSE);
643 RECORD(EXPR_GNU_NULL);
644 RECORD(EXPR_SHUFFLE_VECTOR);
645 RECORD(EXPR_BLOCK);
646 RECORD(EXPR_BLOCK_DECL_REF);
647 RECORD(EXPR_OBJC_STRING_LITERAL);
648 RECORD(EXPR_OBJC_ENCODE);
649 RECORD(EXPR_OBJC_SELECTOR_EXPR);
650 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
651 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
652 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
653 RECORD(EXPR_OBJC_KVC_REF_EXPR);
654 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000655 RECORD(STMT_OBJC_FOR_COLLECTION);
656 RECORD(STMT_OBJC_CATCH);
657 RECORD(STMT_OBJC_FINALLY);
658 RECORD(STMT_OBJC_AT_TRY);
659 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
660 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000661 RECORD(EXPR_CXX_OPERATOR_CALL);
662 RECORD(EXPR_CXX_CONSTRUCT);
663 RECORD(EXPR_CXX_STATIC_CAST);
664 RECORD(EXPR_CXX_DYNAMIC_CAST);
665 RECORD(EXPR_CXX_REINTERPRET_CAST);
666 RECORD(EXPR_CXX_CONST_CAST);
667 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
668 RECORD(EXPR_CXX_BOOL_LITERAL);
669 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000670 RECORD(EXPR_CXX_TYPEID_EXPR);
671 RECORD(EXPR_CXX_TYPEID_TYPE);
672 RECORD(EXPR_CXX_UUIDOF_EXPR);
673 RECORD(EXPR_CXX_UUIDOF_TYPE);
674 RECORD(EXPR_CXX_THIS);
675 RECORD(EXPR_CXX_THROW);
676 RECORD(EXPR_CXX_DEFAULT_ARG);
677 RECORD(EXPR_CXX_BIND_TEMPORARY);
678 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
679 RECORD(EXPR_CXX_NEW);
680 RECORD(EXPR_CXX_DELETE);
681 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
682 RECORD(EXPR_EXPR_WITH_CLEANUPS);
683 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
684 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
685 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
686 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
687 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
688 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
689 RECORD(EXPR_CXX_NOEXCEPT);
690 RECORD(EXPR_OPAQUE_VALUE);
691 RECORD(EXPR_BINARY_TYPE_TRAIT);
692 RECORD(EXPR_PACK_EXPANSION);
693 RECORD(EXPR_SIZEOF_PACK);
694 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000695 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000696#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000697}
Mike Stump11289f42009-09-09 15:08:12 +0000698
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000699void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000700 RecordData Record;
701 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000702
Sebastian Redl539c5062010-08-18 23:57:32 +0000703#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
704#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000705
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000706 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000707 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000708 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000709 RECORD(TYPE_OFFSET);
710 RECORD(DECL_OFFSET);
711 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000712 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000713 RECORD(IDENTIFIER_OFFSET);
714 RECORD(IDENTIFIER_TABLE);
715 RECORD(EXTERNAL_DEFINITIONS);
716 RECORD(SPECIAL_TYPES);
717 RECORD(STATISTICS);
718 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000719 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000720 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
721 RECORD(SELECTOR_OFFSETS);
722 RECORD(METHOD_POOL);
723 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000724 RECORD(SOURCE_LOCATION_OFFSETS);
725 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000726 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000727 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000728 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000729 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000730 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000731 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000732 RECORD(TU_UPDATE_LEXICAL);
733 RECORD(REDECLS_UPDATE_LATEST);
734 RECORD(SEMA_DECL_REFS);
735 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
736 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
737 RECORD(DECL_REPLACEMENTS);
738 RECORD(UPDATE_VISIBLE);
739 RECORD(DECL_UPDATE_OFFSETS);
740 RECORD(DECL_UPDATES);
741 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
742 RECORD(DIAG_PRAGMA_MAPPINGS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000743 RECORD(HEADER_SEARCH_TABLE);
744
Chris Lattner28fa4e62009-04-26 22:26:21 +0000745 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000746 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000747 RECORD(SM_SLOC_FILE_ENTRY);
748 RECORD(SM_SLOC_BUFFER_ENTRY);
749 RECORD(SM_SLOC_BUFFER_BLOB);
750 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
751 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000752
Chris Lattner28fa4e62009-04-26 22:26:21 +0000753 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000754 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000755 RECORD(PP_MACRO_OBJECT_LIKE);
756 RECORD(PP_MACRO_FUNCTION_LIKE);
757 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000758
Douglas Gregor12bfa382009-10-17 00:13:19 +0000759 // Decls and Types block.
760 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000761 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000762 RECORD(TYPE_COMPLEX);
763 RECORD(TYPE_POINTER);
764 RECORD(TYPE_BLOCK_POINTER);
765 RECORD(TYPE_LVALUE_REFERENCE);
766 RECORD(TYPE_RVALUE_REFERENCE);
767 RECORD(TYPE_MEMBER_POINTER);
768 RECORD(TYPE_CONSTANT_ARRAY);
769 RECORD(TYPE_INCOMPLETE_ARRAY);
770 RECORD(TYPE_VARIABLE_ARRAY);
771 RECORD(TYPE_VECTOR);
772 RECORD(TYPE_EXT_VECTOR);
773 RECORD(TYPE_FUNCTION_PROTO);
774 RECORD(TYPE_FUNCTION_NO_PROTO);
775 RECORD(TYPE_TYPEDEF);
776 RECORD(TYPE_TYPEOF_EXPR);
777 RECORD(TYPE_TYPEOF);
778 RECORD(TYPE_RECORD);
779 RECORD(TYPE_ENUM);
780 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000781 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000782 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000783 RECORD(TYPE_DECLTYPE);
784 RECORD(TYPE_ELABORATED);
785 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
786 RECORD(TYPE_UNRESOLVED_USING);
787 RECORD(TYPE_INJECTED_CLASS_NAME);
788 RECORD(TYPE_OBJC_OBJECT);
789 RECORD(TYPE_TEMPLATE_TYPE_PARM);
790 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
791 RECORD(TYPE_DEPENDENT_NAME);
792 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
793 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
794 RECORD(TYPE_PAREN);
795 RECORD(TYPE_PACK_EXPANSION);
796 RECORD(TYPE_ATTRIBUTED);
797 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000798 RECORD(DECL_TRANSLATION_UNIT);
799 RECORD(DECL_TYPEDEF);
800 RECORD(DECL_ENUM);
801 RECORD(DECL_RECORD);
802 RECORD(DECL_ENUM_CONSTANT);
803 RECORD(DECL_FUNCTION);
804 RECORD(DECL_OBJC_METHOD);
805 RECORD(DECL_OBJC_INTERFACE);
806 RECORD(DECL_OBJC_PROTOCOL);
807 RECORD(DECL_OBJC_IVAR);
808 RECORD(DECL_OBJC_AT_DEFS_FIELD);
809 RECORD(DECL_OBJC_CLASS);
810 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
811 RECORD(DECL_OBJC_CATEGORY);
812 RECORD(DECL_OBJC_CATEGORY_IMPL);
813 RECORD(DECL_OBJC_IMPLEMENTATION);
814 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
815 RECORD(DECL_OBJC_PROPERTY);
816 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000817 RECORD(DECL_FIELD);
818 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000819 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000820 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000821 RECORD(DECL_FILE_SCOPE_ASM);
822 RECORD(DECL_BLOCK);
823 RECORD(DECL_CONTEXT_LEXICAL);
824 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000825 RECORD(DECL_NAMESPACE);
826 RECORD(DECL_NAMESPACE_ALIAS);
827 RECORD(DECL_USING);
828 RECORD(DECL_USING_SHADOW);
829 RECORD(DECL_USING_DIRECTIVE);
830 RECORD(DECL_UNRESOLVED_USING_VALUE);
831 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
832 RECORD(DECL_LINKAGE_SPEC);
833 RECORD(DECL_CXX_RECORD);
834 RECORD(DECL_CXX_METHOD);
835 RECORD(DECL_CXX_CONSTRUCTOR);
836 RECORD(DECL_CXX_DESTRUCTOR);
837 RECORD(DECL_CXX_CONVERSION);
838 RECORD(DECL_ACCESS_SPEC);
839 RECORD(DECL_FRIEND);
840 RECORD(DECL_FRIEND_TEMPLATE);
841 RECORD(DECL_CLASS_TEMPLATE);
842 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
843 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
844 RECORD(DECL_FUNCTION_TEMPLATE);
845 RECORD(DECL_TEMPLATE_TYPE_PARM);
846 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
847 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
848 RECORD(DECL_STATIC_ASSERT);
849 RECORD(DECL_CXX_BASE_SPECIFIERS);
850 RECORD(DECL_INDIRECTFIELD);
851 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
852
Douglas Gregor92a96f52011-02-08 21:58:10 +0000853 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
854 RECORD(PPD_MACRO_INSTANTIATION);
855 RECORD(PPD_MACRO_DEFINITION);
856 RECORD(PPD_INCLUSION_DIRECTIVE);
857
Douglas Gregor12bfa382009-10-17 00:13:19 +0000858 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000859 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000860#undef RECORD
861#undef BLOCK
862 Stream.ExitBlock();
863}
864
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000865/// \brief Adjusts the given filename to only write out the portion of the
866/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000867///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000868/// \param Filename the file name to adjust.
869///
870/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
871/// the returned filename will be adjusted by this system root.
872///
873/// \returns either the original filename (if it needs no adjustment) or the
874/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000875static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000876adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
877 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000878
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000879 if (!isysroot)
880 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000881
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000882 // Verify that the filename and the system root have the same prefix.
883 unsigned Pos = 0;
884 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
885 if (Filename[Pos] != isysroot[Pos])
886 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000887
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000888 // We hit the end of the filename before we hit the end of the system root.
889 if (!Filename[Pos])
890 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000891
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000892 // If the file name has a '/' at the current position, skip over the '/'.
893 // We distinguish sysroot-based includes from absolute includes by the
894 // absence of '/' at the beginning of sysroot-based includes.
895 if (Filename[Pos] == '/')
896 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000897
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000898 return Filename + Pos;
899}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000900
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000901/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000902void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000903 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000904
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000905 // Metadata
906 const TargetInfo &Target = Context.Target;
907 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000908 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000909 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000910 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
911 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000912 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
913 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
914 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000915 // Target triple or chained PCH name
916 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000917 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000918
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000919 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000920 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
921 Record.push_back(VERSION_MAJOR);
922 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000923 Record.push_back(CLANG_VERSION_MAJOR);
924 Record.push_back(CLANG_VERSION_MINOR);
925 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000926 // FIXME: This writes the absolute path for chained headers.
927 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
928 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000929
Douglas Gregor45fe0362009-05-12 01:31:05 +0000930 // Original file name
931 SourceManager &SM = Context.getSourceManager();
932 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
933 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000934 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000935 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
936 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
937
Michael J. Spencer740857f2010-12-21 16:45:57 +0000938 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000939
Michael J. Spencer740857f2010-12-21 16:45:57 +0000940 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000941
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000942 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000943 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000944 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000945 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000946 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000947 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000948 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000949
Ted Kremenek18e066f2010-01-22 22:12:47 +0000950 // Repository branch/version information.
951 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000952 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000953 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
954 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000955 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000956 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000957 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
958 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000959}
960
961/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000962void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000963 RecordData Record;
964 Record.push_back(LangOpts.Trigraphs);
965 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
966 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
967 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
968 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000969 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000970 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
971 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
972 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
973 Record.push_back(LangOpts.C99); // C99 Support
974 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000975 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
976 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000977 Record.push_back(LangOpts.CPlusPlus); // C++ Support
978 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000979 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000980
Douglas Gregor55abb232009-04-10 20:39:37 +0000981 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
982 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000983 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000984 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000985 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000986 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +0000987 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +0000988 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
989 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000990 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000991
Douglas Gregor55abb232009-04-10 20:39:37 +0000992 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000993 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
994 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000995 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000996 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000997 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000998
Douglas Gregordbe39272011-02-01 15:15:22 +0000999 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001000 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1001 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1002 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1003
Chris Lattner258172e2009-04-27 07:35:58 +00001004 // Whether static initializers are protected by locks.
1005 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001006 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001007 Record.push_back(LangOpts.Blocks); // block extension to C
1008 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1009 // they are unused.
1010 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1011 // (modulo the platform support).
1012
Chris Lattner51924e512010-06-26 21:25:03 +00001013 Record.push_back(LangOpts.getSignedOverflowBehavior());
1014 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001015
1016 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001017 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001018 // defined.
1019 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1020 // opposed to __DYNAMIC__).
1021 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1022
1023 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1024 // used (instead of C99 semantics).
1025 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001026 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1027 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001028 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1029 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001030 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001031 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1032 // to the smallest integer type with
1033 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001034 Record.push_back(LangOpts.getGCMode());
1035 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001036 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001037 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001038 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001039 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001040 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001041 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001042 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +00001043 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001044}
1045
Douglas Gregora7f71a92009-04-10 03:52:48 +00001046//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001047// stat cache Serialization
1048//===----------------------------------------------------------------------===//
1049
1050namespace {
1051// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001052class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001053public:
1054 typedef const char * key_type;
1055 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001056
Chris Lattner2a6fa472010-11-23 19:28:12 +00001057 typedef struct stat data_type;
1058 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001059
1060 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001061 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001062 }
Mike Stump11289f42009-09-09 15:08:12 +00001063
1064 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001065 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1066 data_type_ref Data) {
1067 unsigned StrLen = strlen(path);
1068 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001069 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001070 clang::io::Emit8(Out, DataLen);
1071 return std::make_pair(StrLen + 1, DataLen);
1072 }
Mike Stump11289f42009-09-09 15:08:12 +00001073
Douglas Gregorc5046832009-04-27 18:38:38 +00001074 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1075 Out.write(path, KeyLen);
1076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Chris Lattner2a6fa472010-11-23 19:28:12 +00001078 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001079 data_type_ref Data, unsigned DataLen) {
1080 using namespace clang::io;
1081 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattner2a6fa472010-11-23 19:28:12 +00001083 Emit32(Out, (uint32_t) Data.st_ino);
1084 Emit32(Out, (uint32_t) Data.st_dev);
1085 Emit16(Out, (uint16_t) Data.st_mode);
1086 Emit64(Out, (uint64_t) Data.st_mtime);
1087 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001088
1089 assert(Out.tell() - Start == DataLen && "Wrong data length");
1090 }
1091};
1092} // end anonymous namespace
1093
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001094/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001095void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 // Build the on-disk hash table containing information about every
1097 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001098 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001099 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001100 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001101 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001102 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1103 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001104 Generator.insert(Filename, Stat->second);
1105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Douglas Gregorc5046832009-04-27 18:38:38 +00001107 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001108 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001109 uint32_t BucketOffset;
1110 {
1111 llvm::raw_svector_ostream Out(StatCacheData);
1112 // Make sure that no bucket is at offset 0
1113 clang::io::Emit32(Out, 0);
1114 BucketOffset = Generator.Emit(Out);
1115 }
1116
1117 // Create a blob abbreviation
1118 using namespace llvm;
1119 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001120 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1124 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1125
1126 // Write the stat cache
1127 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001128 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001129 Record.push_back(BucketOffset);
1130 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001131 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001132}
1133
1134//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001135// Source Manager Serialization
1136//===----------------------------------------------------------------------===//
1137
1138/// \brief Create an abbreviation for the SLocEntry that refers to a
1139/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001140static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001141 using namespace llvm;
1142 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001143 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001148 // FileEntry fields.
1149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001152 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001153}
1154
1155/// \brief Create an abbreviation for the SLocEntry that refers to a
1156/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001157static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001158 using namespace llvm;
1159 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001160 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001166 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001167}
1168
1169/// \brief Create an abbreviation for the SLocEntry that refers to a
1170/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001171static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001172 using namespace llvm;
1173 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001174 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001176 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001177}
1178
1179/// \brief Create an abbreviation for the SLocEntry that refers to an
1180/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001181static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001182 using namespace llvm;
1183 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001184 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001190 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001191}
1192
Douglas Gregor09b69892011-02-10 17:09:37 +00001193namespace {
1194 // Trait used for the on-disk hash table of header search information.
1195 class HeaderFileInfoTrait {
1196 ASTWriter &Writer;
1197 HeaderSearch &HS;
1198
1199 public:
1200 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1201 : Writer(Writer), HS(HS) { }
1202
1203 typedef const char *key_type;
1204 typedef key_type key_type_ref;
1205
1206 typedef HeaderFileInfo data_type;
1207 typedef const data_type &data_type_ref;
1208
1209 static unsigned ComputeHash(const char *path) {
1210 // The hash is based only on the filename portion of the key, so that the
1211 // reader can match based on filenames when symlinking or excess path
1212 // elements ("foo/../", "../") change the form of the name. However,
1213 // complete path is still the key.
1214 return llvm::HashString(llvm::sys::path::filename(path));
1215 }
1216
1217 std::pair<unsigned,unsigned>
1218 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1219 data_type_ref Data) {
1220 unsigned StrLen = strlen(path);
1221 clang::io::Emit16(Out, StrLen);
1222 unsigned DataLen = 1 + 2 + 4;
1223 clang::io::Emit8(Out, DataLen);
1224 return std::make_pair(StrLen + 1, DataLen);
1225 }
1226
1227 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1228 Out.write(path, KeyLen);
1229 }
1230
1231 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1232 data_type_ref Data, unsigned DataLen) {
1233 using namespace clang::io;
1234 uint64_t Start = Out.tell(); (void)Start;
1235
1236 unsigned char Flags = (Data.isImport << 3)
1237 | (Data.DirInfo << 1)
1238 | Data.Resolved;
1239 Emit8(Out, (uint8_t)Flags);
1240 Emit16(Out, (uint16_t) Data.NumIncludes);
1241
1242 if (!Data.ControllingMacro)
1243 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1244 else
1245 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1246 assert(Out.tell() - Start == DataLen && "Wrong data length");
1247 }
1248 };
1249} // end anonymous namespace
1250
1251/// \brief Write the header search block for the list of files that
1252///
1253/// \param HS The header search structure to save.
1254///
1255/// \param Chain Whether we're creating a chained AST file.
1256void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1257 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1258 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1259
1260 if (FilesByUID.size() > HS.header_file_size())
1261 FilesByUID.resize(HS.header_file_size());
1262
1263 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1264 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1265 llvm::SmallVector<const char *, 4> SavedStrings;
1266 unsigned NumHeaderSearchEntries = 0;
1267 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1268 const FileEntry *File = FilesByUID[UID];
1269 if (!File)
1270 continue;
1271
1272 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1273 if (HFI.External && Chain)
1274 continue;
1275
1276 // Turn the file name into an absolute path, if it isn't already.
1277 const char *Filename = File->getName();
1278 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1279
1280 // If we performed any translation on the file name at all, we need to
1281 // save this string, since the generator will refer to it later.
1282 if (Filename != File->getName()) {
1283 Filename = strdup(Filename);
1284 SavedStrings.push_back(Filename);
1285 }
1286
1287 Generator.insert(Filename, HFI, GeneratorTrait);
1288 ++NumHeaderSearchEntries;
1289 }
1290
1291 // Create the on-disk hash table in a buffer.
1292 llvm::SmallString<4096> TableData;
1293 uint32_t BucketOffset;
1294 {
1295 llvm::raw_svector_ostream Out(TableData);
1296 // Make sure that no bucket is at offset 0
1297 clang::io::Emit32(Out, 0);
1298 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1299 }
1300
1301 // Create a blob abbreviation
1302 using namespace llvm;
1303 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1304 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1308 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1309
1310 // Write the stat cache
1311 RecordData Record;
1312 Record.push_back(HEADER_SEARCH_TABLE);
1313 Record.push_back(BucketOffset);
1314 Record.push_back(NumHeaderSearchEntries);
1315 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1316
1317 // Free all of the strings we had to duplicate.
1318 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1319 free((void*)SavedStrings[I]);
1320}
1321
Douglas Gregora7f71a92009-04-10 03:52:48 +00001322/// \brief Writes the block containing the serialized form of the
1323/// source manager.
1324///
1325/// TODO: We should probably use an on-disk hash table (stored in a
1326/// blob), indexed based on the file name, so that we only create
1327/// entries for files that we actually need. In the common case (no
1328/// errors), we probably won't have to create file entries for any of
1329/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001330void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001331 const Preprocessor &PP,
1332 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001333 RecordData Record;
1334
Chris Lattner0910e3b2009-04-10 17:16:57 +00001335 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001336 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001337
1338 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001339 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1340 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1341 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1342 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001343
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001344 // Write the line table.
1345 if (SourceMgr.hasLineTable()) {
1346 LineTableInfo &LineTable = SourceMgr.getLineTable();
1347
1348 // Emit the file names
1349 Record.push_back(LineTable.getNumFilenames());
1350 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1351 // Emit the file name
1352 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001353 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001354 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1355 Record.push_back(FilenameLen);
1356 if (FilenameLen)
1357 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1358 }
Mike Stump11289f42009-09-09 15:08:12 +00001359
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001360 // Emit the line entries
1361 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1362 L != LEnd; ++L) {
1363 // Emit the file ID
1364 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001365
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001366 // Emit the line entries
1367 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001368 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001369 LEEnd = L->second.end();
1370 LE != LEEnd; ++LE) {
1371 Record.push_back(LE->FileOffset);
1372 Record.push_back(LE->LineNo);
1373 Record.push_back(LE->FilenameID);
1374 Record.push_back((unsigned)LE->FileKind);
1375 Record.push_back(LE->IncludeOffset);
1376 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001377 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001378 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001379 }
1380
Douglas Gregor258ae542009-04-27 06:38:32 +00001381 // Write out the source location entry table. We skip the first
1382 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001383 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001384 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001385 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1386 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1387 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1388 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001389 // Get this source location entry.
1390 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001391
Douglas Gregor258ae542009-04-27 06:38:32 +00001392 // Record the offset of this source-location entry.
1393 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1394
1395 // Figure out which record code to use.
1396 unsigned Code;
1397 if (SLoc->isFile()) {
1398 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001399 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001400 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001401 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001402 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001403 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001404 Record.clear();
1405 Record.push_back(Code);
1406
1407 Record.push_back(SLoc->getOffset());
1408 if (SLoc->isFile()) {
1409 const SrcMgr::FileInfo &File = SLoc->getFile();
1410 Record.push_back(File.getIncludeLoc().getRawEncoding());
1411 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1412 Record.push_back(File.hasLineDirectives());
1413
1414 const SrcMgr::ContentCache *Content = File.getContentCache();
1415 if (Content->Entry) {
1416 // The source location entry is a file. The blob associated
1417 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001418
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001419 // Emit size/modification time for this file.
1420 Record.push_back(Content->Entry->getSize());
1421 Record.push_back(Content->Entry->getModificationTime());
1422
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001423 // Turn the file name into an absolute path, if it isn't already.
1424 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001425 llvm::SmallString<128> FilePath(Filename);
1426 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001427 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001428
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001429 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001430 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001431 } else {
1432 // The source location entry is a buffer. The blob associated
1433 // with this entry contains the contents of the buffer.
1434
1435 // We add one to the size so that we capture the trailing NULL
1436 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1437 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001438 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001439 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001440 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001441 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1442 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001443 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001444 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001445 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001446 llvm::StringRef(Buffer->getBufferStart(),
1447 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001448
1449 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001450 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001451 }
1452 } else {
1453 // The source location entry is an instantiation.
1454 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1455 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1456 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1457 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1458
1459 // Compute the token length for this macro expansion.
1460 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001461 if (I + 1 != N)
1462 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001463 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1464 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1465 }
1466 }
1467
Douglas Gregor8f45df52009-04-16 22:23:12 +00001468 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001469
1470 if (SLocEntryOffsets.empty())
1471 return;
1472
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001473 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001474 // table is used for lazily loading source-location information.
1475 using namespace llvm;
1476 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001477 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1481 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001482
Douglas Gregor258ae542009-04-27 06:38:32 +00001483 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001484 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001485 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001486 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1487 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001488 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001489 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001490 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001491
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001492 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001493 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001494 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001495}
1496
Douglas Gregorc5046832009-04-27 18:38:38 +00001497//===----------------------------------------------------------------------===//
1498// Preprocessor Serialization
1499//===----------------------------------------------------------------------===//
1500
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001501static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1502 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1503 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1504 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1505 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1506 return X.first->getName().compare(Y.first->getName());
1507}
1508
Chris Lattnereeffaef2009-04-10 17:15:23 +00001509/// \brief Writes the block containing the serialized form of the
1510/// preprocessor.
1511///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001512void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001513 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001514
Chris Lattner0af3ba12009-04-13 01:29:17 +00001515 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1516 if (PP.getCounterValue() != 0) {
1517 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001518 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001519 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001520 }
1521
1522 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001523 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001524
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001525 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001526 // FIXME: use diagnostics subsystem for localization etc.
1527 if (PP.SawDateOrTime())
1528 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001529
Douglas Gregor796d76a2010-10-20 22:00:55 +00001530
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001531 // Loop over all the macro definitions that are live at the end of the file,
1532 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001533 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001534
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001535 // Construct the list of macro definitions that need to be serialized.
1536 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1537 MacrosToEmit;
1538 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001539 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1540 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001541 MacroDefinitionsSeen.insert(I->first);
1542 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1543 }
1544
1545 // Sort the set of macro definitions that need to be serialized by the
1546 // name of the macro, to provide a stable ordering.
1547 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1548 &compareMacroDefinitions);
1549
1550 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1551 const IdentifierInfo *Name = MacrosToEmit[I].first;
1552 MacroInfo *MI = MacrosToEmit[I].second;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001553
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001554 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001555 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001556 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001557
1558 // FIXME: There is a (probably minor) optimization we could do here, if
1559 // the macro comes from the original PCH but the identifier comes from a
1560 // chained PCH, by storing the offset into the original PCH rather than
1561 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001562 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001563 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001564 continue;
1565
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001566 AddIdentifierRef(Name, Record);
1567 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001568 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1569 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001570
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001571 unsigned Code;
1572 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001573 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001574 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001575 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001576
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001577 Record.push_back(MI->isC99Varargs());
1578 Record.push_back(MI->isGNUVarargs());
1579 Record.push_back(MI->getNumArgs());
1580 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1581 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001582 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001583 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001584
Douglas Gregoraae92242010-03-19 21:51:54 +00001585 // If we have a detailed preprocessing record, record the macro definition
1586 // ID that corresponds to this macro.
1587 if (PPRec)
1588 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001589
Douglas Gregor8f45df52009-04-16 22:23:12 +00001590 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001591 Record.clear();
1592
Chris Lattner2199f5b2009-04-10 18:08:30 +00001593 // Emit the tokens array.
1594 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1595 // Note that we know that the preprocessor does not have any annotation
1596 // tokens in it because they are created by the parser, and thus can't be
1597 // in a macro definition.
1598 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001599
Chris Lattner2199f5b2009-04-10 18:08:30 +00001600 Record.push_back(Tok.getLocation().getRawEncoding());
1601 Record.push_back(Tok.getLength());
1602
Chris Lattner2199f5b2009-04-10 18:08:30 +00001603 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1604 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001605 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001606
Chris Lattner2199f5b2009-04-10 18:08:30 +00001607 // FIXME: Should translate token kind to a stable encoding.
1608 Record.push_back(Tok.getKind());
1609 // FIXME: Should translate token flags to a stable encoding.
1610 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001611
Sebastian Redl539c5062010-08-18 23:57:32 +00001612 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001613 Record.clear();
1614 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001615 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001616 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001617 Stream.ExitBlock();
1618
1619 if (PPRec)
1620 WritePreprocessorDetail(*PPRec);
1621}
1622
1623void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1624 if (PPRec.begin(Chain) == PPRec.end(Chain))
1625 return;
1626
1627 // Enter the preprocessor block.
1628 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001629
Douglas Gregoraae92242010-03-19 21:51:54 +00001630 // If the preprocessor has a preprocessing record, emit it.
1631 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001632 using namespace llvm;
1633
1634 // Set up the abbreviation for
1635 unsigned InclusionAbbrev = 0;
1636 {
1637 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1638 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1639 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1640 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1641 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1642 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1643 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1644 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1646 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1647 }
1648
1649 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1650 RecordData Record;
1651 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1652 EEnd = PPRec.end(Chain);
1653 E != EEnd; ++E) {
1654 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001655
Douglas Gregor92a96f52011-02-08 21:58:10 +00001656 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1657 // Record this macro definition's location.
1658 MacroID ID = getMacroDefinitionID(MD);
1659
1660 // Don't write the macro definition if it is from another AST file.
1661 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001662 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001663
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001664 // Notify the serialization listener that we're serializing this entity.
1665 if (SerializationListener)
1666 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001667 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001668
Douglas Gregor92a96f52011-02-08 21:58:10 +00001669 unsigned Position = ID - FirstMacroID;
1670 if (Position != MacroDefinitionOffsets.size()) {
1671 if (Position > MacroDefinitionOffsets.size())
1672 MacroDefinitionOffsets.resize(Position + 1);
1673
1674 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1675 } else
1676 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001677
Douglas Gregor92a96f52011-02-08 21:58:10 +00001678 Record.push_back(IndexBase + NumPreprocessingRecords++);
1679 Record.push_back(ID);
1680 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1681 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1682 AddIdentifierRef(MD->getName(), Record);
1683 AddSourceLocation(MD->getLocation(), Record);
1684 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1685 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001686 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001687
Douglas Gregor92a96f52011-02-08 21:58:10 +00001688 // Notify the serialization listener that we're serializing this entity.
1689 if (SerializationListener)
1690 SerializationListener->SerializedPreprocessedEntity(*E,
1691 Stream.GetCurrentBitNo());
1692
1693 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1694 Record.push_back(IndexBase + NumPreprocessingRecords++);
1695 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1696 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1697 AddIdentifierRef(MI->getName(), Record);
1698 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1699 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1700 continue;
1701 }
1702
1703 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1704 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1705 Record.push_back(IndexBase + NumPreprocessingRecords++);
1706 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1707 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1708 Record.push_back(ID->getFileName().size());
1709 Record.push_back(ID->wasInQuotes());
1710 Record.push_back(static_cast<unsigned>(ID->getKind()));
1711 llvm::SmallString<64> Buffer;
1712 Buffer += ID->getFileName();
1713 Buffer += ID->getFile()->getName();
1714 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1715 continue;
1716 }
1717
1718 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1719 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001720 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001721
Douglas Gregoraae92242010-03-19 21:51:54 +00001722 // Write the offsets table for the preprocessing record.
1723 if (NumPreprocessingRecords > 0) {
1724 // Write the offsets table for identifier IDs.
1725 using namespace llvm;
1726 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001727 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1731 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001732
Douglas Gregoraae92242010-03-19 21:51:54 +00001733 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001734 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001735 Record.push_back(NumPreprocessingRecords);
1736 Record.push_back(MacroDefinitionOffsets.size());
1737 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001738 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001739 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1740 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001741}
1742
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001743void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001744 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001745 for (Diagnostic::DiagStatePointsTy::const_iterator
1746 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1747 I != E; ++I) {
1748 const Diagnostic::DiagStatePoint &point = *I;
1749 if (point.Loc.isInvalid())
1750 continue;
1751
1752 Record.push_back(point.Loc.getRawEncoding());
1753 for (Diagnostic::DiagState::iterator
1754 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1755 unsigned diag = I->first, map = I->second;
1756 if (map & 0x10) { // mapping from a diagnostic pragma.
1757 Record.push_back(diag);
1758 Record.push_back(map & 0x7);
1759 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001760 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001761 Record.push_back(-1); // mark the end of the diag/map pairs for this
1762 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001763 }
1764
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001765 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001766 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001767}
1768
Douglas Gregorc5046832009-04-27 18:38:38 +00001769//===----------------------------------------------------------------------===//
1770// Type Serialization
1771//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001772
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001773/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001774void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001775 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001776 if (Idx.getIndex() == 0) // we haven't seen this type before.
1777 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001778
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001779 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001780
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001781 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001782 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001783 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001784 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001785 else if (TypeOffsets.size() < Index) {
1786 TypeOffsets.resize(Index + 1);
1787 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001788 }
1789
1790 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001791
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001792 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001793 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001794
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001795 if (T.hasLocalNonFastQualifiers()) {
1796 Qualifiers Qs = T.getLocalQualifiers();
1797 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001798 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001799 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001800 } else {
1801 switch (T->getTypeClass()) {
1802 // For all of the concrete, non-dependent types, call the
1803 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001804#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001805 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001806#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001807#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001808 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001809 }
1810
1811 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001812 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001813
1814 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001815 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001816}
1817
Douglas Gregorc5046832009-04-27 18:38:38 +00001818//===----------------------------------------------------------------------===//
1819// Declaration Serialization
1820//===----------------------------------------------------------------------===//
1821
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001822/// \brief Write the block containing all of the declaration IDs
1823/// lexically declared within the given DeclContext.
1824///
1825/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1826/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001827uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001828 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001829 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001830 return 0;
1831
Douglas Gregor8f45df52009-04-16 22:23:12 +00001832 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001833 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001834 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001835 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001836 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1837 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001838 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001839
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001840 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001841 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001842 reinterpret_cast<char*>(Decls.data()),
1843 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001844 return Offset;
1845}
1846
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001847void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001848 using namespace llvm;
1849 RecordData Record;
1850
1851 // Write the type offsets array
1852 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001853 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001854 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1855 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1856 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1857 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001858 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001859 Record.push_back(TypeOffsets.size());
1860 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001861 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001862 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1863
1864 // Write the declaration offsets array
1865 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001866 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1869 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1870 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001871 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001872 Record.push_back(DeclOffsets.size());
1873 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001874 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001875 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1876}
1877
Douglas Gregorc5046832009-04-27 18:38:38 +00001878//===----------------------------------------------------------------------===//
1879// Global Method Pool and Selector Serialization
1880//===----------------------------------------------------------------------===//
1881
Douglas Gregore84a9da2009-04-20 20:36:09 +00001882namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001883// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001884class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001885 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001886
1887public:
1888 typedef Selector key_type;
1889 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001890
Sebastian Redl834bb972010-08-04 17:20:04 +00001891 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001892 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001893 ObjCMethodList Instance, Factory;
1894 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001895 typedef const data_type& data_type_ref;
1896
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001897 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001898
Douglas Gregorc78d3462009-04-24 21:10:55 +00001899 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001900 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001901 }
Mike Stump11289f42009-09-09 15:08:12 +00001902
1903 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001904 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1905 data_type_ref Methods) {
1906 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1907 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001908 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1909 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001910 Method = Method->Next)
1911 if (Method->Method)
1912 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001913 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001914 Method = Method->Next)
1915 if (Method->Method)
1916 DataLen += 4;
1917 clang::io::Emit16(Out, DataLen);
1918 return std::make_pair(KeyLen, DataLen);
1919 }
Mike Stump11289f42009-09-09 15:08:12 +00001920
Douglas Gregor95c13f52009-04-25 17:48:32 +00001921 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001922 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001923 assert((Start >> 32) == 0 && "Selector key offset too large");
1924 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001925 unsigned N = Sel.getNumArgs();
1926 clang::io::Emit16(Out, N);
1927 if (N == 0)
1928 N = 1;
1929 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001930 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001931 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1932 }
Mike Stump11289f42009-09-09 15:08:12 +00001933
Douglas Gregorc78d3462009-04-24 21:10:55 +00001934 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001935 data_type_ref Methods, unsigned DataLen) {
1936 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001937 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001938 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001939 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001940 Method = Method->Next)
1941 if (Method->Method)
1942 ++NumInstanceMethods;
1943
1944 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001945 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001946 Method = Method->Next)
1947 if (Method->Method)
1948 ++NumFactoryMethods;
1949
1950 clang::io::Emit16(Out, NumInstanceMethods);
1951 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001952 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001953 Method = Method->Next)
1954 if (Method->Method)
1955 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001956 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001957 Method = Method->Next)
1958 if (Method->Method)
1959 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001960
1961 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001962 }
1963};
1964} // end anonymous namespace
1965
Sebastian Redla19a67f2010-08-03 21:58:15 +00001966/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001967///
1968/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001969/// in an on-disk hash table indexed by the selector. The hash table also
1970/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001971void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001972 using namespace llvm;
1973
Sebastian Redla19a67f2010-08-03 21:58:15 +00001974 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001975 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001976 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001977 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001978 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001979 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001980 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001981 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001982
Sebastian Redla19a67f2010-08-03 21:58:15 +00001983 // Create the on-disk hash table representation. We walk through every
1984 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001985 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001986 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001987 I = SelectorIDs.begin(), E = SelectorIDs.end();
1988 I != E; ++I) {
1989 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001990 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001991 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001992 I->second,
1993 ObjCMethodList(),
1994 ObjCMethodList()
1995 };
1996 if (F != SemaRef.MethodPool.end()) {
1997 Data.Instance = F->second.first;
1998 Data.Factory = F->second.second;
1999 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002000 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002001 // changed.
2002 if (Chain && I->second < FirstSelectorID) {
2003 // Selector already exists. Did it change?
2004 bool changed = false;
2005 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2006 M = M->Next) {
2007 if (M->Method->getPCHLevel() == 0)
2008 changed = true;
2009 }
2010 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2011 M = M->Next) {
2012 if (M->Method->getPCHLevel() == 0)
2013 changed = true;
2014 }
2015 if (!changed)
2016 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002017 } else if (Data.Instance.Method || Data.Factory.Method) {
2018 // A new method pool entry.
2019 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002020 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002021 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002022 }
2023
Douglas Gregorc78d3462009-04-24 21:10:55 +00002024 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002025 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002026 uint32_t BucketOffset;
2027 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002028 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002029 llvm::raw_svector_ostream Out(MethodPool);
2030 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002031 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002032 BucketOffset = Generator.Emit(Out, Trait);
2033 }
2034
2035 // Create a blob abbreviation
2036 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002037 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2041 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2042
Douglas Gregor95c13f52009-04-25 17:48:32 +00002043 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002044 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002045 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002046 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002047 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002048 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002049
2050 // Create a blob abbreviation for the selector table offsets.
2051 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002052 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2055 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2056
2057 // Write the selector offsets table.
2058 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002059 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002060 Record.push_back(SelectorOffsets.size());
2061 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002062 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00002063 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002064 }
2065}
2066
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002067/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002068void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002069 using namespace llvm;
2070 if (SemaRef.ReferencedSelectors.empty())
2071 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002072
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002073 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002074
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002075 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002076 // very tricky to fix, and given that @selector shouldn't really appear in
2077 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002078 for (DenseMap<Selector, SourceLocation>::iterator S =
2079 SemaRef.ReferencedSelectors.begin(),
2080 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2081 Selector Sel = (*S).first;
2082 SourceLocation Loc = (*S).second;
2083 AddSelectorRef(Sel, Record);
2084 AddSourceLocation(Loc, Record);
2085 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002086 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002087}
2088
Douglas Gregorc5046832009-04-27 18:38:38 +00002089//===----------------------------------------------------------------------===//
2090// Identifier Table Serialization
2091//===----------------------------------------------------------------------===//
2092
Douglas Gregorc78d3462009-04-24 21:10:55 +00002093namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002094class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002095 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002096 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002097
Douglas Gregor1d583f22009-04-28 21:18:29 +00002098 /// \brief Determines whether this is an "interesting" identifier
2099 /// that needs a full IdentifierInfo structure written into the hash
2100 /// table.
2101 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2102 return II->isPoisoned() ||
2103 II->isExtensionToken() ||
2104 II->hasMacroDefinition() ||
2105 II->getObjCOrBuiltinID() ||
2106 II->getFETokenInfo<void>();
2107 }
2108
Douglas Gregore84a9da2009-04-20 20:36:09 +00002109public:
2110 typedef const IdentifierInfo* key_type;
2111 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002112
Sebastian Redl539c5062010-08-18 23:57:32 +00002113 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002114 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002115
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002116 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002117 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002118
2119 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002120 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002121 }
Mike Stump11289f42009-09-09 15:08:12 +00002122
2123 std::pair<unsigned,unsigned>
2124 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002125 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002126 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002127 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2128 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002129 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002130 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002131 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002132 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002133 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2134 DEnd = IdentifierResolver::end();
2135 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002136 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002137 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002138 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002139 // We emit the key length after the data length so that every
2140 // string is preceded by a 16-bit length. This matches the PTH
2141 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002142 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002143 return std::make_pair(KeyLen, DataLen);
2144 }
Mike Stump11289f42009-09-09 15:08:12 +00002145
2146 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002147 unsigned KeyLen) {
2148 // Record the location of the key data. This is used when generating
2149 // the mapping from persistent IDs to strings.
2150 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002151 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002152 }
Mike Stump11289f42009-09-09 15:08:12 +00002153
2154 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002155 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002156 if (!isInterestingIdentifier(II)) {
2157 clang::io::Emit32(Out, ID << 1);
2158 return;
2159 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002160
Douglas Gregor1d583f22009-04-28 21:18:29 +00002161 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002162 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002163 bool hasMacroDefinition =
2164 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002165 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002166 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002167 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2168 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2169 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002170 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002171 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002172 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002173
Douglas Gregorc3366a52009-04-21 23:56:24 +00002174 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002175 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002176
Douglas Gregora868bbd2009-04-21 22:25:48 +00002177 // Emit the declaration IDs in reverse order, because the
2178 // IdentifierResolver provides the declarations as they would be
2179 // visible (e.g., the function "stat" would come before the struct
2180 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2181 // adds declarations to the end of the list (so we need to see the
2182 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002183 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002184 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002185 IdentifierResolver::end());
2186 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2187 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002188 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002189 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002190 }
2191};
2192} // end anonymous namespace
2193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002194/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002195///
2196/// The identifier table consists of a blob containing string data
2197/// (the actual identifiers themselves) and a separate "offsets" index
2198/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002199void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002200 using namespace llvm;
2201
2202 // Create and write out the blob that contains the identifier
2203 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002204 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002205 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002206 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002207
Douglas Gregore6648fb2009-04-28 20:33:11 +00002208 // Look for any identifiers that were named while processing the
2209 // headers, but are otherwise not needed. We add these to the hash
2210 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002211 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002212 // file.
2213 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2214 IDEnd = PP.getIdentifierTable().end();
2215 ID != IDEnd; ++ID)
2216 getIdentifierRef(ID->second);
2217
Sebastian Redlff4a2952010-07-23 23:49:55 +00002218 // Create the on-disk hash table representation. We only store offsets
2219 // for identifiers that appear here for the first time.
2220 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002221 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002222 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2223 ID != IDEnd; ++ID) {
2224 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002225 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002226 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002227 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002228
Douglas Gregore84a9da2009-04-20 20:36:09 +00002229 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002230 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002231 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002232 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002233 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002234 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002235 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002236 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002237 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002238 }
2239
2240 // Create a blob abbreviation
2241 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002242 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002245 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002246
2247 // Write the identifier table
2248 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002249 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002250 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002251 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002252 }
2253
2254 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002255 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002256 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2259 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2260
2261 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002262 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002263 Record.push_back(IdentifierOffsets.size());
2264 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002265 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002266 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002267}
2268
Douglas Gregorc5046832009-04-27 18:38:38 +00002269//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002270// DeclContext's Name Lookup Table Serialization
2271//===----------------------------------------------------------------------===//
2272
2273namespace {
2274// Trait used for the on-disk hash table used in the method pool.
2275class ASTDeclContextNameLookupTrait {
2276 ASTWriter &Writer;
2277
2278public:
2279 typedef DeclarationName key_type;
2280 typedef key_type key_type_ref;
2281
2282 typedef DeclContext::lookup_result data_type;
2283 typedef const data_type& data_type_ref;
2284
2285 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2286
2287 unsigned ComputeHash(DeclarationName Name) {
2288 llvm::FoldingSetNodeID ID;
2289 ID.AddInteger(Name.getNameKind());
2290
2291 switch (Name.getNameKind()) {
2292 case DeclarationName::Identifier:
2293 ID.AddString(Name.getAsIdentifierInfo()->getName());
2294 break;
2295 case DeclarationName::ObjCZeroArgSelector:
2296 case DeclarationName::ObjCOneArgSelector:
2297 case DeclarationName::ObjCMultiArgSelector:
2298 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2299 break;
2300 case DeclarationName::CXXConstructorName:
2301 case DeclarationName::CXXDestructorName:
2302 case DeclarationName::CXXConversionFunctionName:
2303 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2304 break;
2305 case DeclarationName::CXXOperatorName:
2306 ID.AddInteger(Name.getCXXOverloadedOperator());
2307 break;
2308 case DeclarationName::CXXLiteralOperatorName:
2309 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2310 case DeclarationName::CXXUsingDirective:
2311 break;
2312 }
2313
2314 return ID.ComputeHash();
2315 }
2316
2317 std::pair<unsigned,unsigned>
2318 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2319 data_type_ref Lookup) {
2320 unsigned KeyLen = 1;
2321 switch (Name.getNameKind()) {
2322 case DeclarationName::Identifier:
2323 case DeclarationName::ObjCZeroArgSelector:
2324 case DeclarationName::ObjCOneArgSelector:
2325 case DeclarationName::ObjCMultiArgSelector:
2326 case DeclarationName::CXXConstructorName:
2327 case DeclarationName::CXXDestructorName:
2328 case DeclarationName::CXXConversionFunctionName:
2329 case DeclarationName::CXXLiteralOperatorName:
2330 KeyLen += 4;
2331 break;
2332 case DeclarationName::CXXOperatorName:
2333 KeyLen += 1;
2334 break;
2335 case DeclarationName::CXXUsingDirective:
2336 break;
2337 }
2338 clang::io::Emit16(Out, KeyLen);
2339
2340 // 2 bytes for num of decls and 4 for each DeclID.
2341 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2342 clang::io::Emit16(Out, DataLen);
2343
2344 return std::make_pair(KeyLen, DataLen);
2345 }
2346
2347 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2348 using namespace clang::io;
2349
2350 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2351 Emit8(Out, Name.getNameKind());
2352 switch (Name.getNameKind()) {
2353 case DeclarationName::Identifier:
2354 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2355 break;
2356 case DeclarationName::ObjCZeroArgSelector:
2357 case DeclarationName::ObjCOneArgSelector:
2358 case DeclarationName::ObjCMultiArgSelector:
2359 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2360 break;
2361 case DeclarationName::CXXConstructorName:
2362 case DeclarationName::CXXDestructorName:
2363 case DeclarationName::CXXConversionFunctionName:
2364 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2365 break;
2366 case DeclarationName::CXXOperatorName:
2367 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2368 Emit8(Out, Name.getCXXOverloadedOperator());
2369 break;
2370 case DeclarationName::CXXLiteralOperatorName:
2371 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2372 break;
2373 case DeclarationName::CXXUsingDirective:
2374 break;
2375 }
2376 }
2377
2378 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2379 data_type Lookup, unsigned DataLen) {
2380 uint64_t Start = Out.tell(); (void)Start;
2381 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2382 for (; Lookup.first != Lookup.second; ++Lookup.first)
2383 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2384
2385 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2386 }
2387};
2388} // end anonymous namespace
2389
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002390/// \brief Write the block containing all of the declaration IDs
2391/// visible from the given DeclContext.
2392///
2393/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002394/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002395uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2396 DeclContext *DC) {
2397 if (DC->getPrimaryContext() != DC)
2398 return 0;
2399
2400 // Since there is no name lookup into functions or methods, don't bother to
2401 // build a visible-declarations table for these entities.
2402 if (DC->isFunctionOrMethod())
2403 return 0;
2404
2405 // If not in C++, we perform name lookup for the translation unit via the
2406 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2407 // FIXME: In C++ we need the visible declarations in order to "see" the
2408 // friend declarations, is there a way to do this without writing the table ?
2409 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2410 return 0;
2411
2412 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002413 if (DC->hasExternalVisibleStorage())
2414 DC->MaterializeVisibleDeclsFromExternalStorage();
2415 else
2416 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002417
2418 // Serialize the contents of the mapping used for lookup. Note that,
2419 // although we have two very different code paths, the serialized
2420 // representation is the same for both cases: a declaration name,
2421 // followed by a size, followed by references to the visible
2422 // declarations that have that name.
2423 uint64_t Offset = Stream.GetCurrentBitNo();
2424 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2425 if (!Map || Map->empty())
2426 return 0;
2427
2428 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2429 ASTDeclContextNameLookupTrait Trait(*this);
2430
2431 // Create the on-disk hash table representation.
2432 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2433 D != DEnd; ++D) {
2434 DeclarationName Name = D->first;
2435 DeclContext::lookup_result Result = D->second.getLookupResult();
2436 Generator.insert(Name, Result, Trait);
2437 }
2438
2439 // Create the on-disk hash table in a buffer.
2440 llvm::SmallString<4096> LookupTable;
2441 uint32_t BucketOffset;
2442 {
2443 llvm::raw_svector_ostream Out(LookupTable);
2444 // Make sure that no bucket is at offset 0
2445 clang::io::Emit32(Out, 0);
2446 BucketOffset = Generator.Emit(Out, Trait);
2447 }
2448
2449 // Write the lookup table
2450 RecordData Record;
2451 Record.push_back(DECL_CONTEXT_VISIBLE);
2452 Record.push_back(BucketOffset);
2453 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2454 LookupTable.str());
2455
2456 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2457 ++NumVisibleDeclContexts;
2458 return Offset;
2459}
2460
Sebastian Redla4071b42010-08-24 00:50:09 +00002461/// \brief Write an UPDATE_VISIBLE block for the given context.
2462///
2463/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2464/// DeclContext in a dependent AST file. As such, they only exist for the TU
2465/// (in C++) and for namespaces.
2466void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002467 // Make the context build its lookup table, but don't make it load external
2468 // decls.
2469 DC->lookup(DeclarationName());
2470
2471 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2472 if (!Map || Map->empty())
2473 return;
2474
2475 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2476 ASTDeclContextNameLookupTrait Trait(*this);
2477
2478 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002479 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2480 D != DEnd; ++D) {
2481 DeclarationName Name = D->first;
2482 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002483 // For any name that appears in this table, the results are complete, i.e.
2484 // they overwrite results from previous PCHs. Merging is always a mess.
2485 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002486 }
2487
2488 // Create the on-disk hash table in a buffer.
2489 llvm::SmallString<4096> LookupTable;
2490 uint32_t BucketOffset;
2491 {
2492 llvm::raw_svector_ostream Out(LookupTable);
2493 // Make sure that no bucket is at offset 0
2494 clang::io::Emit32(Out, 0);
2495 BucketOffset = Generator.Emit(Out, Trait);
2496 }
2497
2498 // Write the lookup table
2499 RecordData Record;
2500 Record.push_back(UPDATE_VISIBLE);
2501 Record.push_back(getDeclID(cast<Decl>(DC)));
2502 Record.push_back(BucketOffset);
2503 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2504}
2505
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002506//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002507// General Serialization Routines
2508//===----------------------------------------------------------------------===//
2509
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002510/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002511void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002512 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002513 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2514 const Attr * A = *i;
2515 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2516 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002517
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002518#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002519
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002520 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002521}
2522
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002523void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002524 Record.push_back(Str.size());
2525 Record.insert(Record.end(), Str.begin(), Str.end());
2526}
2527
Douglas Gregore84a9da2009-04-20 20:36:09 +00002528/// \brief Note that the identifier II occurs at the given offset
2529/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002530void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002531 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002532 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002533 // up earlier in the chain and thus don't need an offset.
2534 if (ID >= FirstIdentID)
2535 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002536}
2537
Douglas Gregor95c13f52009-04-25 17:48:32 +00002538/// \brief Note that the selector Sel occurs at the given offset
2539/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002540void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002541 unsigned ID = SelectorIDs[Sel];
2542 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002543 // Don't record offsets for selectors that are also available in a different
2544 // file.
2545 if (ID < FirstSelectorID)
2546 return;
2547 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002548}
2549
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002550ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002551 : Stream(Stream), Chain(0), SerializationListener(0),
2552 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002553 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002554 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002555 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2556 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002557 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002558 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2559 NextCXXBaseSpecifiersID(1)
2560{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002561}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002562
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002563void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002564 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002565 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002566 Stream.Emit((unsigned)'C', 8);
2567 Stream.Emit((unsigned)'P', 8);
2568 Stream.Emit((unsigned)'C', 8);
2569 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002570
Chris Lattner28fa4e62009-04-26 22:26:21 +00002571 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002572
Sebastian Redl143413f2010-07-12 22:02:52 +00002573 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002574 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002575 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002576 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002577}
2578
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002579void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002580 const char *isysroot) {
2581 using namespace llvm;
2582
2583 ASTContext &Context = SemaRef.Context;
2584 Preprocessor &PP = SemaRef.PP;
2585
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002586 // The translation unit is the first declaration we'll emit.
2587 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002588 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002589 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002590
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002591 // Make sure that we emit IdentifierInfos (and any attached
2592 // declarations) for builtins.
2593 {
2594 IdentifierTable &Table = PP.getIdentifierTable();
2595 llvm::SmallVector<const char *, 32> BuiltinNames;
2596 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2597 Context.getLangOptions().NoBuiltin);
2598 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2599 getIdentifierRef(&Table.get(BuiltinNames[I]));
2600 }
2601
Chris Lattner0c797362009-09-08 18:19:27 +00002602 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002603 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002604 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002605 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002606 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2607 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002608 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002609
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002610 // Build a record containing all of the file scoped decls in this file.
2611 RecordData UnusedFileScopedDecls;
2612 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2613 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002614
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002615 RecordData WeakUndeclaredIdentifiers;
2616 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2617 WeakUndeclaredIdentifiers.push_back(
2618 SemaRef.WeakUndeclaredIdentifiers.size());
2619 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2620 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2621 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2622 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2623 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2624 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2625 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2626 }
2627 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002628
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002629 // Build a record containing all of the locally-scoped external
2630 // declarations in this header file. Generally, this record will be
2631 // empty.
2632 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002633 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002634 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002635 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002636 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2637 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2638 TD != TDEnd; ++TD)
2639 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2640
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002641 // Build a record containing all of the ext_vector declarations.
2642 RecordData ExtVectorDecls;
2643 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2644 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2645
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002646 // Build a record containing all of the VTable uses information.
2647 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002648 if (!SemaRef.VTableUses.empty()) {
2649 VTableUses.push_back(SemaRef.VTableUses.size());
2650 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2651 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2652 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2653 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2654 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002655 }
2656
2657 // Build a record containing all of dynamic classes declarations.
2658 RecordData DynamicClasses;
2659 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2660 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2661
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002662 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002663 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002664 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002665 I = SemaRef.PendingInstantiations.begin(),
2666 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2667 AddDeclRef(I->first, PendingInstantiations);
2668 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002669 }
2670 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2671 "There are local ones at end of translation unit!");
2672
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002673 // Build a record containing some declaration references.
2674 RecordData SemaDeclRefs;
2675 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2676 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2677 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2678 }
2679
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002680 RecordData CUDASpecialDeclRefs;
2681 if (Context.getcudaConfigureCallDecl()) {
2682 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2683 }
2684
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002685 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002686 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002687 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002688 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002689 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002690 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002691 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002692 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002693 // Write the record of special types.
2694 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002695
Steve Naroffc277ad12009-07-18 15:33:26 +00002696 AddTypeRef(Context.getBuiltinVaListType(), Record);
2697 AddTypeRef(Context.getObjCIdType(), Record);
2698 AddTypeRef(Context.getObjCSelType(), Record);
2699 AddTypeRef(Context.getObjCProtoType(), Record);
2700 AddTypeRef(Context.getObjCClassType(), Record);
2701 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2702 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2703 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002704 AddTypeRef(Context.getjmp_bufType(), Record);
2705 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002706 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2707 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002708 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002709 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002710 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2711 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002712 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002713 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002714
Douglas Gregor1970d882009-04-26 03:49:13 +00002715 // Keep writing types and declarations until all types and
2716 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002717 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002718 WriteDeclsBlockAbbrevs();
2719 while (!DeclTypesToEmit.empty()) {
2720 DeclOrType DOT = DeclTypesToEmit.front();
2721 DeclTypesToEmit.pop();
2722 if (DOT.isType())
2723 WriteType(DOT.getType());
2724 else
2725 WriteDecl(Context, DOT.getDecl());
2726 }
2727 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002728
Douglas Gregor45053152009-10-17 17:25:45 +00002729 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002730 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002731 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002732 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002733 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002734
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002735 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002736 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002737
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002738 // Write the C++ base-specifier set offsets.
2739 if (!CXXBaseSpecifiersOffsets.empty()) {
2740 // Create a blob abbreviation for the C++ base specifiers offsets.
2741 using namespace llvm;
2742
2743 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2744 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2747 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2748
2749 // Write the selector offsets table.
2750 Record.clear();
2751 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2752 Record.push_back(CXXBaseSpecifiersOffsets.size());
2753 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2754 (const char *)CXXBaseSpecifiersOffsets.data(),
2755 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2756 }
2757
Douglas Gregord4df8652009-04-22 22:02:47 +00002758 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002759 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002760 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002761
2762 // Write the record containing tentative definitions.
2763 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002764 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002765
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002766 // Write the record containing unused file scoped decls.
2767 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002768 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002769
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002770 // Write the record containing weak undeclared identifiers.
2771 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002772 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002773 WeakUndeclaredIdentifiers);
2774
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002775 // Write the record containing locally-scoped external definitions.
2776 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002777 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002778 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002779
2780 // Write the record containing ext_vector type names.
2781 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002782 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002783
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002784 // Write the record containing VTable uses information.
2785 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002786 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002787
2788 // Write the record containing dynamic classes declarations.
2789 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002790 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002791
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002792 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002793 if (!PendingInstantiations.empty())
2794 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002795
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002796 // Write the record containing declaration references of Sema.
2797 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002798 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002799
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002800 // Write the record containing CUDA-specific declaration references.
2801 if (!CUDASpecialDeclRefs.empty())
2802 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2803
Douglas Gregor08f01292009-04-17 22:13:46 +00002804 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002805 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002806 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002807 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002808 Record.push_back(NumLexicalDeclContexts);
2809 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002810 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002811 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002812}
2813
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002814void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002815 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002816 using namespace llvm;
2817
2818 ASTContext &Context = SemaRef.Context;
2819 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002820
Sebastian Redl143413f2010-07-12 22:02:52 +00002821 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002822 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002823 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002824 if (StatCalls && !isysroot)
2825 WriteStatCache(*StatCalls);
2826 // FIXME: Source manager block should only write new stuff, which could be
2827 // done by tracking the largest ID in the chain
2828 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002829
2830 // The special types are in the chained PCH.
2831
2832 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002833 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002834 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002835 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002836 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2837 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002838 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002839 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002840 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002841 else if ((*I)->isChangedSinceDeserialization())
2842 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002843 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002844 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002845 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002846 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002847 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2848 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2849 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002850 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002851 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2852 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002853 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002854 // And a visible updates block for the DeclContexts.
2855 Abv = new llvm::BitCodeAbbrev();
2856 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2857 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2858 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2859 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2860 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2861 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002862
Sebastian Redl98912122010-07-27 23:01:28 +00002863 // Build a record containing all of the new tentative definitions in this
2864 // file, in TentativeDefinitions order.
2865 RecordData TentativeDefinitions;
2866 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2867 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2868 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2869 }
2870
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002871 // Build a record containing all of the file scoped decls in this file.
2872 RecordData UnusedFileScopedDecls;
2873 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2874 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2875 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002876 }
2877
Sebastian Redl08aca90252010-08-05 18:21:25 +00002878 // We write the entire table, overwriting the tables from the chain.
2879 RecordData WeakUndeclaredIdentifiers;
2880 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2881 WeakUndeclaredIdentifiers.push_back(
2882 SemaRef.WeakUndeclaredIdentifiers.size());
2883 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2884 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2885 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2886 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2887 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2888 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2889 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2890 }
2891 }
2892
Sebastian Redl98912122010-07-27 23:01:28 +00002893 // Build a record containing all of the locally-scoped external
2894 // declarations in this header file. Generally, this record will be
2895 // empty.
2896 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002897 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002898 // nondeterminstic!
2899 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2900 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2901 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2902 TD != TDEnd; ++TD) {
2903 if (TD->second->getPCHLevel() == 0)
2904 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2905 }
2906
2907 // Build a record containing all of the ext_vector declarations.
2908 RecordData ExtVectorDecls;
2909 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2910 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2911 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2912 }
2913
Sebastian Redl08aca90252010-08-05 18:21:25 +00002914 // Build a record containing all of the VTable uses information.
2915 // We write everything here, because it's too hard to determine whether
2916 // a use is new to this part.
2917 RecordData VTableUses;
2918 if (!SemaRef.VTableUses.empty()) {
2919 VTableUses.push_back(SemaRef.VTableUses.size());
2920 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2921 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2922 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2923 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2924 }
2925 }
2926
2927 // Build a record containing all of dynamic classes declarations.
2928 RecordData DynamicClasses;
2929 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2930 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2931 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2932
2933 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002934 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002935 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002936 I = SemaRef.PendingInstantiations.begin(),
2937 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002938 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002939 AddDeclRef(I->first, PendingInstantiations);
2940 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002941 }
2942 }
2943 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2944 "There are local ones at end of translation unit!");
2945
2946 // Build a record containing some declaration references.
2947 // It's not worth the effort to avoid duplication here.
2948 RecordData SemaDeclRefs;
2949 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2950 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2951 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2952 }
2953
Sebastian Redl539c5062010-08-18 23:57:32 +00002954 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002955 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002956 for (DeclsToRewriteTy::iterator
2957 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2958 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002959 while (!DeclTypesToEmit.empty()) {
2960 DeclOrType DOT = DeclTypesToEmit.front();
2961 DeclTypesToEmit.pop();
2962 if (DOT.isType())
2963 WriteType(DOT.getType());
2964 else
2965 WriteDecl(Context, DOT.getDecl());
2966 }
2967 Stream.ExitBlock();
2968
Sebastian Redl98912122010-07-27 23:01:28 +00002969 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002970 WriteSelectors(SemaRef);
2971 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002972 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002973 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002974 // FIXME: For chained PCH only write the new mappings (we currently
2975 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002976 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002977
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002978 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002979 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002980 RecordData FirstLatestDeclIDs;
2981 for (FirstLatestDeclMap::iterator
2982 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2983 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2984 "Expected first & second to be in different PCHs");
2985 AddDeclRef(I->first, FirstLatestDeclIDs);
2986 AddDeclRef(I->second, FirstLatestDeclIDs);
2987 }
2988 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002989 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002990
Sebastian Redl98912122010-07-27 23:01:28 +00002991 // Write the record containing external, unnamed definitions.
2992 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002993 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002994
2995 // Write the record containing tentative definitions.
2996 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002997 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002998
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002999 // Write the record containing unused file scoped decls.
3000 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003001 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003002
Sebastian Redl08aca90252010-08-05 18:21:25 +00003003 // Write the record containing weak undeclared identifiers.
3004 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003005 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003006 WeakUndeclaredIdentifiers);
3007
Sebastian Redl98912122010-07-27 23:01:28 +00003008 // Write the record containing locally-scoped external definitions.
3009 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003010 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003011 LocallyScopedExternalDecls);
3012
3013 // Write the record containing ext_vector type names.
3014 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003015 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003016
Sebastian Redl08aca90252010-08-05 18:21:25 +00003017 // Write the record containing VTable uses information.
3018 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003019 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003020
3021 // Write the record containing dynamic classes declarations.
3022 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003023 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003024
3025 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003026 if (!PendingInstantiations.empty())
3027 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003028
3029 // Write the record containing declaration references of Sema.
3030 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003031 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00003032
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003033 // Write the updates to DeclContexts.
3034 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3035 I = UpdatedDeclContexts.begin(),
3036 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003037 I != E; ++I)
3038 WriteDeclContextVisibleUpdate(*I);
3039
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003040 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003041
Sebastian Redl98912122010-07-27 23:01:28 +00003042 Record.clear();
3043 Record.push_back(NumStatements);
3044 Record.push_back(NumMacros);
3045 Record.push_back(NumLexicalDeclContexts);
3046 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003047 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003048 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003049 Stream.ExitBlock();
3050}
3051
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003052void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003053 if (DeclUpdates.empty())
3054 return;
3055
3056 RecordData OffsetsRecord;
3057 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3058 for (DeclUpdateMap::iterator
3059 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3060 const Decl *D = I->first;
3061 UpdateRecord &URec = I->second;
3062
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003063 if (DeclsToRewrite.count(D))
3064 continue; // The decl will be written completely,no need to store updates.
3065
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003066 uint64_t Offset = Stream.GetCurrentBitNo();
3067 Stream.EmitRecord(DECL_UPDATES, URec);
3068
3069 OffsetsRecord.push_back(GetDeclRef(D));
3070 OffsetsRecord.push_back(Offset);
3071 }
3072 Stream.ExitBlock();
3073 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3074}
3075
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003076void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003077 if (ReplacedDecls.empty())
3078 return;
3079
3080 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003081 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003082 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3083 Record.push_back(I->first);
3084 Record.push_back(I->second);
3085 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003086 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003087}
3088
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003089void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003090 Record.push_back(Loc.getRawEncoding());
3091}
3092
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003093void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003094 AddSourceLocation(Range.getBegin(), Record);
3095 AddSourceLocation(Range.getEnd(), Record);
3096}
3097
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003098void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003099 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003100 const uint64_t *Words = Value.getRawData();
3101 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003102}
3103
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003104void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003105 Record.push_back(Value.isUnsigned());
3106 AddAPInt(Value, Record);
3107}
3108
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003109void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003110 AddAPInt(Value.bitcastToAPInt(), Record);
3111}
3112
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003113void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003114 Record.push_back(getIdentifierRef(II));
3115}
3116
Sebastian Redl539c5062010-08-18 23:57:32 +00003117IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003118 if (II == 0)
3119 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003120
Sebastian Redl539c5062010-08-18 23:57:32 +00003121 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003122 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003123 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003124 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003125}
3126
Sebastian Redl50e26582010-09-15 19:54:06 +00003127MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003128 if (MD == 0)
3129 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003130
3131 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003132 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003133 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003134 return ID;
3135}
3136
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003137void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003138 Record.push_back(getSelectorRef(SelRef));
3139}
3140
Sebastian Redl539c5062010-08-18 23:57:32 +00003141SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003142 if (Sel.getAsOpaquePtr() == 0) {
3143 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003144 }
3145
Sebastian Redl539c5062010-08-18 23:57:32 +00003146 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003147 if (SID == 0 && Chain) {
3148 // This might trigger a ReadSelector callback, which will set the ID for
3149 // this selector.
3150 Chain->LoadSelector(Sel);
3151 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003152 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003153 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003154 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003155 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003156}
3157
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003158void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003159 AddDeclRef(Temp->getDestructor(), Record);
3160}
3161
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003162void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3163 CXXBaseSpecifier const *BasesEnd,
3164 RecordDataImpl &Record) {
3165 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3166 CXXBaseSpecifiersToWrite.push_back(
3167 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3168 Bases, BasesEnd));
3169 Record.push_back(NextCXXBaseSpecifiersID++);
3170}
3171
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003172void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003173 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003174 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003175 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003176 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003177 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003178 break;
3179 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003180 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003181 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003182 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003183 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3184 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003185 break;
3186 case TemplateArgument::TemplateExpansion:
3187 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3188 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003189 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003190 break;
John McCall0ad16662009-10-29 08:12:44 +00003191 case TemplateArgument::Null:
3192 case TemplateArgument::Integral:
3193 case TemplateArgument::Declaration:
3194 case TemplateArgument::Pack:
3195 break;
3196 }
3197}
3198
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003199void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003200 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003201 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003202
3203 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3204 bool InfoHasSameExpr
3205 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3206 Record.push_back(InfoHasSameExpr);
3207 if (InfoHasSameExpr)
3208 return; // Avoid storing the same expr twice.
3209 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003210 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3211 Record);
3212}
3213
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003214void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003215 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003216 AddTypeRef(QualType(), Record);
3217 return;
3218 }
3219
John McCallbcd03502009-12-07 02:54:59 +00003220 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00003221 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00003222 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003223 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003224}
3225
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003226void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003227 Record.push_back(GetOrCreateTypeID(T));
3228}
3229
3230TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003231 return MakeTypeID(T,
3232 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3233}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003234
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003235TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003236 return MakeTypeID(T,
3237 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003238}
3239
3240TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3241 if (T.isNull())
3242 return TypeIdx();
3243 assert(!T.getLocalFastQualifiers());
3244
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003245 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003246 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003247 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003248 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003249 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003250 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003251 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003252 return Idx;
3253}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003254
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003255TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003256 if (T.isNull())
3257 return TypeIdx();
3258 assert(!T.getLocalFastQualifiers());
3259
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003260 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3261 assert(I != TypeIdxs.end() && "Type not emitted!");
3262 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003263}
3264
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003265void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003266 Record.push_back(GetDeclRef(D));
3267}
3268
Sebastian Redl539c5062010-08-18 23:57:32 +00003269DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003270 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003271 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003272 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003273 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003274 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003275 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003276 // We haven't seen this declaration before. Give it a new ID and
3277 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003278 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003279 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003280 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3281 // We don't add it to the replacement collection here, because we don't
3282 // have the offset yet.
3283 DeclTypesToEmit.push(const_cast<Decl *>(D));
3284 // Reset the flag, so that we don't add this decl multiple times.
3285 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003286 }
3287
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003288 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003289}
3290
Sebastian Redl539c5062010-08-18 23:57:32 +00003291DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003292 if (D == 0)
3293 return 0;
3294
3295 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3296 return DeclIDs[D];
3297}
3298
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003299void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003300 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003301 Record.push_back(Name.getNameKind());
3302 switch (Name.getNameKind()) {
3303 case DeclarationName::Identifier:
3304 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3305 break;
3306
3307 case DeclarationName::ObjCZeroArgSelector:
3308 case DeclarationName::ObjCOneArgSelector:
3309 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003310 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003311 break;
3312
3313 case DeclarationName::CXXConstructorName:
3314 case DeclarationName::CXXDestructorName:
3315 case DeclarationName::CXXConversionFunctionName:
3316 AddTypeRef(Name.getCXXNameType(), Record);
3317 break;
3318
3319 case DeclarationName::CXXOperatorName:
3320 Record.push_back(Name.getCXXOverloadedOperator());
3321 break;
3322
Alexis Hunt3d221f22009-11-29 07:34:05 +00003323 case DeclarationName::CXXLiteralOperatorName:
3324 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3325 break;
3326
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003327 case DeclarationName::CXXUsingDirective:
3328 // No extra data to emit
3329 break;
3330 }
3331}
Chris Lattnerca025db2010-05-07 21:43:38 +00003332
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003333void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003334 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003335 switch (Name.getNameKind()) {
3336 case DeclarationName::CXXConstructorName:
3337 case DeclarationName::CXXDestructorName:
3338 case DeclarationName::CXXConversionFunctionName:
3339 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3340 break;
3341
3342 case DeclarationName::CXXOperatorName:
3343 AddSourceLocation(
3344 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3345 Record);
3346 AddSourceLocation(
3347 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3348 Record);
3349 break;
3350
3351 case DeclarationName::CXXLiteralOperatorName:
3352 AddSourceLocation(
3353 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3354 Record);
3355 break;
3356
3357 case DeclarationName::Identifier:
3358 case DeclarationName::ObjCZeroArgSelector:
3359 case DeclarationName::ObjCOneArgSelector:
3360 case DeclarationName::ObjCMultiArgSelector:
3361 case DeclarationName::CXXUsingDirective:
3362 break;
3363 }
3364}
3365
3366void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003367 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003368 AddDeclarationName(NameInfo.getName(), Record);
3369 AddSourceLocation(NameInfo.getLoc(), Record);
3370 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3371}
3372
3373void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003374 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003375 AddNestedNameSpecifier(Info.NNS, Record);
3376 AddSourceRange(Info.NNSRange, Record);
3377 Record.push_back(Info.NumTemplParamLists);
3378 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3379 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3380}
3381
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003382void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003383 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003384 // Nested name specifiers usually aren't too long. I think that 8 would
3385 // typically accomodate the vast majority.
3386 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3387
3388 // Push each of the NNS's onto a stack for serialization in reverse order.
3389 while (NNS) {
3390 NestedNames.push_back(NNS);
3391 NNS = NNS->getPrefix();
3392 }
3393
3394 Record.push_back(NestedNames.size());
3395 while(!NestedNames.empty()) {
3396 NNS = NestedNames.pop_back_val();
3397 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3398 Record.push_back(Kind);
3399 switch (Kind) {
3400 case NestedNameSpecifier::Identifier:
3401 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3402 break;
3403
3404 case NestedNameSpecifier::Namespace:
3405 AddDeclRef(NNS->getAsNamespace(), Record);
3406 break;
3407
3408 case NestedNameSpecifier::TypeSpec:
3409 case NestedNameSpecifier::TypeSpecWithTemplate:
3410 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3411 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3412 break;
3413
3414 case NestedNameSpecifier::Global:
3415 // Don't need to write an associated value.
3416 break;
3417 }
3418 }
3419}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003420
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003421void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003422 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003423 Record.push_back(Kind);
3424 switch (Kind) {
3425 case TemplateName::Template:
3426 AddDeclRef(Name.getAsTemplateDecl(), Record);
3427 break;
3428
3429 case TemplateName::OverloadedTemplate: {
3430 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3431 Record.push_back(OvT->size());
3432 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3433 I != E; ++I)
3434 AddDeclRef(*I, Record);
3435 break;
3436 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003437
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003438 case TemplateName::QualifiedTemplate: {
3439 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3440 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3441 Record.push_back(QualT->hasTemplateKeyword());
3442 AddDeclRef(QualT->getTemplateDecl(), Record);
3443 break;
3444 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003445
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003446 case TemplateName::DependentTemplate: {
3447 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3448 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3449 Record.push_back(DepT->isIdentifier());
3450 if (DepT->isIdentifier())
3451 AddIdentifierRef(DepT->getIdentifier(), Record);
3452 else
3453 Record.push_back(DepT->getOperator());
3454 break;
3455 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003456
3457 case TemplateName::SubstTemplateTemplateParmPack: {
3458 SubstTemplateTemplateParmPackStorage *SubstPack
3459 = Name.getAsSubstTemplateTemplateParmPack();
3460 AddDeclRef(SubstPack->getParameterPack(), Record);
3461 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3462 break;
3463 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003464 }
3465}
3466
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003467void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003468 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003469 Record.push_back(Arg.getKind());
3470 switch (Arg.getKind()) {
3471 case TemplateArgument::Null:
3472 break;
3473 case TemplateArgument::Type:
3474 AddTypeRef(Arg.getAsType(), Record);
3475 break;
3476 case TemplateArgument::Declaration:
3477 AddDeclRef(Arg.getAsDecl(), Record);
3478 break;
3479 case TemplateArgument::Integral:
3480 AddAPSInt(*Arg.getAsIntegral(), Record);
3481 AddTypeRef(Arg.getIntegralType(), Record);
3482 break;
3483 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003484 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3485 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003486 case TemplateArgument::TemplateExpansion:
3487 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003488 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3489 Record.push_back(*NumExpansions + 1);
3490 else
3491 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003492 break;
3493 case TemplateArgument::Expression:
3494 AddStmt(Arg.getAsExpr());
3495 break;
3496 case TemplateArgument::Pack:
3497 Record.push_back(Arg.pack_size());
3498 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3499 I != E; ++I)
3500 AddTemplateArgument(*I, Record);
3501 break;
3502 }
3503}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003504
3505void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003506ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003507 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003508 assert(TemplateParams && "No TemplateParams!");
3509 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3510 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3511 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3512 Record.push_back(TemplateParams->size());
3513 for (TemplateParameterList::const_iterator
3514 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3515 P != PEnd; ++P)
3516 AddDeclRef(*P, Record);
3517}
3518
3519/// \brief Emit a template argument list.
3520void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003521ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003522 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003523 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003524 Record.push_back(TemplateArgs->size());
3525 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003526 AddTemplateArgument(TemplateArgs->get(i), Record);
3527}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003528
3529
3530void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003531ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003532 Record.push_back(Set.size());
3533 for (UnresolvedSetImpl::const_iterator
3534 I = Set.begin(), E = Set.end(); I != E; ++I) {
3535 AddDeclRef(I.getDecl(), Record);
3536 Record.push_back(I.getAccess());
3537 }
3538}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003539
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003540void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003541 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003542 Record.push_back(Base.isVirtual());
3543 Record.push_back(Base.isBaseOfClass());
3544 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003545 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003546 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003547 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003548 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3549 : SourceLocation(),
3550 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003551}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003552
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003553void ASTWriter::FlushCXXBaseSpecifiers() {
3554 RecordData Record;
3555 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3556 Record.clear();
3557
3558 // Record the offset of this base-specifier set.
3559 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3560 if (Index == CXXBaseSpecifiersOffsets.size())
3561 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3562 else {
3563 if (Index > CXXBaseSpecifiersOffsets.size())
3564 CXXBaseSpecifiersOffsets.resize(Index + 1);
3565 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3566 }
3567
3568 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3569 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3570 Record.push_back(BEnd - B);
3571 for (; B != BEnd; ++B)
3572 AddCXXBaseSpecifier(*B, Record);
3573 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003574
3575 // Flush any expressions that were written as part of the base specifiers.
3576 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003577 }
3578
3579 CXXBaseSpecifiersToWrite.clear();
3580}
3581
Alexis Hunt1d792652011-01-08 20:30:50 +00003582void ASTWriter::AddCXXCtorInitializers(
3583 const CXXCtorInitializer * const *CtorInitializers,
3584 unsigned NumCtorInitializers,
3585 RecordDataImpl &Record) {
3586 Record.push_back(NumCtorInitializers);
3587 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3588 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003589
3590 Record.push_back(Init->isBaseInitializer());
3591 if (Init->isBaseInitializer()) {
3592 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3593 Record.push_back(Init->isBaseVirtual());
3594 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003595 Record.push_back(Init->isIndirectMemberInitializer());
3596 if (Init->isIndirectMemberInitializer())
3597 AddDeclRef(Init->getIndirectMember(), Record);
3598 else
3599 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003600 }
Francois Pichetd583da02010-12-04 09:14:42 +00003601
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003602 AddSourceLocation(Init->getMemberLocation(), Record);
3603 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003604 AddSourceLocation(Init->getLParenLoc(), Record);
3605 AddSourceLocation(Init->getRParenLoc(), Record);
3606 Record.push_back(Init->isWritten());
3607 if (Init->isWritten()) {
3608 Record.push_back(Init->getSourceOrder());
3609 } else {
3610 Record.push_back(Init->getNumArrayIndices());
3611 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3612 AddDeclRef(Init->getArrayIndex(i), Record);
3613 }
3614 }
3615}
3616
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003617void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3618 assert(D->DefinitionData);
3619 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3620 Record.push_back(Data.UserDeclaredConstructor);
3621 Record.push_back(Data.UserDeclaredCopyConstructor);
3622 Record.push_back(Data.UserDeclaredCopyAssignment);
3623 Record.push_back(Data.UserDeclaredDestructor);
3624 Record.push_back(Data.Aggregate);
3625 Record.push_back(Data.PlainOldData);
3626 Record.push_back(Data.Empty);
3627 Record.push_back(Data.Polymorphic);
3628 Record.push_back(Data.Abstract);
3629 Record.push_back(Data.HasTrivialConstructor);
3630 Record.push_back(Data.HasTrivialCopyConstructor);
3631 Record.push_back(Data.HasTrivialCopyAssignment);
3632 Record.push_back(Data.HasTrivialDestructor);
3633 Record.push_back(Data.ComputedVisibleConversions);
3634 Record.push_back(Data.DeclaredDefaultConstructor);
3635 Record.push_back(Data.DeclaredCopyConstructor);
3636 Record.push_back(Data.DeclaredCopyAssignment);
3637 Record.push_back(Data.DeclaredDestructor);
3638
3639 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003640 if (Data.NumBases > 0)
3641 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3642 Record);
3643
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003644 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3645 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003646 if (Data.NumVBases > 0)
3647 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3648 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003649
3650 AddUnresolvedSet(Data.Conversions, Record);
3651 AddUnresolvedSet(Data.VisibleConversions, Record);
3652 // Data.Definition is the owning decl, no need to write it.
3653 AddDeclRef(Data.FirstFriend, Record);
3654}
3655
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003656void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003657 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003658 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003659 assert(FirstDeclID == NextDeclID &&
3660 FirstTypeID == NextTypeID &&
3661 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003662 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003663 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003664 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003665 "Setting chain after writing has started.");
3666 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003667
3668 FirstDeclID += Chain->getTotalNumDecls();
3669 FirstTypeID += Chain->getTotalNumTypes();
3670 FirstIdentID += Chain->getTotalNumIdentifiers();
3671 FirstSelectorID += Chain->getTotalNumSelectors();
3672 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003673 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003674 NextDeclID = FirstDeclID;
3675 NextTypeID = FirstTypeID;
3676 NextIdentID = FirstIdentID;
3677 NextSelectorID = FirstSelectorID;
3678 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003679 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003680}
3681
Sebastian Redl539c5062010-08-18 23:57:32 +00003682void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003683 IdentifierIDs[II] = ID;
3684}
3685
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003686void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003687 // Always take the highest-numbered type index. This copes with an interesting
3688 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003689 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003690 // keep the higher-numbered entry so that we can properly write it out to
3691 // the AST file.
3692 TypeIdx &StoredIdx = TypeIdxs[T];
3693 if (Idx.getIndex() >= StoredIdx.getIndex())
3694 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003695}
3696
Sebastian Redl539c5062010-08-18 23:57:32 +00003697void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003698 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003699}
Sebastian Redl834bb972010-08-04 17:20:04 +00003700
Sebastian Redl539c5062010-08-18 23:57:32 +00003701void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003702 SelectorIDs[S] = ID;
3703}
Douglas Gregor91096292010-10-02 19:29:26 +00003704
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003705void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003706 MacroDefinition *MD) {
3707 MacroDefinitions[MD] = ID;
3708}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003709
3710void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3711 assert(D->isDefinition());
3712 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3713 // We are interested when a PCH decl is modified.
3714 if (RD->getPCHLevel() > 0) {
3715 // A forward reference was mutated into a definition. Rewrite it.
3716 // FIXME: This happens during template instantiation, should we
3717 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003718 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003719 }
3720
3721 for (CXXRecordDecl::redecl_iterator
3722 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3723 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3724 if (Redecl == RD)
3725 continue;
3726
3727 // We are interested when a PCH decl is modified.
3728 if (Redecl->getPCHLevel() > 0) {
3729 UpdateRecord &Record = DeclUpdates[Redecl];
3730 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3731 assert(Redecl->DefinitionData);
3732 assert(Redecl->DefinitionData->Definition == D);
3733 AddDeclRef(D, Record); // the DefinitionDecl
3734 }
3735 }
3736 }
3737}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003738void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3739 // TU and namespaces are handled elsewhere.
3740 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3741 return;
3742
3743 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3744 return; // Not a source decl added to a DeclContext from PCH.
3745
3746 AddUpdatedDeclContext(DC);
3747}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003748
3749void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3750 assert(D->isImplicit());
3751 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3752 return; // Not a source member added to a class from PCH.
3753 if (!isa<CXXMethodDecl>(D))
3754 return; // We are interested in lazily declared implicit methods.
3755
3756 // A decl coming from PCH was modified.
3757 assert(RD->isDefinition());
3758 UpdateRecord &Record = DeclUpdates[RD];
3759 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3760 AddDeclRef(D, Record);
3761}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003762
3763void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3764 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003765 // The specializations set is kept in the canonical template.
3766 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003767 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3768 return; // Not a source specialization added to a template from PCH.
3769
3770 UpdateRecord &Record = DeclUpdates[TD];
3771 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3772 AddDeclRef(D, Record);
3773}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003774
3775ASTSerializationListener::~ASTSerializationListener() { }