blob: cb3206844260c7f3a389d5620812e5fedefa0724 [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);
Peter Collingbournef111d932011-04-15 00:35:48 +0000696 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000697 RECORD(EXPR_OBJC_STRING_LITERAL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000698 RECORD(EXPR_OBJC_NUMERIC_LITERAL);
699 RECORD(EXPR_OBJC_ARRAY_LITERAL);
700 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000701 RECORD(EXPR_OBJC_ENCODE);
702 RECORD(EXPR_OBJC_SELECTOR_EXPR);
703 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
704 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
705 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
706 RECORD(EXPR_OBJC_KVC_REF_EXPR);
707 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000708 RECORD(STMT_OBJC_FOR_COLLECTION);
709 RECORD(STMT_OBJC_CATCH);
710 RECORD(STMT_OBJC_FINALLY);
711 RECORD(STMT_OBJC_AT_TRY);
712 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
713 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000714 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000715 RECORD(EXPR_CXX_OPERATOR_CALL);
716 RECORD(EXPR_CXX_CONSTRUCT);
717 RECORD(EXPR_CXX_STATIC_CAST);
718 RECORD(EXPR_CXX_DYNAMIC_CAST);
719 RECORD(EXPR_CXX_REINTERPRET_CAST);
720 RECORD(EXPR_CXX_CONST_CAST);
721 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000722 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000723 RECORD(EXPR_CXX_BOOL_LITERAL);
724 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000725 RECORD(EXPR_CXX_TYPEID_EXPR);
726 RECORD(EXPR_CXX_TYPEID_TYPE);
727 RECORD(EXPR_CXX_UUIDOF_EXPR);
728 RECORD(EXPR_CXX_UUIDOF_TYPE);
729 RECORD(EXPR_CXX_THIS);
730 RECORD(EXPR_CXX_THROW);
731 RECORD(EXPR_CXX_DEFAULT_ARG);
732 RECORD(EXPR_CXX_BIND_TEMPORARY);
733 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
734 RECORD(EXPR_CXX_NEW);
735 RECORD(EXPR_CXX_DELETE);
736 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
737 RECORD(EXPR_EXPR_WITH_CLEANUPS);
738 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
739 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
740 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
741 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
742 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
743 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
744 RECORD(EXPR_CXX_NOEXCEPT);
745 RECORD(EXPR_OPAQUE_VALUE);
746 RECORD(EXPR_BINARY_TYPE_TRAIT);
747 RECORD(EXPR_PACK_EXPANSION);
748 RECORD(EXPR_SIZEOF_PACK);
749 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000750 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000751#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000752}
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Sebastian Redla4232eb2010-08-18 23:56:21 +0000754void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000755 RecordData Record;
756 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000758#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
759#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Sebastian Redl3397c552010-08-18 23:56:27 +0000761 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000762 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000763 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000764 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000765 RECORD(TYPE_OFFSET);
766 RECORD(DECL_OFFSET);
767 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000768 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000769 RECORD(IDENTIFIER_OFFSET);
770 RECORD(IDENTIFIER_TABLE);
771 RECORD(EXTERNAL_DEFINITIONS);
772 RECORD(SPECIAL_TYPES);
773 RECORD(STATISTICS);
774 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000775 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000776 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
777 RECORD(SELECTOR_OFFSETS);
778 RECORD(METHOD_POOL);
779 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000780 RECORD(SOURCE_LOCATION_OFFSETS);
781 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000782 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000783 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000784 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000785 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000786 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000787 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000788 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000789 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000790 RECORD(SEMA_DECL_REFS);
791 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
792 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
793 RECORD(DECL_REPLACEMENTS);
794 RECORD(UPDATE_VISIBLE);
795 RECORD(DECL_UPDATE_OFFSETS);
796 RECORD(DECL_UPDATES);
797 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
798 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000799 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000800 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000801 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000802 RECORD(FP_PRAGMA_OPTIONS);
803 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000804 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000805 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
806 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000807 RECORD(MODULE_OFFSET_MAP);
808 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000809 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000810 RECORD(FILE_SORTED_DECLS);
811 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000812 RECORD(MERGED_DECLARATIONS);
813 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000814 RECORD(OBJC_CATEGORIES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000815
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000816 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000817 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000818 RECORD(SM_SLOC_FILE_ENTRY);
819 RECORD(SM_SLOC_BUFFER_ENTRY);
820 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000821 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000823 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000824 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000825 RECORD(PP_MACRO_OBJECT_LIKE);
826 RECORD(PP_MACRO_FUNCTION_LIKE);
827 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000828
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000829 // Decls and Types block.
830 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000831 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000832 RECORD(TYPE_COMPLEX);
833 RECORD(TYPE_POINTER);
834 RECORD(TYPE_BLOCK_POINTER);
835 RECORD(TYPE_LVALUE_REFERENCE);
836 RECORD(TYPE_RVALUE_REFERENCE);
837 RECORD(TYPE_MEMBER_POINTER);
838 RECORD(TYPE_CONSTANT_ARRAY);
839 RECORD(TYPE_INCOMPLETE_ARRAY);
840 RECORD(TYPE_VARIABLE_ARRAY);
841 RECORD(TYPE_VECTOR);
842 RECORD(TYPE_EXT_VECTOR);
843 RECORD(TYPE_FUNCTION_PROTO);
844 RECORD(TYPE_FUNCTION_NO_PROTO);
845 RECORD(TYPE_TYPEDEF);
846 RECORD(TYPE_TYPEOF_EXPR);
847 RECORD(TYPE_TYPEOF);
848 RECORD(TYPE_RECORD);
849 RECORD(TYPE_ENUM);
850 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000851 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000852 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000853 RECORD(TYPE_DECLTYPE);
854 RECORD(TYPE_ELABORATED);
855 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
856 RECORD(TYPE_UNRESOLVED_USING);
857 RECORD(TYPE_INJECTED_CLASS_NAME);
858 RECORD(TYPE_OBJC_OBJECT);
859 RECORD(TYPE_TEMPLATE_TYPE_PARM);
860 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
861 RECORD(TYPE_DEPENDENT_NAME);
862 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
863 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
864 RECORD(TYPE_PAREN);
865 RECORD(TYPE_PACK_EXPANSION);
866 RECORD(TYPE_ATTRIBUTED);
867 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000868 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000869 RECORD(DECL_TYPEDEF);
870 RECORD(DECL_ENUM);
871 RECORD(DECL_RECORD);
872 RECORD(DECL_ENUM_CONSTANT);
873 RECORD(DECL_FUNCTION);
874 RECORD(DECL_OBJC_METHOD);
875 RECORD(DECL_OBJC_INTERFACE);
876 RECORD(DECL_OBJC_PROTOCOL);
877 RECORD(DECL_OBJC_IVAR);
878 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000879 RECORD(DECL_OBJC_CATEGORY);
880 RECORD(DECL_OBJC_CATEGORY_IMPL);
881 RECORD(DECL_OBJC_IMPLEMENTATION);
882 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
883 RECORD(DECL_OBJC_PROPERTY);
884 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000885 RECORD(DECL_FIELD);
886 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000887 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000888 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000889 RECORD(DECL_FILE_SCOPE_ASM);
890 RECORD(DECL_BLOCK);
891 RECORD(DECL_CONTEXT_LEXICAL);
892 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000893 RECORD(DECL_NAMESPACE);
894 RECORD(DECL_NAMESPACE_ALIAS);
895 RECORD(DECL_USING);
896 RECORD(DECL_USING_SHADOW);
897 RECORD(DECL_USING_DIRECTIVE);
898 RECORD(DECL_UNRESOLVED_USING_VALUE);
899 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
900 RECORD(DECL_LINKAGE_SPEC);
901 RECORD(DECL_CXX_RECORD);
902 RECORD(DECL_CXX_METHOD);
903 RECORD(DECL_CXX_CONSTRUCTOR);
904 RECORD(DECL_CXX_DESTRUCTOR);
905 RECORD(DECL_CXX_CONVERSION);
906 RECORD(DECL_ACCESS_SPEC);
907 RECORD(DECL_FRIEND);
908 RECORD(DECL_FRIEND_TEMPLATE);
909 RECORD(DECL_CLASS_TEMPLATE);
910 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
911 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
912 RECORD(DECL_FUNCTION_TEMPLATE);
913 RECORD(DECL_TEMPLATE_TYPE_PARM);
914 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
915 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
916 RECORD(DECL_STATIC_ASSERT);
917 RECORD(DECL_CXX_BASE_SPECIFIERS);
918 RECORD(DECL_INDIRECTFIELD);
919 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
920
Douglas Gregora72d8c42011-06-03 02:27:19 +0000921 // Statements and Exprs can occur in the Decls and Types block.
922 AddStmtsExprs(Stream, Record);
923
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000924 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000925 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000926 RECORD(PPD_MACRO_DEFINITION);
927 RECORD(PPD_INCLUSION_DIRECTIVE);
928
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000929#undef RECORD
930#undef BLOCK
931 Stream.ExitBlock();
932}
933
Douglas Gregore650c8c2009-07-07 00:12:59 +0000934/// \brief Adjusts the given filename to only write out the portion of the
935/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000936///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000937/// \param Filename the file name to adjust.
938///
939/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
940/// the returned filename will be adjusted by this system root.
941///
942/// \returns either the original filename (if it needs no adjustment) or the
943/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000944static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000945adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000946 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Douglas Gregor832d6202011-07-22 16:35:34 +0000948 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000949 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Douglas Gregore650c8c2009-07-07 00:12:59 +0000951 // Verify that the filename and the system root have the same prefix.
952 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000953 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000954 if (Filename[Pos] != isysroot[Pos])
955 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957 // We hit the end of the filename before we hit the end of the system root.
958 if (!Filename[Pos])
959 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961 // If the file name has a '/' at the current position, skip over the '/'.
962 // We distinguish sysroot-based includes from absolute includes by the
963 // absence of '/' at the beginning of sysroot-based includes.
964 if (Filename[Pos] == '/')
965 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 return Filename + Pos;
968}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000969
Sebastian Redl3397c552010-08-18 23:56:27 +0000970/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000971void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000972 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000973 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000976 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000978 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000979 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
980 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
982 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
983 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000984 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors
Douglas Gregore95b9192011-08-17 21:07:30 +0000985 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000986 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Douglas Gregore650c8c2009-07-07 00:12:59 +0000988 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000989 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000990 Record.push_back(VERSION_MAJOR);
991 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000992 Record.push_back(CLANG_VERSION_MAJOR);
993 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000994 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000995 Record.push_back(ASTHasCompilerErrors);
Douglas Gregore95b9192011-08-17 21:07:30 +0000996 const std::string &Triple = Target.getTriple().getTriple();
997 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
998
999 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001000 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1001 llvm::SmallVector<char, 128> ModulePaths;
1002 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001003
1004 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1005 M != MEnd; ++M) {
1006 // Skip modules that weren't directly imported.
1007 if (!(*M)->isDirectlyImported())
1008 continue;
1009
1010 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1011 // FIXME: Write import location, once it matters.
1012 // FIXME: This writes the absolute path for AST files we depend on.
1013 const std::string &FileName = (*M)->FileName;
1014 Record.push_back(FileName.size());
1015 Record.append(FileName.begin(), FileName.end());
1016 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001017 Stream.EmitRecord(IMPORTS, Record);
1018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Douglas Gregor31d375f2011-05-06 21:43:30 +00001020 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001021 SourceManager &SM = Context.getSourceManager();
1022 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1023 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001024 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001025 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1026 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1027
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001028 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001030 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001031
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001032 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001033 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001034 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001035 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001036 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001037 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001038
1039 Record.clear();
1040 Record.push_back(SM.getMainFileID().getOpaqueValue());
1041 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001042 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001043
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001044 // Original PCH directory
1045 if (!OutputFile.empty() && OutputFile != "-") {
1046 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1047 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1049 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1050
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001051 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001052
1053 llvm::sys::fs::make_absolute(OutputPath);
1054 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1055
1056 RecordData Record;
1057 Record.push_back(ORIGINAL_PCH_DIR);
1058 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1059 }
1060
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001061 // Repository branch/version information.
1062 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001063 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001064 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1065 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001066 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001067 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001068 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1069 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001070}
1071
1072/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001073void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001074 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001075#define LANGOPT(Name, Bits, Default, Description) \
1076 Record.push_back(LangOpts.Name);
1077#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1078 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1079#include "clang/Basic/LangOptions.def"
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001080
1081 Record.push_back(LangOpts.CurrentModule.size());
1082 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001083 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001084}
1085
Douglas Gregor14f79002009-04-10 03:52:48 +00001086//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087// stat cache Serialization
1088//===----------------------------------------------------------------------===//
1089
1090namespace {
1091// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001092class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001093public:
1094 typedef const char * key_type;
1095 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Chris Lattner74e976b2010-11-23 19:28:12 +00001097 typedef struct stat data_type;
1098 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001099
1100 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001101 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
1104 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001105 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001106 data_type_ref Data) {
1107 unsigned StrLen = strlen(path);
1108 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001109 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001110 clang::io::Emit8(Out, DataLen);
1111 return std::make_pair(StrLen + 1, DataLen);
1112 }
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Chris Lattner5f9e2722011-07-23 10:55:15 +00001114 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001115 Out.write(path, KeyLen);
1116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Chris Lattner5f9e2722011-07-23 10:55:15 +00001118 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001119 data_type_ref Data, unsigned DataLen) {
1120 using namespace clang::io;
1121 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Chris Lattner74e976b2010-11-23 19:28:12 +00001123 Emit32(Out, (uint32_t) Data.st_ino);
1124 Emit32(Out, (uint32_t) Data.st_dev);
1125 Emit16(Out, (uint16_t) Data.st_mode);
1126 Emit64(Out, (uint64_t) Data.st_mtime);
1127 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128
1129 assert(Out.tell() - Start == DataLen && "Wrong data length");
1130 }
1131};
1132} // end anonymous namespace
1133
Sebastian Redl3397c552010-08-18 23:56:27 +00001134/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001135void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001136 // Build the on-disk hash table containing information about every
1137 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001138 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001139 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001140 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001141 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001142 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001143 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001144 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001147 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001148 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001149 uint32_t BucketOffset;
1150 {
1151 llvm::raw_svector_ostream Out(StatCacheData);
1152 // Make sure that no bucket is at offset 0
1153 clang::io::Emit32(Out, 0);
1154 BucketOffset = Generator.Emit(Out);
1155 }
1156
1157 // Create a blob abbreviation
1158 using namespace llvm;
1159 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001160 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1164 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1165
1166 // Write the stat cache
1167 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001168 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001169 Record.push_back(BucketOffset);
1170 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001171 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001172}
1173
1174//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001175// Source Manager Serialization
1176//===----------------------------------------------------------------------===//
1177
1178/// \brief Create an abbreviation for the SLocEntry that refers to a
1179/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001180static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001181 using namespace llvm;
1182 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001183 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001188 // FileEntry fields.
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001196 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001197}
1198
1199/// \brief Create an abbreviation for the SLocEntry that refers to a
1200/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001201static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001202 using namespace llvm;
1203 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001204 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001210 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001211}
1212
1213/// \brief Create an abbreviation for the SLocEntry that refers to a
1214/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001215static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001216 using namespace llvm;
1217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001218 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001220 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001221}
1222
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001223/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1224/// expansion.
1225static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001226 using namespace llvm;
1227 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001228 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001234 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001235}
1236
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001237namespace {
1238 // Trait used for the on-disk hash table of header search information.
1239 class HeaderFileInfoTrait {
1240 ASTWriter &Writer;
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001241 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001242
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001243 // Keep track of the framework names we've used during serialization.
1244 SmallVector<char, 128> FrameworkStringData;
1245 llvm::StringMap<unsigned> FrameworkNameOffset;
1246
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001247 public:
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001248 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001249 : Writer(Writer), HS(HS) { }
1250
1251 typedef const char *key_type;
1252 typedef key_type key_type_ref;
1253
1254 typedef HeaderFileInfo data_type;
1255 typedef const data_type &data_type_ref;
1256
1257 static unsigned ComputeHash(const char *path) {
1258 // The hash is based only on the filename portion of the key, so that the
1259 // reader can match based on filenames when symlinking or excess path
1260 // elements ("foo/../", "../") change the form of the name. However,
1261 // complete path is still the key.
1262 return llvm::HashString(llvm::sys::path::filename(path));
1263 }
1264
1265 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001266 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001267 data_type_ref Data) {
1268 unsigned StrLen = strlen(path);
1269 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001270 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001271 clang::io::Emit8(Out, DataLen);
1272 return std::make_pair(StrLen + 1, DataLen);
1273 }
1274
Chris Lattner5f9e2722011-07-23 10:55:15 +00001275 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001276 Out.write(path, KeyLen);
1277 }
1278
Chris Lattner5f9e2722011-07-23 10:55:15 +00001279 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001280 data_type_ref Data, unsigned DataLen) {
1281 using namespace clang::io;
1282 uint64_t Start = Out.tell(); (void)Start;
1283
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001284 unsigned char Flags = (Data.isImport << 5)
1285 | (Data.isPragmaOnce << 4)
1286 | (Data.DirInfo << 2)
1287 | (Data.Resolved << 1)
1288 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001289 Emit8(Out, (uint8_t)Flags);
1290 Emit16(Out, (uint16_t) Data.NumIncludes);
1291
1292 if (!Data.ControllingMacro)
1293 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1294 else
1295 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001296
1297 unsigned Offset = 0;
1298 if (!Data.Framework.empty()) {
1299 // If this header refers into a framework, save the framework name.
1300 llvm::StringMap<unsigned>::iterator Pos
1301 = FrameworkNameOffset.find(Data.Framework);
1302 if (Pos == FrameworkNameOffset.end()) {
1303 Offset = FrameworkStringData.size() + 1;
1304 FrameworkStringData.append(Data.Framework.begin(),
1305 Data.Framework.end());
1306 FrameworkStringData.push_back(0);
1307
1308 FrameworkNameOffset[Data.Framework] = Offset;
1309 } else
1310 Offset = Pos->second;
1311 }
1312 Emit32(Out, Offset);
1313
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001314 assert(Out.tell() - Start == DataLen && "Wrong data length");
1315 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001316
1317 const char *strings_begin() const { return FrameworkStringData.begin(); }
1318 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001319 };
1320} // end anonymous namespace
1321
1322/// \brief Write the header search block for the list of files that
1323///
1324/// \param HS The header search structure to save.
1325///
1326/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001327void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001328 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001329 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1330
1331 if (FilesByUID.size() > HS.header_file_size())
1332 FilesByUID.resize(HS.header_file_size());
1333
1334 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1335 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001336 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001337 unsigned NumHeaderSearchEntries = 0;
1338 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1339 const FileEntry *File = FilesByUID[UID];
1340 if (!File)
1341 continue;
1342
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001343 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1344 // from the external source if it was not provided already.
1345 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001346 if (HFI.External && Chain)
1347 continue;
1348
1349 // Turn the file name into an absolute path, if it isn't already.
1350 const char *Filename = File->getName();
1351 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1352
1353 // If we performed any translation on the file name at all, we need to
1354 // save this string, since the generator will refer to it later.
1355 if (Filename != File->getName()) {
1356 Filename = strdup(Filename);
1357 SavedStrings.push_back(Filename);
1358 }
1359
1360 Generator.insert(Filename, HFI, GeneratorTrait);
1361 ++NumHeaderSearchEntries;
1362 }
1363
1364 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001365 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001366 uint32_t BucketOffset;
1367 {
1368 llvm::raw_svector_ostream Out(TableData);
1369 // Make sure that no bucket is at offset 0
1370 clang::io::Emit32(Out, 0);
1371 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1372 }
1373
1374 // Create a blob abbreviation
1375 using namespace llvm;
1376 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1377 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1382 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1383
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001384 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001385 RecordData Record;
1386 Record.push_back(HEADER_SEARCH_TABLE);
1387 Record.push_back(BucketOffset);
1388 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001389 Record.push_back(TableData.size());
1390 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001391 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1392
1393 // Free all of the strings we had to duplicate.
1394 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1395 free((void*)SavedStrings[I]);
1396}
1397
Douglas Gregor14f79002009-04-10 03:52:48 +00001398/// \brief Writes the block containing the serialized form of the
1399/// source manager.
1400///
1401/// TODO: We should probably use an on-disk hash table (stored in a
1402/// blob), indexed based on the file name, so that we only create
1403/// entries for files that we actually need. In the common case (no
1404/// errors), we probably won't have to create file entries for any of
1405/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001406void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001407 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001408 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001409 RecordData Record;
1410
Chris Lattnerf04ad692009-04-10 17:16:57 +00001411 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001412 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001413
1414 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001415 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1416 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1417 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001418 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001419
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001420 // Write out the source location entry table. We skip the first
1421 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001422 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001423 // Write out the offsets of only source location file entries.
1424 // We will go through them in ASTReader::validateFileEntries().
1425 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001426 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001427 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1428 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001429 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001430 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001431 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001432
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001433 // Record the offset of this source-location entry.
1434 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1435
1436 // Figure out which record code to use.
1437 unsigned Code;
1438 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001439 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1440 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001441 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001442 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1443 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001444 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001445 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001446 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001447 Record.clear();
1448 Record.push_back(Code);
1449
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001450 // Starting offset of this entry within this module, so skip the dummy.
1451 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001452 if (SLoc->isFile()) {
1453 const SrcMgr::FileInfo &File = SLoc->getFile();
1454 Record.push_back(File.getIncludeLoc().getRawEncoding());
1455 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1456 Record.push_back(File.hasLineDirectives());
1457
1458 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001459 if (Content->OrigEntry) {
1460 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001461 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001462
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001463 // The source location entry is a file. The blob associated
1464 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Douglas Gregor2d52be52010-03-21 22:49:54 +00001466 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001467 Record.push_back(Content->OrigEntry->getSize());
1468 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001469 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001470 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001471
1472 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1473 if (FDI != FileDeclIDs.end()) {
1474 Record.push_back(FDI->second->FirstDeclIndex);
1475 Record.push_back(FDI->second->DeclIDs.size());
1476 } else {
1477 Record.push_back(0);
1478 Record.push_back(0);
1479 }
Douglas Gregora081da52011-11-16 20:05:18 +00001480
Douglas Gregore650c8c2009-07-07 00:12:59 +00001481 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001482 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001483 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001484
1485 // Ask the file manager to fixup the relative path for us. This will
1486 // honor the working directory.
1487 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1488
1489 // FIXME: This call to make_absolute shouldn't be necessary, the
1490 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001491 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001492 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Douglas Gregore650c8c2009-07-07 00:12:59 +00001494 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001495 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001496
1497 if (Content->BufferOverridden) {
1498 Record.clear();
1499 Record.push_back(SM_SLOC_BUFFER_BLOB);
1500 const llvm::MemoryBuffer *Buffer
1501 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1502 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1503 StringRef(Buffer->getBufferStart(),
1504 Buffer->getBufferSize() + 1));
1505 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001506 } else {
1507 // The source location entry is a buffer. The blob associated
1508 // with this entry contains the contents of the buffer.
1509
1510 // We add one to the size so that we capture the trailing NULL
1511 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1512 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001513 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001514 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001515 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001516 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001517 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001518 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001519 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001520 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001521 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001522 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001523
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001524 if (strcmp(Name, "<built-in>") == 0) {
1525 PreloadSLocs.push_back(SLocEntryOffsets.size());
1526 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001527 }
1528 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001529 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001530 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001531 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1532 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001533 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1534 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001535
1536 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001537 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001538 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001539 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001540 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001541 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001542 }
1543 }
1544
Douglas Gregorc9490c02009-04-16 22:23:12 +00001545 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001546
1547 if (SLocEntryOffsets.empty())
1548 return;
1549
Sebastian Redl3397c552010-08-18 23:56:27 +00001550 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001551 // table is used for lazily loading source-location information.
1552 using namespace llvm;
1553 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001554 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1558 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001560 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001561 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001562 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001563 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001564 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001565
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001566 Abbrev = new BitCodeAbbrev();
1567 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1570 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1571
1572 Record.clear();
1573 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1574 Record.push_back(SLocFileEntryOffsets.size());
1575 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1576 data(SLocFileEntryOffsets));
1577
Sebastian Redl3397c552010-08-18 23:56:27 +00001578 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001579 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001580 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001581
1582 // Write the line table. It depends on remapping working, so it must come
1583 // after the source location offsets.
1584 if (SourceMgr.hasLineTable()) {
1585 LineTableInfo &LineTable = SourceMgr.getLineTable();
1586
1587 Record.clear();
1588 // Emit the file names
1589 Record.push_back(LineTable.getNumFilenames());
1590 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1591 // Emit the file name
1592 const char *Filename = LineTable.getFilename(I);
1593 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1594 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1595 Record.push_back(FilenameLen);
1596 if (FilenameLen)
1597 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1598 }
1599
1600 // Emit the line entries
1601 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1602 L != LEnd; ++L) {
1603 // Only emit entries for local files.
1604 if (L->first < 0)
1605 continue;
1606
1607 // Emit the file ID
1608 Record.push_back(L->first);
1609
1610 // Emit the line entries
1611 Record.push_back(L->second.size());
1612 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1613 LEEnd = L->second.end();
1614 LE != LEEnd; ++LE) {
1615 Record.push_back(LE->FileOffset);
1616 Record.push_back(LE->LineNo);
1617 Record.push_back(LE->FilenameID);
1618 Record.push_back((unsigned)LE->FileKind);
1619 Record.push_back(LE->IncludeOffset);
1620 }
1621 }
1622 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1623 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001624}
1625
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001626//===----------------------------------------------------------------------===//
1627// Preprocessor Serialization
1628//===----------------------------------------------------------------------===//
1629
Douglas Gregor9c736102011-02-10 18:20:09 +00001630static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1631 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1632 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1633 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1634 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1635 return X.first->getName().compare(Y.first->getName());
1636}
1637
Chris Lattner0b1fb982009-04-10 17:15:23 +00001638/// \brief Writes the block containing the serialized form of the
1639/// preprocessor.
1640///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001641void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001642 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1643 if (PPRec)
1644 WritePreprocessorDetail(*PPRec);
1645
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001646 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001647
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001648 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1649 if (PP.getCounterValue() != 0) {
1650 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001651 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001652 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001653 }
1654
1655 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001656 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Sebastian Redl3397c552010-08-18 23:56:27 +00001658 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001659 // FIXME: use diagnostics subsystem for localization etc.
1660 if (PP.SawDateOrTime())
1661 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Douglas Gregorecdcb882010-10-20 22:00:55 +00001663
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001664 // Loop over all the macro definitions that are live at the end of the file,
1665 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001666
Douglas Gregor9c736102011-02-10 18:20:09 +00001667 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001668 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001669 MacrosToEmit;
1670 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001671 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1672 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001673 I != E; ++I) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001674 const IdentifierInfo *Name = I->first;
Douglas Gregoraa93a872011-10-17 15:32:29 +00001675 if (!IsModule || I->second->isPublic()) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001676 MacroDefinitionsSeen.insert(Name);
Douglas Gregor7143aab2011-09-01 17:04:32 +00001677 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1678 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001679 }
1680
1681 // Sort the set of macro definitions that need to be serialized by the
1682 // name of the macro, to provide a stable ordering.
1683 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1684 &compareMacroDefinitions);
1685
Douglas Gregor040a8042011-02-11 00:26:14 +00001686 // Resolve any identifiers that defined macros at the time they were
1687 // deserialized, adding them to the list of macros to emit (if appropriate).
1688 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1689 IdentifierInfo *Name
1690 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1691 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1692 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1693 }
1694
Douglas Gregor9c736102011-02-10 18:20:09 +00001695 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1696 const IdentifierInfo *Name = MacrosToEmit[I].first;
1697 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001698 if (!MI)
1699 continue;
1700
Sebastian Redl3397c552010-08-18 23:56:27 +00001701 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001702 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001703 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001704
1705 // FIXME: There is a (probably minor) optimization we could do here, if
1706 // the macro comes from the original PCH but the identifier comes from a
1707 // chained PCH, by storing the offset into the original PCH rather than
1708 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001709 if (MI->isBuiltinMacro() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00001710 (Chain &&
1711 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1712 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001713 continue;
1714
Douglas Gregor9c736102011-02-10 18:20:09 +00001715 AddIdentifierRef(Name, Record);
1716 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001717 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1718 Record.push_back(MI->isUsed());
Douglas Gregoraa93a872011-10-17 15:32:29 +00001719 Record.push_back(MI->isPublic());
1720 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001721 unsigned Code;
1722 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001723 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001724 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001725 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001727 Record.push_back(MI->isC99Varargs());
1728 Record.push_back(MI->isGNUVarargs());
1729 Record.push_back(MI->getNumArgs());
1730 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1731 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001732 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001733 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001734
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001735 // If we have a detailed preprocessing record, record the macro definition
1736 // ID that corresponds to this macro.
1737 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001738 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001739
Douglas Gregorc9490c02009-04-16 22:23:12 +00001740 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001741 Record.clear();
1742
Chris Lattnerdf961c22009-04-10 18:08:30 +00001743 // Emit the tokens array.
1744 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1745 // Note that we know that the preprocessor does not have any annotation
1746 // tokens in it because they are created by the parser, and thus can't be
1747 // in a macro definition.
1748 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Chris Lattnerdf961c22009-04-10 18:08:30 +00001750 Record.push_back(Tok.getLocation().getRawEncoding());
1751 Record.push_back(Tok.getLength());
1752
Chris Lattnerdf961c22009-04-10 18:08:30 +00001753 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1754 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001755 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001756 // FIXME: Should translate token kind to a stable encoding.
1757 Record.push_back(Tok.getKind());
1758 // FIXME: Should translate token flags to a stable encoding.
1759 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001761 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001762 Record.clear();
1763 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001764 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001765 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001766 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001767}
1768
1769void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001770 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001771 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001772
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001773 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001774
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001775 // Enter the preprocessor block.
1776 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001777
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001778 // If the preprocessor has a preprocessing record, emit it.
1779 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001780 using namespace llvm;
1781
1782 // Set up the abbreviation for
1783 unsigned InclusionAbbrev = 0;
1784 {
1785 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1786 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1791 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1792 }
1793
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001794 unsigned FirstPreprocessorEntityID
1795 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1796 + NUM_PREDEF_PP_ENTITY_IDS;
1797 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001798 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001799 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1800 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001801 E != EEnd;
1802 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001803 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001804
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001805 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1806 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001807
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001808 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001809 // Record this macro definition's ID.
1810 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001811
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001812 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001813 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1814 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001815 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001816
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001817 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001818 Record.push_back(ME->isBuiltinMacro());
1819 if (ME->isBuiltinMacro())
1820 AddIdentifierRef(ME->getName(), Record);
1821 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001822 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001823 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001824 continue;
1825 }
1826
1827 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1828 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001829 Record.push_back(ID->getFileName().size());
1830 Record.push_back(ID->wasInQuotes());
1831 Record.push_back(static_cast<unsigned>(ID->getKind()));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001832 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001833 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001834 // Check that the FileEntry is not null because it was not resolved and
1835 // we create a PCH even with compiler errors.
1836 if (ID->getFile())
1837 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001838 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1839 continue;
1840 }
1841
1842 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1843 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001844 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001845
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001846 // Write the offsets table for the preprocessing record.
1847 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001848 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1849
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001850 // Write the offsets table for identifier IDs.
1851 using namespace llvm;
1852 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001853 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001854 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001855 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001856 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001857
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001858 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001859 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001860 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001861 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1862 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001863 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001864}
1865
Douglas Gregore209e502011-12-06 01:10:29 +00001866unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1867 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1868 if (Known != SubmoduleIDs.end())
1869 return Known->second;
1870
1871 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1872}
1873
Douglas Gregor26ced122011-12-01 00:59:36 +00001874/// \brief Compute the number of modules within the given tree (including the
1875/// given module).
1876static unsigned getNumberOfModules(Module *Mod) {
1877 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001878 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1879 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001880 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001881 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001882
1883 return ChildModules + 1;
1884}
1885
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001886void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001887 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001888 // FIXME: This feels like it belongs somewhere else, but there are no
1889 // other consumers of this information.
1890 SourceManager &SrcMgr = PP->getSourceManager();
1891 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1892 for (ASTContext::import_iterator I = Context->local_import_begin(),
1893 IEnd = Context->local_import_end();
1894 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001895 if (Module *ImportedFrom
1896 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1897 SrcMgr))) {
1898 ImportedFrom->Imports.push_back(I->getImportedModule());
1899 }
1900 }
1901
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001902 // Enter the submodule description block.
1903 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1904
1905 // Write the abbreviations needed for the submodules block.
1906 using namespace llvm;
1907 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1908 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1918 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1919
1920 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001921 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1923 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1924
1925 Abbrev = new BitCodeAbbrev();
1926 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1928 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001929
1930 Abbrev = new BitCodeAbbrev();
1931 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1933 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1934
Douglas Gregor51f564f2011-12-31 04:05:44 +00001935 Abbrev = new BitCodeAbbrev();
1936 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1938 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1939
Douglas Gregor26ced122011-12-01 00:59:36 +00001940 // Write the submodule metadata block.
1941 RecordData Record;
1942 Record.push_back(getNumberOfModules(WritingModule));
1943 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1944 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1945
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001946 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001947 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001948 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001949 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001950 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001951 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001952 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001953
1954 // Emit the definition of the block.
1955 Record.clear();
1956 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00001957 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001958 if (Mod->Parent) {
1959 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1960 Record.push_back(SubmoduleIDs[Mod->Parent]);
1961 } else {
1962 Record.push_back(0);
1963 }
1964 Record.push_back(Mod->IsFramework);
1965 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001966 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00001967 Record.push_back(Mod->InferSubmodules);
1968 Record.push_back(Mod->InferExplicitSubmodules);
1969 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001970 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1971
Douglas Gregor51f564f2011-12-31 04:05:44 +00001972 // Emit the requirements.
1973 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1974 Record.clear();
1975 Record.push_back(SUBMODULE_REQUIRES);
1976 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1977 Mod->Requires[I].data(),
1978 Mod->Requires[I].size());
1979 }
1980
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001981 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00001982 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001983 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001984 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001985 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00001986 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00001987 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1988 Record.clear();
1989 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1990 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1991 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001992 }
1993
1994 // Emit the headers.
1995 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1996 Record.clear();
1997 Record.push_back(SUBMODULE_HEADER);
1998 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1999 Mod->Headers[I]->getName());
2000 }
Douglas Gregor55988682011-12-05 16:33:54 +00002001
2002 // Emit the imports.
2003 if (!Mod->Imports.empty()) {
2004 Record.clear();
2005 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002006 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002007 assert(ImportedID && "Unknown submodule!");
2008 Record.push_back(ImportedID);
2009 }
2010 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2011 }
2012
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002013 // Emit the exports.
2014 if (!Mod->Exports.empty()) {
2015 Record.clear();
2016 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002017 if (Module *Exported = Mod->Exports[I].getPointer()) {
2018 unsigned ExportedID = SubmoduleIDs[Exported];
2019 assert(ExportedID > 0 && "Unknown submodule ID?");
2020 Record.push_back(ExportedID);
2021 } else {
2022 Record.push_back(0);
2023 }
2024
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002025 Record.push_back(Mod->Exports[I].getInt());
2026 }
2027 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2028 }
2029
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002030 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002031 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2032 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002033 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002034 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002035 }
2036
2037 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002038
2039 assert((NextSubmoduleID - FirstSubmoduleID
2040 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002041}
2042
Douglas Gregor185dbd72011-12-01 02:07:58 +00002043serialization::SubmoduleID
2044ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002045 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002046 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002047
2048 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002049 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002050 Module *OwningMod
2051 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002052 if (!OwningMod)
2053 return 0;
2054
Douglas Gregore209e502011-12-06 01:10:29 +00002055 // Check whether this submodule is part of our own module.
2056 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002057 return 0;
2058
Douglas Gregore209e502011-12-06 01:10:29 +00002059 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002060}
2061
David Blaikied6471f72011-09-25 23:23:43 +00002062void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002063 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002064 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002065 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2066 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002067 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002068 if (point.Loc.isInvalid())
2069 continue;
2070
2071 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002072 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002073 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002074 if (I->second.isPragma()) {
2075 Record.push_back(I->first);
2076 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002077 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002078 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002079 Record.push_back(-1); // mark the end of the diag/map pairs for this
2080 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002081 }
2082
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002083 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002084 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002085}
2086
Anders Carlssonc8505782011-03-06 18:41:18 +00002087void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2088 if (CXXBaseSpecifiersOffsets.empty())
2089 return;
2090
2091 RecordData Record;
2092
2093 // Create a blob abbreviation for the C++ base specifiers offsets.
2094 using namespace llvm;
2095
2096 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2097 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2099 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2100 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2101
Douglas Gregore92b8a12011-08-04 00:01:48 +00002102 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002103 Record.clear();
2104 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2105 Record.push_back(CXXBaseSpecifiersOffsets.size());
2106 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002107 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002108}
2109
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002110//===----------------------------------------------------------------------===//
2111// Type Serialization
2112//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002113
Sebastian Redl3397c552010-08-18 23:56:27 +00002114/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002115void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002116 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002117 if (Idx.getIndex() == 0) // we haven't seen this type before.
2118 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Douglas Gregor97475832010-10-05 18:37:06 +00002120 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002121
Douglas Gregor2cf26342009-04-09 22:27:44 +00002122 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002123 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002124 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002125 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002126 else if (TypeOffsets.size() < Index) {
2127 TypeOffsets.resize(Index + 1);
2128 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002129 }
2130
2131 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002132
Douglas Gregor2cf26342009-04-09 22:27:44 +00002133 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002134 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002135
Douglas Gregora4923eb2009-11-16 21:35:15 +00002136 if (T.hasLocalNonFastQualifiers()) {
2137 Qualifiers Qs = T.getLocalQualifiers();
2138 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002139 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002140 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002141 } else {
2142 switch (T->getTypeClass()) {
2143 // For all of the concrete, non-dependent types, call the
2144 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002145#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002146 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002147#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002148#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002149 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002150 }
2151
2152 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002153 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002154
2155 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002156 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002157}
2158
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002159//===----------------------------------------------------------------------===//
2160// Declaration Serialization
2161//===----------------------------------------------------------------------===//
2162
Douglas Gregor2cf26342009-04-09 22:27:44 +00002163/// \brief Write the block containing all of the declaration IDs
2164/// lexically declared within the given DeclContext.
2165///
2166/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2167/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002168uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002169 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002170 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002171 return 0;
2172
Douglas Gregorc9490c02009-04-16 22:23:12 +00002173 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002174 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002175 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002176 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002177 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2178 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002179 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002180
Douglas Gregor25123082009-04-22 22:34:57 +00002181 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002182 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002183 return Offset;
2184}
2185
Sebastian Redla4232eb2010-08-18 23:56:21 +00002186void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002187 using namespace llvm;
2188 RecordData Record;
2189
2190 // Write the type offsets array
2191 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002192 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2196 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2197 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002198 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002199 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002200 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002201 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002202
2203 // Write the declaration offsets array
2204 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002205 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2209 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2210 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002211 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002212 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002213 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002214 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002215}
2216
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002217void ASTWriter::WriteFileDeclIDsMap() {
2218 using namespace llvm;
2219 RecordData Record;
2220
2221 // Join the vectors of DeclIDs from all files.
2222 SmallVector<DeclID, 256> FileSortedIDs;
2223 for (FileDeclIDsTy::iterator
2224 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2225 DeclIDInFileInfo &Info = *FI->second;
2226 Info.FirstDeclIndex = FileSortedIDs.size();
2227 for (LocDeclIDsTy::iterator
2228 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2229 FileSortedIDs.push_back(DI->second);
2230 }
2231
2232 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2233 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2235 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2236 Record.push_back(FILE_SORTED_DECLS);
2237 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2238}
2239
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002240//===----------------------------------------------------------------------===//
2241// Global Method Pool and Selector Serialization
2242//===----------------------------------------------------------------------===//
2243
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002244namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002245// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002246class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002247 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002248
2249public:
2250 typedef Selector key_type;
2251 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Sebastian Redl5d050072010-08-04 17:20:04 +00002253 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002254 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002255 ObjCMethodList Instance, Factory;
2256 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002257 typedef const data_type& data_type_ref;
2258
Sebastian Redl3397c552010-08-18 23:56:27 +00002259 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002261 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002262 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002263 }
Mike Stump1eb44332009-09-09 15:08:12 +00002264
2265 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002266 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002267 data_type_ref Methods) {
2268 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2269 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002270 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2271 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002272 Method = Method->Next)
2273 if (Method->Method)
2274 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002275 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002276 Method = Method->Next)
2277 if (Method->Method)
2278 DataLen += 4;
2279 clang::io::Emit16(Out, DataLen);
2280 return std::make_pair(KeyLen, DataLen);
2281 }
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Chris Lattner5f9e2722011-07-23 10:55:15 +00002283 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002284 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002285 assert((Start >> 32) == 0 && "Selector key offset too large");
2286 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002287 unsigned N = Sel.getNumArgs();
2288 clang::io::Emit16(Out, N);
2289 if (N == 0)
2290 N = 1;
2291 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002292 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002293 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2294 }
Mike Stump1eb44332009-09-09 15:08:12 +00002295
Chris Lattner5f9e2722011-07-23 10:55:15 +00002296 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002297 data_type_ref Methods, unsigned DataLen) {
2298 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002299 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002300 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002301 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002302 Method = Method->Next)
2303 if (Method->Method)
2304 ++NumInstanceMethods;
2305
2306 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002307 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002308 Method = Method->Next)
2309 if (Method->Method)
2310 ++NumFactoryMethods;
2311
2312 clang::io::Emit16(Out, NumInstanceMethods);
2313 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002314 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002315 Method = Method->Next)
2316 if (Method->Method)
2317 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002318 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002319 Method = Method->Next)
2320 if (Method->Method)
2321 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002322
2323 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002324 }
2325};
2326} // end anonymous namespace
2327
Sebastian Redl059612d2010-08-03 21:58:15 +00002328/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002329///
2330/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002331/// in an on-disk hash table indexed by the selector. The hash table also
2332/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002333void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002334 using namespace llvm;
2335
Sebastian Redl059612d2010-08-03 21:58:15 +00002336 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002337 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002338 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002339 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002340 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002341 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002342 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002343 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002344
Sebastian Redl059612d2010-08-03 21:58:15 +00002345 // Create the on-disk hash table representation. We walk through every
2346 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002347 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002348 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002349 I = SelectorIDs.begin(), E = SelectorIDs.end();
2350 I != E; ++I) {
2351 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002352 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002353 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002354 I->second,
2355 ObjCMethodList(),
2356 ObjCMethodList()
2357 };
2358 if (F != SemaRef.MethodPool.end()) {
2359 Data.Instance = F->second.first;
2360 Data.Factory = F->second.second;
2361 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002362 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002363 // changed.
2364 if (Chain && I->second < FirstSelectorID) {
2365 // Selector already exists. Did it change?
2366 bool changed = false;
2367 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2368 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002369 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002370 changed = true;
2371 }
2372 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2373 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002374 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002375 changed = true;
2376 }
2377 if (!changed)
2378 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002379 } else if (Data.Instance.Method || Data.Factory.Method) {
2380 // A new method pool entry.
2381 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002382 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002383 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002384 }
2385
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002386 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002387 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002388 uint32_t BucketOffset;
2389 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002390 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002391 llvm::raw_svector_ostream Out(MethodPool);
2392 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002393 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002394 BucketOffset = Generator.Emit(Out, Trait);
2395 }
2396
2397 // Create a blob abbreviation
2398 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002399 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2403 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2404
Douglas Gregor83941df2009-04-25 17:48:32 +00002405 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002406 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002407 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002408 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002409 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002410 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002411
2412 // Create a blob abbreviation for the selector table offsets.
2413 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002414 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2418 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2419
2420 // Write the selector offsets table.
2421 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002422 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002423 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002424 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002425 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002426 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002427 }
2428}
2429
Sebastian Redl3397c552010-08-18 23:56:27 +00002430/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002431void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002432 using namespace llvm;
2433 if (SemaRef.ReferencedSelectors.empty())
2434 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002435
Fariborz Jahanian32019832010-07-23 19:11:11 +00002436 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002437
Sebastian Redl3397c552010-08-18 23:56:27 +00002438 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002439 // very tricky to fix, and given that @selector shouldn't really appear in
2440 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002441 for (DenseMap<Selector, SourceLocation>::iterator S =
2442 SemaRef.ReferencedSelectors.begin(),
2443 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2444 Selector Sel = (*S).first;
2445 SourceLocation Loc = (*S).second;
2446 AddSelectorRef(Sel, Record);
2447 AddSourceLocation(Loc, Record);
2448 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002449 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002450}
2451
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002452//===----------------------------------------------------------------------===//
2453// Identifier Table Serialization
2454//===----------------------------------------------------------------------===//
2455
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002456namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002457class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002458 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002459 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002460 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002461 bool IsModule;
2462
Douglas Gregora92193e2009-04-28 21:18:29 +00002463 /// \brief Determines whether this is an "interesting" identifier
2464 /// that needs a full IdentifierInfo structure written into the hash
2465 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002466 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002467 if (II->isPoisoned() ||
2468 II->isExtensionToken() ||
2469 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002470 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002471 II->getFETokenInfo<void>())
2472 return true;
2473
Douglas Gregorce835df2011-09-14 22:14:14 +00002474 return hasMacroDefinition(II, Macro);
2475 }
2476
2477 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002478 if (!II->hasMacroDefinition())
2479 return false;
2480
Douglas Gregorce835df2011-09-14 22:14:14 +00002481 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002482 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002483
Douglas Gregorce835df2011-09-14 22:14:14 +00002484 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002485 }
2486
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002487public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002488 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002489 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002491 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002492 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002493
Douglas Gregoreee242f2011-10-27 09:33:13 +00002494 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2495 IdentifierResolver &IdResolver, bool IsModule)
2496 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002497
2498 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002499 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002500 }
Mike Stump1eb44332009-09-09 15:08:12 +00002501
2502 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002503 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002504 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002505 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002506 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002507 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002508 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002509 if (hasMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002510 DataLen += 8;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002511
2512 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2513 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002514 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002515 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002516 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002517 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002518 // We emit the key length after the data length so that every
2519 // string is preceded by a 16-bit length. This matches the PTH
2520 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002521 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002522 return std::make_pair(KeyLen, DataLen);
2523 }
Mike Stump1eb44332009-09-09 15:08:12 +00002524
Chris Lattner5f9e2722011-07-23 10:55:15 +00002525 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002526 unsigned KeyLen) {
2527 // Record the location of the key data. This is used when generating
2528 // the mapping from persistent IDs to strings.
2529 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002530 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002531 }
Mike Stump1eb44332009-09-09 15:08:12 +00002532
Douglas Gregor7143aab2011-09-01 17:04:32 +00002533 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002534 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002535 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002536 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002537 clang::io::Emit32(Out, ID << 1);
2538 return;
2539 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002540
Douglas Gregora92193e2009-04-28 21:18:29 +00002541 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002542 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002543 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002544 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topper925be542011-12-19 05:04:33 +00002545 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregorce835df2011-09-14 22:14:14 +00002546 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002547 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2548 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002549 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002550 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002551 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002552
Douglas Gregor13292642011-12-02 15:45:10 +00002553 if (HasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002554 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor13292642011-12-02 15:45:10 +00002555 clang::io::Emit32(Out,
2556 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2557 }
2558
Douglas Gregor668c1a42009-04-21 22:25:48 +00002559 // Emit the declaration IDs in reverse order, because the
2560 // IdentifierResolver provides the declarations as they would be
2561 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002562 // "stat"), but the ASTReader adds declarations to the end of the list
2563 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002564 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002565 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2566 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002567 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002568 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002569 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002570 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002571 }
2572};
2573} // end anonymous namespace
2574
Sebastian Redl3397c552010-08-18 23:56:27 +00002575/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002576///
2577/// The identifier table consists of a blob containing string data
2578/// (the actual identifiers themselves) and a separate "offsets" index
2579/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002580void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2581 IdentifierResolver &IdResolver,
2582 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002583 using namespace llvm;
2584
2585 // Create and write out the blob that contains the identifier
2586 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002587 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002588 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002589 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002590
Douglas Gregor92b059e2009-04-28 20:33:11 +00002591 // Look for any identifiers that were named while processing the
2592 // headers, but are otherwise not needed. We add these to the hash
2593 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002594 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002595 // file.
2596 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2597 IDEnd = PP.getIdentifierTable().end();
2598 ID != IDEnd; ++ID)
2599 getIdentifierRef(ID->second);
2600
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002601 // Create the on-disk hash table representation. We only store offsets
2602 // for identifiers that appear here for the first time.
2603 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002604 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002605 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2606 ID != IDEnd; ++ID) {
2607 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002608 if (!Chain || !ID->first->isFromAST() ||
2609 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002610 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2611 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002612 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002613
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002614 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002615 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002616 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002617 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002618 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002619 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002620 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002621 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002622 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002623 }
2624
2625 // Create a blob abbreviation
2626 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002627 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002630 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002631
2632 // Write the identifier table
2633 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002634 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002635 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002636 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002637 }
2638
2639 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002640 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002641 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002642 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002643 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002644 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2645 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2646
2647 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002648 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002649 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002650 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002651 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002652 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002653}
2654
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002655//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002656// DeclContext's Name Lookup Table Serialization
2657//===----------------------------------------------------------------------===//
2658
2659namespace {
2660// Trait used for the on-disk hash table used in the method pool.
2661class ASTDeclContextNameLookupTrait {
2662 ASTWriter &Writer;
2663
2664public:
2665 typedef DeclarationName key_type;
2666 typedef key_type key_type_ref;
2667
2668 typedef DeclContext::lookup_result data_type;
2669 typedef const data_type& data_type_ref;
2670
2671 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2672
2673 unsigned ComputeHash(DeclarationName Name) {
2674 llvm::FoldingSetNodeID ID;
2675 ID.AddInteger(Name.getNameKind());
2676
2677 switch (Name.getNameKind()) {
2678 case DeclarationName::Identifier:
2679 ID.AddString(Name.getAsIdentifierInfo()->getName());
2680 break;
2681 case DeclarationName::ObjCZeroArgSelector:
2682 case DeclarationName::ObjCOneArgSelector:
2683 case DeclarationName::ObjCMultiArgSelector:
2684 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2685 break;
2686 case DeclarationName::CXXConstructorName:
2687 case DeclarationName::CXXDestructorName:
2688 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002689 break;
2690 case DeclarationName::CXXOperatorName:
2691 ID.AddInteger(Name.getCXXOverloadedOperator());
2692 break;
2693 case DeclarationName::CXXLiteralOperatorName:
2694 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2695 case DeclarationName::CXXUsingDirective:
2696 break;
2697 }
2698
2699 return ID.ComputeHash();
2700 }
2701
2702 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002703 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002704 data_type_ref Lookup) {
2705 unsigned KeyLen = 1;
2706 switch (Name.getNameKind()) {
2707 case DeclarationName::Identifier:
2708 case DeclarationName::ObjCZeroArgSelector:
2709 case DeclarationName::ObjCOneArgSelector:
2710 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002711 case DeclarationName::CXXLiteralOperatorName:
2712 KeyLen += 4;
2713 break;
2714 case DeclarationName::CXXOperatorName:
2715 KeyLen += 1;
2716 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002717 case DeclarationName::CXXConstructorName:
2718 case DeclarationName::CXXDestructorName:
2719 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002720 case DeclarationName::CXXUsingDirective:
2721 break;
2722 }
2723 clang::io::Emit16(Out, KeyLen);
2724
2725 // 2 bytes for num of decls and 4 for each DeclID.
2726 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2727 clang::io::Emit16(Out, DataLen);
2728
2729 return std::make_pair(KeyLen, DataLen);
2730 }
2731
Chris Lattner5f9e2722011-07-23 10:55:15 +00002732 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002733 using namespace clang::io;
2734
2735 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2736 Emit8(Out, Name.getNameKind());
2737 switch (Name.getNameKind()) {
2738 case DeclarationName::Identifier:
2739 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2740 break;
2741 case DeclarationName::ObjCZeroArgSelector:
2742 case DeclarationName::ObjCOneArgSelector:
2743 case DeclarationName::ObjCMultiArgSelector:
2744 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2745 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002746 case DeclarationName::CXXOperatorName:
2747 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2748 Emit8(Out, Name.getCXXOverloadedOperator());
2749 break;
2750 case DeclarationName::CXXLiteralOperatorName:
2751 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2752 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002753 case DeclarationName::CXXConstructorName:
2754 case DeclarationName::CXXDestructorName:
2755 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002756 case DeclarationName::CXXUsingDirective:
2757 break;
2758 }
2759 }
2760
Chris Lattner5f9e2722011-07-23 10:55:15 +00002761 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002762 data_type Lookup, unsigned DataLen) {
2763 uint64_t Start = Out.tell(); (void)Start;
2764 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2765 for (; Lookup.first != Lookup.second; ++Lookup.first)
2766 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2767
2768 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2769 }
2770};
2771} // end anonymous namespace
2772
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002773/// \brief Write the block containing all of the declaration IDs
2774/// visible from the given DeclContext.
2775///
2776/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002777/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002778uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2779 DeclContext *DC) {
2780 if (DC->getPrimaryContext() != DC)
2781 return 0;
2782
2783 // Since there is no name lookup into functions or methods, don't bother to
2784 // build a visible-declarations table for these entities.
2785 if (DC->isFunctionOrMethod())
2786 return 0;
2787
2788 // If not in C++, we perform name lookup for the translation unit via the
2789 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2790 // FIXME: In C++ we need the visible declarations in order to "see" the
2791 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002792 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002793 return 0;
2794
2795 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002796 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002797 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002798
2799 // Serialize the contents of the mapping used for lookup. Note that,
2800 // although we have two very different code paths, the serialized
2801 // representation is the same for both cases: a declaration name,
2802 // followed by a size, followed by references to the visible
2803 // declarations that have that name.
2804 uint64_t Offset = Stream.GetCurrentBitNo();
2805 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2806 if (!Map || Map->empty())
2807 return 0;
2808
2809 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2810 ASTDeclContextNameLookupTrait Trait(*this);
2811
2812 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002813 DeclarationName ConversionName;
2814 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002815 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2816 D != DEnd; ++D) {
2817 DeclarationName Name = D->first;
2818 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002819 if (Result.first != Result.second) {
2820 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2821 // Hash all conversion function names to the same name. The actual
2822 // type information in conversion function name is not used in the
2823 // key (since such type information is not stable across different
2824 // modules), so the intended effect is to coalesce all of the conversion
2825 // functions under a single key.
2826 if (!ConversionName)
2827 ConversionName = Name;
2828 ConversionDecls.append(Result.first, Result.second);
2829 continue;
2830 }
2831
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002832 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002833 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002834 }
2835
Douglas Gregore5a54b62011-08-30 20:49:19 +00002836 // Add the conversion functions
2837 if (!ConversionDecls.empty()) {
2838 Generator.insert(ConversionName,
2839 DeclContext::lookup_result(ConversionDecls.begin(),
2840 ConversionDecls.end()),
2841 Trait);
2842 }
2843
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002844 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002845 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002846 uint32_t BucketOffset;
2847 {
2848 llvm::raw_svector_ostream Out(LookupTable);
2849 // Make sure that no bucket is at offset 0
2850 clang::io::Emit32(Out, 0);
2851 BucketOffset = Generator.Emit(Out, Trait);
2852 }
2853
2854 // Write the lookup table
2855 RecordData Record;
2856 Record.push_back(DECL_CONTEXT_VISIBLE);
2857 Record.push_back(BucketOffset);
2858 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2859 LookupTable.str());
2860
2861 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2862 ++NumVisibleDeclContexts;
2863 return Offset;
2864}
2865
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002866/// \brief Write an UPDATE_VISIBLE block for the given context.
2867///
2868/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2869/// DeclContext in a dependent AST file. As such, they only exist for the TU
2870/// (in C++) and for namespaces.
2871void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002872 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2873 if (!Map || Map->empty())
2874 return;
2875
2876 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2877 ASTDeclContextNameLookupTrait Trait(*this);
2878
2879 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002880 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2881 D != DEnd; ++D) {
2882 DeclarationName Name = D->first;
2883 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002884 // For any name that appears in this table, the results are complete, i.e.
2885 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002886 if (Result.first != Result.second)
2887 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002888 }
2889
2890 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002891 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002892 uint32_t BucketOffset;
2893 {
2894 llvm::raw_svector_ostream Out(LookupTable);
2895 // Make sure that no bucket is at offset 0
2896 clang::io::Emit32(Out, 0);
2897 BucketOffset = Generator.Emit(Out, Trait);
2898 }
2899
2900 // Write the lookup table
2901 RecordData Record;
2902 Record.push_back(UPDATE_VISIBLE);
2903 Record.push_back(getDeclID(cast<Decl>(DC)));
2904 Record.push_back(BucketOffset);
2905 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2906}
2907
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002908/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2909void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2910 RecordData Record;
2911 Record.push_back(Opts.fp_contract);
2912 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2913}
2914
2915/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2916void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002917 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002918 return;
2919
2920 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2921 RecordData Record;
2922#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2923#include "clang/Basic/OpenCLExtensions.def"
2924 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2925}
2926
Douglas Gregor2171bf12012-01-15 16:58:34 +00002927void ASTWriter::WriteRedeclarations() {
2928 RecordData LocalRedeclChains;
2929 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2930
2931 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2932 Decl *First = Redeclarations[I];
2933 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2934
2935 Decl *MostRecent = First->getMostRecentDecl();
2936
2937 // If we only have a single declaration, there is no point in storing
2938 // a redeclaration chain.
2939 if (First == MostRecent)
2940 continue;
2941
2942 unsigned Offset = LocalRedeclChains.size();
2943 unsigned Size = 0;
2944 LocalRedeclChains.push_back(0); // Placeholder for the size.
2945
2946 // Collect the set of local redeclarations of this declaration.
2947 for (Decl *Prev = MostRecent; Prev != First;
2948 Prev = Prev->getPreviousDecl()) {
2949 if (!Prev->isFromASTFile()) {
2950 AddDeclRef(Prev, LocalRedeclChains);
2951 ++Size;
2952 }
2953 }
2954 LocalRedeclChains[Offset] = Size;
2955
2956 // Reverse the set of local redeclarations, so that we store them in
2957 // order (since we found them in reverse order).
2958 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2959
2960 // Add the mapping from the first ID to the set of local declarations.
2961 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2962 LocalRedeclsMap.push_back(Info);
2963
2964 assert(N == Redeclarations.size() &&
2965 "Deserialized a declaration we shouldn't have");
2966 }
2967
2968 if (LocalRedeclChains.empty())
2969 return;
2970
2971 // Sort the local redeclarations map by the first declaration ID,
2972 // since the reader will be performing binary searches on this information.
2973 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2974
2975 // Emit the local redeclarations map.
2976 using namespace llvm;
2977 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2978 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
2980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2981 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
2982
2983 RecordData Record;
2984 Record.push_back(LOCAL_REDECLARATIONS_MAP);
2985 Record.push_back(LocalRedeclsMap.size());
2986 Stream.EmitRecordWithBlob(AbbrevID, Record,
2987 reinterpret_cast<char*>(LocalRedeclsMap.data()),
2988 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
2989
2990 // Emit the redeclaration chains.
2991 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
2992}
2993
Douglas Gregorcff9f262012-01-27 01:47:08 +00002994void ASTWriter::WriteObjCCategories() {
2995 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
2996 RecordData Categories;
2997
2998 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
2999 unsigned Size = 0;
3000 unsigned StartIndex = Categories.size();
3001
3002 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3003
3004 // Allocate space for the size.
3005 Categories.push_back(0);
3006
3007 // Add the categories.
3008 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3009 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3010 assert(getDeclID(Cat) != 0 && "Bogus category");
3011 AddDeclRef(Cat, Categories);
3012 }
3013
3014 // Update the size.
3015 Categories[StartIndex] = Size;
3016
3017 // Record this interface -> category map.
3018 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3019 CategoriesMap.push_back(CatInfo);
3020 }
3021
3022 // Sort the categories map by the definition ID, since the reader will be
3023 // performing binary searches on this information.
3024 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3025
3026 // Emit the categories map.
3027 using namespace llvm;
3028 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3029 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3032 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3033
3034 RecordData Record;
3035 Record.push_back(OBJC_CATEGORIES_MAP);
3036 Record.push_back(CategoriesMap.size());
3037 Stream.EmitRecordWithBlob(AbbrevID, Record,
3038 reinterpret_cast<char*>(CategoriesMap.data()),
3039 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3040
3041 // Emit the category lists.
3042 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3043}
3044
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003045void ASTWriter::WriteMergedDecls() {
3046 if (!Chain || Chain->MergedDecls.empty())
3047 return;
3048
3049 RecordData Record;
3050 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3051 IEnd = Chain->MergedDecls.end();
3052 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003053 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003054 : getDeclID(I->first);
3055 assert(CanonID && "Merged declaration not known?");
3056
3057 Record.push_back(CanonID);
3058 Record.push_back(I->second.size());
3059 Record.append(I->second.begin(), I->second.end());
3060 }
3061 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3062}
3063
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003064//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003065// General Serialization Routines
3066//===----------------------------------------------------------------------===//
3067
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003068/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003069void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003070 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00003071 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
3072 const Attr * A = *i;
3073 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003074 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003075
Sean Huntcf807c42010-08-18 23:23:40 +00003076#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003077
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003078 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003079}
3080
Chris Lattner5f9e2722011-07-23 10:55:15 +00003081void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003082 Record.push_back(Str.size());
3083 Record.insert(Record.end(), Str.begin(), Str.end());
3084}
3085
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003086void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3087 RecordDataImpl &Record) {
3088 Record.push_back(Version.getMajor());
3089 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3090 Record.push_back(*Minor + 1);
3091 else
3092 Record.push_back(0);
3093 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3094 Record.push_back(*Subminor + 1);
3095 else
3096 Record.push_back(0);
3097}
3098
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003099/// \brief Note that the identifier II occurs at the given offset
3100/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003101void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003102 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003103 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003104 // up earlier in the chain and thus don't need an offset.
3105 if (ID >= FirstIdentID)
3106 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003107}
3108
Douglas Gregor83941df2009-04-25 17:48:32 +00003109/// \brief Note that the selector Sel occurs at the given offset
3110/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003111void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003112 unsigned ID = SelectorIDs[Sel];
3113 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003114 // Don't record offsets for selectors that are also available in a different
3115 // file.
3116 if (ID < FirstSelectorID)
3117 return;
3118 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003119}
3120
Sebastian Redla4232eb2010-08-18 23:56:21 +00003121ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003122 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003123 WritingAST(false), ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003124 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003125 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003126 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003127 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3128 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003129 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003130 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003131 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003132 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003133 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003134 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003135 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3136 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3137 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003138 DeclTypedefAbbrev(0),
3139 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3140 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003141{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003142}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003143
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003144ASTWriter::~ASTWriter() {
3145 for (FileDeclIDsTy::iterator
3146 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3147 delete I->second;
3148}
3149
Sebastian Redla4232eb2010-08-18 23:56:21 +00003150void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003151 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003152 Module *WritingModule, StringRef isysroot,
3153 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003154 WritingAST = true;
3155
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003156 ASTHasCompilerErrors = hasErrors;
3157
Douglas Gregor2cf26342009-04-09 22:27:44 +00003158 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003159 Stream.Emit((unsigned)'C', 8);
3160 Stream.Emit((unsigned)'P', 8);
3161 Stream.Emit((unsigned)'C', 8);
3162 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003163
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003164 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003165
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003166 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003167 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003168 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003169 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003170 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003171 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003172 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003173
3174 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003175}
3176
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003177template<typename Vector>
3178static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3179 ASTWriter::RecordData &Record) {
3180 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3181 I != E; ++I) {
3182 Writer.AddDeclRef(*I, Record);
3183 }
3184}
3185
Sebastian Redla4232eb2010-08-18 23:56:21 +00003186void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003187 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003188 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003189 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003190 using namespace llvm;
3191
Douglas Gregorecc2c092011-12-01 22:20:10 +00003192 // Make sure that the AST reader knows to finalize itself.
3193 if (Chain)
3194 Chain->finalizeForWriting();
3195
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003196 ASTContext &Context = SemaRef.Context;
3197 Preprocessor &PP = SemaRef.PP;
3198
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003199 // Set up predefined declaration IDs.
3200 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003201 if (Context.ObjCIdDecl)
3202 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003203 if (Context.ObjCSelDecl)
3204 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003205 if (Context.ObjCClassDecl)
3206 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003207 if (Context.ObjCProtocolClassDecl)
3208 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003209 if (Context.Int128Decl)
3210 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3211 if (Context.UInt128Decl)
3212 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003213 if (Context.ObjCInstanceTypeDecl)
3214 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003215
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003216 if (!Chain) {
3217 // Make sure that we emit IdentifierInfos (and any attached
3218 // declarations) for builtins. We don't need to do this when we're
3219 // emitting chained PCH files, because all of the builtins will be
3220 // in the original PCH file.
3221 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003222 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003223 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003224 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003225 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003226 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3227 getIdentifierRef(&Table.get(BuiltinNames[I]));
3228 }
3229
Douglas Gregoreee242f2011-10-27 09:33:13 +00003230 // If there are any out-of-date identifiers, bring them up to date.
3231 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3232 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3233 IDEnd = PP.getIdentifierTable().end();
3234 ID != IDEnd; ++ID)
3235 if (ID->second->isOutOfDate())
3236 ExtSource->updateOutOfDateIdentifier(*ID->second);
3237 }
3238
Chris Lattner63d65f82009-09-08 18:19:27 +00003239 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003240 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003241 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003242 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003243 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003244
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003245 // Build a record containing all of the file scoped decls in this file.
3246 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003247 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3248 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003249
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003250 // Build a record containing all of the delegating constructors we still need
3251 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003252 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003253 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003254
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003255 // Write the set of weak, undeclared identifiers. We always write the
3256 // entire table, since later PCH files in a PCH chain are only interested in
3257 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003258 RecordData WeakUndeclaredIdentifiers;
3259 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003260 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003261 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3262 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3263 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3264 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3265 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3266 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3267 }
3268 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003269
Douglas Gregor14c22f22009-04-22 22:18:58 +00003270 // Build a record containing all of the locally-scoped external
3271 // declarations in this header file. Generally, this record will be
3272 // empty.
3273 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003274 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003275 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003276 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003277 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3278 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003279 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003280 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003281 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3282 }
3283
Douglas Gregorb81c1702009-04-27 20:06:05 +00003284 // Build a record containing all of the ext_vector declarations.
3285 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003286 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003287
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003288 // Build a record containing all of the VTable uses information.
3289 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003290 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003291 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3292 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3293 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3294 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3295 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003296 }
3297
3298 // Build a record containing all of dynamic classes declarations.
3299 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003300 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003301
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003302 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003303 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003304 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003305 I = SemaRef.PendingInstantiations.begin(),
3306 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3307 AddDeclRef(I->first, PendingInstantiations);
3308 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003309 }
3310 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3311 "There are local ones at end of translation unit!");
3312
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003313 // Build a record containing some declaration references.
3314 RecordData SemaDeclRefs;
3315 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3316 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3317 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3318 }
3319
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003320 RecordData CUDASpecialDeclRefs;
3321 if (Context.getcudaConfigureCallDecl()) {
3322 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3323 }
3324
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003325 // Build a record containing all of the known namespaces.
3326 RecordData KnownNamespaces;
3327 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3328 I = SemaRef.KnownNamespaces.begin(),
3329 IEnd = SemaRef.KnownNamespaces.end();
3330 I != IEnd; ++I) {
3331 if (!I->second)
3332 AddDeclRef(I->first, KnownNamespaces);
3333 }
3334
Sebastian Redl3397c552010-08-18 23:56:27 +00003335 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003336 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003337 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003338 WriteMetadata(Context, isysroot, OutputFile);
David Blaikie4e4d0842012-03-11 07:00:24 +00003339 WriteLanguageOptions(Context.getLangOpts());
Douglas Gregor832d6202011-07-22 16:35:34 +00003340 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003341 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003342
3343 // Create a lexical update block containing all of the declarations in the
3344 // translation unit that do not come from other AST files.
3345 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3346 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3347 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3348 E = TU->noload_decls_end();
3349 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003350 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003351 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003352 }
3353
3354 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3355 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3356 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3357 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3358 Record.clear();
3359 Record.push_back(TU_UPDATE_LEXICAL);
3360 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3361 data(NewGlobalDecls));
3362
3363 // And a visible updates block for the translation unit.
3364 Abv = new llvm::BitCodeAbbrev();
3365 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3366 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3367 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3368 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3369 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3370 WriteDeclContextVisibleUpdate(TU);
3371
3372 // If the translation unit has an anonymous namespace, and we don't already
3373 // have an update block for it, write it as an update block.
3374 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3375 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3376 if (Record.empty()) {
3377 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003378 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003379 }
3380 }
3381
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003382 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003383 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003384
Douglas Gregora119da02011-08-02 16:26:37 +00003385 // Form the record of special types.
3386 RecordData SpecialTypes;
3387 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003388 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003389 AddTypeRef(Context.getFILEType(), SpecialTypes);
3390 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3391 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3392 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3393 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003394 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003395 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003396
Douglas Gregor366809a2009-04-26 03:49:13 +00003397 // Keep writing types and declarations until all types and
3398 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003399 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003400 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003401 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3402 E = DeclsToRewrite.end();
3403 I != E; ++I)
3404 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003405 while (!DeclTypesToEmit.empty()) {
3406 DeclOrType DOT = DeclTypesToEmit.front();
3407 DeclTypesToEmit.pop();
3408 if (DOT.isType())
3409 WriteType(DOT.getType());
3410 else
3411 WriteDecl(Context, DOT.getDecl());
3412 }
3413 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003414
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003415 WriteFileDeclIDsMap();
3416 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3417
3418 if (Chain) {
3419 // Write the mapping information describing our module dependencies and how
3420 // each of those modules were mapped into our own offset/ID space, so that
3421 // the reader can build the appropriate mapping to its own offset/ID space.
3422 // The map consists solely of a blob with the following format:
3423 // *(module-name-len:i16 module-name:len*i8
3424 // source-location-offset:i32
3425 // identifier-id:i32
3426 // preprocessed-entity-id:i32
3427 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003428 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003429 // selector-id:i32
3430 // declaration-id:i32
3431 // c++-base-specifiers-id:i32
3432 // type-id:i32)
3433 //
3434 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3435 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3437 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003438 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003439 {
3440 llvm::raw_svector_ostream Out(Buffer);
3441 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003442 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003443 M != MEnd; ++M) {
3444 StringRef FileName = (*M)->FileName;
3445 io::Emit16(Out, FileName.size());
3446 Out.write(FileName.data(), FileName.size());
3447 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3448 io::Emit32(Out, (*M)->BaseIdentifierID);
3449 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003450 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003451 io::Emit32(Out, (*M)->BaseSelectorID);
3452 io::Emit32(Out, (*M)->BaseDeclID);
3453 io::Emit32(Out, (*M)->BaseTypeIndex);
3454 }
3455 }
3456 Record.clear();
3457 Record.push_back(MODULE_OFFSET_MAP);
3458 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3459 Buffer.data(), Buffer.size());
3460 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003461 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003462 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003463 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003464 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003465 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003466 WriteFPPragmaOptions(SemaRef.getFPOptions());
3467 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003468
Sebastian Redl1476ed42010-07-16 16:36:56 +00003469 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003470 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003471
Anders Carlssonc8505782011-03-06 18:41:18 +00003472 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003473
Douglas Gregore209e502011-12-06 01:10:29 +00003474 // If we're emitting a module, write out the submodule information.
3475 if (WritingModule)
3476 WriteSubmodules(WritingModule);
3477
Douglas Gregora119da02011-08-02 16:26:37 +00003478 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3479
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003480 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003481 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003482 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003483
3484 // Write the record containing tentative definitions.
3485 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003486 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003487
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003488 // Write the record containing unused file scoped decls.
3489 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003490 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003491
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003492 // Write the record containing weak undeclared identifiers.
3493 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003494 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003495 WeakUndeclaredIdentifiers);
3496
Douglas Gregor14c22f22009-04-22 22:18:58 +00003497 // Write the record containing locally-scoped external definitions.
3498 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003499 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003500 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003501
3502 // Write the record containing ext_vector type names.
3503 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003504 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003505
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003506 // Write the record containing VTable uses information.
3507 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003508 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003509
3510 // Write the record containing dynamic classes declarations.
3511 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003512 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003513
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003514 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003515 if (!PendingInstantiations.empty())
3516 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003517
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003518 // Write the record containing declaration references of Sema.
3519 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003520 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003521
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003522 // Write the record containing CUDA-specific declaration references.
3523 if (!CUDASpecialDeclRefs.empty())
3524 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003525
3526 // Write the delegating constructors.
3527 if (!DelegatingCtorDecls.empty())
3528 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003529
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003530 // Write the known namespaces.
3531 if (!KnownNamespaces.empty())
3532 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3533
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003534 // Write the visible updates to DeclContexts.
3535 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3536 I = UpdatedDeclContexts.begin(),
3537 E = UpdatedDeclContexts.end();
3538 I != E; ++I)
3539 WriteDeclContextVisibleUpdate(*I);
3540
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003541 if (!WritingModule) {
3542 // Write the submodules that were imported, if any.
3543 RecordData ImportedModules;
3544 for (ASTContext::import_iterator I = Context.local_import_begin(),
3545 IEnd = Context.local_import_end();
3546 I != IEnd; ++I) {
3547 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3548 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3549 }
3550 if (!ImportedModules.empty()) {
3551 // Sort module IDs.
3552 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3553
3554 // Unique module IDs.
3555 ImportedModules.erase(std::unique(ImportedModules.begin(),
3556 ImportedModules.end()),
3557 ImportedModules.end());
3558
3559 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3560 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003561 }
3562
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003563 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003564 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003565 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003566 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003567 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003568
Douglas Gregor3e1af842009-04-17 22:13:46 +00003569 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003570 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003571 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003572 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003573 Record.push_back(NumLexicalDeclContexts);
3574 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003575 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003576 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003577}
3578
Douglas Gregor61c5e342011-09-17 00:05:03 +00003579/// \brief Go through the declaration update blocks and resolve declaration
3580/// pointers into declaration IDs.
3581void ASTWriter::ResolveDeclUpdatesBlocks() {
3582 for (DeclUpdateMap::iterator
3583 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3584 const Decl *D = I->first;
3585 UpdateRecord &URec = I->second;
3586
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003587 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003588 continue; // The decl will be written completely
3589
3590 unsigned Idx = 0, N = URec.size();
3591 while (Idx < N) {
3592 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003593 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3594 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3595 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3596 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3597 ++Idx;
3598 break;
3599
3600 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3601 ++Idx;
3602 break;
3603 }
3604 }
3605 }
3606}
3607
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003608void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003609 if (DeclUpdates.empty())
3610 return;
3611
3612 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003613 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003614 for (DeclUpdateMap::iterator
3615 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3616 const Decl *D = I->first;
3617 UpdateRecord &URec = I->second;
3618
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003619 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003620 continue; // The decl will be written completely,no need to store updates.
3621
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003622 uint64_t Offset = Stream.GetCurrentBitNo();
3623 Stream.EmitRecord(DECL_UPDATES, URec);
3624
3625 OffsetsRecord.push_back(GetDeclRef(D));
3626 OffsetsRecord.push_back(Offset);
3627 }
3628 Stream.ExitBlock();
3629 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3630}
3631
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003632void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003633 if (ReplacedDecls.empty())
3634 return;
3635
3636 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003637 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003638 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003639 Record.push_back(I->ID);
3640 Record.push_back(I->Offset);
3641 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003642 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003643 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003644}
3645
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003646void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003647 Record.push_back(Loc.getRawEncoding());
3648}
3649
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003650void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003651 AddSourceLocation(Range.getBegin(), Record);
3652 AddSourceLocation(Range.getEnd(), Record);
3653}
3654
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003655void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003656 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003657 const uint64_t *Words = Value.getRawData();
3658 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003659}
3660
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003661void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003662 Record.push_back(Value.isUnsigned());
3663 AddAPInt(Value, Record);
3664}
3665
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003666void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003667 AddAPInt(Value.bitcastToAPInt(), Record);
3668}
3669
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003670void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003671 Record.push_back(getIdentifierRef(II));
3672}
3673
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003674IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003675 if (II == 0)
3676 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003677
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003678 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003679 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003680 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003681 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003682}
3683
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003684void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003685 Record.push_back(getSelectorRef(SelRef));
3686}
3687
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003688SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003689 if (Sel.getAsOpaquePtr() == 0) {
3690 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003691 }
3692
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003693 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003694 if (SID == 0 && Chain) {
3695 // This might trigger a ReadSelector callback, which will set the ID for
3696 // this selector.
3697 Chain->LoadSelector(Sel);
3698 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003699 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003700 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003701 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003702 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003703}
3704
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003705void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003706 AddDeclRef(Temp->getDestructor(), Record);
3707}
3708
Douglas Gregor7c789c12010-10-29 22:39:52 +00003709void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3710 CXXBaseSpecifier const *BasesEnd,
3711 RecordDataImpl &Record) {
3712 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3713 CXXBaseSpecifiersToWrite.push_back(
3714 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3715 Bases, BasesEnd));
3716 Record.push_back(NextCXXBaseSpecifiersID++);
3717}
3718
Sebastian Redla4232eb2010-08-18 23:56:21 +00003719void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003720 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003721 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003722 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003723 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003724 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003725 break;
3726 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003727 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003728 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003729 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003730 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003731 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003732 break;
3733 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003734 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003735 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003736 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003737 break;
John McCall833ca992009-10-29 08:12:44 +00003738 case TemplateArgument::Null:
3739 case TemplateArgument::Integral:
3740 case TemplateArgument::Declaration:
3741 case TemplateArgument::Pack:
3742 break;
3743 }
3744}
3745
Sebastian Redla4232eb2010-08-18 23:56:21 +00003746void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003747 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003748 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003749
3750 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3751 bool InfoHasSameExpr
3752 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3753 Record.push_back(InfoHasSameExpr);
3754 if (InfoHasSameExpr)
3755 return; // Avoid storing the same expr twice.
3756 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003757 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3758 Record);
3759}
3760
Douglas Gregordc355712011-02-25 00:36:19 +00003761void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3762 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003763 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003764 AddTypeRef(QualType(), Record);
3765 return;
3766 }
3767
Douglas Gregordc355712011-02-25 00:36:19 +00003768 AddTypeLoc(TInfo->getTypeLoc(), Record);
3769}
3770
3771void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3772 AddTypeRef(TL.getType(), Record);
3773
John McCalla1ee0c52009-10-16 21:56:05 +00003774 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003775 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003776 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003777}
3778
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003779void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003780 Record.push_back(GetOrCreateTypeID(T));
3781}
3782
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003783TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3784 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003785 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3786}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003787
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003788TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003789 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003790 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003791}
3792
3793TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3794 if (T.isNull())
3795 return TypeIdx();
3796 assert(!T.getLocalFastQualifiers());
3797
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003798 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003799 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003800 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003801 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003802 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003803 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003804 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003805 return Idx;
3806}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003807
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003808TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003809 if (T.isNull())
3810 return TypeIdx();
3811 assert(!T.getLocalFastQualifiers());
3812
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003813 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3814 assert(I != TypeIdxs.end() && "Type not emitted!");
3815 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003816}
3817
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003818void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003819 Record.push_back(GetDeclRef(D));
3820}
3821
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003822DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003823 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3824
Douglas Gregor2cf26342009-04-09 22:27:44 +00003825 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003826 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003827 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003828
3829 // If D comes from an AST file, its declaration ID is already known and
3830 // fixed.
3831 if (D->isFromASTFile())
3832 return D->getGlobalID();
3833
Douglas Gregor97475832010-10-05 18:37:06 +00003834 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003835 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003836 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003837 // We haven't seen this declaration before. Give it a new ID and
3838 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003839 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003840 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003841 }
3842
Sebastian Redl681d7232010-07-27 00:17:23 +00003843 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003844}
3845
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003846DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003847 if (D == 0)
3848 return 0;
3849
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003850 // If D comes from an AST file, its declaration ID is already known and
3851 // fixed.
3852 if (D->isFromASTFile())
3853 return D->getGlobalID();
3854
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003855 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3856 return DeclIDs[D];
3857}
3858
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003859static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3860 std::pair<unsigned, serialization::DeclID> R) {
3861 return L.first < R.first;
3862}
3863
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003864void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003865 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003866 assert(D);
3867
3868 SourceLocation Loc = D->getLocation();
3869 if (Loc.isInvalid())
3870 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003871
3872 // We only keep track of the file-level declarations of each file.
3873 if (!D->getLexicalDeclContext()->isFileContext())
3874 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00003875 // FIXME: ParmVarDecls that are part of a function type of a parameter of
3876 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00003877 if (isa<ParmVarDecl>(D))
3878 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003879
3880 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003881 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003882 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003883 FileID FID;
3884 unsigned Offset;
3885 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003886 if (FID.isInvalid())
3887 return;
3888 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3889 assert(Entry->isFile());
3890
3891 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3892 if (!Info)
3893 Info = new DeclIDInFileInfo();
3894
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003895 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003896 LocDeclIDsTy &Decls = Info->DeclIDs;
3897
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003898 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003899 Decls.push_back(LocDecl);
3900 return;
3901 }
3902
3903 LocDeclIDsTy::iterator
3904 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3905
3906 Decls.insert(I, LocDecl);
3907}
3908
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003909void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003910 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003911 Record.push_back(Name.getNameKind());
3912 switch (Name.getNameKind()) {
3913 case DeclarationName::Identifier:
3914 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3915 break;
3916
3917 case DeclarationName::ObjCZeroArgSelector:
3918 case DeclarationName::ObjCOneArgSelector:
3919 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003920 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003921 break;
3922
3923 case DeclarationName::CXXConstructorName:
3924 case DeclarationName::CXXDestructorName:
3925 case DeclarationName::CXXConversionFunctionName:
3926 AddTypeRef(Name.getCXXNameType(), Record);
3927 break;
3928
3929 case DeclarationName::CXXOperatorName:
3930 Record.push_back(Name.getCXXOverloadedOperator());
3931 break;
3932
Sean Hunt3e518bd2009-11-29 07:34:05 +00003933 case DeclarationName::CXXLiteralOperatorName:
3934 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3935 break;
3936
Douglas Gregor2cf26342009-04-09 22:27:44 +00003937 case DeclarationName::CXXUsingDirective:
3938 // No extra data to emit
3939 break;
3940 }
3941}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003942
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003943void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003944 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003945 switch (Name.getNameKind()) {
3946 case DeclarationName::CXXConstructorName:
3947 case DeclarationName::CXXDestructorName:
3948 case DeclarationName::CXXConversionFunctionName:
3949 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3950 break;
3951
3952 case DeclarationName::CXXOperatorName:
3953 AddSourceLocation(
3954 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3955 Record);
3956 AddSourceLocation(
3957 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3958 Record);
3959 break;
3960
3961 case DeclarationName::CXXLiteralOperatorName:
3962 AddSourceLocation(
3963 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3964 Record);
3965 break;
3966
3967 case DeclarationName::Identifier:
3968 case DeclarationName::ObjCZeroArgSelector:
3969 case DeclarationName::ObjCOneArgSelector:
3970 case DeclarationName::ObjCMultiArgSelector:
3971 case DeclarationName::CXXUsingDirective:
3972 break;
3973 }
3974}
3975
3976void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003977 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003978 AddDeclarationName(NameInfo.getName(), Record);
3979 AddSourceLocation(NameInfo.getLoc(), Record);
3980 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3981}
3982
3983void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003984 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003985 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003986 Record.push_back(Info.NumTemplParamLists);
3987 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3988 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3989}
3990
Sebastian Redla4232eb2010-08-18 23:56:21 +00003991void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003992 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003993 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003994 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003995 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003996
3997 // Push each of the NNS's onto a stack for serialization in reverse order.
3998 while (NNS) {
3999 NestedNames.push_back(NNS);
4000 NNS = NNS->getPrefix();
4001 }
4002
4003 Record.push_back(NestedNames.size());
4004 while(!NestedNames.empty()) {
4005 NNS = NestedNames.pop_back_val();
4006 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4007 Record.push_back(Kind);
4008 switch (Kind) {
4009 case NestedNameSpecifier::Identifier:
4010 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4011 break;
4012
4013 case NestedNameSpecifier::Namespace:
4014 AddDeclRef(NNS->getAsNamespace(), Record);
4015 break;
4016
Douglas Gregor14aba762011-02-24 02:36:08 +00004017 case NestedNameSpecifier::NamespaceAlias:
4018 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4019 break;
4020
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004021 case NestedNameSpecifier::TypeSpec:
4022 case NestedNameSpecifier::TypeSpecWithTemplate:
4023 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4024 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4025 break;
4026
4027 case NestedNameSpecifier::Global:
4028 // Don't need to write an associated value.
4029 break;
4030 }
4031 }
4032}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004033
Douglas Gregordc355712011-02-25 00:36:19 +00004034void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4035 RecordDataImpl &Record) {
4036 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004037 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004038 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004039
4040 // Push each of the nested-name-specifiers's onto a stack for
4041 // serialization in reverse order.
4042 while (NNS) {
4043 NestedNames.push_back(NNS);
4044 NNS = NNS.getPrefix();
4045 }
4046
4047 Record.push_back(NestedNames.size());
4048 while(!NestedNames.empty()) {
4049 NNS = NestedNames.pop_back_val();
4050 NestedNameSpecifier::SpecifierKind Kind
4051 = NNS.getNestedNameSpecifier()->getKind();
4052 Record.push_back(Kind);
4053 switch (Kind) {
4054 case NestedNameSpecifier::Identifier:
4055 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4056 AddSourceRange(NNS.getLocalSourceRange(), Record);
4057 break;
4058
4059 case NestedNameSpecifier::Namespace:
4060 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4061 AddSourceRange(NNS.getLocalSourceRange(), Record);
4062 break;
4063
4064 case NestedNameSpecifier::NamespaceAlias:
4065 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4066 AddSourceRange(NNS.getLocalSourceRange(), Record);
4067 break;
4068
4069 case NestedNameSpecifier::TypeSpec:
4070 case NestedNameSpecifier::TypeSpecWithTemplate:
4071 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4072 AddTypeLoc(NNS.getTypeLoc(), Record);
4073 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4074 break;
4075
4076 case NestedNameSpecifier::Global:
4077 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4078 break;
4079 }
4080 }
4081}
4082
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004083void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004084 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004085 Record.push_back(Kind);
4086 switch (Kind) {
4087 case TemplateName::Template:
4088 AddDeclRef(Name.getAsTemplateDecl(), Record);
4089 break;
4090
4091 case TemplateName::OverloadedTemplate: {
4092 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4093 Record.push_back(OvT->size());
4094 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4095 I != E; ++I)
4096 AddDeclRef(*I, Record);
4097 break;
4098 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004099
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004100 case TemplateName::QualifiedTemplate: {
4101 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4102 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4103 Record.push_back(QualT->hasTemplateKeyword());
4104 AddDeclRef(QualT->getTemplateDecl(), Record);
4105 break;
4106 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004107
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004108 case TemplateName::DependentTemplate: {
4109 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4110 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4111 Record.push_back(DepT->isIdentifier());
4112 if (DepT->isIdentifier())
4113 AddIdentifierRef(DepT->getIdentifier(), Record);
4114 else
4115 Record.push_back(DepT->getOperator());
4116 break;
4117 }
John McCall14606042011-06-30 08:33:18 +00004118
4119 case TemplateName::SubstTemplateTemplateParm: {
4120 SubstTemplateTemplateParmStorage *subst
4121 = Name.getAsSubstTemplateTemplateParm();
4122 AddDeclRef(subst->getParameter(), Record);
4123 AddTemplateName(subst->getReplacement(), Record);
4124 break;
4125 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004126
4127 case TemplateName::SubstTemplateTemplateParmPack: {
4128 SubstTemplateTemplateParmPackStorage *SubstPack
4129 = Name.getAsSubstTemplateTemplateParmPack();
4130 AddDeclRef(SubstPack->getParameterPack(), Record);
4131 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4132 break;
4133 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004134 }
4135}
4136
Michael J. Spencer20249a12010-10-21 03:16:25 +00004137void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004138 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004139 Record.push_back(Arg.getKind());
4140 switch (Arg.getKind()) {
4141 case TemplateArgument::Null:
4142 break;
4143 case TemplateArgument::Type:
4144 AddTypeRef(Arg.getAsType(), Record);
4145 break;
4146 case TemplateArgument::Declaration:
4147 AddDeclRef(Arg.getAsDecl(), Record);
4148 break;
4149 case TemplateArgument::Integral:
4150 AddAPSInt(*Arg.getAsIntegral(), Record);
4151 AddTypeRef(Arg.getIntegralType(), Record);
4152 break;
4153 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004154 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4155 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004156 case TemplateArgument::TemplateExpansion:
4157 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004158 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4159 Record.push_back(*NumExpansions + 1);
4160 else
4161 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004162 break;
4163 case TemplateArgument::Expression:
4164 AddStmt(Arg.getAsExpr());
4165 break;
4166 case TemplateArgument::Pack:
4167 Record.push_back(Arg.pack_size());
4168 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4169 I != E; ++I)
4170 AddTemplateArgument(*I, Record);
4171 break;
4172 }
4173}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004174
4175void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004176ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004177 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004178 assert(TemplateParams && "No TemplateParams!");
4179 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4180 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4181 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4182 Record.push_back(TemplateParams->size());
4183 for (TemplateParameterList::const_iterator
4184 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4185 P != PEnd; ++P)
4186 AddDeclRef(*P, Record);
4187}
4188
4189/// \brief Emit a template argument list.
4190void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004191ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004192 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004193 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004194 Record.push_back(TemplateArgs->size());
4195 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004196 AddTemplateArgument(TemplateArgs->get(i), Record);
4197}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004198
4199
4200void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004201ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004202 Record.push_back(Set.size());
4203 for (UnresolvedSetImpl::const_iterator
4204 I = Set.begin(), E = Set.end(); I != E; ++I) {
4205 AddDeclRef(I.getDecl(), Record);
4206 Record.push_back(I.getAccess());
4207 }
4208}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004209
Sebastian Redla4232eb2010-08-18 23:56:21 +00004210void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004211 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004212 Record.push_back(Base.isVirtual());
4213 Record.push_back(Base.isBaseOfClass());
4214 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004215 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004216 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004217 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004218 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4219 : SourceLocation(),
4220 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004221}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004222
Douglas Gregor7c789c12010-10-29 22:39:52 +00004223void ASTWriter::FlushCXXBaseSpecifiers() {
4224 RecordData Record;
4225 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4226 Record.clear();
4227
4228 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004229 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004230 if (Index == CXXBaseSpecifiersOffsets.size())
4231 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4232 else {
4233 if (Index > CXXBaseSpecifiersOffsets.size())
4234 CXXBaseSpecifiersOffsets.resize(Index + 1);
4235 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4236 }
4237
4238 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4239 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4240 Record.push_back(BEnd - B);
4241 for (; B != BEnd; ++B)
4242 AddCXXBaseSpecifier(*B, Record);
4243 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004244
4245 // Flush any expressions that were written as part of the base specifiers.
4246 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004247 }
4248
4249 CXXBaseSpecifiersToWrite.clear();
4250}
4251
Sean Huntcbb67482011-01-08 20:30:50 +00004252void ASTWriter::AddCXXCtorInitializers(
4253 const CXXCtorInitializer * const *CtorInitializers,
4254 unsigned NumCtorInitializers,
4255 RecordDataImpl &Record) {
4256 Record.push_back(NumCtorInitializers);
4257 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4258 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004259
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004260 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004261 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004262 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004263 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004264 } else if (Init->isDelegatingInitializer()) {
4265 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004266 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004267 } else if (Init->isMemberInitializer()){
4268 Record.push_back(CTOR_INITIALIZER_MEMBER);
4269 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004270 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004271 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4272 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004273 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004274
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004275 AddSourceLocation(Init->getMemberLocation(), Record);
4276 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004277 AddSourceLocation(Init->getLParenLoc(), Record);
4278 AddSourceLocation(Init->getRParenLoc(), Record);
4279 Record.push_back(Init->isWritten());
4280 if (Init->isWritten()) {
4281 Record.push_back(Init->getSourceOrder());
4282 } else {
4283 Record.push_back(Init->getNumArrayIndices());
4284 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4285 AddDeclRef(Init->getArrayIndex(i), Record);
4286 }
4287 }
4288}
4289
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004290void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4291 assert(D->DefinitionData);
4292 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004293 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004294 Record.push_back(Data.UserDeclaredConstructor);
4295 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004296 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004297 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004298 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004299 Record.push_back(Data.UserDeclaredDestructor);
4300 Record.push_back(Data.Aggregate);
4301 Record.push_back(Data.PlainOldData);
4302 Record.push_back(Data.Empty);
4303 Record.push_back(Data.Polymorphic);
4304 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004305 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004306 Record.push_back(Data.HasNoNonEmptyBases);
4307 Record.push_back(Data.HasPrivateFields);
4308 Record.push_back(Data.HasProtectedFields);
4309 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004310 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004311 Record.push_back(Data.HasOnlyCMembers);
Sean Hunt023df372011-05-09 18:22:59 +00004312 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004313 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004314 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
4315 Record.push_back(Data.DefaultedCopyConstructorIsConstexpr);
4316 Record.push_back(Data.DefaultedMoveConstructorIsConstexpr);
4317 Record.push_back(Data.HasConstexprDefaultConstructor);
4318 Record.push_back(Data.HasConstexprCopyConstructor);
4319 Record.push_back(Data.HasConstexprMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004320 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004321 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004322 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004323 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004324 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004325 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004326 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004327 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004328 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004329 Record.push_back(Data.DeclaredDefaultConstructor);
4330 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004331 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004332 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004333 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004334 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004335 Record.push_back(Data.FailedImplicitMoveConstructor);
4336 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004337 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004338
4339 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004340 if (Data.NumBases > 0)
4341 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4342 Record);
4343
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004344 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4345 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004346 if (Data.NumVBases > 0)
4347 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4348 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004349
4350 AddUnresolvedSet(Data.Conversions, Record);
4351 AddUnresolvedSet(Data.VisibleConversions, Record);
4352 // Data.Definition is the owning decl, no need to write it.
4353 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004354
4355 // Add lambda-specific data.
4356 if (Data.IsLambda) {
4357 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004358 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004359 Record.push_back(Lambda.NumCaptures);
4360 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004361 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004362 AddDeclRef(Lambda.ContextDecl, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004363 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4364 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4365 AddSourceLocation(Capture.getLocation(), Record);
4366 Record.push_back(Capture.isImplicit());
4367 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4368 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4369 AddDeclRef(Var, Record);
4370 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4371 : SourceLocation(),
4372 Record);
4373 }
4374 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004375}
4376
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004377void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004378 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004379 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004380 assert(FirstDeclID == NextDeclID &&
4381 FirstTypeID == NextTypeID &&
4382 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004383 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004384 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004385 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004386
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004387 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004388
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004389 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4390 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4391 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004392 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004393 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004394 NextDeclID = FirstDeclID;
4395 NextTypeID = FirstTypeID;
4396 NextIdentID = FirstIdentID;
4397 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004398 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004399}
4400
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004401void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004402 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004403 if (II->hasMacroDefinition())
4404 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004405}
4406
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004407void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004408 // Always take the highest-numbered type index. This copes with an interesting
4409 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004410 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004411 // keep the higher-numbered entry so that we can properly write it out to
4412 // the AST file.
4413 TypeIdx &StoredIdx = TypeIdxs[T];
4414 if (Idx.getIndex() >= StoredIdx.getIndex())
4415 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004416}
4417
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004418void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004419 SelectorIDs[S] = ID;
4420}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004421
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004422void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004423 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004424 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004425 MacroDefinitions[MD] = ID;
4426}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004427
Douglas Gregor1d4c1132011-12-20 22:06:13 +00004428void ASTWriter::MacroVisible(IdentifierInfo *II) {
4429 DeserializedMacroNames.push_back(II);
4430}
4431
Douglas Gregora015cab2011-12-02 17:30:13 +00004432void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4433 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4434 SubmoduleIDs[Mod] = ID;
4435}
4436
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004437void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004438 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004439 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004440 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4441 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004442 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004443 // A forward reference was mutated into a definition. Rewrite it.
4444 // FIXME: This happens during template instantiation, should we
4445 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004446 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004447 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004448 }
4449}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004450void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004451 assert(!WritingAST && "Already writing the AST!");
4452
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004453 // TU and namespaces are handled elsewhere.
4454 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4455 return;
4456
Douglas Gregor919814d2011-09-09 23:01:35 +00004457 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004458 return; // Not a source decl added to a DeclContext from PCH.
4459
4460 AddUpdatedDeclContext(DC);
4461}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004462
4463void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004464 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004465 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004466 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004467 return; // Not a source member added to a class from PCH.
4468 if (!isa<CXXMethodDecl>(D))
4469 return; // We are interested in lazily declared implicit methods.
4470
4471 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004472 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004473 UpdateRecord &Record = DeclUpdates[RD];
4474 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004475 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004476}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004477
4478void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4479 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004480 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004481 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004482 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004483 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004484 return; // Not a source specialization added to a template from PCH.
4485
4486 UpdateRecord &Record = DeclUpdates[TD];
4487 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004488 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004489}
Douglas Gregor89d99802010-11-30 06:16:57 +00004490
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004491void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4492 const FunctionDecl *D) {
4493 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004494 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004495 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004496 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004497 return; // Not a source specialization added to a template from PCH.
4498
4499 UpdateRecord &Record = DeclUpdates[TD];
4500 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004501 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004502}
4503
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004504void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004505 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004506 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004507 return; // Declaration not imported from PCH.
4508
4509 // Implicit decl from a PCH was defined.
4510 // FIXME: Should implicit definition be a separate FunctionDecl?
4511 RewriteDecl(D);
4512}
4513
Sebastian Redlf79a7192011-04-29 08:19:30 +00004514void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004515 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004516 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004517 return;
4518
4519 // Since the actual instantiation is delayed, this really means that we need
4520 // to update the instantiation location.
4521 UpdateRecord &Record = DeclUpdates[D];
4522 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4523 AddSourceLocation(
4524 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4525}
4526
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004527void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4528 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004529 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004530 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004531 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004532
4533 assert(IFD->getDefinition() && "Category on a class without a definition?");
4534 ObjCClassesWithCategories.insert(
4535 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004536}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004537
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004538
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004539void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4540 const ObjCPropertyDecl *OrigProp,
4541 const ObjCCategoryDecl *ClassExt) {
4542 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4543 if (!D)
4544 return;
4545
4546 assert(!WritingAST && "Already writing the AST!");
4547 if (!D->isFromASTFile())
4548 return; // Declaration not imported from PCH.
4549
4550 RewriteDecl(D);
4551}