blob: 48b14e3bd7f37860b75048876394baa639b96c5d [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-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 Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
Sebastian Redlade50002010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000059}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000065}
66
Douglas Gregor2cf26342009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000070
Douglas Gregor2cf26342009-04-09 22:27:44 +000071namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl3397c552010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000095}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000116}
117
Sebastian Redl3397c552010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159}
160
Sebastian Redl3397c552010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175}
176
Sebastian Redl3397c552010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000188 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000189 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
198 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000199 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200}
201
Sebastian Redl3397c552010-08-18 23:56:27 +0000202void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000203 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000204 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000205}
John McCalled976492009-12-04 22:46:56 +0000206
Sebastian Redl3397c552010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000209 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
210 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000211 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000212}
213
Sebastian Redl3397c552010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000215 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000216 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217}
218
Sebastian Redl3397c552010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222}
223
Sebastian Redl3397c552010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000225 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000226 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000227 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000228}
229
Sean Huntca63c202011-05-24 22:41:36 +0000230void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
231 Writer.AddTypeRef(T->getBaseType(), Record);
232 Writer.AddTypeRef(T->getUnderlyingType(), Record);
233 Record.push_back(T->getUTTKind());
234 Code = TYPE_UNARY_TRANSFORM;
235}
236
Richard Smith34b41d92011-02-20 03:19:35 +0000237void ASTTypeWriter::VisitAutoType(const AutoType *T) {
238 Writer.AddTypeRef(T->getDeducedType(), Record);
239 Code = TYPE_AUTO;
240}
241
Sebastian Redl3397c552010-08-18 23:56:27 +0000242void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000243 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000244 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000245 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000246 "Cannot serialize in the middle of a type definition");
247}
248
Sebastian Redl3397c552010-08-18 23:56:27 +0000249void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000250 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000251 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000252}
253
Sebastian Redl3397c552010-08-18 23:56:27 +0000254void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000256 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257}
258
John McCall9d156a72011-01-06 01:58:22 +0000259void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
260 Writer.AddTypeRef(T->getModifiedType(), Record);
261 Writer.AddTypeRef(T->getEquivalentType(), Record);
262 Record.push_back(T->getAttrKind());
263 Code = TYPE_ATTRIBUTED;
264}
265
Mike Stump1eb44332009-09-09 15:08:12 +0000266void
Sebastian Redl3397c552010-08-18 23:56:27 +0000267ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000268 const SubstTemplateTypeParmType *T) {
269 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
270 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000271 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000272}
273
274void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000275ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
276 const SubstTemplateTypeParmPackType *T) {
277 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
278 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
279 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
280}
281
282void
Sebastian Redl3397c552010-08-18 23:56:27 +0000283ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000284 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000285 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000286 Writer.AddTemplateName(T->getTemplateName(), Record);
287 Record.push_back(T->getNumArgs());
288 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
289 ArgI != ArgE; ++ArgI)
290 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000291 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
292 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000293 : T->getCanonicalTypeInternal(),
294 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000295 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000296}
297
298void
Sebastian Redl3397c552010-08-18 23:56:27 +0000299ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000300 VisitArrayType(T);
301 Writer.AddStmt(T->getSizeExpr());
302 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000303 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl3397c552010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000308 const DependentSizedExtVectorType *T) {
309 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000310 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000311}
312
313void
Sebastian Redl3397c552010-08-18 23:56:27 +0000314ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000315 Record.push_back(T->getDepth());
316 Record.push_back(T->getIndex());
317 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000318 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000319 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000320}
321
322void
Sebastian Redl3397c552010-08-18 23:56:27 +0000323ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000324 Record.push_back(T->getKeyword());
325 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
326 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000327 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
328 : T->getCanonicalTypeInternal(),
329 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000330 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000331}
332
333void
Sebastian Redl3397c552010-08-18 23:56:27 +0000334ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000335 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000336 Record.push_back(T->getKeyword());
337 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
338 Writer.AddIdentifierRef(T->getIdentifier(), Record);
339 Record.push_back(T->getNumArgs());
340 for (DependentTemplateSpecializationType::iterator
341 I = T->begin(), E = T->end(); I != E; ++I)
342 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000343 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000344}
345
Douglas Gregor7536dd52010-12-20 02:24:11 +0000346void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
347 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000348 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
349 Record.push_back(*NumExpansions + 1);
350 else
351 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000352 Code = TYPE_PACK_EXPANSION;
353}
354
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000355void ASTTypeWriter::VisitParenType(const ParenType *T) {
356 Writer.AddTypeRef(T->getInnerType(), Record);
357 Code = TYPE_PAREN;
358}
359
Sebastian Redl3397c552010-08-18 23:56:27 +0000360void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000361 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000362 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
363 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000364 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000365}
366
Sebastian Redl3397c552010-08-18 23:56:27 +0000367void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000368 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000369 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000370 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000371}
372
Sebastian Redl3397c552010-08-18 23:56:27 +0000373void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000374 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000375 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000376}
377
Sebastian Redl3397c552010-08-18 23:56:27 +0000378void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000379 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000380 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000381 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000382 E = T->qual_end(); I != E; ++I)
383 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000384 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000385}
386
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000387void
Sebastian Redl3397c552010-08-18 23:56:27 +0000388ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000389 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000390 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000391}
392
Eli Friedmanb001de72011-10-06 23:00:33 +0000393void
394ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
395 Writer.AddTypeRef(T->getValueType(), Record);
396 Code = TYPE_ATOMIC;
397}
398
John McCalla1ee0c52009-10-16 21:56:05 +0000399namespace {
400
401class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000402 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000403 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000404
405public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000406 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000407 : Writer(Writer), Record(Record) { }
408
John McCall51bd8032009-10-18 01:05:36 +0000409#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000410#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000411 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000412#include "clang/AST/TypeLocNodes.def"
413
John McCall51bd8032009-10-18 01:05:36 +0000414 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
415 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000416};
417
418}
419
John McCall51bd8032009-10-18 01:05:36 +0000420void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
421 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000422}
John McCall51bd8032009-10-18 01:05:36 +0000423void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000424 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
425 if (TL.needsExtraLocalData()) {
426 Record.push_back(TL.getWrittenTypeSpec());
427 Record.push_back(TL.getWrittenSignSpec());
428 Record.push_back(TL.getWrittenWidthSpec());
429 Record.push_back(TL.hasModeAttr());
430 }
John McCalla1ee0c52009-10-16 21:56:05 +0000431}
John McCall51bd8032009-10-18 01:05:36 +0000432void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
433 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000434}
John McCall51bd8032009-10-18 01:05:36 +0000435void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
436 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000437}
John McCall51bd8032009-10-18 01:05:36 +0000438void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000446}
John McCall51bd8032009-10-18 01:05:36 +0000447void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000449 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000450}
John McCall51bd8032009-10-18 01:05:36 +0000451void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
452 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
453 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
454 Record.push_back(TL.getSizeExpr() ? 1 : 0);
455 if (TL.getSizeExpr())
456 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000457}
John McCall51bd8032009-10-18 01:05:36 +0000458void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
459 VisitArrayTypeLoc(TL);
460}
461void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
462 VisitArrayTypeLoc(TL);
463}
464void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
465 VisitArrayTypeLoc(TL);
466}
467void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
468 DependentSizedArrayTypeLoc TL) {
469 VisitArrayTypeLoc(TL);
470}
471void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
472 DependentSizedExtVectorTypeLoc TL) {
473 Writer.AddSourceLocation(TL.getNameLoc(), Record);
474}
475void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
477}
478void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
480}
481void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000482 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
483 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000484 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000485 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
486 Writer.AddDeclRef(TL.getArg(i), Record);
487}
488void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
489 VisitFunctionTypeLoc(TL);
490}
491void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
492 VisitFunctionTypeLoc(TL);
493}
John McCalled976492009-12-04 22:46:56 +0000494void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
495 Writer.AddSourceLocation(TL.getNameLoc(), Record);
496}
John McCall51bd8032009-10-18 01:05:36 +0000497void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
498 Writer.AddSourceLocation(TL.getNameLoc(), Record);
499}
500void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000501 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
502 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
503 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000504}
505void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000506 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
507 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
508 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
509 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000510}
511void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
513}
Sean Huntca63c202011-05-24 22:41:36 +0000514void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
515 Writer.AddSourceLocation(TL.getKWLoc(), Record);
516 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
517 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
518 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
519}
Richard Smith34b41d92011-02-20 03:19:35 +0000520void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
John McCall51bd8032009-10-18 01:05:36 +0000523void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getNameLoc(), Record);
525}
526void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
527 Writer.AddSourceLocation(TL.getNameLoc(), Record);
528}
John McCall9d156a72011-01-06 01:58:22 +0000529void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
531 if (TL.hasAttrOperand()) {
532 SourceRange range = TL.getAttrOperandParensRange();
533 Writer.AddSourceLocation(range.getBegin(), Record);
534 Writer.AddSourceLocation(range.getEnd(), Record);
535 }
536 if (TL.hasAttrExprOperand()) {
537 Expr *operand = TL.getAttrExprOperand();
538 Record.push_back(operand ? 1 : 0);
539 if (operand) Writer.AddStmt(operand);
540 } else if (TL.hasAttrEnumOperand()) {
541 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
542 }
543}
John McCall51bd8032009-10-18 01:05:36 +0000544void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getNameLoc(), Record);
546}
John McCall49a832b2009-10-18 09:09:24 +0000547void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
548 SubstTemplateTypeParmTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000551void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
552 SubstTemplateTypeParmPackTypeLoc TL) {
553 Writer.AddSourceLocation(TL.getNameLoc(), Record);
554}
John McCall51bd8032009-10-18 01:05:36 +0000555void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
556 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000557 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000558 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
559 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
560 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
561 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000562 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
563 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000564}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000565void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
567 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
568}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000569void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000570 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000571 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000572}
John McCall3cb0ebd2010-03-10 03:28:59 +0000573void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575}
Douglas Gregor4714c122010-03-31 17:34:00 +0000576void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000577 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000578 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000579 Writer.AddSourceLocation(TL.getNameLoc(), Record);
580}
John McCall33500952010-06-11 00:33:02 +0000581void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
582 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000583 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000584 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000585 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000586 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000587 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
588 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
589 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000590 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
591 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000592}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000593void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
594 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
595}
John McCall51bd8032009-10-18 01:05:36 +0000596void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
597 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000598}
599void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
600 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000601 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
602 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
603 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
604 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000605}
John McCall54e14c42009-10-22 22:37:11 +0000606void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
607 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000608}
Eli Friedmanb001de72011-10-06 23:00:33 +0000609void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
610 Writer.AddSourceLocation(TL.getKWLoc(), Record);
611 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
612 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
613}
John McCalla1ee0c52009-10-16 21:56:05 +0000614
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000615//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000616// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000617//===----------------------------------------------------------------------===//
618
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000619static void EmitBlockID(unsigned ID, const char *Name,
620 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000621 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000622 Record.clear();
623 Record.push_back(ID);
624 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
625
626 // Emit the block name if present.
627 if (Name == 0 || Name[0] == 0) return;
628 Record.clear();
629 while (*Name)
630 Record.push_back(*Name++);
631 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
632}
633
634static void EmitRecordID(unsigned ID, const char *Name,
635 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000636 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000637 Record.clear();
638 Record.push_back(ID);
639 while (*Name)
640 Record.push_back(*Name++);
641 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000642}
643
644static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000645 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000646#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000647 RECORD(STMT_STOP);
648 RECORD(STMT_NULL_PTR);
649 RECORD(STMT_NULL);
650 RECORD(STMT_COMPOUND);
651 RECORD(STMT_CASE);
652 RECORD(STMT_DEFAULT);
653 RECORD(STMT_LABEL);
654 RECORD(STMT_IF);
655 RECORD(STMT_SWITCH);
656 RECORD(STMT_WHILE);
657 RECORD(STMT_DO);
658 RECORD(STMT_FOR);
659 RECORD(STMT_GOTO);
660 RECORD(STMT_INDIRECT_GOTO);
661 RECORD(STMT_CONTINUE);
662 RECORD(STMT_BREAK);
663 RECORD(STMT_RETURN);
664 RECORD(STMT_DECL);
665 RECORD(STMT_ASM);
666 RECORD(EXPR_PREDEFINED);
667 RECORD(EXPR_DECL_REF);
668 RECORD(EXPR_INTEGER_LITERAL);
669 RECORD(EXPR_FLOATING_LITERAL);
670 RECORD(EXPR_IMAGINARY_LITERAL);
671 RECORD(EXPR_STRING_LITERAL);
672 RECORD(EXPR_CHARACTER_LITERAL);
673 RECORD(EXPR_PAREN);
674 RECORD(EXPR_UNARY_OPERATOR);
675 RECORD(EXPR_SIZEOF_ALIGN_OF);
676 RECORD(EXPR_ARRAY_SUBSCRIPT);
677 RECORD(EXPR_CALL);
678 RECORD(EXPR_MEMBER);
679 RECORD(EXPR_BINARY_OPERATOR);
680 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
681 RECORD(EXPR_CONDITIONAL_OPERATOR);
682 RECORD(EXPR_IMPLICIT_CAST);
683 RECORD(EXPR_CSTYLE_CAST);
684 RECORD(EXPR_COMPOUND_LITERAL);
685 RECORD(EXPR_EXT_VECTOR_ELEMENT);
686 RECORD(EXPR_INIT_LIST);
687 RECORD(EXPR_DESIGNATED_INIT);
688 RECORD(EXPR_IMPLICIT_VALUE_INIT);
689 RECORD(EXPR_VA_ARG);
690 RECORD(EXPR_ADDR_LABEL);
691 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000692 RECORD(EXPR_CHOOSE);
693 RECORD(EXPR_GNU_NULL);
694 RECORD(EXPR_SHUFFLE_VECTOR);
695 RECORD(EXPR_BLOCK);
696 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000697 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000698 RECORD(EXPR_OBJC_STRING_LITERAL);
699 RECORD(EXPR_OBJC_ENCODE);
700 RECORD(EXPR_OBJC_SELECTOR_EXPR);
701 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
702 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
703 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
704 RECORD(EXPR_OBJC_KVC_REF_EXPR);
705 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000706 RECORD(STMT_OBJC_FOR_COLLECTION);
707 RECORD(STMT_OBJC_CATCH);
708 RECORD(STMT_OBJC_FINALLY);
709 RECORD(STMT_OBJC_AT_TRY);
710 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
711 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000712 RECORD(EXPR_CXX_OPERATOR_CALL);
713 RECORD(EXPR_CXX_CONSTRUCT);
714 RECORD(EXPR_CXX_STATIC_CAST);
715 RECORD(EXPR_CXX_DYNAMIC_CAST);
716 RECORD(EXPR_CXX_REINTERPRET_CAST);
717 RECORD(EXPR_CXX_CONST_CAST);
718 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
719 RECORD(EXPR_CXX_BOOL_LITERAL);
720 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000721 RECORD(EXPR_CXX_TYPEID_EXPR);
722 RECORD(EXPR_CXX_TYPEID_TYPE);
723 RECORD(EXPR_CXX_UUIDOF_EXPR);
724 RECORD(EXPR_CXX_UUIDOF_TYPE);
725 RECORD(EXPR_CXX_THIS);
726 RECORD(EXPR_CXX_THROW);
727 RECORD(EXPR_CXX_DEFAULT_ARG);
728 RECORD(EXPR_CXX_BIND_TEMPORARY);
729 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
730 RECORD(EXPR_CXX_NEW);
731 RECORD(EXPR_CXX_DELETE);
732 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
733 RECORD(EXPR_EXPR_WITH_CLEANUPS);
734 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
735 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
736 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
737 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
738 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
739 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
740 RECORD(EXPR_CXX_NOEXCEPT);
741 RECORD(EXPR_OPAQUE_VALUE);
742 RECORD(EXPR_BINARY_TYPE_TRAIT);
743 RECORD(EXPR_PACK_EXPANSION);
744 RECORD(EXPR_SIZEOF_PACK);
745 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000746 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000747#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000748}
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Sebastian Redla4232eb2010-08-18 23:56:21 +0000750void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000751 RecordData Record;
752 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000754#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
755#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Sebastian Redl3397c552010-08-18 23:56:27 +0000757 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000758 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000759 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000760 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000761 RECORD(TYPE_OFFSET);
762 RECORD(DECL_OFFSET);
763 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000764 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000765 RECORD(IDENTIFIER_OFFSET);
766 RECORD(IDENTIFIER_TABLE);
767 RECORD(EXTERNAL_DEFINITIONS);
768 RECORD(SPECIAL_TYPES);
769 RECORD(STATISTICS);
770 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000771 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000772 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
773 RECORD(SELECTOR_OFFSETS);
774 RECORD(METHOD_POOL);
775 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000776 RECORD(SOURCE_LOCATION_OFFSETS);
777 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000778 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000779 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000780 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000781 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000782 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000783 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000784 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000785 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000786 RECORD(SEMA_DECL_REFS);
787 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
788 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
789 RECORD(DECL_REPLACEMENTS);
790 RECORD(UPDATE_VISIBLE);
791 RECORD(DECL_UPDATE_OFFSETS);
792 RECORD(DECL_UPDATES);
793 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
794 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000795 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000796 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000797 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000798 RECORD(FP_PRAGMA_OPTIONS);
799 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000800 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000801 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
802 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000803 RECORD(MODULE_OFFSET_MAP);
804 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000805 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000806 RECORD(FILE_SORTED_DECLS);
807 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000808 RECORD(MERGED_DECLARATIONS);
809 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000810 RECORD(OBJC_CATEGORIES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000811
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000812 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000813 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000814 RECORD(SM_SLOC_FILE_ENTRY);
815 RECORD(SM_SLOC_BUFFER_ENTRY);
816 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000817 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000819 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000820 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000821 RECORD(PP_MACRO_OBJECT_LIKE);
822 RECORD(PP_MACRO_FUNCTION_LIKE);
823 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000824
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000825 // Decls and Types block.
826 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000827 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000828 RECORD(TYPE_COMPLEX);
829 RECORD(TYPE_POINTER);
830 RECORD(TYPE_BLOCK_POINTER);
831 RECORD(TYPE_LVALUE_REFERENCE);
832 RECORD(TYPE_RVALUE_REFERENCE);
833 RECORD(TYPE_MEMBER_POINTER);
834 RECORD(TYPE_CONSTANT_ARRAY);
835 RECORD(TYPE_INCOMPLETE_ARRAY);
836 RECORD(TYPE_VARIABLE_ARRAY);
837 RECORD(TYPE_VECTOR);
838 RECORD(TYPE_EXT_VECTOR);
839 RECORD(TYPE_FUNCTION_PROTO);
840 RECORD(TYPE_FUNCTION_NO_PROTO);
841 RECORD(TYPE_TYPEDEF);
842 RECORD(TYPE_TYPEOF_EXPR);
843 RECORD(TYPE_TYPEOF);
844 RECORD(TYPE_RECORD);
845 RECORD(TYPE_ENUM);
846 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000847 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000848 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000849 RECORD(TYPE_DECLTYPE);
850 RECORD(TYPE_ELABORATED);
851 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
852 RECORD(TYPE_UNRESOLVED_USING);
853 RECORD(TYPE_INJECTED_CLASS_NAME);
854 RECORD(TYPE_OBJC_OBJECT);
855 RECORD(TYPE_TEMPLATE_TYPE_PARM);
856 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
857 RECORD(TYPE_DEPENDENT_NAME);
858 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
859 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
860 RECORD(TYPE_PAREN);
861 RECORD(TYPE_PACK_EXPANSION);
862 RECORD(TYPE_ATTRIBUTED);
863 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000864 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000865 RECORD(DECL_TYPEDEF);
866 RECORD(DECL_ENUM);
867 RECORD(DECL_RECORD);
868 RECORD(DECL_ENUM_CONSTANT);
869 RECORD(DECL_FUNCTION);
870 RECORD(DECL_OBJC_METHOD);
871 RECORD(DECL_OBJC_INTERFACE);
872 RECORD(DECL_OBJC_PROTOCOL);
873 RECORD(DECL_OBJC_IVAR);
874 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000875 RECORD(DECL_OBJC_CATEGORY);
876 RECORD(DECL_OBJC_CATEGORY_IMPL);
877 RECORD(DECL_OBJC_IMPLEMENTATION);
878 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
879 RECORD(DECL_OBJC_PROPERTY);
880 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000881 RECORD(DECL_FIELD);
882 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000883 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000884 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000885 RECORD(DECL_FILE_SCOPE_ASM);
886 RECORD(DECL_BLOCK);
887 RECORD(DECL_CONTEXT_LEXICAL);
888 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000889 RECORD(DECL_NAMESPACE);
890 RECORD(DECL_NAMESPACE_ALIAS);
891 RECORD(DECL_USING);
892 RECORD(DECL_USING_SHADOW);
893 RECORD(DECL_USING_DIRECTIVE);
894 RECORD(DECL_UNRESOLVED_USING_VALUE);
895 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
896 RECORD(DECL_LINKAGE_SPEC);
897 RECORD(DECL_CXX_RECORD);
898 RECORD(DECL_CXX_METHOD);
899 RECORD(DECL_CXX_CONSTRUCTOR);
900 RECORD(DECL_CXX_DESTRUCTOR);
901 RECORD(DECL_CXX_CONVERSION);
902 RECORD(DECL_ACCESS_SPEC);
903 RECORD(DECL_FRIEND);
904 RECORD(DECL_FRIEND_TEMPLATE);
905 RECORD(DECL_CLASS_TEMPLATE);
906 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
907 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
908 RECORD(DECL_FUNCTION_TEMPLATE);
909 RECORD(DECL_TEMPLATE_TYPE_PARM);
910 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
911 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
912 RECORD(DECL_STATIC_ASSERT);
913 RECORD(DECL_CXX_BASE_SPECIFIERS);
914 RECORD(DECL_INDIRECTFIELD);
915 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
916
Douglas Gregora72d8c42011-06-03 02:27:19 +0000917 // Statements and Exprs can occur in the Decls and Types block.
918 AddStmtsExprs(Stream, Record);
919
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000920 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000921 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000922 RECORD(PPD_MACRO_DEFINITION);
923 RECORD(PPD_INCLUSION_DIRECTIVE);
924
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000925#undef RECORD
926#undef BLOCK
927 Stream.ExitBlock();
928}
929
Douglas Gregore650c8c2009-07-07 00:12:59 +0000930/// \brief Adjusts the given filename to only write out the portion of the
931/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000932///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000933/// \param Filename the file name to adjust.
934///
935/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
936/// the returned filename will be adjusted by this system root.
937///
938/// \returns either the original filename (if it needs no adjustment) or the
939/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000940static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000941adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000942 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Douglas Gregor832d6202011-07-22 16:35:34 +0000944 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000945 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Douglas Gregore650c8c2009-07-07 00:12:59 +0000947 // Verify that the filename and the system root have the same prefix.
948 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000949 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000950 if (Filename[Pos] != isysroot[Pos])
951 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Douglas Gregore650c8c2009-07-07 00:12:59 +0000953 // We hit the end of the filename before we hit the end of the system root.
954 if (!Filename[Pos])
955 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957 // If the file name has a '/' at the current position, skip over the '/'.
958 // We distinguish sysroot-based includes from absolute includes by the
959 // absence of '/' at the beginning of sysroot-based includes.
960 if (Filename[Pos] == '/')
961 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Douglas Gregore650c8c2009-07-07 00:12:59 +0000963 return Filename + Pos;
964}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000965
Sebastian Redl3397c552010-08-18 23:56:27 +0000966/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000967void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000968 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000969 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000970
Douglas Gregore650c8c2009-07-07 00:12:59 +0000971 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000972 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000974 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000975 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
976 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
978 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
979 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000980 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000984 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000985 Record.push_back(VERSION_MAJOR);
986 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000987 Record.push_back(CLANG_VERSION_MAJOR);
988 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000989 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000990 const std::string &Triple = Target.getTriple().getTriple();
991 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
992
993 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000994 serialization::ModuleManager &Mgr = Chain->getModuleManager();
995 llvm::SmallVector<char, 128> ModulePaths;
996 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000997
998 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
999 M != MEnd; ++M) {
1000 // Skip modules that weren't directly imported.
1001 if (!(*M)->isDirectlyImported())
1002 continue;
1003
1004 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1005 // FIXME: Write import location, once it matters.
1006 // FIXME: This writes the absolute path for AST files we depend on.
1007 const std::string &FileName = (*M)->FileName;
1008 Record.push_back(FileName.size());
1009 Record.append(FileName.begin(), FileName.end());
1010 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001011 Stream.EmitRecord(IMPORTS, Record);
1012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Douglas Gregor31d375f2011-05-06 21:43:30 +00001014 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001015 SourceManager &SM = Context.getSourceManager();
1016 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1017 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001018 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001019 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1020 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1021
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001022 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001024 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001025
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001026 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001027 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001028 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001029 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001030 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001031 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001032
1033 Record.clear();
1034 Record.push_back(SM.getMainFileID().getOpaqueValue());
1035 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001036 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001037
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001038 // Original PCH directory
1039 if (!OutputFile.empty() && OutputFile != "-") {
1040 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1041 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1043 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1044
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001045 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001046
1047 llvm::sys::fs::make_absolute(OutputPath);
1048 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1049
1050 RecordData Record;
1051 Record.push_back(ORIGINAL_PCH_DIR);
1052 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1053 }
1054
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001055 // Repository branch/version information.
1056 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001057 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001058 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1059 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001060 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001061 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001062 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1063 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001064}
1065
1066/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001067void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001068 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001069#define LANGOPT(Name, Bits, Default, Description) \
1070 Record.push_back(LangOpts.Name);
1071#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1072 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1073#include "clang/Basic/LangOptions.def"
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001074
1075 Record.push_back(LangOpts.CurrentModule.size());
1076 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001077 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001078}
1079
Douglas Gregor14f79002009-04-10 03:52:48 +00001080//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001081// stat cache Serialization
1082//===----------------------------------------------------------------------===//
1083
1084namespace {
1085// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001086class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087public:
1088 typedef const char * key_type;
1089 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Chris Lattner74e976b2010-11-23 19:28:12 +00001091 typedef struct stat data_type;
1092 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001093
1094 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001095 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096 }
Mike Stump1eb44332009-09-09 15:08:12 +00001097
1098 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001099 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001100 data_type_ref Data) {
1101 unsigned StrLen = strlen(path);
1102 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001103 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001104 clang::io::Emit8(Out, DataLen);
1105 return std::make_pair(StrLen + 1, DataLen);
1106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Chris Lattner5f9e2722011-07-23 10:55:15 +00001108 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001109 Out.write(path, KeyLen);
1110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner5f9e2722011-07-23 10:55:15 +00001112 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001113 data_type_ref Data, unsigned DataLen) {
1114 using namespace clang::io;
1115 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Chris Lattner74e976b2010-11-23 19:28:12 +00001117 Emit32(Out, (uint32_t) Data.st_ino);
1118 Emit32(Out, (uint32_t) Data.st_dev);
1119 Emit16(Out, (uint16_t) Data.st_mode);
1120 Emit64(Out, (uint64_t) Data.st_mtime);
1121 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001122
1123 assert(Out.tell() - Start == DataLen && "Wrong data length");
1124 }
1125};
1126} // end anonymous namespace
1127
Sebastian Redl3397c552010-08-18 23:56:27 +00001128/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001129void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001130 // Build the on-disk hash table containing information about every
1131 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001132 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001133 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001134 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001135 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001136 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001137 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001138 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001139 }
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001141 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001142 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001143 uint32_t BucketOffset;
1144 {
1145 llvm::raw_svector_ostream Out(StatCacheData);
1146 // Make sure that no bucket is at offset 0
1147 clang::io::Emit32(Out, 0);
1148 BucketOffset = Generator.Emit(Out);
1149 }
1150
1151 // Create a blob abbreviation
1152 using namespace llvm;
1153 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001154 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1158 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1159
1160 // Write the stat cache
1161 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001162 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001163 Record.push_back(BucketOffset);
1164 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001165 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001166}
1167
1168//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001169// Source Manager Serialization
1170//===----------------------------------------------------------------------===//
1171
1172/// \brief Create an abbreviation for the SLocEntry that refers to a
1173/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001174static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001175 using namespace llvm;
1176 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001177 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001182 // FileEntry fields.
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001190 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001191}
1192
1193/// \brief Create an abbreviation for the SLocEntry that refers to a
1194/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001195static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001196 using namespace llvm;
1197 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001198 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001204 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001205}
1206
1207/// \brief Create an abbreviation for the SLocEntry that refers to a
1208/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001209static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001210 using namespace llvm;
1211 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001212 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001214 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001215}
1216
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001217/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1218/// expansion.
1219static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001220 using namespace llvm;
1221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001222 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001228 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001229}
1230
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001231namespace {
1232 // Trait used for the on-disk hash table of header search information.
1233 class HeaderFileInfoTrait {
1234 ASTWriter &Writer;
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001235 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001236
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001237 // Keep track of the framework names we've used during serialization.
1238 SmallVector<char, 128> FrameworkStringData;
1239 llvm::StringMap<unsigned> FrameworkNameOffset;
1240
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001241 public:
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001242 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001243 : Writer(Writer), HS(HS) { }
1244
1245 typedef const char *key_type;
1246 typedef key_type key_type_ref;
1247
1248 typedef HeaderFileInfo data_type;
1249 typedef const data_type &data_type_ref;
1250
1251 static unsigned ComputeHash(const char *path) {
1252 // The hash is based only on the filename portion of the key, so that the
1253 // reader can match based on filenames when symlinking or excess path
1254 // elements ("foo/../", "../") change the form of the name. However,
1255 // complete path is still the key.
1256 return llvm::HashString(llvm::sys::path::filename(path));
1257 }
1258
1259 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001260 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001261 data_type_ref Data) {
1262 unsigned StrLen = strlen(path);
1263 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001264 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001265 clang::io::Emit8(Out, DataLen);
1266 return std::make_pair(StrLen + 1, DataLen);
1267 }
1268
Chris Lattner5f9e2722011-07-23 10:55:15 +00001269 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001270 Out.write(path, KeyLen);
1271 }
1272
Chris Lattner5f9e2722011-07-23 10:55:15 +00001273 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001274 data_type_ref Data, unsigned DataLen) {
1275 using namespace clang::io;
1276 uint64_t Start = Out.tell(); (void)Start;
1277
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001278 unsigned char Flags = (Data.isImport << 5)
1279 | (Data.isPragmaOnce << 4)
1280 | (Data.DirInfo << 2)
1281 | (Data.Resolved << 1)
1282 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001283 Emit8(Out, (uint8_t)Flags);
1284 Emit16(Out, (uint16_t) Data.NumIncludes);
1285
1286 if (!Data.ControllingMacro)
1287 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1288 else
1289 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001290
1291 unsigned Offset = 0;
1292 if (!Data.Framework.empty()) {
1293 // If this header refers into a framework, save the framework name.
1294 llvm::StringMap<unsigned>::iterator Pos
1295 = FrameworkNameOffset.find(Data.Framework);
1296 if (Pos == FrameworkNameOffset.end()) {
1297 Offset = FrameworkStringData.size() + 1;
1298 FrameworkStringData.append(Data.Framework.begin(),
1299 Data.Framework.end());
1300 FrameworkStringData.push_back(0);
1301
1302 FrameworkNameOffset[Data.Framework] = Offset;
1303 } else
1304 Offset = Pos->second;
1305 }
1306 Emit32(Out, Offset);
1307
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001308 assert(Out.tell() - Start == DataLen && "Wrong data length");
1309 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001310
1311 const char *strings_begin() const { return FrameworkStringData.begin(); }
1312 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001313 };
1314} // end anonymous namespace
1315
1316/// \brief Write the header search block for the list of files that
1317///
1318/// \param HS The header search structure to save.
1319///
1320/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001321void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001322 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001323 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1324
1325 if (FilesByUID.size() > HS.header_file_size())
1326 FilesByUID.resize(HS.header_file_size());
1327
1328 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1329 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001330 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001331 unsigned NumHeaderSearchEntries = 0;
1332 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1333 const FileEntry *File = FilesByUID[UID];
1334 if (!File)
1335 continue;
1336
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001337 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1338 // from the external source if it was not provided already.
1339 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001340 if (HFI.External && Chain)
1341 continue;
1342
1343 // Turn the file name into an absolute path, if it isn't already.
1344 const char *Filename = File->getName();
1345 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1346
1347 // If we performed any translation on the file name at all, we need to
1348 // save this string, since the generator will refer to it later.
1349 if (Filename != File->getName()) {
1350 Filename = strdup(Filename);
1351 SavedStrings.push_back(Filename);
1352 }
1353
1354 Generator.insert(Filename, HFI, GeneratorTrait);
1355 ++NumHeaderSearchEntries;
1356 }
1357
1358 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001359 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001360 uint32_t BucketOffset;
1361 {
1362 llvm::raw_svector_ostream Out(TableData);
1363 // Make sure that no bucket is at offset 0
1364 clang::io::Emit32(Out, 0);
1365 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1366 }
1367
1368 // Create a blob abbreviation
1369 using namespace llvm;
1370 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1371 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1376 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1377
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001378 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001379 RecordData Record;
1380 Record.push_back(HEADER_SEARCH_TABLE);
1381 Record.push_back(BucketOffset);
1382 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001383 Record.push_back(TableData.size());
1384 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001385 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1386
1387 // Free all of the strings we had to duplicate.
1388 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1389 free((void*)SavedStrings[I]);
1390}
1391
Douglas Gregor14f79002009-04-10 03:52:48 +00001392/// \brief Writes the block containing the serialized form of the
1393/// source manager.
1394///
1395/// TODO: We should probably use an on-disk hash table (stored in a
1396/// blob), indexed based on the file name, so that we only create
1397/// entries for files that we actually need. In the common case (no
1398/// errors), we probably won't have to create file entries for any of
1399/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001400void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001401 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001402 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001403 RecordData Record;
1404
Chris Lattnerf04ad692009-04-10 17:16:57 +00001405 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001406 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001407
1408 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001409 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1410 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1411 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001412 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001413
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001414 // Write out the source location entry table. We skip the first
1415 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001416 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001417 // Write out the offsets of only source location file entries.
1418 // We will go through them in ASTReader::validateFileEntries().
1419 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001420 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001421 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1422 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001423 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001424 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001425 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001426
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001427 // Record the offset of this source-location entry.
1428 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1429
1430 // Figure out which record code to use.
1431 unsigned Code;
1432 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001433 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1434 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001435 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001436 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1437 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001438 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001439 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001440 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001441 Record.clear();
1442 Record.push_back(Code);
1443
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001444 // Starting offset of this entry within this module, so skip the dummy.
1445 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001446 if (SLoc->isFile()) {
1447 const SrcMgr::FileInfo &File = SLoc->getFile();
1448 Record.push_back(File.getIncludeLoc().getRawEncoding());
1449 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1450 Record.push_back(File.hasLineDirectives());
1451
1452 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001453 if (Content->OrigEntry) {
1454 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001455 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001456
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001457 // The source location entry is a file. The blob associated
1458 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Douglas Gregor2d52be52010-03-21 22:49:54 +00001460 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001461 Record.push_back(Content->OrigEntry->getSize());
1462 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001463 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001464 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001465
1466 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1467 if (FDI != FileDeclIDs.end()) {
1468 Record.push_back(FDI->second->FirstDeclIndex);
1469 Record.push_back(FDI->second->DeclIDs.size());
1470 } else {
1471 Record.push_back(0);
1472 Record.push_back(0);
1473 }
Douglas Gregora081da52011-11-16 20:05:18 +00001474
Douglas Gregore650c8c2009-07-07 00:12:59 +00001475 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001476 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001477 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001478
1479 // Ask the file manager to fixup the relative path for us. This will
1480 // honor the working directory.
1481 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1482
1483 // FIXME: This call to make_absolute shouldn't be necessary, the
1484 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001485 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001486 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Douglas Gregore650c8c2009-07-07 00:12:59 +00001488 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001489 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001490
1491 if (Content->BufferOverridden) {
1492 Record.clear();
1493 Record.push_back(SM_SLOC_BUFFER_BLOB);
1494 const llvm::MemoryBuffer *Buffer
1495 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1496 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1497 StringRef(Buffer->getBufferStart(),
1498 Buffer->getBufferSize() + 1));
1499 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001500 } else {
1501 // The source location entry is a buffer. The blob associated
1502 // with this entry contains the contents of the buffer.
1503
1504 // We add one to the size so that we capture the trailing NULL
1505 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1506 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001507 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001508 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001509 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001510 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001511 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001512 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001513 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001514 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001515 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001516 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001517
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001518 if (strcmp(Name, "<built-in>") == 0) {
1519 PreloadSLocs.push_back(SLocEntryOffsets.size());
1520 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001521 }
1522 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001523 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001524 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001525 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1526 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001527 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1528 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001529
1530 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001531 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001532 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001533 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001534 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001535 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001536 }
1537 }
1538
Douglas Gregorc9490c02009-04-16 22:23:12 +00001539 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001540
1541 if (SLocEntryOffsets.empty())
1542 return;
1543
Sebastian Redl3397c552010-08-18 23:56:27 +00001544 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001545 // table is used for lazily loading source-location information.
1546 using namespace llvm;
1547 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001548 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1552 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001554 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001555 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001556 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001557 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001558 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001559
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001560 Abbrev = new BitCodeAbbrev();
1561 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1564 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1565
1566 Record.clear();
1567 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1568 Record.push_back(SLocFileEntryOffsets.size());
1569 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1570 data(SLocFileEntryOffsets));
1571
Sebastian Redl3397c552010-08-18 23:56:27 +00001572 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001573 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001574 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001575
1576 // Write the line table. It depends on remapping working, so it must come
1577 // after the source location offsets.
1578 if (SourceMgr.hasLineTable()) {
1579 LineTableInfo &LineTable = SourceMgr.getLineTable();
1580
1581 Record.clear();
1582 // Emit the file names
1583 Record.push_back(LineTable.getNumFilenames());
1584 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1585 // Emit the file name
1586 const char *Filename = LineTable.getFilename(I);
1587 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1588 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1589 Record.push_back(FilenameLen);
1590 if (FilenameLen)
1591 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1592 }
1593
1594 // Emit the line entries
1595 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1596 L != LEnd; ++L) {
1597 // Only emit entries for local files.
1598 if (L->first < 0)
1599 continue;
1600
1601 // Emit the file ID
1602 Record.push_back(L->first);
1603
1604 // Emit the line entries
1605 Record.push_back(L->second.size());
1606 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1607 LEEnd = L->second.end();
1608 LE != LEEnd; ++LE) {
1609 Record.push_back(LE->FileOffset);
1610 Record.push_back(LE->LineNo);
1611 Record.push_back(LE->FilenameID);
1612 Record.push_back((unsigned)LE->FileKind);
1613 Record.push_back(LE->IncludeOffset);
1614 }
1615 }
1616 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1617 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001618}
1619
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001620//===----------------------------------------------------------------------===//
1621// Preprocessor Serialization
1622//===----------------------------------------------------------------------===//
1623
Douglas Gregor9c736102011-02-10 18:20:09 +00001624static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1625 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1626 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1627 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1628 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1629 return X.first->getName().compare(Y.first->getName());
1630}
1631
Chris Lattner0b1fb982009-04-10 17:15:23 +00001632/// \brief Writes the block containing the serialized form of the
1633/// preprocessor.
1634///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001635void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001636 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1637 if (PPRec)
1638 WritePreprocessorDetail(*PPRec);
1639
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001640 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001641
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001642 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1643 if (PP.getCounterValue() != 0) {
1644 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001645 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001646 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001647 }
1648
1649 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001650 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Sebastian Redl3397c552010-08-18 23:56:27 +00001652 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001653 // FIXME: use diagnostics subsystem for localization etc.
1654 if (PP.SawDateOrTime())
1655 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Douglas Gregorecdcb882010-10-20 22:00:55 +00001657
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001658 // Loop over all the macro definitions that are live at the end of the file,
1659 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001660
Douglas Gregor9c736102011-02-10 18:20:09 +00001661 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001662 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001663 MacrosToEmit;
1664 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001665 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1666 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001667 I != E; ++I) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001668 const IdentifierInfo *Name = I->first;
Douglas Gregoraa93a872011-10-17 15:32:29 +00001669 if (!IsModule || I->second->isPublic()) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001670 MacroDefinitionsSeen.insert(Name);
Douglas Gregor7143aab2011-09-01 17:04:32 +00001671 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1672 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001673 }
1674
1675 // Sort the set of macro definitions that need to be serialized by the
1676 // name of the macro, to provide a stable ordering.
1677 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1678 &compareMacroDefinitions);
1679
Douglas Gregor040a8042011-02-11 00:26:14 +00001680 // Resolve any identifiers that defined macros at the time they were
1681 // deserialized, adding them to the list of macros to emit (if appropriate).
1682 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1683 IdentifierInfo *Name
1684 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1685 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1686 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1687 }
1688
Douglas Gregor9c736102011-02-10 18:20:09 +00001689 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1690 const IdentifierInfo *Name = MacrosToEmit[I].first;
1691 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001692 if (!MI)
1693 continue;
1694
Sebastian Redl3397c552010-08-18 23:56:27 +00001695 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001696 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001697 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001698
1699 // FIXME: There is a (probably minor) optimization we could do here, if
1700 // the macro comes from the original PCH but the identifier comes from a
1701 // chained PCH, by storing the offset into the original PCH rather than
1702 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001703 if (MI->isBuiltinMacro() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00001704 (Chain &&
1705 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1706 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001707 continue;
1708
Douglas Gregor9c736102011-02-10 18:20:09 +00001709 AddIdentifierRef(Name, Record);
1710 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001711 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1712 Record.push_back(MI->isUsed());
Douglas Gregoraa93a872011-10-17 15:32:29 +00001713 Record.push_back(MI->isPublic());
1714 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001715 unsigned Code;
1716 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001717 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001718 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001719 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001721 Record.push_back(MI->isC99Varargs());
1722 Record.push_back(MI->isGNUVarargs());
1723 Record.push_back(MI->getNumArgs());
1724 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1725 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001726 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001727 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001728
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001729 // If we have a detailed preprocessing record, record the macro definition
1730 // ID that corresponds to this macro.
1731 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001732 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001733
Douglas Gregorc9490c02009-04-16 22:23:12 +00001734 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001735 Record.clear();
1736
Chris Lattnerdf961c22009-04-10 18:08:30 +00001737 // Emit the tokens array.
1738 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1739 // Note that we know that the preprocessor does not have any annotation
1740 // tokens in it because they are created by the parser, and thus can't be
1741 // in a macro definition.
1742 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Chris Lattnerdf961c22009-04-10 18:08:30 +00001744 Record.push_back(Tok.getLocation().getRawEncoding());
1745 Record.push_back(Tok.getLength());
1746
Chris Lattnerdf961c22009-04-10 18:08:30 +00001747 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1748 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001749 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001750 // FIXME: Should translate token kind to a stable encoding.
1751 Record.push_back(Tok.getKind());
1752 // FIXME: Should translate token flags to a stable encoding.
1753 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001755 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001756 Record.clear();
1757 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001758 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001759 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001760 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001761}
1762
1763void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001764 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001765 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001766
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001767 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001768
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001769 // Enter the preprocessor block.
1770 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001771
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001772 // If the preprocessor has a preprocessing record, emit it.
1773 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001774 using namespace llvm;
1775
1776 // Set up the abbreviation for
1777 unsigned InclusionAbbrev = 0;
1778 {
1779 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1780 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1783 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1784 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1785 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1786 }
1787
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001788 unsigned FirstPreprocessorEntityID
1789 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1790 + NUM_PREDEF_PP_ENTITY_IDS;
1791 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001792 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001793 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1794 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001795 E != EEnd;
1796 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001797 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001798
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001799 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1800 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001801
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001802 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001803 // Record this macro definition's ID.
1804 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001805
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001806 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001807 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1808 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001809 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001810
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001811 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001812 Record.push_back(ME->isBuiltinMacro());
1813 if (ME->isBuiltinMacro())
1814 AddIdentifierRef(ME->getName(), Record);
1815 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001816 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001817 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001818 continue;
1819 }
1820
1821 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1822 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001823 Record.push_back(ID->getFileName().size());
1824 Record.push_back(ID->wasInQuotes());
1825 Record.push_back(static_cast<unsigned>(ID->getKind()));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001826 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001827 Buffer += ID->getFileName();
1828 Buffer += ID->getFile()->getName();
1829 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1830 continue;
1831 }
1832
1833 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1834 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001835 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001836
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001837 // Write the offsets table for the preprocessing record.
1838 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001839 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1840
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001841 // Write the offsets table for identifier IDs.
1842 using namespace llvm;
1843 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001844 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001847 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001848
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001849 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001850 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001851 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001852 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1853 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001854 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001855}
1856
Douglas Gregore209e502011-12-06 01:10:29 +00001857unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1858 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1859 if (Known != SubmoduleIDs.end())
1860 return Known->second;
1861
1862 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1863}
1864
Douglas Gregor26ced122011-12-01 00:59:36 +00001865/// \brief Compute the number of modules within the given tree (including the
1866/// given module).
1867static unsigned getNumberOfModules(Module *Mod) {
1868 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001869 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1870 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001871 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001872 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001873
1874 return ChildModules + 1;
1875}
1876
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001877void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001878 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001879 // FIXME: This feels like it belongs somewhere else, but there are no
1880 // other consumers of this information.
1881 SourceManager &SrcMgr = PP->getSourceManager();
1882 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1883 for (ASTContext::import_iterator I = Context->local_import_begin(),
1884 IEnd = Context->local_import_end();
1885 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001886 if (Module *ImportedFrom
1887 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1888 SrcMgr))) {
1889 ImportedFrom->Imports.push_back(I->getImportedModule());
1890 }
1891 }
1892
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001893 // Enter the submodule description block.
1894 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1895
1896 // Write the abbreviations needed for the submodules block.
1897 using namespace llvm;
1898 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1899 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1909 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1910
1911 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001912 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1914 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1915
1916 Abbrev = new BitCodeAbbrev();
1917 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1919 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001920
1921 Abbrev = new BitCodeAbbrev();
1922 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1924 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1925
Douglas Gregor51f564f2011-12-31 04:05:44 +00001926 Abbrev = new BitCodeAbbrev();
1927 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1929 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1930
Douglas Gregor26ced122011-12-01 00:59:36 +00001931 // Write the submodule metadata block.
1932 RecordData Record;
1933 Record.push_back(getNumberOfModules(WritingModule));
1934 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1935 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1936
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001937 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001938 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001939 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001940 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001941 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001942 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001943 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001944
1945 // Emit the definition of the block.
1946 Record.clear();
1947 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00001948 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001949 if (Mod->Parent) {
1950 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1951 Record.push_back(SubmoduleIDs[Mod->Parent]);
1952 } else {
1953 Record.push_back(0);
1954 }
1955 Record.push_back(Mod->IsFramework);
1956 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001957 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00001958 Record.push_back(Mod->InferSubmodules);
1959 Record.push_back(Mod->InferExplicitSubmodules);
1960 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001961 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1962
Douglas Gregor51f564f2011-12-31 04:05:44 +00001963 // Emit the requirements.
1964 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1965 Record.clear();
1966 Record.push_back(SUBMODULE_REQUIRES);
1967 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1968 Mod->Requires[I].data(),
1969 Mod->Requires[I].size());
1970 }
1971
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001972 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00001973 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001974 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001975 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001976 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00001977 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00001978 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1979 Record.clear();
1980 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1981 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1982 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001983 }
1984
1985 // Emit the headers.
1986 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1987 Record.clear();
1988 Record.push_back(SUBMODULE_HEADER);
1989 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1990 Mod->Headers[I]->getName());
1991 }
Douglas Gregor55988682011-12-05 16:33:54 +00001992
1993 // Emit the imports.
1994 if (!Mod->Imports.empty()) {
1995 Record.clear();
1996 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00001997 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00001998 assert(ImportedID && "Unknown submodule!");
1999 Record.push_back(ImportedID);
2000 }
2001 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2002 }
2003
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002004 // Emit the exports.
2005 if (!Mod->Exports.empty()) {
2006 Record.clear();
2007 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002008 if (Module *Exported = Mod->Exports[I].getPointer()) {
2009 unsigned ExportedID = SubmoduleIDs[Exported];
2010 assert(ExportedID > 0 && "Unknown submodule ID?");
2011 Record.push_back(ExportedID);
2012 } else {
2013 Record.push_back(0);
2014 }
2015
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002016 Record.push_back(Mod->Exports[I].getInt());
2017 }
2018 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2019 }
2020
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002021 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002022 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2023 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002024 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002025 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002026 }
2027
2028 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002029
2030 assert((NextSubmoduleID - FirstSubmoduleID
2031 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002032}
2033
Douglas Gregor185dbd72011-12-01 02:07:58 +00002034serialization::SubmoduleID
2035ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002036 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002037 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002038
2039 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002040 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002041 Module *OwningMod
2042 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002043 if (!OwningMod)
2044 return 0;
2045
Douglas Gregore209e502011-12-06 01:10:29 +00002046 // Check whether this submodule is part of our own module.
2047 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002048 return 0;
2049
Douglas Gregore209e502011-12-06 01:10:29 +00002050 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002051}
2052
David Blaikied6471f72011-09-25 23:23:43 +00002053void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002054 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002055 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002056 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2057 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002058 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002059 if (point.Loc.isInvalid())
2060 continue;
2061
2062 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002063 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002064 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002065 if (I->second.isPragma()) {
2066 Record.push_back(I->first);
2067 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002068 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002069 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002070 Record.push_back(-1); // mark the end of the diag/map pairs for this
2071 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002072 }
2073
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002074 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002075 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002076}
2077
Anders Carlssonc8505782011-03-06 18:41:18 +00002078void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2079 if (CXXBaseSpecifiersOffsets.empty())
2080 return;
2081
2082 RecordData Record;
2083
2084 // Create a blob abbreviation for the C++ base specifiers offsets.
2085 using namespace llvm;
2086
2087 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2088 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2091 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2092
Douglas Gregore92b8a12011-08-04 00:01:48 +00002093 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002094 Record.clear();
2095 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2096 Record.push_back(CXXBaseSpecifiersOffsets.size());
2097 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002098 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002099}
2100
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002101//===----------------------------------------------------------------------===//
2102// Type Serialization
2103//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002104
Sebastian Redl3397c552010-08-18 23:56:27 +00002105/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002106void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002107 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002108 if (Idx.getIndex() == 0) // we haven't seen this type before.
2109 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Douglas Gregor97475832010-10-05 18:37:06 +00002111 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002112
Douglas Gregor2cf26342009-04-09 22:27:44 +00002113 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002114 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002115 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002116 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002117 else if (TypeOffsets.size() < Index) {
2118 TypeOffsets.resize(Index + 1);
2119 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002120 }
2121
2122 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002123
Douglas Gregor2cf26342009-04-09 22:27:44 +00002124 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002125 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002126
Douglas Gregora4923eb2009-11-16 21:35:15 +00002127 if (T.hasLocalNonFastQualifiers()) {
2128 Qualifiers Qs = T.getLocalQualifiers();
2129 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002130 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002131 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002132 } else {
2133 switch (T->getTypeClass()) {
2134 // For all of the concrete, non-dependent types, call the
2135 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002136#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002137 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002138#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002139#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002140 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002141 }
2142
2143 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002144 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002145
2146 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002147 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002148}
2149
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002150//===----------------------------------------------------------------------===//
2151// Declaration Serialization
2152//===----------------------------------------------------------------------===//
2153
Douglas Gregor2cf26342009-04-09 22:27:44 +00002154/// \brief Write the block containing all of the declaration IDs
2155/// lexically declared within the given DeclContext.
2156///
2157/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2158/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002159uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002160 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002161 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002162 return 0;
2163
Douglas Gregorc9490c02009-04-16 22:23:12 +00002164 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002165 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002166 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002167 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002168 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2169 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002170 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002171
Douglas Gregor25123082009-04-22 22:34:57 +00002172 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002173 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002174 return Offset;
2175}
2176
Sebastian Redla4232eb2010-08-18 23:56:21 +00002177void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002178 using namespace llvm;
2179 RecordData Record;
2180
2181 // Write the type offsets array
2182 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002183 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2187 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2188 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002189 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002190 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002191 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002192 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002193
2194 // Write the declaration offsets array
2195 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002196 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2200 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2201 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002202 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002203 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002204 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002205 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002206}
2207
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002208void ASTWriter::WriteFileDeclIDsMap() {
2209 using namespace llvm;
2210 RecordData Record;
2211
2212 // Join the vectors of DeclIDs from all files.
2213 SmallVector<DeclID, 256> FileSortedIDs;
2214 for (FileDeclIDsTy::iterator
2215 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2216 DeclIDInFileInfo &Info = *FI->second;
2217 Info.FirstDeclIndex = FileSortedIDs.size();
2218 for (LocDeclIDsTy::iterator
2219 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2220 FileSortedIDs.push_back(DI->second);
2221 }
2222
2223 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2224 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2226 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2227 Record.push_back(FILE_SORTED_DECLS);
2228 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2229}
2230
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002231//===----------------------------------------------------------------------===//
2232// Global Method Pool and Selector Serialization
2233//===----------------------------------------------------------------------===//
2234
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002235namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002236// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002237class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002238 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002239
2240public:
2241 typedef Selector key_type;
2242 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Sebastian Redl5d050072010-08-04 17:20:04 +00002244 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002245 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002246 ObjCMethodList Instance, Factory;
2247 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002248 typedef const data_type& data_type_ref;
2249
Sebastian Redl3397c552010-08-18 23:56:27 +00002250 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002252 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002253 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002254 }
Mike Stump1eb44332009-09-09 15:08:12 +00002255
2256 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002257 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002258 data_type_ref Methods) {
2259 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2260 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002261 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2262 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002263 Method = Method->Next)
2264 if (Method->Method)
2265 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002266 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002267 Method = Method->Next)
2268 if (Method->Method)
2269 DataLen += 4;
2270 clang::io::Emit16(Out, DataLen);
2271 return std::make_pair(KeyLen, DataLen);
2272 }
Mike Stump1eb44332009-09-09 15:08:12 +00002273
Chris Lattner5f9e2722011-07-23 10:55:15 +00002274 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002275 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002276 assert((Start >> 32) == 0 && "Selector key offset too large");
2277 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002278 unsigned N = Sel.getNumArgs();
2279 clang::io::Emit16(Out, N);
2280 if (N == 0)
2281 N = 1;
2282 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002283 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002284 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2285 }
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Chris Lattner5f9e2722011-07-23 10:55:15 +00002287 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002288 data_type_ref Methods, unsigned DataLen) {
2289 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002290 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002291 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002292 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002293 Method = Method->Next)
2294 if (Method->Method)
2295 ++NumInstanceMethods;
2296
2297 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002298 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002299 Method = Method->Next)
2300 if (Method->Method)
2301 ++NumFactoryMethods;
2302
2303 clang::io::Emit16(Out, NumInstanceMethods);
2304 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002305 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002306 Method = Method->Next)
2307 if (Method->Method)
2308 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002309 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002310 Method = Method->Next)
2311 if (Method->Method)
2312 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002313
2314 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002315 }
2316};
2317} // end anonymous namespace
2318
Sebastian Redl059612d2010-08-03 21:58:15 +00002319/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002320///
2321/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002322/// in an on-disk hash table indexed by the selector. The hash table also
2323/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002324void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002325 using namespace llvm;
2326
Sebastian Redl059612d2010-08-03 21:58:15 +00002327 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002328 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002329 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002330 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002331 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002332 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002333 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002334 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Sebastian Redl059612d2010-08-03 21:58:15 +00002336 // Create the on-disk hash table representation. We walk through every
2337 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002338 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002339 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002340 I = SelectorIDs.begin(), E = SelectorIDs.end();
2341 I != E; ++I) {
2342 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002343 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002344 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002345 I->second,
2346 ObjCMethodList(),
2347 ObjCMethodList()
2348 };
2349 if (F != SemaRef.MethodPool.end()) {
2350 Data.Instance = F->second.first;
2351 Data.Factory = F->second.second;
2352 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002353 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002354 // changed.
2355 if (Chain && I->second < FirstSelectorID) {
2356 // Selector already exists. Did it change?
2357 bool changed = false;
2358 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2359 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002360 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002361 changed = true;
2362 }
2363 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2364 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002365 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002366 changed = true;
2367 }
2368 if (!changed)
2369 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002370 } else if (Data.Instance.Method || Data.Factory.Method) {
2371 // A new method pool entry.
2372 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002373 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002374 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002375 }
2376
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002377 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002378 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002379 uint32_t BucketOffset;
2380 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002381 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002382 llvm::raw_svector_ostream Out(MethodPool);
2383 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002384 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002385 BucketOffset = Generator.Emit(Out, Trait);
2386 }
2387
2388 // Create a blob abbreviation
2389 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002390 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2394 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2395
Douglas Gregor83941df2009-04-25 17:48:32 +00002396 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002397 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002398 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002399 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002400 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002401 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002402
2403 // Create a blob abbreviation for the selector table offsets.
2404 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002405 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2409 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2410
2411 // Write the selector offsets table.
2412 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002413 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002414 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002415 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002416 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002417 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002418 }
2419}
2420
Sebastian Redl3397c552010-08-18 23:56:27 +00002421/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002422void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002423 using namespace llvm;
2424 if (SemaRef.ReferencedSelectors.empty())
2425 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002426
Fariborz Jahanian32019832010-07-23 19:11:11 +00002427 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002428
Sebastian Redl3397c552010-08-18 23:56:27 +00002429 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002430 // very tricky to fix, and given that @selector shouldn't really appear in
2431 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002432 for (DenseMap<Selector, SourceLocation>::iterator S =
2433 SemaRef.ReferencedSelectors.begin(),
2434 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2435 Selector Sel = (*S).first;
2436 SourceLocation Loc = (*S).second;
2437 AddSelectorRef(Sel, Record);
2438 AddSourceLocation(Loc, Record);
2439 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002440 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002441}
2442
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002443//===----------------------------------------------------------------------===//
2444// Identifier Table Serialization
2445//===----------------------------------------------------------------------===//
2446
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002447namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002448class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002449 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002450 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002451 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002452 bool IsModule;
2453
Douglas Gregora92193e2009-04-28 21:18:29 +00002454 /// \brief Determines whether this is an "interesting" identifier
2455 /// that needs a full IdentifierInfo structure written into the hash
2456 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002457 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002458 if (II->isPoisoned() ||
2459 II->isExtensionToken() ||
2460 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002461 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002462 II->getFETokenInfo<void>())
2463 return true;
2464
Douglas Gregorce835df2011-09-14 22:14:14 +00002465 return hasMacroDefinition(II, Macro);
2466 }
2467
2468 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002469 if (!II->hasMacroDefinition())
2470 return false;
2471
Douglas Gregorce835df2011-09-14 22:14:14 +00002472 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002473 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002474
Douglas Gregorce835df2011-09-14 22:14:14 +00002475 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002476 }
2477
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002478public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002479 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002480 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002482 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002483 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Douglas Gregoreee242f2011-10-27 09:33:13 +00002485 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2486 IdentifierResolver &IdResolver, bool IsModule)
2487 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002488
2489 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002490 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002491 }
Mike Stump1eb44332009-09-09 15:08:12 +00002492
2493 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002494 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002495 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002496 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002497 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002498 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002499 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002500 if (hasMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002501 DataLen += 8;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002502
2503 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2504 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002505 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002506 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002507 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002508 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002509 // We emit the key length after the data length so that every
2510 // string is preceded by a 16-bit length. This matches the PTH
2511 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002512 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002513 return std::make_pair(KeyLen, DataLen);
2514 }
Mike Stump1eb44332009-09-09 15:08:12 +00002515
Chris Lattner5f9e2722011-07-23 10:55:15 +00002516 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002517 unsigned KeyLen) {
2518 // Record the location of the key data. This is used when generating
2519 // the mapping from persistent IDs to strings.
2520 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002521 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002522 }
Mike Stump1eb44332009-09-09 15:08:12 +00002523
Douglas Gregor7143aab2011-09-01 17:04:32 +00002524 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002525 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002526 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002527 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002528 clang::io::Emit32(Out, ID << 1);
2529 return;
2530 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002531
Douglas Gregora92193e2009-04-28 21:18:29 +00002532 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002533 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002534 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002535 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topper925be542011-12-19 05:04:33 +00002536 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregorce835df2011-09-14 22:14:14 +00002537 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002538 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2539 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002540 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002541 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002542 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002543
Douglas Gregor13292642011-12-02 15:45:10 +00002544 if (HasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002545 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor13292642011-12-02 15:45:10 +00002546 clang::io::Emit32(Out,
2547 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2548 }
2549
Douglas Gregor668c1a42009-04-21 22:25:48 +00002550 // Emit the declaration IDs in reverse order, because the
2551 // IdentifierResolver provides the declarations as they would be
2552 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002553 // "stat"), but the ASTReader adds declarations to the end of the list
2554 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002555 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002556 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2557 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002558 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002559 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002560 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002561 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002562 }
2563};
2564} // end anonymous namespace
2565
Sebastian Redl3397c552010-08-18 23:56:27 +00002566/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002567///
2568/// The identifier table consists of a blob containing string data
2569/// (the actual identifiers themselves) and a separate "offsets" index
2570/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002571void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2572 IdentifierResolver &IdResolver,
2573 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002574 using namespace llvm;
2575
2576 // Create and write out the blob that contains the identifier
2577 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002578 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002579 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002580 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Douglas Gregor92b059e2009-04-28 20:33:11 +00002582 // Look for any identifiers that were named while processing the
2583 // headers, but are otherwise not needed. We add these to the hash
2584 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002585 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002586 // file.
2587 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2588 IDEnd = PP.getIdentifierTable().end();
2589 ID != IDEnd; ++ID)
2590 getIdentifierRef(ID->second);
2591
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002592 // Create the on-disk hash table representation. We only store offsets
2593 // for identifiers that appear here for the first time.
2594 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002595 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002596 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2597 ID != IDEnd; ++ID) {
2598 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002599 if (!Chain || !ID->first->isFromAST() ||
2600 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002601 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2602 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002603 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002604
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002605 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002606 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002607 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002608 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002609 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002610 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002611 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002612 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002613 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002614 }
2615
2616 // Create a blob abbreviation
2617 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002618 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002621 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002622
2623 // Write the identifier table
2624 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002625 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002626 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002627 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002628 }
2629
2630 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002631 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002632 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2636 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2637
2638 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002639 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002640 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002641 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002642 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002643 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002644}
2645
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002646//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002647// DeclContext's Name Lookup Table Serialization
2648//===----------------------------------------------------------------------===//
2649
2650namespace {
2651// Trait used for the on-disk hash table used in the method pool.
2652class ASTDeclContextNameLookupTrait {
2653 ASTWriter &Writer;
2654
2655public:
2656 typedef DeclarationName key_type;
2657 typedef key_type key_type_ref;
2658
2659 typedef DeclContext::lookup_result data_type;
2660 typedef const data_type& data_type_ref;
2661
2662 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2663
2664 unsigned ComputeHash(DeclarationName Name) {
2665 llvm::FoldingSetNodeID ID;
2666 ID.AddInteger(Name.getNameKind());
2667
2668 switch (Name.getNameKind()) {
2669 case DeclarationName::Identifier:
2670 ID.AddString(Name.getAsIdentifierInfo()->getName());
2671 break;
2672 case DeclarationName::ObjCZeroArgSelector:
2673 case DeclarationName::ObjCOneArgSelector:
2674 case DeclarationName::ObjCMultiArgSelector:
2675 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2676 break;
2677 case DeclarationName::CXXConstructorName:
2678 case DeclarationName::CXXDestructorName:
2679 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002680 break;
2681 case DeclarationName::CXXOperatorName:
2682 ID.AddInteger(Name.getCXXOverloadedOperator());
2683 break;
2684 case DeclarationName::CXXLiteralOperatorName:
2685 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2686 case DeclarationName::CXXUsingDirective:
2687 break;
2688 }
2689
2690 return ID.ComputeHash();
2691 }
2692
2693 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002694 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002695 data_type_ref Lookup) {
2696 unsigned KeyLen = 1;
2697 switch (Name.getNameKind()) {
2698 case DeclarationName::Identifier:
2699 case DeclarationName::ObjCZeroArgSelector:
2700 case DeclarationName::ObjCOneArgSelector:
2701 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002702 case DeclarationName::CXXLiteralOperatorName:
2703 KeyLen += 4;
2704 break;
2705 case DeclarationName::CXXOperatorName:
2706 KeyLen += 1;
2707 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002708 case DeclarationName::CXXConstructorName:
2709 case DeclarationName::CXXDestructorName:
2710 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002711 case DeclarationName::CXXUsingDirective:
2712 break;
2713 }
2714 clang::io::Emit16(Out, KeyLen);
2715
2716 // 2 bytes for num of decls and 4 for each DeclID.
2717 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2718 clang::io::Emit16(Out, DataLen);
2719
2720 return std::make_pair(KeyLen, DataLen);
2721 }
2722
Chris Lattner5f9e2722011-07-23 10:55:15 +00002723 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002724 using namespace clang::io;
2725
2726 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2727 Emit8(Out, Name.getNameKind());
2728 switch (Name.getNameKind()) {
2729 case DeclarationName::Identifier:
2730 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2731 break;
2732 case DeclarationName::ObjCZeroArgSelector:
2733 case DeclarationName::ObjCOneArgSelector:
2734 case DeclarationName::ObjCMultiArgSelector:
2735 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2736 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002737 case DeclarationName::CXXOperatorName:
2738 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2739 Emit8(Out, Name.getCXXOverloadedOperator());
2740 break;
2741 case DeclarationName::CXXLiteralOperatorName:
2742 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2743 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002744 case DeclarationName::CXXConstructorName:
2745 case DeclarationName::CXXDestructorName:
2746 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002747 case DeclarationName::CXXUsingDirective:
2748 break;
2749 }
2750 }
2751
Chris Lattner5f9e2722011-07-23 10:55:15 +00002752 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002753 data_type Lookup, unsigned DataLen) {
2754 uint64_t Start = Out.tell(); (void)Start;
2755 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2756 for (; Lookup.first != Lookup.second; ++Lookup.first)
2757 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2758
2759 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2760 }
2761};
2762} // end anonymous namespace
2763
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002764/// \brief Write the block containing all of the declaration IDs
2765/// visible from the given DeclContext.
2766///
2767/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002768/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002769uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2770 DeclContext *DC) {
2771 if (DC->getPrimaryContext() != DC)
2772 return 0;
2773
2774 // Since there is no name lookup into functions or methods, don't bother to
2775 // build a visible-declarations table for these entities.
2776 if (DC->isFunctionOrMethod())
2777 return 0;
2778
2779 // If not in C++, we perform name lookup for the translation unit via the
2780 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2781 // FIXME: In C++ we need the visible declarations in order to "see" the
2782 // friend declarations, is there a way to do this without writing the table ?
2783 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2784 return 0;
2785
2786 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002787 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002788 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002789
2790 // Serialize the contents of the mapping used for lookup. Note that,
2791 // although we have two very different code paths, the serialized
2792 // representation is the same for both cases: a declaration name,
2793 // followed by a size, followed by references to the visible
2794 // declarations that have that name.
2795 uint64_t Offset = Stream.GetCurrentBitNo();
2796 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2797 if (!Map || Map->empty())
2798 return 0;
2799
2800 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2801 ASTDeclContextNameLookupTrait Trait(*this);
2802
2803 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002804 DeclarationName ConversionName;
2805 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002806 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2807 D != DEnd; ++D) {
2808 DeclarationName Name = D->first;
2809 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002810 if (Result.first != Result.second) {
2811 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2812 // Hash all conversion function names to the same name. The actual
2813 // type information in conversion function name is not used in the
2814 // key (since such type information is not stable across different
2815 // modules), so the intended effect is to coalesce all of the conversion
2816 // functions under a single key.
2817 if (!ConversionName)
2818 ConversionName = Name;
2819 ConversionDecls.append(Result.first, Result.second);
2820 continue;
2821 }
2822
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002823 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002824 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002825 }
2826
Douglas Gregore5a54b62011-08-30 20:49:19 +00002827 // Add the conversion functions
2828 if (!ConversionDecls.empty()) {
2829 Generator.insert(ConversionName,
2830 DeclContext::lookup_result(ConversionDecls.begin(),
2831 ConversionDecls.end()),
2832 Trait);
2833 }
2834
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002835 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002836 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002837 uint32_t BucketOffset;
2838 {
2839 llvm::raw_svector_ostream Out(LookupTable);
2840 // Make sure that no bucket is at offset 0
2841 clang::io::Emit32(Out, 0);
2842 BucketOffset = Generator.Emit(Out, Trait);
2843 }
2844
2845 // Write the lookup table
2846 RecordData Record;
2847 Record.push_back(DECL_CONTEXT_VISIBLE);
2848 Record.push_back(BucketOffset);
2849 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2850 LookupTable.str());
2851
2852 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2853 ++NumVisibleDeclContexts;
2854 return Offset;
2855}
2856
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002857/// \brief Write an UPDATE_VISIBLE block for the given context.
2858///
2859/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2860/// DeclContext in a dependent AST file. As such, they only exist for the TU
2861/// (in C++) and for namespaces.
2862void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002863 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2864 if (!Map || Map->empty())
2865 return;
2866
2867 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2868 ASTDeclContextNameLookupTrait Trait(*this);
2869
2870 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002871 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2872 D != DEnd; ++D) {
2873 DeclarationName Name = D->first;
2874 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002875 // For any name that appears in this table, the results are complete, i.e.
2876 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002877 if (Result.first != Result.second)
2878 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002879 }
2880
2881 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002882 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002883 uint32_t BucketOffset;
2884 {
2885 llvm::raw_svector_ostream Out(LookupTable);
2886 // Make sure that no bucket is at offset 0
2887 clang::io::Emit32(Out, 0);
2888 BucketOffset = Generator.Emit(Out, Trait);
2889 }
2890
2891 // Write the lookup table
2892 RecordData Record;
2893 Record.push_back(UPDATE_VISIBLE);
2894 Record.push_back(getDeclID(cast<Decl>(DC)));
2895 Record.push_back(BucketOffset);
2896 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2897}
2898
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002899/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2900void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2901 RecordData Record;
2902 Record.push_back(Opts.fp_contract);
2903 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2904}
2905
2906/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2907void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2908 if (!SemaRef.Context.getLangOptions().OpenCL)
2909 return;
2910
2911 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2912 RecordData Record;
2913#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2914#include "clang/Basic/OpenCLExtensions.def"
2915 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2916}
2917
Douglas Gregor2171bf12012-01-15 16:58:34 +00002918void ASTWriter::WriteRedeclarations() {
2919 RecordData LocalRedeclChains;
2920 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2921
2922 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2923 Decl *First = Redeclarations[I];
2924 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2925
2926 Decl *MostRecent = First->getMostRecentDecl();
2927
2928 // If we only have a single declaration, there is no point in storing
2929 // a redeclaration chain.
2930 if (First == MostRecent)
2931 continue;
2932
2933 unsigned Offset = LocalRedeclChains.size();
2934 unsigned Size = 0;
2935 LocalRedeclChains.push_back(0); // Placeholder for the size.
2936
2937 // Collect the set of local redeclarations of this declaration.
2938 for (Decl *Prev = MostRecent; Prev != First;
2939 Prev = Prev->getPreviousDecl()) {
2940 if (!Prev->isFromASTFile()) {
2941 AddDeclRef(Prev, LocalRedeclChains);
2942 ++Size;
2943 }
2944 }
2945 LocalRedeclChains[Offset] = Size;
2946
2947 // Reverse the set of local redeclarations, so that we store them in
2948 // order (since we found them in reverse order).
2949 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2950
2951 // Add the mapping from the first ID to the set of local declarations.
2952 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2953 LocalRedeclsMap.push_back(Info);
2954
2955 assert(N == Redeclarations.size() &&
2956 "Deserialized a declaration we shouldn't have");
2957 }
2958
2959 if (LocalRedeclChains.empty())
2960 return;
2961
2962 // Sort the local redeclarations map by the first declaration ID,
2963 // since the reader will be performing binary searches on this information.
2964 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2965
2966 // Emit the local redeclarations map.
2967 using namespace llvm;
2968 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2969 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
2971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2972 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
2973
2974 RecordData Record;
2975 Record.push_back(LOCAL_REDECLARATIONS_MAP);
2976 Record.push_back(LocalRedeclsMap.size());
2977 Stream.EmitRecordWithBlob(AbbrevID, Record,
2978 reinterpret_cast<char*>(LocalRedeclsMap.data()),
2979 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
2980
2981 // Emit the redeclaration chains.
2982 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
2983}
2984
Douglas Gregorcff9f262012-01-27 01:47:08 +00002985void ASTWriter::WriteObjCCategories() {
2986 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
2987 RecordData Categories;
2988
2989 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
2990 unsigned Size = 0;
2991 unsigned StartIndex = Categories.size();
2992
2993 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
2994
2995 // Allocate space for the size.
2996 Categories.push_back(0);
2997
2998 // Add the categories.
2999 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3000 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3001 assert(getDeclID(Cat) != 0 && "Bogus category");
3002 AddDeclRef(Cat, Categories);
3003 }
3004
3005 // Update the size.
3006 Categories[StartIndex] = Size;
3007
3008 // Record this interface -> category map.
3009 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3010 CategoriesMap.push_back(CatInfo);
3011 }
3012
3013 // Sort the categories map by the definition ID, since the reader will be
3014 // performing binary searches on this information.
3015 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3016
3017 // Emit the categories map.
3018 using namespace llvm;
3019 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3020 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3023 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3024
3025 RecordData Record;
3026 Record.push_back(OBJC_CATEGORIES_MAP);
3027 Record.push_back(CategoriesMap.size());
3028 Stream.EmitRecordWithBlob(AbbrevID, Record,
3029 reinterpret_cast<char*>(CategoriesMap.data()),
3030 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3031
3032 // Emit the category lists.
3033 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3034}
3035
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003036void ASTWriter::WriteMergedDecls() {
3037 if (!Chain || Chain->MergedDecls.empty())
3038 return;
3039
3040 RecordData Record;
3041 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3042 IEnd = Chain->MergedDecls.end();
3043 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003044 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003045 : getDeclID(I->first);
3046 assert(CanonID && "Merged declaration not known?");
3047
3048 Record.push_back(CanonID);
3049 Record.push_back(I->second.size());
3050 Record.append(I->second.begin(), I->second.end());
3051 }
3052 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3053}
3054
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003055//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003056// General Serialization Routines
3057//===----------------------------------------------------------------------===//
3058
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003059/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003060void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003061 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00003062 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
3063 const Attr * A = *i;
3064 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003065 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003066
Sean Huntcf807c42010-08-18 23:23:40 +00003067#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003068
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003069 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003070}
3071
Chris Lattner5f9e2722011-07-23 10:55:15 +00003072void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003073 Record.push_back(Str.size());
3074 Record.insert(Record.end(), Str.begin(), Str.end());
3075}
3076
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003077void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3078 RecordDataImpl &Record) {
3079 Record.push_back(Version.getMajor());
3080 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3081 Record.push_back(*Minor + 1);
3082 else
3083 Record.push_back(0);
3084 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3085 Record.push_back(*Subminor + 1);
3086 else
3087 Record.push_back(0);
3088}
3089
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003090/// \brief Note that the identifier II occurs at the given offset
3091/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003092void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003093 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003094 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003095 // up earlier in the chain and thus don't need an offset.
3096 if (ID >= FirstIdentID)
3097 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003098}
3099
Douglas Gregor83941df2009-04-25 17:48:32 +00003100/// \brief Note that the selector Sel occurs at the given offset
3101/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003102void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003103 unsigned ID = SelectorIDs[Sel];
3104 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003105 // Don't record offsets for selectors that are also available in a different
3106 // file.
3107 if (ID < FirstSelectorID)
3108 return;
3109 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003110}
3111
Sebastian Redla4232eb2010-08-18 23:56:21 +00003112ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003113 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
3114 WritingAST(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003115 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003116 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003117 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003118 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3119 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003120 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003121 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003122 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003123 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003124 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003125 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003126 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3127 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3128 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003129 DeclTypedefAbbrev(0),
3130 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3131 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003132{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003133}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003134
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003135ASTWriter::~ASTWriter() {
3136 for (FileDeclIDsTy::iterator
3137 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3138 delete I->second;
3139}
3140
Sebastian Redla4232eb2010-08-18 23:56:21 +00003141void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003142 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003143 Module *WritingModule, StringRef isysroot) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003144 WritingAST = true;
3145
Douglas Gregor2cf26342009-04-09 22:27:44 +00003146 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003147 Stream.Emit((unsigned)'C', 8);
3148 Stream.Emit((unsigned)'P', 8);
3149 Stream.Emit((unsigned)'C', 8);
3150 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003151
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003152 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003153
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003154 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003155 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003156 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003157 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003158 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003159 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003160 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003161
3162 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003163}
3164
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003165template<typename Vector>
3166static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3167 ASTWriter::RecordData &Record) {
3168 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3169 I != E; ++I) {
3170 Writer.AddDeclRef(*I, Record);
3171 }
3172}
3173
Sebastian Redla4232eb2010-08-18 23:56:21 +00003174void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003175 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003176 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003177 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003178 using namespace llvm;
3179
Douglas Gregorecc2c092011-12-01 22:20:10 +00003180 // Make sure that the AST reader knows to finalize itself.
3181 if (Chain)
3182 Chain->finalizeForWriting();
3183
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003184 ASTContext &Context = SemaRef.Context;
3185 Preprocessor &PP = SemaRef.PP;
3186
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003187 // Set up predefined declaration IDs.
3188 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003189 if (Context.ObjCIdDecl)
3190 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003191 if (Context.ObjCSelDecl)
3192 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003193 if (Context.ObjCClassDecl)
3194 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003195 if (Context.ObjCProtocolClassDecl)
3196 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003197 if (Context.Int128Decl)
3198 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3199 if (Context.UInt128Decl)
3200 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003201 if (Context.ObjCInstanceTypeDecl)
3202 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003203
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003204 if (!Chain) {
3205 // Make sure that we emit IdentifierInfos (and any attached
3206 // declarations) for builtins. We don't need to do this when we're
3207 // emitting chained PCH files, because all of the builtins will be
3208 // in the original PCH file.
3209 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003210 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003211 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003212 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
3213 Context.getLangOptions().NoBuiltin);
3214 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3215 getIdentifierRef(&Table.get(BuiltinNames[I]));
3216 }
3217
Douglas Gregoreee242f2011-10-27 09:33:13 +00003218 // If there are any out-of-date identifiers, bring them up to date.
3219 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3220 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3221 IDEnd = PP.getIdentifierTable().end();
3222 ID != IDEnd; ++ID)
3223 if (ID->second->isOutOfDate())
3224 ExtSource->updateOutOfDateIdentifier(*ID->second);
3225 }
3226
Chris Lattner63d65f82009-09-08 18:19:27 +00003227 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003228 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003229 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003230 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003231 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003232
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003233 // Build a record containing all of the file scoped decls in this file.
3234 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003235 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3236 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003237
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003238 // Build a record containing all of the delegating constructors we still need
3239 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003240 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003241 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003242
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003243 // Write the set of weak, undeclared identifiers. We always write the
3244 // entire table, since later PCH files in a PCH chain are only interested in
3245 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003246 RecordData WeakUndeclaredIdentifiers;
3247 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003248 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003249 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3250 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3251 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3252 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3253 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3254 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3255 }
3256 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003257
Douglas Gregor14c22f22009-04-22 22:18:58 +00003258 // Build a record containing all of the locally-scoped external
3259 // declarations in this header file. Generally, this record will be
3260 // empty.
3261 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003262 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003263 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003264 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003265 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3266 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003267 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003268 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003269 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3270 }
3271
Douglas Gregorb81c1702009-04-27 20:06:05 +00003272 // Build a record containing all of the ext_vector declarations.
3273 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003274 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003275
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003276 // Build a record containing all of the VTable uses information.
3277 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003278 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003279 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3280 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3281 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3282 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3283 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003284 }
3285
3286 // Build a record containing all of dynamic classes declarations.
3287 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003288 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003289
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003290 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003291 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003292 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003293 I = SemaRef.PendingInstantiations.begin(),
3294 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3295 AddDeclRef(I->first, PendingInstantiations);
3296 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003297 }
3298 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3299 "There are local ones at end of translation unit!");
3300
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003301 // Build a record containing some declaration references.
3302 RecordData SemaDeclRefs;
3303 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3304 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3305 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3306 }
3307
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003308 RecordData CUDASpecialDeclRefs;
3309 if (Context.getcudaConfigureCallDecl()) {
3310 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3311 }
3312
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003313 // Build a record containing all of the known namespaces.
3314 RecordData KnownNamespaces;
3315 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3316 I = SemaRef.KnownNamespaces.begin(),
3317 IEnd = SemaRef.KnownNamespaces.end();
3318 I != IEnd; ++I) {
3319 if (!I->second)
3320 AddDeclRef(I->first, KnownNamespaces);
3321 }
3322
Sebastian Redl3397c552010-08-18 23:56:27 +00003323 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003324 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003325 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003326 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003327 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00003328 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003329 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003330
3331 // Create a lexical update block containing all of the declarations in the
3332 // translation unit that do not come from other AST files.
3333 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3334 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3335 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3336 E = TU->noload_decls_end();
3337 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003338 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003339 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003340 }
3341
3342 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3343 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3344 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3345 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3346 Record.clear();
3347 Record.push_back(TU_UPDATE_LEXICAL);
3348 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3349 data(NewGlobalDecls));
3350
3351 // And a visible updates block for the translation unit.
3352 Abv = new llvm::BitCodeAbbrev();
3353 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3354 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3355 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3356 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3357 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3358 WriteDeclContextVisibleUpdate(TU);
3359
3360 // If the translation unit has an anonymous namespace, and we don't already
3361 // have an update block for it, write it as an update block.
3362 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3363 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3364 if (Record.empty()) {
3365 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003366 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003367 }
3368 }
3369
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003370 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003371 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003372
Douglas Gregora119da02011-08-02 16:26:37 +00003373 // Form the record of special types.
3374 RecordData SpecialTypes;
3375 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003376 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003377 AddTypeRef(Context.getFILEType(), SpecialTypes);
3378 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3379 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3380 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3381 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003382 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003383 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003384
Douglas Gregor366809a2009-04-26 03:49:13 +00003385 // Keep writing types and declarations until all types and
3386 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003387 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003388 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003389 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3390 E = DeclsToRewrite.end();
3391 I != E; ++I)
3392 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003393 while (!DeclTypesToEmit.empty()) {
3394 DeclOrType DOT = DeclTypesToEmit.front();
3395 DeclTypesToEmit.pop();
3396 if (DOT.isType())
3397 WriteType(DOT.getType());
3398 else
3399 WriteDecl(Context, DOT.getDecl());
3400 }
3401 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003402
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003403 WriteFileDeclIDsMap();
3404 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3405
3406 if (Chain) {
3407 // Write the mapping information describing our module dependencies and how
3408 // each of those modules were mapped into our own offset/ID space, so that
3409 // the reader can build the appropriate mapping to its own offset/ID space.
3410 // The map consists solely of a blob with the following format:
3411 // *(module-name-len:i16 module-name:len*i8
3412 // source-location-offset:i32
3413 // identifier-id:i32
3414 // preprocessed-entity-id:i32
3415 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003416 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003417 // selector-id:i32
3418 // declaration-id:i32
3419 // c++-base-specifiers-id:i32
3420 // type-id:i32)
3421 //
3422 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3423 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3425 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003426 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003427 {
3428 llvm::raw_svector_ostream Out(Buffer);
3429 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003430 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003431 M != MEnd; ++M) {
3432 StringRef FileName = (*M)->FileName;
3433 io::Emit16(Out, FileName.size());
3434 Out.write(FileName.data(), FileName.size());
3435 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3436 io::Emit32(Out, (*M)->BaseIdentifierID);
3437 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003438 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003439 io::Emit32(Out, (*M)->BaseSelectorID);
3440 io::Emit32(Out, (*M)->BaseDeclID);
3441 io::Emit32(Out, (*M)->BaseTypeIndex);
3442 }
3443 }
3444 Record.clear();
3445 Record.push_back(MODULE_OFFSET_MAP);
3446 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3447 Buffer.data(), Buffer.size());
3448 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003449 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003450 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003451 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003452 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003453 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003454 WriteFPPragmaOptions(SemaRef.getFPOptions());
3455 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003456
Sebastian Redl1476ed42010-07-16 16:36:56 +00003457 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003458 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003459
Anders Carlssonc8505782011-03-06 18:41:18 +00003460 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003461
Douglas Gregore209e502011-12-06 01:10:29 +00003462 // If we're emitting a module, write out the submodule information.
3463 if (WritingModule)
3464 WriteSubmodules(WritingModule);
3465
Douglas Gregora119da02011-08-02 16:26:37 +00003466 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3467
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003468 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003469 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003470 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003471
3472 // Write the record containing tentative definitions.
3473 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003474 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003475
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003476 // Write the record containing unused file scoped decls.
3477 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003478 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003479
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003480 // Write the record containing weak undeclared identifiers.
3481 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003482 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003483 WeakUndeclaredIdentifiers);
3484
Douglas Gregor14c22f22009-04-22 22:18:58 +00003485 // Write the record containing locally-scoped external definitions.
3486 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003487 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003488 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003489
3490 // Write the record containing ext_vector type names.
3491 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003492 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003493
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003494 // Write the record containing VTable uses information.
3495 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003496 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003497
3498 // Write the record containing dynamic classes declarations.
3499 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003500 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003501
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003502 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003503 if (!PendingInstantiations.empty())
3504 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003505
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003506 // Write the record containing declaration references of Sema.
3507 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003508 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003509
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003510 // Write the record containing CUDA-specific declaration references.
3511 if (!CUDASpecialDeclRefs.empty())
3512 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003513
3514 // Write the delegating constructors.
3515 if (!DelegatingCtorDecls.empty())
3516 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003517
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003518 // Write the known namespaces.
3519 if (!KnownNamespaces.empty())
3520 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3521
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003522 // Write the visible updates to DeclContexts.
3523 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3524 I = UpdatedDeclContexts.begin(),
3525 E = UpdatedDeclContexts.end();
3526 I != E; ++I)
3527 WriteDeclContextVisibleUpdate(*I);
3528
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003529 if (!WritingModule) {
3530 // Write the submodules that were imported, if any.
3531 RecordData ImportedModules;
3532 for (ASTContext::import_iterator I = Context.local_import_begin(),
3533 IEnd = Context.local_import_end();
3534 I != IEnd; ++I) {
3535 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3536 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3537 }
3538 if (!ImportedModules.empty()) {
3539 // Sort module IDs.
3540 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3541
3542 // Unique module IDs.
3543 ImportedModules.erase(std::unique(ImportedModules.begin(),
3544 ImportedModules.end()),
3545 ImportedModules.end());
3546
3547 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3548 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003549 }
3550
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003551 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003552 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003553 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003554 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003555 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003556
Douglas Gregor3e1af842009-04-17 22:13:46 +00003557 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003558 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003559 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003560 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003561 Record.push_back(NumLexicalDeclContexts);
3562 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003563 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003564 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003565}
3566
Douglas Gregor61c5e342011-09-17 00:05:03 +00003567/// \brief Go through the declaration update blocks and resolve declaration
3568/// pointers into declaration IDs.
3569void ASTWriter::ResolveDeclUpdatesBlocks() {
3570 for (DeclUpdateMap::iterator
3571 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3572 const Decl *D = I->first;
3573 UpdateRecord &URec = I->second;
3574
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003575 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003576 continue; // The decl will be written completely
3577
3578 unsigned Idx = 0, N = URec.size();
3579 while (Idx < N) {
3580 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003581 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3582 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3583 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3584 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3585 ++Idx;
3586 break;
3587
3588 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3589 ++Idx;
3590 break;
3591 }
3592 }
3593 }
3594}
3595
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003596void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003597 if (DeclUpdates.empty())
3598 return;
3599
3600 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003601 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003602 for (DeclUpdateMap::iterator
3603 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3604 const Decl *D = I->first;
3605 UpdateRecord &URec = I->second;
3606
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003607 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003608 continue; // The decl will be written completely,no need to store updates.
3609
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003610 uint64_t Offset = Stream.GetCurrentBitNo();
3611 Stream.EmitRecord(DECL_UPDATES, URec);
3612
3613 OffsetsRecord.push_back(GetDeclRef(D));
3614 OffsetsRecord.push_back(Offset);
3615 }
3616 Stream.ExitBlock();
3617 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3618}
3619
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003620void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003621 if (ReplacedDecls.empty())
3622 return;
3623
3624 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003625 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003626 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003627 Record.push_back(I->ID);
3628 Record.push_back(I->Offset);
3629 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003630 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003631 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003632}
3633
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003634void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003635 Record.push_back(Loc.getRawEncoding());
3636}
3637
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003638void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003639 AddSourceLocation(Range.getBegin(), Record);
3640 AddSourceLocation(Range.getEnd(), Record);
3641}
3642
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003643void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003644 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003645 const uint64_t *Words = Value.getRawData();
3646 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003647}
3648
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003649void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003650 Record.push_back(Value.isUnsigned());
3651 AddAPInt(Value, Record);
3652}
3653
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003654void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003655 AddAPInt(Value.bitcastToAPInt(), Record);
3656}
3657
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003658void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003659 Record.push_back(getIdentifierRef(II));
3660}
3661
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003662IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003663 if (II == 0)
3664 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003665
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003666 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003667 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003668 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003669 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003670}
3671
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003672void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003673 Record.push_back(getSelectorRef(SelRef));
3674}
3675
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003676SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003677 if (Sel.getAsOpaquePtr() == 0) {
3678 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003679 }
3680
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003681 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003682 if (SID == 0 && Chain) {
3683 // This might trigger a ReadSelector callback, which will set the ID for
3684 // this selector.
3685 Chain->LoadSelector(Sel);
3686 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003687 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003688 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003689 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003690 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003691}
3692
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003693void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003694 AddDeclRef(Temp->getDestructor(), Record);
3695}
3696
Douglas Gregor7c789c12010-10-29 22:39:52 +00003697void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3698 CXXBaseSpecifier const *BasesEnd,
3699 RecordDataImpl &Record) {
3700 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3701 CXXBaseSpecifiersToWrite.push_back(
3702 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3703 Bases, BasesEnd));
3704 Record.push_back(NextCXXBaseSpecifiersID++);
3705}
3706
Sebastian Redla4232eb2010-08-18 23:56:21 +00003707void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003708 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003709 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003710 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003711 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003712 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003713 break;
3714 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003715 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003716 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003717 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003718 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003719 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003720 break;
3721 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003722 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003723 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003724 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003725 break;
John McCall833ca992009-10-29 08:12:44 +00003726 case TemplateArgument::Null:
3727 case TemplateArgument::Integral:
3728 case TemplateArgument::Declaration:
3729 case TemplateArgument::Pack:
3730 break;
3731 }
3732}
3733
Sebastian Redla4232eb2010-08-18 23:56:21 +00003734void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003735 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003736 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003737
3738 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3739 bool InfoHasSameExpr
3740 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3741 Record.push_back(InfoHasSameExpr);
3742 if (InfoHasSameExpr)
3743 return; // Avoid storing the same expr twice.
3744 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003745 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3746 Record);
3747}
3748
Douglas Gregordc355712011-02-25 00:36:19 +00003749void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3750 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003751 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003752 AddTypeRef(QualType(), Record);
3753 return;
3754 }
3755
Douglas Gregordc355712011-02-25 00:36:19 +00003756 AddTypeLoc(TInfo->getTypeLoc(), Record);
3757}
3758
3759void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3760 AddTypeRef(TL.getType(), Record);
3761
John McCalla1ee0c52009-10-16 21:56:05 +00003762 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003763 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003764 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003765}
3766
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003767void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003768 Record.push_back(GetOrCreateTypeID(T));
3769}
3770
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003771TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3772 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003773 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3774}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003775
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003776TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003777 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003778 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003779}
3780
3781TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3782 if (T.isNull())
3783 return TypeIdx();
3784 assert(!T.getLocalFastQualifiers());
3785
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003786 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003787 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003788 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003789 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003790 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003791 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003792 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003793 return Idx;
3794}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003795
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003796TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003797 if (T.isNull())
3798 return TypeIdx();
3799 assert(!T.getLocalFastQualifiers());
3800
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003801 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3802 assert(I != TypeIdxs.end() && "Type not emitted!");
3803 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003804}
3805
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003806void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003807 Record.push_back(GetDeclRef(D));
3808}
3809
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003810DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003811 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3812
Douglas Gregor2cf26342009-04-09 22:27:44 +00003813 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003814 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003815 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003816
3817 // If D comes from an AST file, its declaration ID is already known and
3818 // fixed.
3819 if (D->isFromASTFile())
3820 return D->getGlobalID();
3821
Douglas Gregor97475832010-10-05 18:37:06 +00003822 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003823 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003824 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003825 // We haven't seen this declaration before. Give it a new ID and
3826 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003827 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003828 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003829 }
3830
Sebastian Redl681d7232010-07-27 00:17:23 +00003831 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003832}
3833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003834DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003835 if (D == 0)
3836 return 0;
3837
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003838 // If D comes from an AST file, its declaration ID is already known and
3839 // fixed.
3840 if (D->isFromASTFile())
3841 return D->getGlobalID();
3842
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003843 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3844 return DeclIDs[D];
3845}
3846
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003847static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3848 std::pair<unsigned, serialization::DeclID> R) {
3849 return L.first < R.first;
3850}
3851
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003852void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003853 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003854 assert(D);
3855
3856 SourceLocation Loc = D->getLocation();
3857 if (Loc.isInvalid())
3858 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003859
3860 // We only keep track of the file-level declarations of each file.
3861 if (!D->getLexicalDeclContext()->isFileContext())
3862 return;
3863
3864 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003865 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003866 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003867 FileID FID;
3868 unsigned Offset;
3869 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003870 if (FID.isInvalid())
3871 return;
3872 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3873 assert(Entry->isFile());
3874
3875 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3876 if (!Info)
3877 Info = new DeclIDInFileInfo();
3878
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003879 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003880 LocDeclIDsTy &Decls = Info->DeclIDs;
3881
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003882 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003883 Decls.push_back(LocDecl);
3884 return;
3885 }
3886
3887 LocDeclIDsTy::iterator
3888 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3889
3890 Decls.insert(I, LocDecl);
3891}
3892
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003893void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003894 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003895 Record.push_back(Name.getNameKind());
3896 switch (Name.getNameKind()) {
3897 case DeclarationName::Identifier:
3898 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3899 break;
3900
3901 case DeclarationName::ObjCZeroArgSelector:
3902 case DeclarationName::ObjCOneArgSelector:
3903 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003904 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003905 break;
3906
3907 case DeclarationName::CXXConstructorName:
3908 case DeclarationName::CXXDestructorName:
3909 case DeclarationName::CXXConversionFunctionName:
3910 AddTypeRef(Name.getCXXNameType(), Record);
3911 break;
3912
3913 case DeclarationName::CXXOperatorName:
3914 Record.push_back(Name.getCXXOverloadedOperator());
3915 break;
3916
Sean Hunt3e518bd2009-11-29 07:34:05 +00003917 case DeclarationName::CXXLiteralOperatorName:
3918 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3919 break;
3920
Douglas Gregor2cf26342009-04-09 22:27:44 +00003921 case DeclarationName::CXXUsingDirective:
3922 // No extra data to emit
3923 break;
3924 }
3925}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003926
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003927void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003928 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003929 switch (Name.getNameKind()) {
3930 case DeclarationName::CXXConstructorName:
3931 case DeclarationName::CXXDestructorName:
3932 case DeclarationName::CXXConversionFunctionName:
3933 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3934 break;
3935
3936 case DeclarationName::CXXOperatorName:
3937 AddSourceLocation(
3938 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3939 Record);
3940 AddSourceLocation(
3941 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3942 Record);
3943 break;
3944
3945 case DeclarationName::CXXLiteralOperatorName:
3946 AddSourceLocation(
3947 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3948 Record);
3949 break;
3950
3951 case DeclarationName::Identifier:
3952 case DeclarationName::ObjCZeroArgSelector:
3953 case DeclarationName::ObjCOneArgSelector:
3954 case DeclarationName::ObjCMultiArgSelector:
3955 case DeclarationName::CXXUsingDirective:
3956 break;
3957 }
3958}
3959
3960void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003961 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003962 AddDeclarationName(NameInfo.getName(), Record);
3963 AddSourceLocation(NameInfo.getLoc(), Record);
3964 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3965}
3966
3967void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003968 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003969 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003970 Record.push_back(Info.NumTemplParamLists);
3971 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3972 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3973}
3974
Sebastian Redla4232eb2010-08-18 23:56:21 +00003975void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003976 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003977 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003978 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003979 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003980
3981 // Push each of the NNS's onto a stack for serialization in reverse order.
3982 while (NNS) {
3983 NestedNames.push_back(NNS);
3984 NNS = NNS->getPrefix();
3985 }
3986
3987 Record.push_back(NestedNames.size());
3988 while(!NestedNames.empty()) {
3989 NNS = NestedNames.pop_back_val();
3990 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3991 Record.push_back(Kind);
3992 switch (Kind) {
3993 case NestedNameSpecifier::Identifier:
3994 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3995 break;
3996
3997 case NestedNameSpecifier::Namespace:
3998 AddDeclRef(NNS->getAsNamespace(), Record);
3999 break;
4000
Douglas Gregor14aba762011-02-24 02:36:08 +00004001 case NestedNameSpecifier::NamespaceAlias:
4002 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4003 break;
4004
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004005 case NestedNameSpecifier::TypeSpec:
4006 case NestedNameSpecifier::TypeSpecWithTemplate:
4007 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4008 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4009 break;
4010
4011 case NestedNameSpecifier::Global:
4012 // Don't need to write an associated value.
4013 break;
4014 }
4015 }
4016}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004017
Douglas Gregordc355712011-02-25 00:36:19 +00004018void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4019 RecordDataImpl &Record) {
4020 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004021 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004022 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004023
4024 // Push each of the nested-name-specifiers's onto a stack for
4025 // serialization in reverse order.
4026 while (NNS) {
4027 NestedNames.push_back(NNS);
4028 NNS = NNS.getPrefix();
4029 }
4030
4031 Record.push_back(NestedNames.size());
4032 while(!NestedNames.empty()) {
4033 NNS = NestedNames.pop_back_val();
4034 NestedNameSpecifier::SpecifierKind Kind
4035 = NNS.getNestedNameSpecifier()->getKind();
4036 Record.push_back(Kind);
4037 switch (Kind) {
4038 case NestedNameSpecifier::Identifier:
4039 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4040 AddSourceRange(NNS.getLocalSourceRange(), Record);
4041 break;
4042
4043 case NestedNameSpecifier::Namespace:
4044 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4045 AddSourceRange(NNS.getLocalSourceRange(), Record);
4046 break;
4047
4048 case NestedNameSpecifier::NamespaceAlias:
4049 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4050 AddSourceRange(NNS.getLocalSourceRange(), Record);
4051 break;
4052
4053 case NestedNameSpecifier::TypeSpec:
4054 case NestedNameSpecifier::TypeSpecWithTemplate:
4055 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4056 AddTypeLoc(NNS.getTypeLoc(), Record);
4057 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4058 break;
4059
4060 case NestedNameSpecifier::Global:
4061 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4062 break;
4063 }
4064 }
4065}
4066
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004067void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004068 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004069 Record.push_back(Kind);
4070 switch (Kind) {
4071 case TemplateName::Template:
4072 AddDeclRef(Name.getAsTemplateDecl(), Record);
4073 break;
4074
4075 case TemplateName::OverloadedTemplate: {
4076 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4077 Record.push_back(OvT->size());
4078 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4079 I != E; ++I)
4080 AddDeclRef(*I, Record);
4081 break;
4082 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004083
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004084 case TemplateName::QualifiedTemplate: {
4085 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4086 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4087 Record.push_back(QualT->hasTemplateKeyword());
4088 AddDeclRef(QualT->getTemplateDecl(), Record);
4089 break;
4090 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004091
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004092 case TemplateName::DependentTemplate: {
4093 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4094 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4095 Record.push_back(DepT->isIdentifier());
4096 if (DepT->isIdentifier())
4097 AddIdentifierRef(DepT->getIdentifier(), Record);
4098 else
4099 Record.push_back(DepT->getOperator());
4100 break;
4101 }
John McCall14606042011-06-30 08:33:18 +00004102
4103 case TemplateName::SubstTemplateTemplateParm: {
4104 SubstTemplateTemplateParmStorage *subst
4105 = Name.getAsSubstTemplateTemplateParm();
4106 AddDeclRef(subst->getParameter(), Record);
4107 AddTemplateName(subst->getReplacement(), Record);
4108 break;
4109 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004110
4111 case TemplateName::SubstTemplateTemplateParmPack: {
4112 SubstTemplateTemplateParmPackStorage *SubstPack
4113 = Name.getAsSubstTemplateTemplateParmPack();
4114 AddDeclRef(SubstPack->getParameterPack(), Record);
4115 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4116 break;
4117 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004118 }
4119}
4120
Michael J. Spencer20249a12010-10-21 03:16:25 +00004121void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004122 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004123 Record.push_back(Arg.getKind());
4124 switch (Arg.getKind()) {
4125 case TemplateArgument::Null:
4126 break;
4127 case TemplateArgument::Type:
4128 AddTypeRef(Arg.getAsType(), Record);
4129 break;
4130 case TemplateArgument::Declaration:
4131 AddDeclRef(Arg.getAsDecl(), Record);
4132 break;
4133 case TemplateArgument::Integral:
4134 AddAPSInt(*Arg.getAsIntegral(), Record);
4135 AddTypeRef(Arg.getIntegralType(), Record);
4136 break;
4137 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004138 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4139 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004140 case TemplateArgument::TemplateExpansion:
4141 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004142 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4143 Record.push_back(*NumExpansions + 1);
4144 else
4145 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004146 break;
4147 case TemplateArgument::Expression:
4148 AddStmt(Arg.getAsExpr());
4149 break;
4150 case TemplateArgument::Pack:
4151 Record.push_back(Arg.pack_size());
4152 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4153 I != E; ++I)
4154 AddTemplateArgument(*I, Record);
4155 break;
4156 }
4157}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004158
4159void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004160ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004161 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004162 assert(TemplateParams && "No TemplateParams!");
4163 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4164 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4165 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4166 Record.push_back(TemplateParams->size());
4167 for (TemplateParameterList::const_iterator
4168 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4169 P != PEnd; ++P)
4170 AddDeclRef(*P, Record);
4171}
4172
4173/// \brief Emit a template argument list.
4174void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004175ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004176 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004177 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004178 Record.push_back(TemplateArgs->size());
4179 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004180 AddTemplateArgument(TemplateArgs->get(i), Record);
4181}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004182
4183
4184void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004185ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004186 Record.push_back(Set.size());
4187 for (UnresolvedSetImpl::const_iterator
4188 I = Set.begin(), E = Set.end(); I != E; ++I) {
4189 AddDeclRef(I.getDecl(), Record);
4190 Record.push_back(I.getAccess());
4191 }
4192}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004193
Sebastian Redla4232eb2010-08-18 23:56:21 +00004194void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004195 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004196 Record.push_back(Base.isVirtual());
4197 Record.push_back(Base.isBaseOfClass());
4198 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004199 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004200 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004201 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004202 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4203 : SourceLocation(),
4204 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004205}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004206
Douglas Gregor7c789c12010-10-29 22:39:52 +00004207void ASTWriter::FlushCXXBaseSpecifiers() {
4208 RecordData Record;
4209 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4210 Record.clear();
4211
4212 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004213 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004214 if (Index == CXXBaseSpecifiersOffsets.size())
4215 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4216 else {
4217 if (Index > CXXBaseSpecifiersOffsets.size())
4218 CXXBaseSpecifiersOffsets.resize(Index + 1);
4219 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4220 }
4221
4222 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4223 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4224 Record.push_back(BEnd - B);
4225 for (; B != BEnd; ++B)
4226 AddCXXBaseSpecifier(*B, Record);
4227 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004228
4229 // Flush any expressions that were written as part of the base specifiers.
4230 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004231 }
4232
4233 CXXBaseSpecifiersToWrite.clear();
4234}
4235
Sean Huntcbb67482011-01-08 20:30:50 +00004236void ASTWriter::AddCXXCtorInitializers(
4237 const CXXCtorInitializer * const *CtorInitializers,
4238 unsigned NumCtorInitializers,
4239 RecordDataImpl &Record) {
4240 Record.push_back(NumCtorInitializers);
4241 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4242 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004243
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004244 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004245 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004246 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004247 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004248 } else if (Init->isDelegatingInitializer()) {
4249 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004250 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004251 } else if (Init->isMemberInitializer()){
4252 Record.push_back(CTOR_INITIALIZER_MEMBER);
4253 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004254 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004255 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4256 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004257 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004258
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004259 AddSourceLocation(Init->getMemberLocation(), Record);
4260 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004261 AddSourceLocation(Init->getLParenLoc(), Record);
4262 AddSourceLocation(Init->getRParenLoc(), Record);
4263 Record.push_back(Init->isWritten());
4264 if (Init->isWritten()) {
4265 Record.push_back(Init->getSourceOrder());
4266 } else {
4267 Record.push_back(Init->getNumArrayIndices());
4268 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4269 AddDeclRef(Init->getArrayIndex(i), Record);
4270 }
4271 }
4272}
4273
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004274void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4275 assert(D->DefinitionData);
4276 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004277 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004278 Record.push_back(Data.UserDeclaredConstructor);
4279 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004280 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004281 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004282 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004283 Record.push_back(Data.UserDeclaredDestructor);
4284 Record.push_back(Data.Aggregate);
4285 Record.push_back(Data.PlainOldData);
4286 Record.push_back(Data.Empty);
4287 Record.push_back(Data.Polymorphic);
4288 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004289 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004290 Record.push_back(Data.HasNoNonEmptyBases);
4291 Record.push_back(Data.HasPrivateFields);
4292 Record.push_back(Data.HasProtectedFields);
4293 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004294 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00004295 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004296 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004297 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004298 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004299 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004300 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004301 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004302 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004303 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004304 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004305 Record.push_back(Data.DeclaredDefaultConstructor);
4306 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004307 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004308 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004309 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004310 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004311 Record.push_back(Data.FailedImplicitMoveConstructor);
4312 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004313
4314 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004315 if (Data.NumBases > 0)
4316 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4317 Record);
4318
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004319 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4320 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004321 if (Data.NumVBases > 0)
4322 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4323 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004324
4325 AddUnresolvedSet(Data.Conversions, Record);
4326 AddUnresolvedSet(Data.VisibleConversions, Record);
4327 // Data.Definition is the owning decl, no need to write it.
4328 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004329
4330 // Add lambda-specific data.
4331 if (Data.IsLambda) {
4332 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
4333 Record.push_back(Lambda.NumCaptures);
4334 Record.push_back(Lambda.NumExplicitCaptures);
4335 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4336 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4337 AddSourceLocation(Capture.getLocation(), Record);
4338 Record.push_back(Capture.isImplicit());
4339 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4340 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4341 AddDeclRef(Var, Record);
4342 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4343 : SourceLocation(),
4344 Record);
4345 }
4346 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004347}
4348
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004349void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004350 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004351 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004352 assert(FirstDeclID == NextDeclID &&
4353 FirstTypeID == NextTypeID &&
4354 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004355 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004356 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004357 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004358
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004359 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004360
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004361 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4362 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4363 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004364 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004365 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004366 NextDeclID = FirstDeclID;
4367 NextTypeID = FirstTypeID;
4368 NextIdentID = FirstIdentID;
4369 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004370 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004371}
4372
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004373void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004374 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004375 if (II->hasMacroDefinition())
4376 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004377}
4378
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004379void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004380 // Always take the highest-numbered type index. This copes with an interesting
4381 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004382 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004383 // keep the higher-numbered entry so that we can properly write it out to
4384 // the AST file.
4385 TypeIdx &StoredIdx = TypeIdxs[T];
4386 if (Idx.getIndex() >= StoredIdx.getIndex())
4387 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004388}
4389
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004390void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004391 SelectorIDs[S] = ID;
4392}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004393
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004394void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004395 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004396 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004397 MacroDefinitions[MD] = ID;
4398}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004399
Douglas Gregor1d4c1132011-12-20 22:06:13 +00004400void ASTWriter::MacroVisible(IdentifierInfo *II) {
4401 DeserializedMacroNames.push_back(II);
4402}
4403
Douglas Gregora015cab2011-12-02 17:30:13 +00004404void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4405 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4406 SubmoduleIDs[Mod] = ID;
4407}
4408
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004409void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004410 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004411 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004412 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4413 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004414 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004415 // A forward reference was mutated into a definition. Rewrite it.
4416 // FIXME: This happens during template instantiation, should we
4417 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004418 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004419 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004420 }
4421}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004422void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004423 assert(!WritingAST && "Already writing the AST!");
4424
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004425 // TU and namespaces are handled elsewhere.
4426 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4427 return;
4428
Douglas Gregor919814d2011-09-09 23:01:35 +00004429 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004430 return; // Not a source decl added to a DeclContext from PCH.
4431
4432 AddUpdatedDeclContext(DC);
4433}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004434
4435void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004436 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004437 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004438 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004439 return; // Not a source member added to a class from PCH.
4440 if (!isa<CXXMethodDecl>(D))
4441 return; // We are interested in lazily declared implicit methods.
4442
4443 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004444 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004445 UpdateRecord &Record = DeclUpdates[RD];
4446 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004447 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004448}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004449
4450void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4451 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004452 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004453 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004454 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004455 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004456 return; // Not a source specialization added to a template from PCH.
4457
4458 UpdateRecord &Record = DeclUpdates[TD];
4459 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004460 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004461}
Douglas Gregor89d99802010-11-30 06:16:57 +00004462
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004463void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4464 const FunctionDecl *D) {
4465 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004466 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004467 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004468 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004469 return; // Not a source specialization added to a template from PCH.
4470
4471 UpdateRecord &Record = DeclUpdates[TD];
4472 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004473 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004474}
4475
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004476void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004477 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004478 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004479 return; // Declaration not imported from PCH.
4480
4481 // Implicit decl from a PCH was defined.
4482 // FIXME: Should implicit definition be a separate FunctionDecl?
4483 RewriteDecl(D);
4484}
4485
Sebastian Redlf79a7192011-04-29 08:19:30 +00004486void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004487 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004488 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004489 return;
4490
4491 // Since the actual instantiation is delayed, this really means that we need
4492 // to update the instantiation location.
4493 UpdateRecord &Record = DeclUpdates[D];
4494 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4495 AddSourceLocation(
4496 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4497}
4498
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004499void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4500 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004501 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004502 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004503 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004504
4505 assert(IFD->getDefinition() && "Category on a class without a definition?");
4506 ObjCClassesWithCategories.insert(
4507 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004508}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004509
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004510
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004511void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4512 const ObjCPropertyDecl *OrigProp,
4513 const ObjCCategoryDecl *ClassExt) {
4514 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4515 if (!D)
4516 return;
4517
4518 assert(!WritingAST && "Already writing the AST!");
4519 if (!D->isFromASTFile())
4520 return; // Declaration not imported from PCH.
4521
4522 RewriteDecl(D);
4523}