blob: ce6864914280beb5b39ac9c80a0db45feb0abf54 [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;
Douglas Gregor68051a72011-02-11 00:26:14 +00001539 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1540 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001541 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001542 MacroDefinitionsSeen.insert(I->first);
1543 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1544 }
1545
1546 // Sort the set of macro definitions that need to be serialized by the
1547 // name of the macro, to provide a stable ordering.
1548 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1549 &compareMacroDefinitions);
1550
Douglas Gregor68051a72011-02-11 00:26:14 +00001551 // Resolve any identifiers that defined macros at the time they were
1552 // deserialized, adding them to the list of macros to emit (if appropriate).
1553 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1554 IdentifierInfo *Name
1555 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1556 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1557 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1558 }
1559
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001560 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1561 const IdentifierInfo *Name = MacrosToEmit[I].first;
1562 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001563 if (!MI)
1564 continue;
1565
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001566 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001567 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001568 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001569
1570 // FIXME: There is a (probably minor) optimization we could do here, if
1571 // the macro comes from the original PCH but the identifier comes from a
1572 // chained PCH, by storing the offset into the original PCH rather than
1573 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001574 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001575 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001576 continue;
1577
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001578 AddIdentifierRef(Name, Record);
1579 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001580 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1581 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001582
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001583 unsigned Code;
1584 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001585 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001586 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001587 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001588
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001589 Record.push_back(MI->isC99Varargs());
1590 Record.push_back(MI->isGNUVarargs());
1591 Record.push_back(MI->getNumArgs());
1592 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1593 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001594 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001595 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001596
Douglas Gregoraae92242010-03-19 21:51:54 +00001597 // If we have a detailed preprocessing record, record the macro definition
1598 // ID that corresponds to this macro.
1599 if (PPRec)
1600 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001601
Douglas Gregor8f45df52009-04-16 22:23:12 +00001602 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001603 Record.clear();
1604
Chris Lattner2199f5b2009-04-10 18:08:30 +00001605 // Emit the tokens array.
1606 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1607 // Note that we know that the preprocessor does not have any annotation
1608 // tokens in it because they are created by the parser, and thus can't be
1609 // in a macro definition.
1610 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001611
Chris Lattner2199f5b2009-04-10 18:08:30 +00001612 Record.push_back(Tok.getLocation().getRawEncoding());
1613 Record.push_back(Tok.getLength());
1614
Chris Lattner2199f5b2009-04-10 18:08:30 +00001615 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1616 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001617 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001618 // FIXME: Should translate token kind to a stable encoding.
1619 Record.push_back(Tok.getKind());
1620 // FIXME: Should translate token flags to a stable encoding.
1621 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001622
Sebastian Redl539c5062010-08-18 23:57:32 +00001623 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001624 Record.clear();
1625 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001626 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001627 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001628 Stream.ExitBlock();
1629
1630 if (PPRec)
1631 WritePreprocessorDetail(*PPRec);
1632}
1633
1634void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1635 if (PPRec.begin(Chain) == PPRec.end(Chain))
1636 return;
1637
1638 // Enter the preprocessor block.
1639 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001640
Douglas Gregoraae92242010-03-19 21:51:54 +00001641 // If the preprocessor has a preprocessing record, emit it.
1642 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001643 using namespace llvm;
1644
1645 // Set up the abbreviation for
1646 unsigned InclusionAbbrev = 0;
1647 {
1648 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1649 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1650 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1651 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1652 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1653 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1656 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1657 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1658 }
1659
1660 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1661 RecordData Record;
1662 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1663 EEnd = PPRec.end(Chain);
1664 E != EEnd; ++E) {
1665 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001666
Douglas Gregor92a96f52011-02-08 21:58:10 +00001667 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1668 // Record this macro definition's location.
1669 MacroID ID = getMacroDefinitionID(MD);
1670
1671 // Don't write the macro definition if it is from another AST file.
1672 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001673 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001674
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001675 // Notify the serialization listener that we're serializing this entity.
1676 if (SerializationListener)
1677 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001678 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001679
Douglas Gregor92a96f52011-02-08 21:58:10 +00001680 unsigned Position = ID - FirstMacroID;
1681 if (Position != MacroDefinitionOffsets.size()) {
1682 if (Position > MacroDefinitionOffsets.size())
1683 MacroDefinitionOffsets.resize(Position + 1);
1684
1685 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1686 } else
1687 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001688
Douglas Gregor92a96f52011-02-08 21:58:10 +00001689 Record.push_back(IndexBase + NumPreprocessingRecords++);
1690 Record.push_back(ID);
1691 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1692 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1693 AddIdentifierRef(MD->getName(), Record);
1694 AddSourceLocation(MD->getLocation(), Record);
1695 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1696 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001697 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001698
Douglas Gregor92a96f52011-02-08 21:58:10 +00001699 // Notify the serialization listener that we're serializing this entity.
1700 if (SerializationListener)
1701 SerializationListener->SerializedPreprocessedEntity(*E,
1702 Stream.GetCurrentBitNo());
1703
1704 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1705 Record.push_back(IndexBase + NumPreprocessingRecords++);
1706 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1707 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1708 AddIdentifierRef(MI->getName(), Record);
1709 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1710 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1711 continue;
1712 }
1713
1714 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1715 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1716 Record.push_back(IndexBase + NumPreprocessingRecords++);
1717 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1718 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1719 Record.push_back(ID->getFileName().size());
1720 Record.push_back(ID->wasInQuotes());
1721 Record.push_back(static_cast<unsigned>(ID->getKind()));
1722 llvm::SmallString<64> Buffer;
1723 Buffer += ID->getFileName();
1724 Buffer += ID->getFile()->getName();
1725 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1726 continue;
1727 }
1728
1729 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1730 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001731 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001732
Douglas Gregoraae92242010-03-19 21:51:54 +00001733 // Write the offsets table for the preprocessing record.
1734 if (NumPreprocessingRecords > 0) {
1735 // Write the offsets table for identifier IDs.
1736 using namespace llvm;
1737 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001738 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1742 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001743
Douglas Gregoraae92242010-03-19 21:51:54 +00001744 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001745 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001746 Record.push_back(NumPreprocessingRecords);
1747 Record.push_back(MacroDefinitionOffsets.size());
1748 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001749 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001750 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1751 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001752}
1753
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001754void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001755 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001756 for (Diagnostic::DiagStatePointsTy::const_iterator
1757 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1758 I != E; ++I) {
1759 const Diagnostic::DiagStatePoint &point = *I;
1760 if (point.Loc.isInvalid())
1761 continue;
1762
1763 Record.push_back(point.Loc.getRawEncoding());
1764 for (Diagnostic::DiagState::iterator
1765 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1766 unsigned diag = I->first, map = I->second;
1767 if (map & 0x10) { // mapping from a diagnostic pragma.
1768 Record.push_back(diag);
1769 Record.push_back(map & 0x7);
1770 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001771 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001772 Record.push_back(-1); // mark the end of the diag/map pairs for this
1773 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001774 }
1775
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001776 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001777 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001778}
1779
Douglas Gregorc5046832009-04-27 18:38:38 +00001780//===----------------------------------------------------------------------===//
1781// Type Serialization
1782//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001783
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001784/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001785void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001786 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001787 if (Idx.getIndex() == 0) // we haven't seen this type before.
1788 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001789
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001790 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001791
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001792 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001793 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001794 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001795 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001796 else if (TypeOffsets.size() < Index) {
1797 TypeOffsets.resize(Index + 1);
1798 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001799 }
1800
1801 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001802
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001803 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001804 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001805
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001806 if (T.hasLocalNonFastQualifiers()) {
1807 Qualifiers Qs = T.getLocalQualifiers();
1808 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001809 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001810 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001811 } else {
1812 switch (T->getTypeClass()) {
1813 // For all of the concrete, non-dependent types, call the
1814 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001815#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001816 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001817#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001818#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001819 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001820 }
1821
1822 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001823 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001824
1825 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001826 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001827}
1828
Douglas Gregorc5046832009-04-27 18:38:38 +00001829//===----------------------------------------------------------------------===//
1830// Declaration Serialization
1831//===----------------------------------------------------------------------===//
1832
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001833/// \brief Write the block containing all of the declaration IDs
1834/// lexically declared within the given DeclContext.
1835///
1836/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1837/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001838uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001839 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001840 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001841 return 0;
1842
Douglas Gregor8f45df52009-04-16 22:23:12 +00001843 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001844 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001845 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001846 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001847 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1848 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001849 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001850
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001851 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001852 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001853 reinterpret_cast<char*>(Decls.data()),
1854 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001855 return Offset;
1856}
1857
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001858void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001859 using namespace llvm;
1860 RecordData Record;
1861
1862 // Write the type offsets array
1863 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001864 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1867 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1868 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001869 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001870 Record.push_back(TypeOffsets.size());
1871 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001872 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001873 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1874
1875 // Write the declaration offsets array
1876 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001877 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1880 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1881 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001882 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001883 Record.push_back(DeclOffsets.size());
1884 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001885 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001886 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1887}
1888
Douglas Gregorc5046832009-04-27 18:38:38 +00001889//===----------------------------------------------------------------------===//
1890// Global Method Pool and Selector Serialization
1891//===----------------------------------------------------------------------===//
1892
Douglas Gregore84a9da2009-04-20 20:36:09 +00001893namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001894// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001895class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001896 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001897
1898public:
1899 typedef Selector key_type;
1900 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001901
Sebastian Redl834bb972010-08-04 17:20:04 +00001902 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001903 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001904 ObjCMethodList Instance, Factory;
1905 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001906 typedef const data_type& data_type_ref;
1907
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001908 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001909
Douglas Gregorc78d3462009-04-24 21:10:55 +00001910 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001911 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001912 }
Mike Stump11289f42009-09-09 15:08:12 +00001913
1914 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001915 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1916 data_type_ref Methods) {
1917 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1918 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001919 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1920 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001921 Method = Method->Next)
1922 if (Method->Method)
1923 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001924 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001925 Method = Method->Next)
1926 if (Method->Method)
1927 DataLen += 4;
1928 clang::io::Emit16(Out, DataLen);
1929 return std::make_pair(KeyLen, DataLen);
1930 }
Mike Stump11289f42009-09-09 15:08:12 +00001931
Douglas Gregor95c13f52009-04-25 17:48:32 +00001932 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001933 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001934 assert((Start >> 32) == 0 && "Selector key offset too large");
1935 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001936 unsigned N = Sel.getNumArgs();
1937 clang::io::Emit16(Out, N);
1938 if (N == 0)
1939 N = 1;
1940 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001941 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001942 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1943 }
Mike Stump11289f42009-09-09 15:08:12 +00001944
Douglas Gregorc78d3462009-04-24 21:10:55 +00001945 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001946 data_type_ref Methods, unsigned DataLen) {
1947 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001948 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001949 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001950 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001951 Method = Method->Next)
1952 if (Method->Method)
1953 ++NumInstanceMethods;
1954
1955 unsigned NumFactoryMethods = 0;
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 ++NumFactoryMethods;
1960
1961 clang::io::Emit16(Out, NumInstanceMethods);
1962 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001963 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001964 Method = Method->Next)
1965 if (Method->Method)
1966 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001967 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001968 Method = Method->Next)
1969 if (Method->Method)
1970 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001971
1972 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001973 }
1974};
1975} // end anonymous namespace
1976
Sebastian Redla19a67f2010-08-03 21:58:15 +00001977/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001978///
1979/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001980/// in an on-disk hash table indexed by the selector. The hash table also
1981/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001982void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001983 using namespace llvm;
1984
Sebastian Redla19a67f2010-08-03 21:58:15 +00001985 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001986 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001987 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001988 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001989 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001990 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001991 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001992 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001993
Sebastian Redla19a67f2010-08-03 21:58:15 +00001994 // Create the on-disk hash table representation. We walk through every
1995 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001996 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001997 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001998 I = SelectorIDs.begin(), E = SelectorIDs.end();
1999 I != E; ++I) {
2000 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002001 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002002 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002003 I->second,
2004 ObjCMethodList(),
2005 ObjCMethodList()
2006 };
2007 if (F != SemaRef.MethodPool.end()) {
2008 Data.Instance = F->second.first;
2009 Data.Factory = F->second.second;
2010 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002011 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002012 // changed.
2013 if (Chain && I->second < FirstSelectorID) {
2014 // Selector already exists. Did it change?
2015 bool changed = false;
2016 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2017 M = M->Next) {
2018 if (M->Method->getPCHLevel() == 0)
2019 changed = true;
2020 }
2021 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2022 M = M->Next) {
2023 if (M->Method->getPCHLevel() == 0)
2024 changed = true;
2025 }
2026 if (!changed)
2027 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002028 } else if (Data.Instance.Method || Data.Factory.Method) {
2029 // A new method pool entry.
2030 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002031 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002032 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002033 }
2034
Douglas Gregorc78d3462009-04-24 21:10:55 +00002035 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002036 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002037 uint32_t BucketOffset;
2038 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002039 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002040 llvm::raw_svector_ostream Out(MethodPool);
2041 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002042 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002043 BucketOffset = Generator.Emit(Out, Trait);
2044 }
2045
2046 // Create a blob abbreviation
2047 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002048 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2052 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2053
Douglas Gregor95c13f52009-04-25 17:48:32 +00002054 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002055 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002056 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002057 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002058 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002059 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002060
2061 // Create a blob abbreviation for the selector table offsets.
2062 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002063 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2066 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2067
2068 // Write the selector offsets table.
2069 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002070 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002071 Record.push_back(SelectorOffsets.size());
2072 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002073 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00002074 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002075 }
2076}
2077
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002078/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002079void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002080 using namespace llvm;
2081 if (SemaRef.ReferencedSelectors.empty())
2082 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002083
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002084 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002085
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002086 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002087 // very tricky to fix, and given that @selector shouldn't really appear in
2088 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002089 for (DenseMap<Selector, SourceLocation>::iterator S =
2090 SemaRef.ReferencedSelectors.begin(),
2091 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2092 Selector Sel = (*S).first;
2093 SourceLocation Loc = (*S).second;
2094 AddSelectorRef(Sel, Record);
2095 AddSourceLocation(Loc, Record);
2096 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002097 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002098}
2099
Douglas Gregorc5046832009-04-27 18:38:38 +00002100//===----------------------------------------------------------------------===//
2101// Identifier Table Serialization
2102//===----------------------------------------------------------------------===//
2103
Douglas Gregorc78d3462009-04-24 21:10:55 +00002104namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002105class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002106 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002107 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002108
Douglas Gregor1d583f22009-04-28 21:18:29 +00002109 /// \brief Determines whether this is an "interesting" identifier
2110 /// that needs a full IdentifierInfo structure written into the hash
2111 /// table.
2112 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2113 return II->isPoisoned() ||
2114 II->isExtensionToken() ||
2115 II->hasMacroDefinition() ||
2116 II->getObjCOrBuiltinID() ||
2117 II->getFETokenInfo<void>();
2118 }
2119
Douglas Gregore84a9da2009-04-20 20:36:09 +00002120public:
2121 typedef const IdentifierInfo* key_type;
2122 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002123
Sebastian Redl539c5062010-08-18 23:57:32 +00002124 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002125 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002127 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002128 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002129
2130 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002131 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002132 }
Mike Stump11289f42009-09-09 15:08:12 +00002133
2134 std::pair<unsigned,unsigned>
2135 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002136 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002137 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002138 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2139 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002140 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002141 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002142 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002143 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002144 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2145 DEnd = IdentifierResolver::end();
2146 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002147 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002148 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002149 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002150 // We emit the key length after the data length so that every
2151 // string is preceded by a 16-bit length. This matches the PTH
2152 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002153 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002154 return std::make_pair(KeyLen, DataLen);
2155 }
Mike Stump11289f42009-09-09 15:08:12 +00002156
2157 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002158 unsigned KeyLen) {
2159 // Record the location of the key data. This is used when generating
2160 // the mapping from persistent IDs to strings.
2161 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002162 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002163 }
Mike Stump11289f42009-09-09 15:08:12 +00002164
2165 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002166 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002167 if (!isInterestingIdentifier(II)) {
2168 clang::io::Emit32(Out, ID << 1);
2169 return;
2170 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002171
Douglas Gregor1d583f22009-04-28 21:18:29 +00002172 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002173 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002174 bool hasMacroDefinition =
2175 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002176 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002177 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002178 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2179 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2180 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002181 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002182 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002183 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002184
Douglas Gregorc3366a52009-04-21 23:56:24 +00002185 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002186 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002187
Douglas Gregora868bbd2009-04-21 22:25:48 +00002188 // Emit the declaration IDs in reverse order, because the
2189 // IdentifierResolver provides the declarations as they would be
2190 // visible (e.g., the function "stat" would come before the struct
2191 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2192 // adds declarations to the end of the list (so we need to see the
2193 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002194 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002195 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002196 IdentifierResolver::end());
2197 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2198 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002199 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002200 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002201 }
2202};
2203} // end anonymous namespace
2204
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002205/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002206///
2207/// The identifier table consists of a blob containing string data
2208/// (the actual identifiers themselves) and a separate "offsets" index
2209/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002210void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002211 using namespace llvm;
2212
2213 // Create and write out the blob that contains the identifier
2214 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002215 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002216 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002217 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002218
Douglas Gregore6648fb2009-04-28 20:33:11 +00002219 // Look for any identifiers that were named while processing the
2220 // headers, but are otherwise not needed. We add these to the hash
2221 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002222 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002223 // file.
2224 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2225 IDEnd = PP.getIdentifierTable().end();
2226 ID != IDEnd; ++ID)
2227 getIdentifierRef(ID->second);
2228
Sebastian Redlff4a2952010-07-23 23:49:55 +00002229 // Create the on-disk hash table representation. We only store offsets
2230 // for identifiers that appear here for the first time.
2231 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002232 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002233 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2234 ID != IDEnd; ++ID) {
2235 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002236 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002237 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002238 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002239
Douglas Gregore84a9da2009-04-20 20:36:09 +00002240 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002241 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002242 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002243 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002244 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002245 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002246 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002247 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002248 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002249 }
2250
2251 // Create a blob abbreviation
2252 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002253 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002256 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002257
2258 // Write the identifier table
2259 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002260 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002261 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002262 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002263 }
2264
2265 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002266 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002267 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2270 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2271
2272 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002273 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002274 Record.push_back(IdentifierOffsets.size());
2275 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002276 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002277 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002278}
2279
Douglas Gregorc5046832009-04-27 18:38:38 +00002280//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002281// DeclContext's Name Lookup Table Serialization
2282//===----------------------------------------------------------------------===//
2283
2284namespace {
2285// Trait used for the on-disk hash table used in the method pool.
2286class ASTDeclContextNameLookupTrait {
2287 ASTWriter &Writer;
2288
2289public:
2290 typedef DeclarationName key_type;
2291 typedef key_type key_type_ref;
2292
2293 typedef DeclContext::lookup_result data_type;
2294 typedef const data_type& data_type_ref;
2295
2296 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2297
2298 unsigned ComputeHash(DeclarationName Name) {
2299 llvm::FoldingSetNodeID ID;
2300 ID.AddInteger(Name.getNameKind());
2301
2302 switch (Name.getNameKind()) {
2303 case DeclarationName::Identifier:
2304 ID.AddString(Name.getAsIdentifierInfo()->getName());
2305 break;
2306 case DeclarationName::ObjCZeroArgSelector:
2307 case DeclarationName::ObjCOneArgSelector:
2308 case DeclarationName::ObjCMultiArgSelector:
2309 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2310 break;
2311 case DeclarationName::CXXConstructorName:
2312 case DeclarationName::CXXDestructorName:
2313 case DeclarationName::CXXConversionFunctionName:
2314 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2315 break;
2316 case DeclarationName::CXXOperatorName:
2317 ID.AddInteger(Name.getCXXOverloadedOperator());
2318 break;
2319 case DeclarationName::CXXLiteralOperatorName:
2320 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2321 case DeclarationName::CXXUsingDirective:
2322 break;
2323 }
2324
2325 return ID.ComputeHash();
2326 }
2327
2328 std::pair<unsigned,unsigned>
2329 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2330 data_type_ref Lookup) {
2331 unsigned KeyLen = 1;
2332 switch (Name.getNameKind()) {
2333 case DeclarationName::Identifier:
2334 case DeclarationName::ObjCZeroArgSelector:
2335 case DeclarationName::ObjCOneArgSelector:
2336 case DeclarationName::ObjCMultiArgSelector:
2337 case DeclarationName::CXXConstructorName:
2338 case DeclarationName::CXXDestructorName:
2339 case DeclarationName::CXXConversionFunctionName:
2340 case DeclarationName::CXXLiteralOperatorName:
2341 KeyLen += 4;
2342 break;
2343 case DeclarationName::CXXOperatorName:
2344 KeyLen += 1;
2345 break;
2346 case DeclarationName::CXXUsingDirective:
2347 break;
2348 }
2349 clang::io::Emit16(Out, KeyLen);
2350
2351 // 2 bytes for num of decls and 4 for each DeclID.
2352 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2353 clang::io::Emit16(Out, DataLen);
2354
2355 return std::make_pair(KeyLen, DataLen);
2356 }
2357
2358 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2359 using namespace clang::io;
2360
2361 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2362 Emit8(Out, Name.getNameKind());
2363 switch (Name.getNameKind()) {
2364 case DeclarationName::Identifier:
2365 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2366 break;
2367 case DeclarationName::ObjCZeroArgSelector:
2368 case DeclarationName::ObjCOneArgSelector:
2369 case DeclarationName::ObjCMultiArgSelector:
2370 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2371 break;
2372 case DeclarationName::CXXConstructorName:
2373 case DeclarationName::CXXDestructorName:
2374 case DeclarationName::CXXConversionFunctionName:
2375 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2376 break;
2377 case DeclarationName::CXXOperatorName:
2378 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2379 Emit8(Out, Name.getCXXOverloadedOperator());
2380 break;
2381 case DeclarationName::CXXLiteralOperatorName:
2382 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2383 break;
2384 case DeclarationName::CXXUsingDirective:
2385 break;
2386 }
2387 }
2388
2389 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2390 data_type Lookup, unsigned DataLen) {
2391 uint64_t Start = Out.tell(); (void)Start;
2392 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2393 for (; Lookup.first != Lookup.second; ++Lookup.first)
2394 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2395
2396 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2397 }
2398};
2399} // end anonymous namespace
2400
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002401/// \brief Write the block containing all of the declaration IDs
2402/// visible from the given DeclContext.
2403///
2404/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002405/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002406uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2407 DeclContext *DC) {
2408 if (DC->getPrimaryContext() != DC)
2409 return 0;
2410
2411 // Since there is no name lookup into functions or methods, don't bother to
2412 // build a visible-declarations table for these entities.
2413 if (DC->isFunctionOrMethod())
2414 return 0;
2415
2416 // If not in C++, we perform name lookup for the translation unit via the
2417 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2418 // FIXME: In C++ we need the visible declarations in order to "see" the
2419 // friend declarations, is there a way to do this without writing the table ?
2420 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2421 return 0;
2422
2423 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002424 if (DC->hasExternalVisibleStorage())
2425 DC->MaterializeVisibleDeclsFromExternalStorage();
2426 else
2427 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002428
2429 // Serialize the contents of the mapping used for lookup. Note that,
2430 // although we have two very different code paths, the serialized
2431 // representation is the same for both cases: a declaration name,
2432 // followed by a size, followed by references to the visible
2433 // declarations that have that name.
2434 uint64_t Offset = Stream.GetCurrentBitNo();
2435 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2436 if (!Map || Map->empty())
2437 return 0;
2438
2439 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2440 ASTDeclContextNameLookupTrait Trait(*this);
2441
2442 // Create the on-disk hash table representation.
2443 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2444 D != DEnd; ++D) {
2445 DeclarationName Name = D->first;
2446 DeclContext::lookup_result Result = D->second.getLookupResult();
2447 Generator.insert(Name, Result, Trait);
2448 }
2449
2450 // Create the on-disk hash table in a buffer.
2451 llvm::SmallString<4096> LookupTable;
2452 uint32_t BucketOffset;
2453 {
2454 llvm::raw_svector_ostream Out(LookupTable);
2455 // Make sure that no bucket is at offset 0
2456 clang::io::Emit32(Out, 0);
2457 BucketOffset = Generator.Emit(Out, Trait);
2458 }
2459
2460 // Write the lookup table
2461 RecordData Record;
2462 Record.push_back(DECL_CONTEXT_VISIBLE);
2463 Record.push_back(BucketOffset);
2464 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2465 LookupTable.str());
2466
2467 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2468 ++NumVisibleDeclContexts;
2469 return Offset;
2470}
2471
Sebastian Redla4071b42010-08-24 00:50:09 +00002472/// \brief Write an UPDATE_VISIBLE block for the given context.
2473///
2474/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2475/// DeclContext in a dependent AST file. As such, they only exist for the TU
2476/// (in C++) and for namespaces.
2477void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002478 // Make the context build its lookup table, but don't make it load external
2479 // decls.
2480 DC->lookup(DeclarationName());
2481
2482 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2483 if (!Map || Map->empty())
2484 return;
2485
2486 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2487 ASTDeclContextNameLookupTrait Trait(*this);
2488
2489 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002490 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2491 D != DEnd; ++D) {
2492 DeclarationName Name = D->first;
2493 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002494 // For any name that appears in this table, the results are complete, i.e.
2495 // they overwrite results from previous PCHs. Merging is always a mess.
2496 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002497 }
2498
2499 // Create the on-disk hash table in a buffer.
2500 llvm::SmallString<4096> LookupTable;
2501 uint32_t BucketOffset;
2502 {
2503 llvm::raw_svector_ostream Out(LookupTable);
2504 // Make sure that no bucket is at offset 0
2505 clang::io::Emit32(Out, 0);
2506 BucketOffset = Generator.Emit(Out, Trait);
2507 }
2508
2509 // Write the lookup table
2510 RecordData Record;
2511 Record.push_back(UPDATE_VISIBLE);
2512 Record.push_back(getDeclID(cast<Decl>(DC)));
2513 Record.push_back(BucketOffset);
2514 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2515}
2516
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002517//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002518// General Serialization Routines
2519//===----------------------------------------------------------------------===//
2520
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002521/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002522void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002523 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002524 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2525 const Attr * A = *i;
2526 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2527 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002528
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002529#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002530
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002531 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002532}
2533
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002534void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002535 Record.push_back(Str.size());
2536 Record.insert(Record.end(), Str.begin(), Str.end());
2537}
2538
Douglas Gregore84a9da2009-04-20 20:36:09 +00002539/// \brief Note that the identifier II occurs at the given offset
2540/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002541void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002542 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002543 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002544 // up earlier in the chain and thus don't need an offset.
2545 if (ID >= FirstIdentID)
2546 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002547}
2548
Douglas Gregor95c13f52009-04-25 17:48:32 +00002549/// \brief Note that the selector Sel occurs at the given offset
2550/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002551void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002552 unsigned ID = SelectorIDs[Sel];
2553 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002554 // Don't record offsets for selectors that are also available in a different
2555 // file.
2556 if (ID < FirstSelectorID)
2557 return;
2558 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002559}
2560
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002561ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002562 : Stream(Stream), Chain(0), SerializationListener(0),
2563 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002564 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002565 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002566 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2567 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002568 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002569 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2570 NextCXXBaseSpecifiersID(1)
2571{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002572}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002573
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002574void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002575 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002576 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002577 Stream.Emit((unsigned)'C', 8);
2578 Stream.Emit((unsigned)'P', 8);
2579 Stream.Emit((unsigned)'C', 8);
2580 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002581
Chris Lattner28fa4e62009-04-26 22:26:21 +00002582 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002583
Sebastian Redl143413f2010-07-12 22:02:52 +00002584 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002585 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002586 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002587 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002588}
2589
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002590void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002591 const char *isysroot) {
2592 using namespace llvm;
2593
2594 ASTContext &Context = SemaRef.Context;
2595 Preprocessor &PP = SemaRef.PP;
2596
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002597 // The translation unit is the first declaration we'll emit.
2598 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002599 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002600 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002601
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002602 // Make sure that we emit IdentifierInfos (and any attached
2603 // declarations) for builtins.
2604 {
2605 IdentifierTable &Table = PP.getIdentifierTable();
2606 llvm::SmallVector<const char *, 32> BuiltinNames;
2607 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2608 Context.getLangOptions().NoBuiltin);
2609 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2610 getIdentifierRef(&Table.get(BuiltinNames[I]));
2611 }
2612
Chris Lattner0c797362009-09-08 18:19:27 +00002613 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002614 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002615 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002616 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002617 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2618 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002619 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002620
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002621 // Build a record containing all of the file scoped decls in this file.
2622 RecordData UnusedFileScopedDecls;
2623 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2624 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002625
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002626 RecordData WeakUndeclaredIdentifiers;
2627 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2628 WeakUndeclaredIdentifiers.push_back(
2629 SemaRef.WeakUndeclaredIdentifiers.size());
2630 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2631 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2632 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2633 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2634 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2635 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2636 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2637 }
2638 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002639
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002640 // Build a record containing all of the locally-scoped external
2641 // declarations in this header file. Generally, this record will be
2642 // empty.
2643 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002644 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002645 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002646 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002647 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2648 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2649 TD != TDEnd; ++TD)
2650 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2651
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002652 // Build a record containing all of the ext_vector declarations.
2653 RecordData ExtVectorDecls;
2654 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2655 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2656
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002657 // Build a record containing all of the VTable uses information.
2658 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002659 if (!SemaRef.VTableUses.empty()) {
2660 VTableUses.push_back(SemaRef.VTableUses.size());
2661 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2662 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2663 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2664 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2665 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002666 }
2667
2668 // Build a record containing all of dynamic classes declarations.
2669 RecordData DynamicClasses;
2670 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2671 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2672
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002673 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002674 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002675 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002676 I = SemaRef.PendingInstantiations.begin(),
2677 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2678 AddDeclRef(I->first, PendingInstantiations);
2679 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002680 }
2681 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2682 "There are local ones at end of translation unit!");
2683
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002684 // Build a record containing some declaration references.
2685 RecordData SemaDeclRefs;
2686 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2687 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2688 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2689 }
2690
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002691 RecordData CUDASpecialDeclRefs;
2692 if (Context.getcudaConfigureCallDecl()) {
2693 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2694 }
2695
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002696 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002697 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002698 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002699 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002700 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002701 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002702 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002703 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002704 // Write the record of special types.
2705 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002706
Steve Naroffc277ad12009-07-18 15:33:26 +00002707 AddTypeRef(Context.getBuiltinVaListType(), Record);
2708 AddTypeRef(Context.getObjCIdType(), Record);
2709 AddTypeRef(Context.getObjCSelType(), Record);
2710 AddTypeRef(Context.getObjCProtoType(), Record);
2711 AddTypeRef(Context.getObjCClassType(), Record);
2712 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2713 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2714 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002715 AddTypeRef(Context.getjmp_bufType(), Record);
2716 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002717 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2718 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002719 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002720 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002721 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2722 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002723 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002724 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002725
Douglas Gregor1970d882009-04-26 03:49:13 +00002726 // Keep writing types and declarations until all types and
2727 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002728 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002729 WriteDeclsBlockAbbrevs();
2730 while (!DeclTypesToEmit.empty()) {
2731 DeclOrType DOT = DeclTypesToEmit.front();
2732 DeclTypesToEmit.pop();
2733 if (DOT.isType())
2734 WriteType(DOT.getType());
2735 else
2736 WriteDecl(Context, DOT.getDecl());
2737 }
2738 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002739
Douglas Gregor45053152009-10-17 17:25:45 +00002740 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002741 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002742 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002743 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002744 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002745
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002746 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002747 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002748
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002749 // Write the C++ base-specifier set offsets.
2750 if (!CXXBaseSpecifiersOffsets.empty()) {
2751 // Create a blob abbreviation for the C++ base specifiers offsets.
2752 using namespace llvm;
2753
2754 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2755 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2758 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2759
2760 // Write the selector offsets table.
2761 Record.clear();
2762 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2763 Record.push_back(CXXBaseSpecifiersOffsets.size());
2764 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2765 (const char *)CXXBaseSpecifiersOffsets.data(),
2766 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2767 }
2768
Douglas Gregord4df8652009-04-22 22:02:47 +00002769 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002770 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002771 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002772
2773 // Write the record containing tentative definitions.
2774 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002775 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002776
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002777 // Write the record containing unused file scoped decls.
2778 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002779 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002780
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002781 // Write the record containing weak undeclared identifiers.
2782 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002783 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002784 WeakUndeclaredIdentifiers);
2785
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002786 // Write the record containing locally-scoped external definitions.
2787 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002788 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002789 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002790
2791 // Write the record containing ext_vector type names.
2792 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002793 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002794
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002795 // Write the record containing VTable uses information.
2796 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002797 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002798
2799 // Write the record containing dynamic classes declarations.
2800 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002801 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002802
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002803 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002804 if (!PendingInstantiations.empty())
2805 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002806
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002807 // Write the record containing declaration references of Sema.
2808 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002809 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002810
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002811 // Write the record containing CUDA-specific declaration references.
2812 if (!CUDASpecialDeclRefs.empty())
2813 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2814
Douglas Gregor08f01292009-04-17 22:13:46 +00002815 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002816 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002817 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002818 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002819 Record.push_back(NumLexicalDeclContexts);
2820 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002821 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002822 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002823}
2824
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002825void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002826 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002827 using namespace llvm;
2828
2829 ASTContext &Context = SemaRef.Context;
2830 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002831
Sebastian Redl143413f2010-07-12 22:02:52 +00002832 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002833 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002834 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002835 if (StatCalls && !isysroot)
2836 WriteStatCache(*StatCalls);
2837 // FIXME: Source manager block should only write new stuff, which could be
2838 // done by tracking the largest ID in the chain
2839 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002840
2841 // The special types are in the chained PCH.
2842
2843 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002844 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002845 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002846 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002847 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2848 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002849 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002850 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002851 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002852 else if ((*I)->isChangedSinceDeserialization())
2853 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002854 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002855 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002856 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002857 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002858 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2859 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2860 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002861 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002862 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2863 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002864 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002865 // And a visible updates block for the DeclContexts.
2866 Abv = new llvm::BitCodeAbbrev();
2867 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2868 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2869 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2870 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2871 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2872 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002873
Sebastian Redl98912122010-07-27 23:01:28 +00002874 // Build a record containing all of the new tentative definitions in this
2875 // file, in TentativeDefinitions order.
2876 RecordData TentativeDefinitions;
2877 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2878 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2879 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2880 }
2881
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002882 // Build a record containing all of the file scoped decls in this file.
2883 RecordData UnusedFileScopedDecls;
2884 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2885 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2886 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002887 }
2888
Sebastian Redl08aca90252010-08-05 18:21:25 +00002889 // We write the entire table, overwriting the tables from the chain.
2890 RecordData WeakUndeclaredIdentifiers;
2891 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2892 WeakUndeclaredIdentifiers.push_back(
2893 SemaRef.WeakUndeclaredIdentifiers.size());
2894 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2895 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2896 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2897 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2898 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2899 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2900 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2901 }
2902 }
2903
Sebastian Redl98912122010-07-27 23:01:28 +00002904 // Build a record containing all of the locally-scoped external
2905 // declarations in this header file. Generally, this record will be
2906 // empty.
2907 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002908 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002909 // nondeterminstic!
2910 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2911 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2912 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2913 TD != TDEnd; ++TD) {
2914 if (TD->second->getPCHLevel() == 0)
2915 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2916 }
2917
2918 // Build a record containing all of the ext_vector declarations.
2919 RecordData ExtVectorDecls;
2920 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2921 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2922 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2923 }
2924
Sebastian Redl08aca90252010-08-05 18:21:25 +00002925 // Build a record containing all of the VTable uses information.
2926 // We write everything here, because it's too hard to determine whether
2927 // a use is new to this part.
2928 RecordData VTableUses;
2929 if (!SemaRef.VTableUses.empty()) {
2930 VTableUses.push_back(SemaRef.VTableUses.size());
2931 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2932 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2933 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2934 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2935 }
2936 }
2937
2938 // Build a record containing all of dynamic classes declarations.
2939 RecordData DynamicClasses;
2940 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2941 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2942 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2943
2944 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002945 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002946 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002947 I = SemaRef.PendingInstantiations.begin(),
2948 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002949 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002950 AddDeclRef(I->first, PendingInstantiations);
2951 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002952 }
2953 }
2954 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2955 "There are local ones at end of translation unit!");
2956
2957 // Build a record containing some declaration references.
2958 // It's not worth the effort to avoid duplication here.
2959 RecordData SemaDeclRefs;
2960 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2961 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2962 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2963 }
2964
Sebastian Redl539c5062010-08-18 23:57:32 +00002965 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002966 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002967 for (DeclsToRewriteTy::iterator
2968 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2969 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002970 while (!DeclTypesToEmit.empty()) {
2971 DeclOrType DOT = DeclTypesToEmit.front();
2972 DeclTypesToEmit.pop();
2973 if (DOT.isType())
2974 WriteType(DOT.getType());
2975 else
2976 WriteDecl(Context, DOT.getDecl());
2977 }
2978 Stream.ExitBlock();
2979
Sebastian Redl98912122010-07-27 23:01:28 +00002980 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002981 WriteSelectors(SemaRef);
2982 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002983 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002984 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002985 // FIXME: For chained PCH only write the new mappings (we currently
2986 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002987 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002988
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002989 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002990 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002991 RecordData FirstLatestDeclIDs;
2992 for (FirstLatestDeclMap::iterator
2993 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2994 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2995 "Expected first & second to be in different PCHs");
2996 AddDeclRef(I->first, FirstLatestDeclIDs);
2997 AddDeclRef(I->second, FirstLatestDeclIDs);
2998 }
2999 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003000 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003001
Sebastian Redl98912122010-07-27 23:01:28 +00003002 // Write the record containing external, unnamed definitions.
3003 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003004 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003005
3006 // Write the record containing tentative definitions.
3007 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003008 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003009
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003010 // Write the record containing unused file scoped decls.
3011 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003012 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003013
Sebastian Redl08aca90252010-08-05 18:21:25 +00003014 // Write the record containing weak undeclared identifiers.
3015 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003016 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003017 WeakUndeclaredIdentifiers);
3018
Sebastian Redl98912122010-07-27 23:01:28 +00003019 // Write the record containing locally-scoped external definitions.
3020 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003021 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003022 LocallyScopedExternalDecls);
3023
3024 // Write the record containing ext_vector type names.
3025 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003026 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003027
Sebastian Redl08aca90252010-08-05 18:21:25 +00003028 // Write the record containing VTable uses information.
3029 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003030 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003031
3032 // Write the record containing dynamic classes declarations.
3033 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003034 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003035
3036 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003037 if (!PendingInstantiations.empty())
3038 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003039
3040 // Write the record containing declaration references of Sema.
3041 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003042 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00003043
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003044 // Write the updates to DeclContexts.
3045 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3046 I = UpdatedDeclContexts.begin(),
3047 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003048 I != E; ++I)
3049 WriteDeclContextVisibleUpdate(*I);
3050
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003051 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003052
Sebastian Redl98912122010-07-27 23:01:28 +00003053 Record.clear();
3054 Record.push_back(NumStatements);
3055 Record.push_back(NumMacros);
3056 Record.push_back(NumLexicalDeclContexts);
3057 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003058 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003059 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003060 Stream.ExitBlock();
3061}
3062
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003063void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003064 if (DeclUpdates.empty())
3065 return;
3066
3067 RecordData OffsetsRecord;
3068 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3069 for (DeclUpdateMap::iterator
3070 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3071 const Decl *D = I->first;
3072 UpdateRecord &URec = I->second;
3073
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003074 if (DeclsToRewrite.count(D))
3075 continue; // The decl will be written completely,no need to store updates.
3076
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003077 uint64_t Offset = Stream.GetCurrentBitNo();
3078 Stream.EmitRecord(DECL_UPDATES, URec);
3079
3080 OffsetsRecord.push_back(GetDeclRef(D));
3081 OffsetsRecord.push_back(Offset);
3082 }
3083 Stream.ExitBlock();
3084 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3085}
3086
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003087void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003088 if (ReplacedDecls.empty())
3089 return;
3090
3091 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003092 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003093 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3094 Record.push_back(I->first);
3095 Record.push_back(I->second);
3096 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003097 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003098}
3099
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003100void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003101 Record.push_back(Loc.getRawEncoding());
3102}
3103
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003104void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003105 AddSourceLocation(Range.getBegin(), Record);
3106 AddSourceLocation(Range.getEnd(), Record);
3107}
3108
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003109void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003110 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003111 const uint64_t *Words = Value.getRawData();
3112 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003113}
3114
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003115void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003116 Record.push_back(Value.isUnsigned());
3117 AddAPInt(Value, Record);
3118}
3119
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003120void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003121 AddAPInt(Value.bitcastToAPInt(), Record);
3122}
3123
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003124void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003125 Record.push_back(getIdentifierRef(II));
3126}
3127
Sebastian Redl539c5062010-08-18 23:57:32 +00003128IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003129 if (II == 0)
3130 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003131
Sebastian Redl539c5062010-08-18 23:57:32 +00003132 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003133 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003134 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003135 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003136}
3137
Sebastian Redl50e26582010-09-15 19:54:06 +00003138MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003139 if (MD == 0)
3140 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003141
3142 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003143 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003144 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003145 return ID;
3146}
3147
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003148void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003149 Record.push_back(getSelectorRef(SelRef));
3150}
3151
Sebastian Redl539c5062010-08-18 23:57:32 +00003152SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003153 if (Sel.getAsOpaquePtr() == 0) {
3154 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003155 }
3156
Sebastian Redl539c5062010-08-18 23:57:32 +00003157 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003158 if (SID == 0 && Chain) {
3159 // This might trigger a ReadSelector callback, which will set the ID for
3160 // this selector.
3161 Chain->LoadSelector(Sel);
3162 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003163 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003164 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003165 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003166 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003167}
3168
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003169void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003170 AddDeclRef(Temp->getDestructor(), Record);
3171}
3172
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003173void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3174 CXXBaseSpecifier const *BasesEnd,
3175 RecordDataImpl &Record) {
3176 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3177 CXXBaseSpecifiersToWrite.push_back(
3178 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3179 Bases, BasesEnd));
3180 Record.push_back(NextCXXBaseSpecifiersID++);
3181}
3182
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003183void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003184 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003185 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003186 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003187 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003188 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003189 break;
3190 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003191 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003192 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003193 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003194 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3195 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003196 break;
3197 case TemplateArgument::TemplateExpansion:
3198 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3199 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003200 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003201 break;
John McCall0ad16662009-10-29 08:12:44 +00003202 case TemplateArgument::Null:
3203 case TemplateArgument::Integral:
3204 case TemplateArgument::Declaration:
3205 case TemplateArgument::Pack:
3206 break;
3207 }
3208}
3209
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003210void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003211 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003212 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003213
3214 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3215 bool InfoHasSameExpr
3216 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3217 Record.push_back(InfoHasSameExpr);
3218 if (InfoHasSameExpr)
3219 return; // Avoid storing the same expr twice.
3220 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003221 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3222 Record);
3223}
3224
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003225void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003226 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003227 AddTypeRef(QualType(), Record);
3228 return;
3229 }
3230
John McCallbcd03502009-12-07 02:54:59 +00003231 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00003232 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00003233 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003234 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003235}
3236
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003237void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003238 Record.push_back(GetOrCreateTypeID(T));
3239}
3240
3241TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003242 return MakeTypeID(T,
3243 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3244}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003245
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003246TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003247 return MakeTypeID(T,
3248 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003249}
3250
3251TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3252 if (T.isNull())
3253 return TypeIdx();
3254 assert(!T.getLocalFastQualifiers());
3255
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003256 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003257 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003258 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003259 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003260 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003261 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003262 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003263 return Idx;
3264}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003265
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003266TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003267 if (T.isNull())
3268 return TypeIdx();
3269 assert(!T.getLocalFastQualifiers());
3270
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003271 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3272 assert(I != TypeIdxs.end() && "Type not emitted!");
3273 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003274}
3275
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003276void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003277 Record.push_back(GetDeclRef(D));
3278}
3279
Sebastian Redl539c5062010-08-18 23:57:32 +00003280DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003281 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003282 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003283 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003284 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003285 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003286 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003287 // We haven't seen this declaration before. Give it a new ID and
3288 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003289 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003290 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003291 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3292 // We don't add it to the replacement collection here, because we don't
3293 // have the offset yet.
3294 DeclTypesToEmit.push(const_cast<Decl *>(D));
3295 // Reset the flag, so that we don't add this decl multiple times.
3296 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003297 }
3298
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003299 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003300}
3301
Sebastian Redl539c5062010-08-18 23:57:32 +00003302DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003303 if (D == 0)
3304 return 0;
3305
3306 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3307 return DeclIDs[D];
3308}
3309
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003310void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003311 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003312 Record.push_back(Name.getNameKind());
3313 switch (Name.getNameKind()) {
3314 case DeclarationName::Identifier:
3315 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3316 break;
3317
3318 case DeclarationName::ObjCZeroArgSelector:
3319 case DeclarationName::ObjCOneArgSelector:
3320 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003321 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003322 break;
3323
3324 case DeclarationName::CXXConstructorName:
3325 case DeclarationName::CXXDestructorName:
3326 case DeclarationName::CXXConversionFunctionName:
3327 AddTypeRef(Name.getCXXNameType(), Record);
3328 break;
3329
3330 case DeclarationName::CXXOperatorName:
3331 Record.push_back(Name.getCXXOverloadedOperator());
3332 break;
3333
Alexis Hunt3d221f22009-11-29 07:34:05 +00003334 case DeclarationName::CXXLiteralOperatorName:
3335 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3336 break;
3337
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003338 case DeclarationName::CXXUsingDirective:
3339 // No extra data to emit
3340 break;
3341 }
3342}
Chris Lattnerca025db2010-05-07 21:43:38 +00003343
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003344void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003345 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003346 switch (Name.getNameKind()) {
3347 case DeclarationName::CXXConstructorName:
3348 case DeclarationName::CXXDestructorName:
3349 case DeclarationName::CXXConversionFunctionName:
3350 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3351 break;
3352
3353 case DeclarationName::CXXOperatorName:
3354 AddSourceLocation(
3355 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3356 Record);
3357 AddSourceLocation(
3358 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3359 Record);
3360 break;
3361
3362 case DeclarationName::CXXLiteralOperatorName:
3363 AddSourceLocation(
3364 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3365 Record);
3366 break;
3367
3368 case DeclarationName::Identifier:
3369 case DeclarationName::ObjCZeroArgSelector:
3370 case DeclarationName::ObjCOneArgSelector:
3371 case DeclarationName::ObjCMultiArgSelector:
3372 case DeclarationName::CXXUsingDirective:
3373 break;
3374 }
3375}
3376
3377void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003378 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003379 AddDeclarationName(NameInfo.getName(), Record);
3380 AddSourceLocation(NameInfo.getLoc(), Record);
3381 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3382}
3383
3384void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003385 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003386 AddNestedNameSpecifier(Info.NNS, Record);
3387 AddSourceRange(Info.NNSRange, Record);
3388 Record.push_back(Info.NumTemplParamLists);
3389 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3390 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3391}
3392
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003393void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003394 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003395 // Nested name specifiers usually aren't too long. I think that 8 would
3396 // typically accomodate the vast majority.
3397 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3398
3399 // Push each of the NNS's onto a stack for serialization in reverse order.
3400 while (NNS) {
3401 NestedNames.push_back(NNS);
3402 NNS = NNS->getPrefix();
3403 }
3404
3405 Record.push_back(NestedNames.size());
3406 while(!NestedNames.empty()) {
3407 NNS = NestedNames.pop_back_val();
3408 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3409 Record.push_back(Kind);
3410 switch (Kind) {
3411 case NestedNameSpecifier::Identifier:
3412 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3413 break;
3414
3415 case NestedNameSpecifier::Namespace:
3416 AddDeclRef(NNS->getAsNamespace(), Record);
3417 break;
3418
3419 case NestedNameSpecifier::TypeSpec:
3420 case NestedNameSpecifier::TypeSpecWithTemplate:
3421 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3422 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3423 break;
3424
3425 case NestedNameSpecifier::Global:
3426 // Don't need to write an associated value.
3427 break;
3428 }
3429 }
3430}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003431
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003432void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003433 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003434 Record.push_back(Kind);
3435 switch (Kind) {
3436 case TemplateName::Template:
3437 AddDeclRef(Name.getAsTemplateDecl(), Record);
3438 break;
3439
3440 case TemplateName::OverloadedTemplate: {
3441 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3442 Record.push_back(OvT->size());
3443 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3444 I != E; ++I)
3445 AddDeclRef(*I, Record);
3446 break;
3447 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003448
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003449 case TemplateName::QualifiedTemplate: {
3450 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3451 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3452 Record.push_back(QualT->hasTemplateKeyword());
3453 AddDeclRef(QualT->getTemplateDecl(), Record);
3454 break;
3455 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003456
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003457 case TemplateName::DependentTemplate: {
3458 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3459 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3460 Record.push_back(DepT->isIdentifier());
3461 if (DepT->isIdentifier())
3462 AddIdentifierRef(DepT->getIdentifier(), Record);
3463 else
3464 Record.push_back(DepT->getOperator());
3465 break;
3466 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003467
3468 case TemplateName::SubstTemplateTemplateParmPack: {
3469 SubstTemplateTemplateParmPackStorage *SubstPack
3470 = Name.getAsSubstTemplateTemplateParmPack();
3471 AddDeclRef(SubstPack->getParameterPack(), Record);
3472 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3473 break;
3474 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003475 }
3476}
3477
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003478void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003479 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003480 Record.push_back(Arg.getKind());
3481 switch (Arg.getKind()) {
3482 case TemplateArgument::Null:
3483 break;
3484 case TemplateArgument::Type:
3485 AddTypeRef(Arg.getAsType(), Record);
3486 break;
3487 case TemplateArgument::Declaration:
3488 AddDeclRef(Arg.getAsDecl(), Record);
3489 break;
3490 case TemplateArgument::Integral:
3491 AddAPSInt(*Arg.getAsIntegral(), Record);
3492 AddTypeRef(Arg.getIntegralType(), Record);
3493 break;
3494 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003495 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3496 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003497 case TemplateArgument::TemplateExpansion:
3498 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003499 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3500 Record.push_back(*NumExpansions + 1);
3501 else
3502 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003503 break;
3504 case TemplateArgument::Expression:
3505 AddStmt(Arg.getAsExpr());
3506 break;
3507 case TemplateArgument::Pack:
3508 Record.push_back(Arg.pack_size());
3509 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3510 I != E; ++I)
3511 AddTemplateArgument(*I, Record);
3512 break;
3513 }
3514}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003515
3516void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003517ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003518 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003519 assert(TemplateParams && "No TemplateParams!");
3520 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3521 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3522 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3523 Record.push_back(TemplateParams->size());
3524 for (TemplateParameterList::const_iterator
3525 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3526 P != PEnd; ++P)
3527 AddDeclRef(*P, Record);
3528}
3529
3530/// \brief Emit a template argument list.
3531void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003532ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003533 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003534 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003535 Record.push_back(TemplateArgs->size());
3536 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003537 AddTemplateArgument(TemplateArgs->get(i), Record);
3538}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003539
3540
3541void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003542ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003543 Record.push_back(Set.size());
3544 for (UnresolvedSetImpl::const_iterator
3545 I = Set.begin(), E = Set.end(); I != E; ++I) {
3546 AddDeclRef(I.getDecl(), Record);
3547 Record.push_back(I.getAccess());
3548 }
3549}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003550
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003551void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003552 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003553 Record.push_back(Base.isVirtual());
3554 Record.push_back(Base.isBaseOfClass());
3555 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003556 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003557 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003558 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003559 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3560 : SourceLocation(),
3561 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003562}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003563
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003564void ASTWriter::FlushCXXBaseSpecifiers() {
3565 RecordData Record;
3566 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3567 Record.clear();
3568
3569 // Record the offset of this base-specifier set.
3570 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3571 if (Index == CXXBaseSpecifiersOffsets.size())
3572 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3573 else {
3574 if (Index > CXXBaseSpecifiersOffsets.size())
3575 CXXBaseSpecifiersOffsets.resize(Index + 1);
3576 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3577 }
3578
3579 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3580 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3581 Record.push_back(BEnd - B);
3582 for (; B != BEnd; ++B)
3583 AddCXXBaseSpecifier(*B, Record);
3584 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003585
3586 // Flush any expressions that were written as part of the base specifiers.
3587 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003588 }
3589
3590 CXXBaseSpecifiersToWrite.clear();
3591}
3592
Alexis Hunt1d792652011-01-08 20:30:50 +00003593void ASTWriter::AddCXXCtorInitializers(
3594 const CXXCtorInitializer * const *CtorInitializers,
3595 unsigned NumCtorInitializers,
3596 RecordDataImpl &Record) {
3597 Record.push_back(NumCtorInitializers);
3598 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3599 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003600
3601 Record.push_back(Init->isBaseInitializer());
3602 if (Init->isBaseInitializer()) {
3603 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3604 Record.push_back(Init->isBaseVirtual());
3605 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003606 Record.push_back(Init->isIndirectMemberInitializer());
3607 if (Init->isIndirectMemberInitializer())
3608 AddDeclRef(Init->getIndirectMember(), Record);
3609 else
3610 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003611 }
Francois Pichetd583da02010-12-04 09:14:42 +00003612
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003613 AddSourceLocation(Init->getMemberLocation(), Record);
3614 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003615 AddSourceLocation(Init->getLParenLoc(), Record);
3616 AddSourceLocation(Init->getRParenLoc(), Record);
3617 Record.push_back(Init->isWritten());
3618 if (Init->isWritten()) {
3619 Record.push_back(Init->getSourceOrder());
3620 } else {
3621 Record.push_back(Init->getNumArrayIndices());
3622 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3623 AddDeclRef(Init->getArrayIndex(i), Record);
3624 }
3625 }
3626}
3627
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003628void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3629 assert(D->DefinitionData);
3630 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3631 Record.push_back(Data.UserDeclaredConstructor);
3632 Record.push_back(Data.UserDeclaredCopyConstructor);
3633 Record.push_back(Data.UserDeclaredCopyAssignment);
3634 Record.push_back(Data.UserDeclaredDestructor);
3635 Record.push_back(Data.Aggregate);
3636 Record.push_back(Data.PlainOldData);
3637 Record.push_back(Data.Empty);
3638 Record.push_back(Data.Polymorphic);
3639 Record.push_back(Data.Abstract);
3640 Record.push_back(Data.HasTrivialConstructor);
3641 Record.push_back(Data.HasTrivialCopyConstructor);
3642 Record.push_back(Data.HasTrivialCopyAssignment);
3643 Record.push_back(Data.HasTrivialDestructor);
3644 Record.push_back(Data.ComputedVisibleConversions);
3645 Record.push_back(Data.DeclaredDefaultConstructor);
3646 Record.push_back(Data.DeclaredCopyConstructor);
3647 Record.push_back(Data.DeclaredCopyAssignment);
3648 Record.push_back(Data.DeclaredDestructor);
3649
3650 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003651 if (Data.NumBases > 0)
3652 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3653 Record);
3654
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003655 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3656 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003657 if (Data.NumVBases > 0)
3658 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3659 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003660
3661 AddUnresolvedSet(Data.Conversions, Record);
3662 AddUnresolvedSet(Data.VisibleConversions, Record);
3663 // Data.Definition is the owning decl, no need to write it.
3664 AddDeclRef(Data.FirstFriend, Record);
3665}
3666
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003667void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003668 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003669 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003670 assert(FirstDeclID == NextDeclID &&
3671 FirstTypeID == NextTypeID &&
3672 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003673 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003674 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003675 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003676 "Setting chain after writing has started.");
3677 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003678
3679 FirstDeclID += Chain->getTotalNumDecls();
3680 FirstTypeID += Chain->getTotalNumTypes();
3681 FirstIdentID += Chain->getTotalNumIdentifiers();
3682 FirstSelectorID += Chain->getTotalNumSelectors();
3683 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003684 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003685 NextDeclID = FirstDeclID;
3686 NextTypeID = FirstTypeID;
3687 NextIdentID = FirstIdentID;
3688 NextSelectorID = FirstSelectorID;
3689 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003690 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003691}
3692
Sebastian Redl539c5062010-08-18 23:57:32 +00003693void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003694 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003695 if (II->hasMacroDefinition())
3696 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003697}
3698
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003699void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003700 // Always take the highest-numbered type index. This copes with an interesting
3701 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003702 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003703 // keep the higher-numbered entry so that we can properly write it out to
3704 // the AST file.
3705 TypeIdx &StoredIdx = TypeIdxs[T];
3706 if (Idx.getIndex() >= StoredIdx.getIndex())
3707 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003708}
3709
Sebastian Redl539c5062010-08-18 23:57:32 +00003710void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003711 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003712}
Sebastian Redl834bb972010-08-04 17:20:04 +00003713
Sebastian Redl539c5062010-08-18 23:57:32 +00003714void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003715 SelectorIDs[S] = ID;
3716}
Douglas Gregor91096292010-10-02 19:29:26 +00003717
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003718void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003719 MacroDefinition *MD) {
3720 MacroDefinitions[MD] = ID;
3721}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003722
3723void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3724 assert(D->isDefinition());
3725 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3726 // We are interested when a PCH decl is modified.
3727 if (RD->getPCHLevel() > 0) {
3728 // A forward reference was mutated into a definition. Rewrite it.
3729 // FIXME: This happens during template instantiation, should we
3730 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003731 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003732 }
3733
3734 for (CXXRecordDecl::redecl_iterator
3735 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3736 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3737 if (Redecl == RD)
3738 continue;
3739
3740 // We are interested when a PCH decl is modified.
3741 if (Redecl->getPCHLevel() > 0) {
3742 UpdateRecord &Record = DeclUpdates[Redecl];
3743 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3744 assert(Redecl->DefinitionData);
3745 assert(Redecl->DefinitionData->Definition == D);
3746 AddDeclRef(D, Record); // the DefinitionDecl
3747 }
3748 }
3749 }
3750}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003751void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3752 // TU and namespaces are handled elsewhere.
3753 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3754 return;
3755
3756 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3757 return; // Not a source decl added to a DeclContext from PCH.
3758
3759 AddUpdatedDeclContext(DC);
3760}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003761
3762void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3763 assert(D->isImplicit());
3764 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3765 return; // Not a source member added to a class from PCH.
3766 if (!isa<CXXMethodDecl>(D))
3767 return; // We are interested in lazily declared implicit methods.
3768
3769 // A decl coming from PCH was modified.
3770 assert(RD->isDefinition());
3771 UpdateRecord &Record = DeclUpdates[RD];
3772 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3773 AddDeclRef(D, Record);
3774}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003775
3776void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3777 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003778 // The specializations set is kept in the canonical template.
3779 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003780 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3781 return; // Not a source specialization added to a template from PCH.
3782
3783 UpdateRecord &Record = DeclUpdates[TD];
3784 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3785 AddDeclRef(D, Record);
3786}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003787
3788ASTSerializationListener::~ASTSerializationListener() { }